def __init__(self): self.status = False self.select_sound = mixer.Sound( Utils.get_path('sound/select-menu.wav')) self.select_sound.set_volume(2) self.check_sound = mixer.Sound(Utils.get_path('sound/select-menu.wav')) self.check_sound.set_volume(5)
def init_screen(self): self.sounds = { 'ping': pygame.mixer.Sound(Utils.get_path('sound/click.wav')), 'click': pygame.mixer.Sound(Utils.get_path('sound/paddle-hit.wav')), 'da-ding': pygame.mixer.Sound(Utils.get_path('sound/da-ding.wav')) } self.sounds['ping'].set_volume(0.05) self.sounds['click'].set_volume(0.5) self.sounds['da-ding'].set_volume(0.5) self.bg = pygame.image.load(Utils.get_path('image/pingpong-table.png')) self.font = { 18: pygame.font.SysFont('Times New Roman', 18), 72: pygame.font.SysFont('Times New Roman', 72) } up_key = pygame.K_w down_key = pygame.K_s if Setting.PLAY_MODE == Setting.SINGLE_MODE: up_key = None down_key = None paddle_1 = Paddle(5, self.screen_size[1]/2-30, 10, 60, None, None, down_key, up_key, self.surface, self.dt, self.screen_size) paddle_2 = Paddle(self.screen_size[0]-5-10, self.screen_size[1]/2-30, 10, 60, None, None, pygame.K_DOWN, pygame.K_UP, self.surface, self.dt, self.screen_size) self.paddles = [paddle_1, paddle_2] player_1 = Player((0, 255, 0), self.paddles[:1], self.sounds['click'], self.sounds['da-ding']) player_2 = Player((247, 52, 12), self.paddles[1:], self.sounds['click'], self.sounds['da-ding']) self.players = [player_1, player_2] self.ball = Ball(self.screen_size[0]/2, self.screen_size[1]/2,200.0, self.surface)
def move(self, rel_x, rel_y): self.pos[0] += self.dt * rel_x self.pos[1] -= self.dt * rel_y self.pos[0] = Utils.clamp(self.pos[0], 0, self.screen_size[0] - self.dim[0]) self.pos[1] = Utils.clamp(self.pos[1], 0, self.screen_size[1] - self.dim[1])
def __init__(self, base_game, surface): BaseScreen.__init__(self) self.base_game = base_game self.status = False self.surface = surface self.dt = 1.0 / 60.0 self.bg = pygame.image.load(Utils.get_path('image/menu-bg.png')) self.font = { 20: pygame.font.Font(Utils.get_path('font/ActualBook.otf'), 20), 55: pygame.font.Font(Utils.get_path('font/ActualBook.otf'), 55) } self.high_score_list = [] self.get_high_score()
def __init__(self, base_game, surface): BaseScreen.__init__(self) self.base_game = base_game self.status = False self.surface = surface self.dt = 1.0/60.0 self.bg = pygame.image.load(Utils.get_path('image/menu-bg.png')) self.font = { 20: pygame.font.Font(Utils.get_path('font/ActualBook.otf'), 20), 55: pygame.font.Font(Utils.get_path('font/ActualBook.otf'), 55) } self.high_score_list = [] self.get_high_score()
def draw(self): self.surface.blit(self.bg, (0, 0)) for ball in self.balls: ball.draw() for player in self.players: for paddle in player.paddles: paddle.draw(player.color) p1_score_text = self.font[18].render('Score '+str(self.players[0].score), True, (255, 255, 255)) p2_score_text = self.font[18].render('Score '+str(self.players[1].score), True, (255, 255, 255)) self.surface.blit(p1_score_text, (20, 20)) self.surface.blit(p2_score_text, (self.screen_size[0]-p2_score_text.get_width()-20, 20)) if Setting.between_rounds_timer > 0: alpha = Setting.between_rounds_timer - int(Setting.between_rounds_timer) alpha = Utils.round_int(255*alpha) count = self.font[72].render(str(int(Setting.between_rounds_timer)+1),True,(alpha,alpha,alpha)) sc = 0.5*(1.0 + Setting.between_rounds_timer-int(Setting.between_rounds_timer)) count = pygame.transform.smoothscale(count,list(map(Utils.round_int,[count.get_width()*sc,count.get_height()*sc]))) self.surface.blit(count,(self.screen_size[0]/2-count.get_width()/2,self.screen_size[1]/2-count.get_height()/2)) pygame.display.flip()
def __init__(self): display.init() font.init() mixer.init(buffer=0) self.screen_size = [800, 500] self.surface = display.set_mode(self.screen_size) life_image = image.load(Utils.get_path("image/icon.png")).convert_alpha() display.set_icon(life_image) display.set_caption("Ping Pong") display.set_icon(life_image) self.theme_sound = mixer.Sound(Utils.get_path("sound/menu-screen-theme.wav")) self.theme_sound.set_volume(0.25) if Setting.MUSIC: self.theme_sound.play(-1) self.switch_screen(Setting.MENU_SCREEN)
def __init__(self, surface, text, font_size, x, y): self.font_size = font_size self.font = { self.font_size: font.Font(Utils.get_path('font/FEASFBI.TTF'), self.font_size), } self.text = text self.surface = surface self.x = x self.y = y
def init_screen(self): self.sounds = { 'ping': pygame.mixer.Sound(Utils.get_path('sound/click.wav')), 'click': pygame.mixer.Sound(Utils.get_path('sound/paddle-hit.wav')), 'da-ding': pygame.mixer.Sound(Utils.get_path('sound/da-ding.wav')) } self.sounds['ping'].set_volume(0.05) self.sounds['click'].set_volume(0.5) self.sounds['da-ding'].set_volume(0.5) self.bg = pygame.image.load(Utils.get_path('image/pingpong-table.png')) self.font = { 18: pygame.font.SysFont('Times New Roman', 18), 72: pygame.font.SysFont('Times New Roman', 72) } up_key = pygame.K_w down_key = pygame.K_s if Setting.PLAY_MODE == Setting.SINGLE_MODE: up_key = None down_key = None paddle_1 = Paddle(5, self.screen_size[1] / 2 - 30, 10, 60, None, None, down_key, up_key, self.surface, self.dt, self.screen_size) paddle_2 = Paddle(self.screen_size[0] - 5 - 10, self.screen_size[1] / 2 - 30, 10, 60, None, None, pygame.K_DOWN, pygame.K_UP, self.surface, self.dt, self.screen_size) self.paddles = [paddle_1, paddle_2] player_1 = Player((0, 255, 0), self.paddles[:1], self.sounds['click'], self.sounds['da-ding']) player_2 = Player((247, 52, 12), self.paddles[1:], self.sounds['click'], self.sounds['da-ding']) self.players = [player_1, player_2] self.ball = Ball(self.screen_size[0] / 2, self.screen_size[1] / 2, 200.0, self.surface)
def __init__(self): display.init() font.init() mixer.init(buffer=0) self.screen_size = [800, 500] self.surface = display.set_mode(self.screen_size) life_image = image.load( Utils.get_path('image/icon.png')).convert_alpha() display.set_icon(life_image) display.set_caption('Ping Pong') display.set_icon(life_image) self.theme_sound = mixer.Sound( Utils.get_path('sound/menu-screen-theme.wav')) self.theme_sound.set_volume(0.25) if Setting.MUSIC: self.theme_sound.play(-1) self.switch_screen(Setting.MENU_SCREEN)
def play(self): clock = pygame.time.Clock() while True: if not self.get_input() or self.stop: break self.update() self.move() if Setting.PLAY_MODE == Setting.SINGLE_MODE: self.ai_move() self.draw() clock.tick(60) self.dt = 1.0 / Utils.clamp(clock.get_fps(), 30, 90)
def play(self): clock = pygame.time.Clock() while True: if not self.get_input() or self.stop: break self.update() self.move() if Setting.PLAY_MODE == Setting.SINGLE_MODE: self.ai_move() self.draw() clock.tick(60) self.dt = 1.0/Utils.clamp(clock.get_fps(), 30, 90)
def ai_move(self): move_speed = 5 if self.balls and self.balls[0].speed[0] < 0: for paddle in self.players[0].paddles: if abs(paddle.pos[1]-self.balls[0].pos[1]) > 50: if paddle.pos[1] > self.balls[0].pos[1]: paddle.pos[1] -= move_speed else: paddle.pos[1] += move_speed else: paddle.pos[1] = self.balls[0].pos[1] - paddle.dim[1]/2 # paddle.pos[1] += 5 paddle.pos[1] = Utils.clamp(paddle.pos[1], 0, self.screen_size[1] - paddle.dim[1])
def ai_move(self): move_speed = 5 if self.balls and self.balls[0].speed[0] < 0: for paddle in self.players[0].paddles: if abs(paddle.pos[1] - self.balls[0].pos[1]) > 50: if paddle.pos[1] > self.balls[0].pos[1]: paddle.pos[1] -= move_speed else: paddle.pos[1] += move_speed else: paddle.pos[1] = self.balls[0].pos[1] - paddle.dim[1] / 2 # paddle.pos[1] += 5 paddle.pos[1] = Utils.clamp(paddle.pos[1], 0, self.screen_size[1] - paddle.dim[1])
def __init__(self, base_game, surface): BaseScreen.__init__(self) self.base_game = base_game self.status = False self.surface = surface self.dt = 1.0 / 60.0 self.bg = pygame.image.load(Utils.get_path('image/menu-bg.png')) self.menu_text = Menu(self.surface, 'Setting', 70, 565, 60) self.menu_text.is_selected = True menu_sound = SettingMenu(self.surface, 'Sound', 35, 560, 150) menu_sound.is_selected = True menu_music = SettingMenu(self.surface, 'Music', 35, 565, 190) menu_exit = Menu(self.surface, 'Back', 35, 570, 230) self.menus = [menu_sound, menu_music, menu_exit]
def __init__(self, base_game, surface): BaseScreen.__init__(self) self.base_game = base_game self.status = False self.surface = surface self.dt = 1.0/60.0 self.bg = pygame.image.load(Utils.get_path('image/menu-bg.png')) self.menu_text = Menu(self.surface, 'Setting', 70, 565, 60) self.menu_text.is_selected = True menu_sound = SettingMenu(self.surface, 'Sound', 35, 560, 150) menu_sound.is_selected = True menu_music = SettingMenu(self.surface, 'Music', 35, 565, 190) menu_exit = Menu(self.surface, 'Back', 35, 570, 230) self.menus = [menu_sound, menu_music, menu_exit]
def __init__(self, base_game, surface): BaseScreen.__init__(self) self.base_game = base_game self.status = False self.surface = surface self.bg = pygame.image.load(Utils.get_path('image/menu-bg.png')) self.menu_text = Menu(self.surface, 'Menu', 70, 565, 30) menu_single = Menu(self.surface, 'Single Player', 35, 560, 120) menu_single.is_selected = True menu_multi = Menu(self.surface, 'Multi Player', 35, 565, 160) menu_setting = Menu(self.surface, 'Settings', 35, 570, 200) menu_high_score = Menu(self.surface, 'High Score', 35, 575, 240) menu_exit = Menu(self.surface, 'Exit Game', 35, 577, 280) self.menus = [menu_single, menu_multi, menu_setting, menu_high_score, menu_exit]
def draw(self): self.surface.blit(self.bg, (0, 0)) for ball in self.balls: ball.draw() for player in self.players: for paddle in player.paddles: paddle.draw(player.color) p1_score_text = self.font[18].render( 'Score ' + str(self.players[0].score), True, (255, 255, 255)) p2_score_text = self.font[18].render( 'Score ' + str(self.players[1].score), True, (255, 255, 255)) self.surface.blit(p1_score_text, (20, 20)) self.surface.blit( p2_score_text, (self.screen_size[0] - p2_score_text.get_width() - 20, 20)) if Setting.between_rounds_timer > 0: alpha = Setting.between_rounds_timer - int( Setting.between_rounds_timer) alpha = Utils.round_int(255 * alpha) count = self.font[72].render( str(int(Setting.between_rounds_timer) + 1), True, (alpha, alpha, alpha)) sc = 0.5 * (1.0 + Setting.between_rounds_timer - int(Setting.between_rounds_timer)) count = pygame.transform.smoothscale( count, list( map(Utils.round_int, [count.get_width() * sc, count.get_height() * sc]))) self.surface.blit( count, (self.screen_size[0] / 2 - count.get_width() / 2, self.screen_size[1] / 2 - count.get_height() / 2)) pygame.display.flip()
def __init__(self): self.status = False self.select_sound = mixer.Sound(Utils.get_path('sound/select-menu.wav')) self.select_sound.set_volume(2) self.check_sound = mixer.Sound(Utils.get_path('sound/select-menu.wav')) self.check_sound.set_volume(5)
def get_high_score(self): high_score_file = open(Utils.get_path('doc/high-score.txt'), 'r') stuff = high_score_file.readlines() for line in stuff: self.high_score_list.append(line) high_score_file.close()