【问题标题】:pygame collisions with bullets cannot find collisions and bullets disappearpygame与子弹碰撞找不到碰撞和子弹消失
【发布时间】:2018-11-30 15:39:54
【问题描述】:

当我运行我的代码时,程序对我说:

    Traceback (most recent call last):
  File "C:\Users\User\Documents\projects\two_player_gun_game.py", line 192, in <module>
    if player_one_bullet.is_collided_with(player_two):
NameError: name 'player_one_bullet' is not defined

我不明白为什么会出现这个原因我已经在 is_collided_with 但是的类之一中创建了一个函数。好像还是不行可以。我在底部放了一个检查冲突的 if 语句。碰撞意味着玩家 1 和 2 的子弹会发生碰撞。这是我的帮助代码:

import pygame
import random
import sys

pygame.init()
screen = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock()  # A clock to limit the frame rate.
pygame.display.set_caption("this game")


class Background:
    picture = pygame.image.load("C:/images/space.jpg").convert()
    picture = pygame.transform.scale(picture, (1280, 720))

    def __init__(self, x, y):
        self.xpos = x
        self.ypos = y

    def draw(self):
        screen.blit(self.picture, (self.xpos, self.ypos))


class player_first:
    picture = pygame.image.load("C:/aliens/ezgif.com-crop.gif")
    picture = pygame.transform.scale(picture, (200, 200))

    def __init__(self, x, y):
        self.xpos = x
        self.ypos = y
        self.speed_x = 0
        self.speed_y = 0
        self.rect = self.picture.get_rect()


    def update(self):
        self.xpos += self.speed_x
        self.ypos += self.speed_y

    def draw(self):      #left right
        #screen.blit(pygame.transform.flip(self.picture, True, False), self.rect)
        screen.blit(self.picture, (self.xpos, self.ypos))


class player_second:
    picture = pygame.image.load("C:/aliens/Giantmechanicalcrab2 - Copy.gif")
    picture = pygame.transform.scale(picture, (300, 200))

    def __init__(self, x, y):
        self.xpos = x
        self.ypos = y
        self.speed_x = 0
        self.speed_y = 0
        self.rect = self.picture.get_rect()


    def update(self):
        self.xpos += self.speed_x
        self.ypos += self.speed_y

    def draw(self):      #left right
        #screen.blit(pygame.transform.flip(self.picture, True, False), self.rect)
        screen.blit(self.picture, (self.xpos, self.ypos))







class player_one_Bullet(pygame.sprite.Sprite):
    picture = pygame.image.load("C:/aliens/giphy.gif").convert_alpha()
    picture = pygame.transform.scale(picture, (100, 100))


    def __init__(self):
        self.xpos = 360
        self.ypos = 360
        self.speed_x = 0
        super().__init__()
        self.rect = self.picture.get_rect()


    def update(self):
        self.xpos += self.speed_x

    def draw(self):
        screen.blit(self.picture, (self.xpos, self.ypos))
        #self.screen.blit(pygame.transform.flip(self.picture, False, True), self.rect)

    def is_collided_with(self, sprite):
        return self.rect.colliderect(sprite.rect)


class player_two_Bullet(pygame.sprite.Sprite):
    picture = pygame.image.load("C:/aliens/MajesticLavishBackswimmer-size_restricted.gif").convert_alpha()
    picture = pygame.transform.scale(picture, (100, 100))
    picture = pygame.transform.rotate(picture, 180)


    def __init__(self):
        self.xpos = 360
        self.ypos = 360
        self.speed_x = 0
        super().__init__()
        self.rect = self.picture.get_rect()


    def update(self):
        self.xpos -= self.speed_x

    def draw(self):
        screen.blit(self.picture, (self.xpos, self.ypos))
        #self.screen.blit(pygame.transform.flip(self.picture, False, True), self.rect)

    def is_collided_with(self, sprite):
        return self.rect.colliderect(sprite.rect)


player_one = player_first(0, 0)
player_two = player_second(1000, 0)
cliff = Background(0, 0)
player_one_bullet_list = pygame.sprite.Group()
player_two_bullet_list = pygame.sprite.Group()



while True:

    for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_w:
                    player_one.speed_y = -5

                elif event.key == pygame.K_s:
                    player_one.speed_y = 5

                elif event.key == pygame.K_UP:
                    player_two.speed_y = -5

                elif event.key == pygame.K_DOWN:
                    player_two.speed_y = 5







                elif event.key == pygame.K_SPACE:
                    player_one_bullet = player_one_Bullet()

                    player_one_bullet.ypos = player_one.ypos
                    player_one_bullet.xpos = player_one.xpos

                    player_one_bullet.speed_x = 14

                    player_one_bullet_list.add(player_one_bullet)



                elif event.key == pygame.K_KP0:
                    player_two_bullet = player_two_Bullet()

                    player_two_bullet.ypos = player_two.ypos
                    player_two_bullet.xpos = player_two.xpos

                    player_two_bullet.speed_x = 14

                    player_two_bullet_list.add(player_two_bullet)




            elif event.type == pygame.KEYUP:
                # Stop moving when the keys are released.
                if event.key == pygame.K_s and player_one.speed_y > 0:
                    player_one.speed_y = 0
                elif event.key == pygame.K_w and player_one.speed_y < 0:
                    player_one.speed_y = 0


                if event.key == pygame.K_DOWN and player_two.speed_y > 0:
                    player_two.speed_y = 0
                elif event.key == pygame.K_UP and player_two.speed_y < 0:
                    player_two.speed_y = 0


    if player_one_bullet.is_collided_with(player_two):  
            player_one_bullet.kill()


    if player_two_bullet.is_collided_with(player_one):  
            player_two_bullet.kill()

    player_one.update()
    player_two.update()
    cliff.draw()
    player_one.draw()
    player_two.draw()

    player_one_bullet_list.update()
    player_two_bullet_list.update()

    for player_one_bullet in player_one_bullet_list:
            player_one_bullet.draw()


    for player_two_bullet in player_two_bullet_list:
            player_two_bullet.draw()

    for player_one_bullet in player_one_bullet_list.sprites():
        if player_one_bullet.is_collided_with(player_two):
            player_one_bullet.kill()

    for player_two_bullet in player_two_bullet_list.sprites():
        if player_two_bullet.is_collided_with(player_one):
            player_two_bullet.kill()



    pygame.display.flip()
    clock.tick(60)

我现在已经添加了其他人的答案,但不幸的是,子弹.draw() 函数发生后子弹突然离开屏幕

还是一头雾水,能否给我看看代码,这会有所帮助。

【问题讨论】:

  • @Blckknght 你能告诉我它需要在代码中的什么位置吗?这对我有更大的帮助。
  • @Blckknght 这不起作用,子弹仍在消失
  • @skrx 你知道如何解决这个问题

标签: python oop pygame collision kill


【解决方案1】:

它是在 elif 语句的范围内定义的。将其移至此处,并将其设置为 None ,然后将其分配到 elif 中:

player_one = player_first(0, 0)
player_two = player_second(1000, 0)
cliff = Background(0, 0)
player_one_bullet_list = pygame.sprite.Group()
player_two_bullet_list = pygame.sprite.Group()
player_one_bullet = None
player_two_bullet = None

【讨论】:

  • 在 elif 语句中我应该为玩家分配一个和两个项目符号
  • 谢谢你这真的很有用,我已经设法实现了加载
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-22
  • 1970-01-01
  • 1970-01-01
  • 2014-10-25
相关资源
最近更新 更多