def __init__(self, configuration): self.config = configuration self.score = Score() self.score_drawer = ScoreDrawer(self.config.background) self.ball = Ball(*self._init_sounds()) racket_x_position = 60 self.racket1 = Racket(racket_x_position, constants.SCREEN_WIDTH/2 - 35) self.racket2 = Racket( constants.SCREEN_WIDTH - racket_x_position - 10, constants.SCREEN_WIDTH/2 - 35 ) self.net = Net() self.the_winner_is = ''
class GamingScene(object): R1_DOWN = 1 R1_UP = 2 R2_DOWN = 3 R2_UP = 4 def __init__(self, configuration): self.config = configuration self.score = Score() self.score_drawer = ScoreDrawer(self.config.background) self.ball = Ball(*self._init_sounds()) racket_x_position = 60 self.racket1 = Racket(racket_x_position, constants.SCREEN_WIDTH/2 - 35) self.racket2 = Racket( constants.SCREEN_WIDTH - racket_x_position - 10, constants.SCREEN_WIDTH/2 - 35 ) self.net = Net() self.the_winner_is = '' def reset(self): """Use this method to reset the scene (sprite position, score,...)""" racket_x_position = 60 self.racket1 = Racket(racket_x_position, constants.SCREEN_WIDTH/2 - 35) self.racket2 = Racket( constants.SCREEN_WIDTH - racket_x_position - 10, constants.SCREEN_WIDTH/2 - 35 ) self.score.reset() def play(self): y_offset_one = 0 y_offset_two = 0 accel_r1 = 0 accel_r2 = 0 previous_r1 = 0 previous_r2 = 0 end_game = False while not end_game: if previous_r1 == self.R1_DOWN and accel_r1 < 30: accel_r1 += 1 if previous_r1 == self.R1_UP and accel_r1 > -30: accel_r1 -= 1 if previous_r2 == self.R2_DOWN and accel_r2 < 30: accel_r2 += 1 if previous_r2 == self.R2_UP and accel_r2 > -30: accel_r2 -= 1 if previous_r1 == 0: accel_r1 = 0 if previous_r2 == 0: accel_r2 = 0 for event in pygame.event.get(): if event.type == pygame.QUIT: exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_e: y_offset_one = -1 previous_r1 = self.R1_UP if event.key == pygame.K_d: y_offset_one = 1 previous_r1 = self.R1_DOWN if event.key == pygame.K_UP: y_offset_two = -1 previous_r2 = self.R2_UP if event.key == pygame.K_DOWN: y_offset_two = 1 previous_r2 = self.R2_DOWN if event.type == pygame.KEYUP: if event.key in [pygame.K_e, pygame.K_d]: y_offset_one = 0 previous_r1 = 0 if event.key in [pygame.K_UP, pygame.K_DOWN]: y_offset_two = 0 previous_r2 = 0 # Update sprites self.racket1.update_position(y_offset_one, accel_r1) self.racket2.update_position(y_offset_two, accel_r2) self.ball.update_position(self.racket1, self.racket2) self.score.check_score(self.ball) # This fill call is shitty... self.config.background.fill((0, 0, 0)) self.net.draw(self.config.background) self.score_drawer.draw(self.score) self.ball.draw(self.config.background) self.racket1.draw(self.config.background) self.racket2.draw(self.config.background) self.config.screen.blit(self.config.background, (0, 0)) pygame.display.flip() self.config.clock.tick(constants.REFRESH_RATE) if self.score.winner != 0: end_game = True self.the_winner_is = 'Player %d' % self.score.winner def _init_sounds(self): """This functions initializes the sounds, stored in yapong/sounds, that will be played during the game.""" beep_filename_1 = os.path.join(SOUND_DIR, 'beep-7.wav') beep_filename_2 = os.path.join(SOUND_DIR, 'beep-8.wav') sound_1 = sound_2 = None if os.path.isfile(beep_filename_1): sound_1 = pygame.mixer.Sound(beep_filename_1) else: print("ERROR : beep_filename_1 not found") if os.path.isfile(beep_filename_2): sound_2 = pygame.mixer.Sound(beep_filename_2) else: print("ERROR : beep_filename_2 not found") return sound_1, sound_2