Reputation: 13
Something is wrong on line 54... I don't know how to fix this... (builtins.AttributeError: 'Game' object has no attribute 'center') Help______________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
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()
Upvotes: 0
Views: 120
Reputation: 5735
Indeed your Game class does not have a center. Your Dot does.
Add a center to your Game class in the __init__
function.
Or
Maybe you meant to call self.small_dot.center
instead (in line 54)
Upvotes: 1