@Kostas132242

Как сделать, чтобы при врезании в стену пуля пропадала, а игрок не мог пройти сквозь?

from pygame import *  
class GameSprite(sprite.Sprite):  
    def __init__(self, player_image, player_x, player_y, size_x, size_y, player_speed):  
        super().__init__()  
        self.image = transform.scale(image.load(player_image), (size_x, size_y))  
        self.speed = player_speed  
        self.rect = self.image.get_rect()  
        self.rect.x = player_x  
        self.rect.y = player_y   

    def reset (self):  
        win.blit(self.image, (self.rect.x, self.rect.y))  
  
class Player(GameSprite):  
    def update(self):  
        keys = key.get_pressed()  
        if keys[K_LEFT] and self.rect.x > 5:  
            self.rect.x -= self.speed  
        if keys[K_RIGHT] and self.rect.x < win_width - 80:  
            self.rect.x += self.speed  
        if keys[K_UP] and self.rect.y > 5:  
            self.rect.y -= self.speed  
        if keys[K_DOWN] and self.rect.y < win_height - 80:  
            self.rect.y += self.speed  
    
    def fire(self): 
        bullet = Bullet(img_bullet, self.rect.centerx, self.rect.top, 15, 20, -20) 
        bullets.add(bullet)

class Player_2(GameSprite):  
    def update(self):  
        keys = key.get_pressed()  
        if keys[K_a] and self.rect.x > 5:  
            self.rect.x -= self.speed  
        if keys[K_d] and self.rect.x < win_width - 80:  
            self.rect.x += self.speed  
        if keys[K_w] and self.rect.y > 5:  
            self.rect.y -= self.speed  
        if keys[K_s] and self.rect.y < win_height - 80:  
            self.rect.y += self.speed
    
    def fire(self): 
        bullet_2 = Bullet(img_bullet1, self.rect.centerx, self.rect.top + 45, 15, 20, 20) 
        bullets.add(bullet_2)

class Wall(sprite.Sprite):  
    def __init__(self,  color1, color2, color3, wall_x, wall_y, wall_width, wall_height):  
        super().__init__()  
        self.color1 = color1  
        self.color2 = color2  
        self.color3 = color3  
        self.width = wall_width  
        self.height = wall_height  
        self.image = Surface((self.width, self.height)) 
        self.image.fill((color1, color2, color3))  
        self.rect = self.image.get_rect()  
        self.rect.x = wall_x  
        self.rect.y = wall_y  
    def draw_wall(self):  
        win.blit(self.image, (self.rect.x, self.rect.y))  

class Bullet(GameSprite): 
    def update(self): 
        self.rect.y += self.speed 
        if self.rect.y < 0: 
            self.kill() 
        

wall_1 = Wall(154, 205, 50, 80, 80, 460, 10) #верхня стінка 
wall_2 = Wall(154, 205, 40, 80, 400, 460, 10) #нижня стінка 

img_bullet = "bullet.png"  
img_bullet1 = "bullet1.png" 
font.init()  
font = font.Font(None, 70)  
win_1 = font.render("Гравець 1 переміг!", True, (0, 180, 0))  
lose = font.render("Гравець 2 переміг!", True, (180, 0, 0))  
  
  
win_width = 1000  
win_height = 800  

bullets = sprite.Group()

win = display.set_mode((1000, 800))  
display.set_caption("Mama")  
background = transform.scale(image.load("background.jpg"), (win_width, win_height))  
  
player = Player("hero.png", 5, win_height - 80, 65, 65, 4)  
player_2 = Player_2("hero.png", 5, win_height - 800, 65, 65, 4)    
final = GameSprite("treasure.png", win_width - 120, win_height - 80, 65, 65, 0)  



clock = time.Clock()  
FPS = 60 
mixer.init()  
mixer.music.load("Jungles.ogg")  
mixer.music.play()  
  
money = mixer.Sound('money.ogg')  
kick = mixer.Sound('kick.ogg')  
fire_sound = mixer.Sound("fire.ogg")  
game = True  
finish = False  
  
  
while game:  
  
    for e in event.get():  
        if e.type == QUIT:  
            game = False  
        elif e.type == KEYDOWN:
            if e.key == K_e:
                fire_sound.play()
                player_2.fire()

            elif e.key == K_SPACE:
                fire_sound.play()
                player.fire()


    if finish != True:  
        win.blit(background,(0, 0))  
  
        player_2.update() 
        player.update()  
        final.update()
        bullets.update()
 
        player_2.reset() 
        player.reset()    
        final.reset()  
  
        wall_1.draw_wall()  
        wall_2.draw_wall()  
        
        bullets.draw(win)

    if sprite.collide_rect(player, wall_1) or sprite.collide_rect(player, wall_2) or sprite.collide_rect(player_2, wall_1) or sprite.collide_rect(player_2, wall_2):
        print('I dont know :(')

    if sprite.spritecollide(wall_1, bullets, False, ) or sprite.spritecollide(wall_2, bullets, False, ):        
        print("I dont know :(")

    if sprite.spritecollide(player, bullets, False, ):
        finish = True
        win.blit(lose, (300, 400))
        kick.play()

    if sprite.spritecollide(player_2, bullets, False, ):
        finish = True
        win.blit(win_1, (300, 400))
        kick.play()


    display.update()
    clock.tick(FPS)
  • Вопрос задан
  • 110 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы