我在pygames中创建移动平台时遇到了困难。我遇到一个错误,有人能帮我吗?

2024-04-29 03:33:00 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我刚刚开始学习如何编程,我一直在尝试用移动平台创建这个小游戏。我可以让普通的墙壁/平台工作,但我似乎不知道如何让这些移动的工作。我一直在做这样的回溯:

Traceback (most recent call last):
  File "C:\Users\Jacob\Desktop\gametest.py", line 227, in <module>
    walls.update()
  File "C:\Python34\lib\site-packages\pygame\sprite.py", line 462, in update
    s.update(*args)
  File "C:\Users\Jacob\Desktop\gametest.py", line 111, in update
    hit = pygame.sprite.collide_rect(self, self.player)
  File "C:\Python34\lib\site-packages\pygame\sprite.py", line 1300, in collide_rect
    return left.rect.colliderect(right.rect)
AttributeError: 'NoneType' object has no attribute 'rect'

我认为问题与我的一些代码有关,但我不完全确定。在

^{pr2}$

我不确定我是否提供了正确的信息以获得帮助,但我真诚地在努力。我在网上浏览了好几个小时,想找到一个办法来做到这一点,但我所尝试的一切似乎都不管用。如果有人能理解我现在的混乱局面并帮我解决问题,我会非常感激的。在

编辑: 我有一个播放器类,我应该把MovingEnemy中的播放器设置为这个类吗?我不确定这是否可行,也不知道我应该把它设置为什么。这是我的球员课程,如果这更容易的话。在

class Player(pygame.sprite.Sprite):

    #Sets the starting speed
    change_x = 0
    change_y = 0
    walls = None


    def __init__(self, x, y):
        #creates the sprite for the player
        pygame.sprite.Sprite.__init__(self)

        #sets the size
        self.image = pygame.Surface([25,25])
        self.image.fill(green)

        self.rect = self.image.get_rect()
        self.rect.y = y
        self.rect.x = x

    def movement(self, x, y):

        self.change_x += x
        self.change_y += y

    def update(self):

        #changes the position of the player moving left and right
        self.rect.x += self.change_x



        #checks to see if the player sprite hits the wall
        collision = pygame.sprite.spritecollide(self, self.walls, False)
        for block in collision:
            # If the player hits a block while moving right, it is set back
            # to the left side of the block that was hit.
            if self.change_x > 0:
                self.rect.right = block.rect.left
            # Does the same as above, except with moving left.   
            else:
                self.rect.left = block.rect.right


        #changes the position of the player moving up and down    
        self.rect.y += self.change_y

        collision = pygame.sprite.spritecollide(self, self.walls, False)
        for block in collision:

            # Does the same as the above "for" except for moving up and down
            if self.change_y > 0:
                self.rect.bottom = block.rect.top
            else:
                self.rect.top = block.rect.bottom

Tags: theinrectselfrightforupdatechange
2条回答

你的编译器告诉你出了什么问题。在

walls.update()

首先,你的程序调用墙.更新,这就是麻烦的开始。在

^{pr2}$

然后,你试着检测墙和播放器之间的命中检测。在

^{3}$

注意下一条线索出现在这一行,因为这条线最终引发了问题,它说:

AttributeError: 'NoneType' object has no attribute 'rect'

要么你的墙(自己)要么(自我。玩家)类型为None,因此没有可用于确定冲突的属性'rect'。现在请注意:

player = None

你从来没有设置过球员!因此,player的类型是None,并且没有属性'rect'来执行碰撞检测。在

在您的MovingEnemy类中,有一个名为player的属性。player开始于None。没有在代码中的任何地方您都不会将player改为None,因此player的类型是none或NoneType。任何东西都没有您调用的collide_rect方法使用的方法rect。在

相关问题 更多 >