python - pygame与子弹的碰撞找不到碰撞

标签 python pygame collision-detection

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

    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()



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



#IGNORE THIS 
#player_one_bullet_list = pygame.sprite.Group()



#elif event.key == pygame.K_SPACE:
#                    player_one_bullet = player_one_Bullet()
#
#                    player_one_bullet.ypos = player_one.ypos
#                    player_one.xpos = player_one.xpos
#
#                    player_one_bullet.speed_x = 14
#
#                    player_one_bullet_list.add(player_one_bullet)
#
#    
#player_one_bullet_list.update()
#
#    for player_one_bullet in player_one_bullet_list:
#            player_one_bullet.draw()


#if hammerhood.rect.colliderect(crab):  #.rect.top
#            hammerhood.speed_y = 0

最佳答案

当 python 提示某些内容未定义时,通常意味着您正在尝试使用不存在或尚未创建的内容。

这是您唯一一次定义“player_one_bullet”:

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

只要不按空格键,就没有子弹,并且“player_one_bullet”保持未定义状态。

这就是这段代码出错的原因:

if player_one_bullet.is_collided_with(player_two):

你不能使用不存在的东西!

此外,只有每个玩家发射的最后一颗子弹会检查是否击中另一个玩家,其他子弹将忽略一切!

幸运的是,有一个简单的方法可以解决这个问题;而不是这样做:

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_bullet_listplayer_two_bullet_list来检查子弹是否存在以及它们是否相撞!

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

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

现在您有一个 for 循环,它会遍历项目符号列表,如果存在,则它会检查是否存在冲突。

希望这对您有帮助!

关于python - pygame与子弹的碰撞找不到碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53526145/

相关文章:

python - 即使在我注销 SSH 后如何在后台运行脚本?

Python 正则表达式--类型错误 : an integer is required

javascript - 使用javascript从数组中的特定对象获取x和y坐标

collision-detection - 乌龟碰了怎么杀?

python - 使用pygame让子弹移动到你的光标

collision-detection - 游戏对象和地板之间的碰撞与重力有关吗?

python - 如何使用任何库在 python 中将 RTF 字符串转换为纯文本

python - python中有gdb客户端实现吗?

python - 用pygame制作的游戏可以提交到steam吗?

python - Pygame Beat-Em-Up 游戏//敌人攻击计时器永远不会达到零