python-3.x - 如何在 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()
解决方案
事实上,您的 Game 类没有中心。你的点可以。
__init__
在函数中为您的 Game 类添加一个中心。
或者
也许您打算改为调用self.small_dot.center
(在第 54 行)
推荐阅读
- hadoop - 使用 hadoop distcp 命令在 Dataproc 上等效的 s3-dist-cp groupBy
- javascript - 根据状态更改渲染组件
- c# - 如何在变量中使用 sql 查询结果?
- javascript - 如何将文件从外部 API 发送到客户端而不保存在服务器上?
- python - 如何使用许多工作人员发送 slurm 作业,而不仅仅是在本地模式下运行?
- python - conda 无法从 yml 创建环境
- json - Golang 运行时:goroutine 堆栈超过 1000000000 字节限制
- symfony - Xenforo - 传递给 XF\Repository\UserAlert::alertFromUser() 的参数 1 必须是 XF\Entity\User 的实例
- java - 使用 IAM 角色从 Lambda 连接到 RDS MySql 超时
- python - 为什么我的列表值在我的代码的某些部分没有改变?