-3

我使用 Python 做了一个 packman 游戏,但我遇到了 2 个问题。首先我的分数没有更新,所以分数仍然是 1。过了一会儿,我的 packman 游戏崩溃了,它显示了 2 条错误消息,说属性错误:在 packman.handle_collide() 和 self.check_collide() 中。我不知道如何修复它。这是我的代码:

 # Create a SNAPMAN

 from livewires import games, color
 import random

 games.init(screen_width = 200, screen_height = 150, fps = 50)
 explosion_files = ["explosion1.bmp",
       "explosion2.bmp",
       "explosion3.bmp",
       "explosion4.bmp",
       "explosion5.bmp",
       "explosion6.bmp",
       "explosion7.bmp",
       "explosion8.bmp",
       "explosion9.bmp"]
def display_score(score):
    display_score = games.Text(value = score + 1, size = 25, color = color.black,
            top = 5, right = games.screen.width - 10)
    games.screen.add(display_score)
    #increase the score
    score = score + 1
class Packman(games.Sprite):
 """Create a Gumpy that is controlled by the mouse"""
  image = games.load_image("packman.png")
  def __init__(self, x = games.mouse.x, y = games.mouse.y):
    """Initialise packman"""
    super(Packman,self).__init__(image = Packman.image,
                                 x = games.mouse.x,
                                 y = games.mouse.y)
    oldx = games.mouse.x
    oldy = games.mouse.y
 def display_score(score):
    display_score = games.Text(value = score + 1, size = 25, color = color.black,
            top = 5, right = games.screen.width - 10)
    games.screen.add(display_score)
    #increase the score
    score = score + 1

 def update(self):
    """Move Packman's x and y coordinates"""
    oldx = self.x
    oldy = self.y
    self.x = games.mouse.x
    self.y = games.mouse.y
    if(self.x < oldx):
        self.angle = 180
    if(self.x > oldx):
        self.angle = 0

    self.check_collide()

 def check_collide(self):
    """Check if collides with ball"""
    for packman in self.overlapping_sprites:
        packman.handle_collide()



 class Ball(games.Sprite):
  """ Create the moving balls"""
  def update(self):
    """Change the direction when the ball reached the edge"""
    if self.right > games.screen.width or self.left < 0:
        self.dx = -self.dx
    if self.bottom > games.screen.height or self.top < 0:
        self.dy = -self.dy

   def handle_collide(self):
    """Something must happen when the ball collides"""
    #Explosive sound
    sound = games.load_sound("explosion.wav")
    sound.play()
     explosion = games.Animation(images = explosion_files, x = self.x, y = self.y,
                                n_repeats = 5, repeat_interval = 5,
                                is_collideable =        False)
    games.screen.add(explosion)
    self.x = random.randrange(games.screen.width)
    self.y = random.randrange(games.screen.height)
    display_score(1)



 def randomX():
    """Generate random x values"""
    rx = random.randrange(games.screen.width)
    return rx
 def ramdomY():
    """Generate random y values"""
    ry = random.randrange(games.screen.width)
    return ry

 def main():
 #Set background
 wall_image = games.load_image("wall.jpg", transparent = False)
 games.screen.background = wall_image
 games.mouse.is_visible = False
 games.screen.event_grab = True
 #Display score
 display_score(0)
 #Load and display the balls( red, green and blue)
 ball_image = games.load_image("ball_red.png")
 ball_red = Ball(image = ball_image,
        x = random.randrange(games.screen.width),
        y = random.randrange(games.screen.height),
        dx = 1, dy = 1)
 games.screen.add(ball_red)
 ball_image = games.load_image("ball_green.png")
 ball_green = Ball(image = ball_image,
          x = random.randrange(games.screen.width),
          y = random.randrange(games.screen.height),
          dx = 1, dy = 1)
 games.screen.add(ball_green)
 ball_image = games.load_image("ball_blue.png")
 ball_blue = Ball(image = ball_image,
         x = random.randrange(games.screen.width),
         y = random.randrange(games.screen.height),
         dx = 1, dy = 1)
 games.screen.add(ball_blue)

 packman_1 = Packman()
 games.screen.add(packman_1)

 games.screen.mainloop()



 main()
4

1 回答 1

0

分数没有更新,因为您正在递增方法 display_score 的局部变量。你可以让它成为 Packman 的类成员,例如:

def __init__(self, x = games.mouse.x, y = games.mouse.y):
    """Initialise packman"""
    # do something ...

    # initialize the score variable
    self._score = 0

接着,

def display_score(self):
    display_score = games.Text(value = self._score, size = 25,
            color = color.black, top = 5, right = games.screen.width - 10)
    games.screen.add(display_score)
    #increase the score
    self._score += 1    # add one to the current value

您错过了方法定义中的self参数!

于 2013-09-07T05:36:38.187 回答