-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 のローカル変数をインクリメントしているため、スコアが更新されていません。たとえば、Pac​​kman のクラス メンバーにすることができます。

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 に答える