如何在PONG游戏中更新分数

第54行出现了问题...我不知道该如何解决... (builtins.AttributeError:“游戏”对象没有属性“居中”) 帮助______________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

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],100])
      self.rect2 = pygame.Rect([450,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.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('',(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()
cy19900528 回答:如何在PONG游戏中更新分数

实际上,您的游戏班级没有中心。点就可以了。

__init__函数中向游戏类添加中心。

也许您打算改为致电self.small_dot.center(在第54行中)

本文链接:https://www.f2er.com/3163892.html

大家都在问