问题描述
我正在尝试创建一个圆形的精灵,该精灵将在一个窗口中,该窗口在屏幕的左右两侧都具有墙壁。
我不能让class Player
像圆圈一样正确地工作,但是我可以让它像正方形一样工作。
在尝试修复20个小时后,我为使该圈子正常运转而下了弯腰……或者至少了解我的代码为什么会失败。
import pygame
pygame.init()
black = (0,0,0)
white = (255,255,255)
gray = (105,105,105)
red = (255,0,0)
class Barrier(pygame.sprite.Sprite): #Barriers will be walls and "floors"
def __init__(self,x,y,width,height,color):
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.y = y
self.rect.x = x
class Player(pygame.sprite.Sprite): #player will be a circle
def __int__(self,x,y):
super().__init__()
self.image = pygame.Surface((50,50))
self.image.fill(white)
pygame.draw.circle(self.image, black, (25, 25), 25, 0)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.change_x = 0
self.change_y = 0
self.barrier = None
def changespeed(self, x, y):
self.change_x += x
self.change_y += y
def update(self):
self.rect.x += self.change_x
block_hit_list = pygame.sprite.spritecollide(self, self.barrier, False)
for block in block_hit_list:
if self.change_x > 0:
self.rect.right = block.rect.left
else:
self.rect.left = block.rect.right
self.rect.y += self.change_y
block_hit_list = pygame.sprite.spritecollide(self, self.barrier, False)
for block in block_hit_list:
if self.change_y > 0:
self.rect.bottom = block.rect.top
else:
self.rect.top = block.rect.bottom
screen = pygame.display.set_mode((800,480))
pygame.display.set_caption("Test")
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill(white)
all_sprite_list = pygame.sprite.Group()
wall_list = pygame.sprite.Group() #wall will be a vertical Barrier
wall = Barrier(0,0,10,800,red)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Barrier(790,0,10,800,red)
wall_list.add(wall)
all_sprite_list.add(wall)
player = Player(50,50)
player.barrier = wall_list
all_sprite_list.add(player)
clock = pygame.time.Clock()
score = 0
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.changespeed(-3, 0)
elif event.key == pygame.K_RIGHT:
player.changespeed(3, 0)
elif event.key == pygame.K_UP:
player.changespeed(0, -3)
elif event.key == pygame.K_DOWN:
player.changespeed(0, 3)
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.changespeed(3, 0)
elif event.key == pygame.K_RIGHT:
player.changespeed(-3, 0)
elif event.key == pygame.K_UP:
player.changespeed(0, 3)
elif event.key == pygame.K_DOWN:
player.changespeed(0, -3)
all_sprite_list.update()
screen.fill(white)
all_sprite_list.draw(screen)
pygame.display.flip()
clock.tick(40)
pygame.quit()
完全回溯错误:
Traceback (most recent call last):
File "D:/pyDump/FallGame/freeFALL.py", line 114, in <module>
all_sprite_list.update()
File "C:\Program Files\Anaconda3\lib\site-packages\pygame\sprite.py", line 462, in update
s.update(*args)
File "D:/pyDump/FallGame/freeFALL.py", line 43, in update
self.rect.x += self.change_x
AttributeError: 'Player' object has no attribute 'rect'
对于上下文,我在PC上使用Pycharm和python 3.5。 谢谢!
*编辑语法
1楼
在Player类中,您将init拼写为int。 这意味着在创建对象时,该方法中的任何代码都不会运行。