首页 > 解决方案 > 如何在 PONG 游戏中更新分数

问题描述

第 54 行出了点问题...我不知道如何解决此问题...(builtins.AttributeError: 'Game' object has no attribute 'center')

import pygame

def main():
   pygame.init()
   pygame.display.set_mode((500, 400))
   pygame.display.set_caption('Pong')   
   w_surface = pygame.display.get_surface() 
   game = Game(w_surface)
   game.play() 
   pygame.quit() 

class Game:

   def __init__(self, surface):
      self.surface = surface
      self.bg_color = pygame.Color('black')
      self.fg_color = pygame.Color('white')
      self.FPS = 60
      self.game_Clock = pygame.time.Clock()
      self.close_clicked = False
      self.continue_game = True
      self.small_dot = Dot('white', 10, [10, 10], [2, 2], self.surface)
      self.rect1 = pygame.Rect([50,150], [10, 100])
      self.rect2 = pygame.Rect([450,150], [10, 100])      
      self.max_frames = 100000
      self.frame_counter = 0
      self.score1 = 0
      self.score2 = 0

   def play(self):
      while not self.close_clicked:  
         self.handle_events()
         self.draw()            
         if self.continue_game:
            self.update()
            self.decide_continue()
         self.game_Clock.tick(self.FPS)
   def handle_events(self):
      events = pygame.event.get()
      for event in events:
         if event.type == pygame.QUIT:
            self.close_clicked = True
   def draw(self):   
      self.surface.fill(self.bg_color) 
      self.small_dot.draw()
      self.draw_score1()
      self.draw_score2()
      pygame.draw.rect(self.surface, self.fg_color, self.rect1)
      pygame.draw.rect(self.surface, self.fg_color, self.rect2)
      pygame.display.update()

   def update(self):
      self.small_dot.move()
      if self.center[0] > 490:
         self.score1 += self.score1
      self.frame_counter = self.frame_counter + 1

   def decide_continue(self):
      if self.frame_counter > self.max_frames:
         self.continue_game = False

   def draw_score1(self):  
      score_string = 'Score: '+str(self.score1)
      score_fg_color = pygame.Color('white')
      score_font = pygame.font.SysFont('', 35)
      score_image = score_font.render(score_string, True, score_fg_color)
      self.surface.blit(score_image, (0, 0))

   def draw_score2(self):  
      score_string = 'Score: '+str(self.score2)
      score_fg_color = pygame.Color('white')
      score_font = pygame.font.SysFont('', 35)
      score_image = score_font.render(score_string, True, score_fg_color)
      self.surface.blit(score_image, (400, 0))   


class Dot:
   def __init__(self, dot_color, dot_radius, dot_center, dot_velocity, surface):
      self.color = pygame.Color(dot_color)
      self.radius = dot_radius
      self.center = dot_center
      self.velocity = dot_velocity
      self.surface = surface

   def move(self):
      self.center[0] += self.velocity[0]
      self.center[1] += self.velocity[1]

      if self.center[0] < 10:
         self.velocity[0] = -self.velocity[0]
      if self.center[1] < 10:
         self.velocity[1] = -self.velocity[1]
      if self.center[0] > 490:
         self.velocity[0] = -self.velocity[0]
      if self.center[1] > 390:
         self.velocity[1] = -self.velocity[1]           
   def draw(self):
      pygame.draw.circle(self.surface, self.color, self.center, self.radius)
main()

标签: python-3.x

解决方案


事实上,您的 Game 类没有中心。你的点可以。

__init__在函数中为您的 Game 类添加一个中心。

或者

也许您打算改为调用self.small_dot.center(在第 54 行)


推荐阅读