0
我正在通过programarcadegames实验室使用python和pygame工作,我卡住了。当我运行这段代码时,当我初始化块和玩家类时,我做错了什么。玩家类应该是它自己的类,而不是Block类的一个实例。蟒蛇/ pygame帮助实验室13 programarcadegames
当我运行这个程序,我收到此错误: “块”对象有没有属性“changespeed”
import pygame
import random
# Define some colors
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
class Block(pygame.sprite.Sprite):
def __init__(self, color, width, height):
""" Constructor. Pass in the color of the block,
and its x and y position. """
# Call the parent class (Sprite) constructor
super().__init__()
# Create an image of the block, and fill it with a color.
# This could also be an image loaded from the disk.
self.image = pygame.Surface([width, height])
self.image.fill(color)
# Fetch the rectangle object that has the dimensions of the image
# image.
# Update the position of this object by setting the values
# of rect.x and rect.y
self.rect = self.image.get_rect()
class Player (pygame.sprite.Sprite):
""" The class is the player-controlled sprite. """
def __init__(self,x,y):
"""Constructor function"""
# Call the parent's constructor
super().__init__()
# -- methods
# Set height, width
self.image = pygame.Surface([15, 15])
self.image.fill(BLACK)
# Make our top-left corner the passed-in location.
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
# -- Attributes
# Set speed vector
self.change_x = 0
self.change_y = 0
def changespeed(self, x, y):
""" Change the speed of the player"""
self.change_x += x
self.change_y += y
def update(self):
""" Find a new position for the player"""
self.rect.x += self.change_x
self.rect.y += self.change_y
# Initialize Pygame
pygame.init()
# Set the height and width of the screen
screen_width = 700
screen_height = 400
screen = pygame.display.set_mode([screen_width, screen_height])
# This is a list of 'sprites.' Each block in the program is
# added to this list. The list is managed by a class called 'Group.'
block_list = pygame.sprite.Group()
# This is a list of every sprite.
# All blocks and the player block as well.
all_sprites_list = pygame.sprite.Group()
for i in range(50):
# This represents a block
block = Block(BLACK, 20, 15)
# Set a random location for the block
block.rect.x = random.randrange(screen_width)
block.rect.y = random.randrange(screen_height)
# Add the block to the list of objects
block_list.add(block)
all_sprites_list.add(block)
# Create a RED player block
player = Block(BLUE, 20, 15)
all_sprites_list.add(player)
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
score = 0
# -------- Main Program Loop -----------
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# Set the speed based on the key pressed
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)
# Reset speed when key goes up
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)
# Clear the screen
screen.fill(WHITE)
# Get the current mouse position. This returns the position
# as a list of two numbers.
#pos = pygame.mouse.get_pos()
# Fetch the x and y out of the list,
# just like we'd fetch letters out of a string.
# Set the player object to the mouse location
#player.rect.x = pos[0]
#player.rect.y = pos[1]
# See if the player block has collided with anything.
blocks_hit_list = pygame.sprite.spritecollide(player, block_list, True)
# Check the list of collisions.
for block in blocks_hit_list:
score += 1
print(score)
# This calls update on all the sprites
all_sprites_list.update()
# Draw all the spites
all_sprites_list.draw(screen)
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Limit to 60 frames per second
clock.tick(60)
pygame.quit()
该错误似乎表明您正在创建一个块并尝试使用'changingpeed'方法。这些类完全不相关,所以错误不是“他们不是他们自己的类”。我认为正在发生的事情是,你用一个block的实例覆盖player变量,或者把player和block都放到一个你要遍历的列表中,并试图在每个对象上调用'changingpeed'。但是,如果没有[mcve],就无法帮助你。 –
我发布了完整的代码。当我尝试移动播放器对象时,就是当它抛出错误时。 – celtics33
创建[mcve]将帮助您自己解决问题,无需任何帮助,并且可以非常快速地完成(最长5-10分钟)。如果你没有找到错误,那么你至少会让其他人在Stackoverflow中寻找答案时有用。当人们在Stackoverflow上进行搜索时,如果问题包含在一个最简单的例子中,那么他们会更容易地找到解决方案,并且问题的完整解释和易于验证和验证。但是要给你2个主要提示:1.代码中哪行代码初始化(创建)玩家? 2.你什么时候打电话/使用球员课程? –