所以我刚开始学习如何编程,我一直试图用移动平台创建这个小游戏。我可以让普通的墙/平台工作,但我似乎无法弄清楚如何让这些移动的工作。我不断得到一个看起来像这样的追溯:
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'
我认为这个问题与我所拥有的一些代码有关,但我并不完全确定。
class Wall(pygame.sprite.Sprite):
def __init__(self, width, height):
#creates the wall
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([width, height])
self.image.fill(blue)
self.rect = self.image.get_rect()
######################################################
class MovingEnemy(Wall):
change_x = 0
change_y=0
boundary_top = 0
boundary_bottom = 0
boundary_left = 0
boundary_right = 0
player = None
level = None
def update(self):
# Move left/right
self.rect.x += self.change_x
# See if we hit the player
hit = pygame.sprite.collide_rect(self, self.player)
if hit:
# We did hit the player. Shove the player around and
# assume he/she won't hit anything else.
# If we are moving right, set our right side
# to the left side of the item we hit
if self.change_x < 0:
self.player.rect.right = self.rect.left
else:
# Otherwise if we are moving left, do the opposite.
self.player.rect.left = self.rect.right
# Move up/down
self.rect.y += self.change_y
# Check and see if we the player
hit = pygame.sprite.collide_rect(self, self.player)
if hit:
# We did hit the player. Shove the player around and
# assume he/she won't hit anything else.
# Reset our position based on the top/bottom of the object.
if self.change_y < 0:
self.player.rect.bottom = self.rect.top
else:
self.player.rect.top = self.rect.bottom
# Check the boundaries and see if we need to reverse
# direction.
if self.rect.bottom > self.boundary_bottom or self.rect.top < self.boundary_top:
self.change_y *= -1
cur_pos = self.rect.x - self.level.world_shift
if cur_pos < self.boundary_left or cur_pos > self.boundary_right:
self.change_x *= -1
wall = MovingEnemy(70,40)
wall.rect.x = 500
wall.rect.y = 400
wall.boundary_left = 250
wall.boundary_right = 800
wall.change_x = 1
walls.add(wall)
我不确定我是否提供了正确的信息以获得帮助,但我老实说。我已经浏览了几个小时的互联网寻找一种方法来设法做到这一点,我尝试的一切似乎都行不通。如果有人能理解我有这种混乱的混乱并帮助我,我会非常感激。
编辑: 我确实有一个玩家类,我是否应该将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
答案 0 :(得分:1)
在MovingEnemy
班级中,您有一个名为player
的属性。 player
的{{1}}开始于None
。您的代码中没有位置将player
更改为除None
之外的任何内容,因此播放器的类型为none或NoneType
。没有任何方法可以使用您调用的rect
方法使用的方法collide_rect
。
答案 1 :(得分:0)
你的编译器告诉你什么是错的。
walls.update()
首先,你的程序调用了wall.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'
您的墙(自我)或(自我玩家)的类型为无,因此没有属性'rect'可用于确定碰撞。现在注意:
player = None
你永远不会设置玩家!因此,播放器的类型为None,并且没有属性'rect'来进行碰撞检测。