我正在尝试创建一个圆形精灵,它将位于一个窗口的左侧和右侧的墙上。我不能让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。谢谢!
* edit- grammar
答案 0 :(得分:0)
在您的Player类中,您将init拼写为int。这意味着在创建对象时,该方法中的所有代码都不会运行。