def run_game(): # Get access to our game configuration configuration = Config() # initialize game pygame.init() screen = pygame.display.set_mode((configuration.screen_width, configuration.screen_height), 0, 32) clock = pygame.time.Clock() scoreboard = Scoreboard(screen, configuration) play_button = Button(screen, configuration.screen_width/2-configuration.button_width/2, configuration.screen_height/2-configuration.button_height/2, configuration, "Play", None, None, None) game_over_button = Button(screen, play_button.x_position, play_button.y_position-3*configuration.button_height, configuration, "Game Over!", None, None, (160,0,0)) score_button = Button(screen, play_button.x_position, play_button.y_position-2*configuration.button_height, configuration, "Score:", None, None, (255,69,0)) highest_score_button = Button(screen, play_button.x_position, play_button.y_position-configuration.button_height, configuration, "Highest Score:" + str(configuration.highest_score), None, None, (255,69,0)) heading_button = Button(screen, 0, 0, configuration, "Bows and Balloons", 800, None, (160,0,160)) home_page = HomePage(screen, configuration) balloons = [] demons = [] arrows = [] # Create our dagger # sword = Sword(screen, configuration.scoreboard_height) archer = Archer(screen, configuration.scoreboard_height) configuration.set_archer_width(archer.image_w) # Create our game game_play, with access to appropriate game parameters: game_play = GamePlay(screen, configuration, scoreboard, balloons, demons, archer, arrows) # main event loop while True: # Advance our game clock, get the current mouse position, and check for new events time_passed = clock.tick(50) mouse_x, mouse_y = pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1] game_play.check_events(play_button, mouse_x, mouse_y) # Redraw the empty screen before redrawing any game objects screen.fill(configuration.bg_color) if configuration.game_active: # Update the sword's position and check for popped or disappeared balloons game_play.update_archer(mouse_x, mouse_y, time_passed) game_play.check_balloons(time_passed) game_play.check_demons() game_play.check_arrows() scoreboard.update_time(time_passed) # If all balloons have disappeared, either through popping or rising, # release a new batch of balloons. if len(balloons) == 0: # If we are not just starting a game, increase the balloon speed and points per balloon, # and increment batches_finished if scoreboard.balloons_popped > 0: # Increase the balloon speed, and other factors, for each new batch of balloons. configuration.balloon_speed *= configuration.speed_increase_factor configuration.points_per_balloon = int(round(configuration.points_per_balloon * configuration.speed_increase_factor)) scoreboard.batches_finished += 1 # If player has completed required batches, increase batch_size if scoreboard.batches_finished % configuration.batches_needed == 0 and scoreboard.batches_finished > 0: configuration.batch_size += 1 game_play.release_batch() scoreboard.blitme() else: # Game is not active, so... # Show play button score_button.msg = "Score: " + str(scoreboard.score) score_button.prep_msg() play_button.blitme() # Show instructions for first few games. # if configuration.games_played < 3: home_page.blitme() # If a game has just ended, show Game Over button if configuration.games_played > 0: game_over_button.blitme() score_button.blitme() configuration.check_score(scoreboard.score) highest_score_button.msg = "Highest Score: " + str(configuration.highest_score) highest_score_button.prep_msg() highest_score_button.blitme() heading_button.blitme() # Display updated scoreboard # Show the redrawn screen pygame.display.flip()
def run_game(): settings = Settings() # initialise the game pygame.init() # returns a pyGame surface music = pygame.mixer.Sound("music.wav") screen = pygame.display.set_mode( (settings.screen_width, settings.screen_height), 0, 32) clock = pygame.time.Clock() scoreboard = Scoreboard(screen) play_button = Button( screen, settings.screen_width / 2 - settings.button_width / 2, settings.screen_height / 2 - settings.button_height / 2, settings, "Play Food Fetcher") instructions = Instructions(screen, settings) draw_title = Title(screen) foods = [] poisons = [] basket = Basket(screen) engine = Engine(screen, settings, scoreboard, foods, poisons, basket) # play music music.play(loops=-1) # main event loop # while True: while True: time_passed = clock.tick(50) mouse_x = pygame.mouse.get_pos()[0] mouse_y = pygame.mouse.get_pos()[1] engine.check_events(play_button, mouse_x, mouse_y) screen.fill(settings.bg_color) if settings.game_active: engine.update_basket(mouse_x) engine.check_foods(time_passed) engine.check_poisons(time_passed) if len(foods) == 0: if scoreboard.food_caught > 0: # Increase the balloon speed for each new batch of balloons. settings.food_speed *= settings.speed_increase_factor settings.poison_ratio *= settings.speed_increase_factor scoreboard.batches_finished += 1 # If player has completed required batches, increase batch_size if scoreboard.batches_finished % settings.batches_needed == 0 and scoreboard.batches_finished > 0: settings.batch_size += 1 engine.release_batch() else: play_button.blitme() draw_title.blitme( settings.screen_width / 2 - settings.button_width / 2, settings.screen_height / 2 - settings.button_height * 4) # If a game has just ended, show Game Over button if settings.games_played > 0: score = scoreboard.get_score() pizza_caught = scoreboard.get_caught_pizza() poison_caught = scoreboard.get_caught_poison() displayScore = DisplayScore(screen, settings, score, pizza_caught, poison_caught) displayScore.blitme() if settings.games_played < 1: instructions.blitme() # Display scoreboard scoreboard.blitme() pygame.display.flip()
def run_game(): # Get access to our game settings settings = Settings() engine = Engine() # initialize game pygame.init() screen = pygame.display.set_mode( (settings.screen_width, settings.screen_height), 0, 32) clock = pygame.time.Clock() scoreboard = Scoreboard(screen, settings) play_button = Button(screen, settings.screen_width/2-settings.button_width/2, settings.screen_height/2-settings.button_height/2, settings, "Play Balloon Ninja") game_over_button = Button(screen, play_button.x_position, play_button.y_position-2*settings.button_height, settings, "Game Over") instructions = Instructions(screen, settings) # Create a list to hold our balloons, and our kittens balloons = [] kittens = [] # Create our dagger sword = Sword(screen, settings.scoreboard_height) # main event loop while True: # Advance our game clock, get the current mouse position, and check for new events time_passed = clock.tick(50) mouse_x, mouse_y = pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1] engine.check_events(settings, scoreboard, sword, play_button, mouse_x, mouse_y, balloons) # Redraw the empty screen before redrawing any game objects screen.fill(settings.bg_color) if settings.game_active: # Update the sword's position and check for popped or disappeared balloons engine.update_sword(sword, mouse_x, mouse_y, settings) engine.check_balloons(balloons, kittens, sword, scoreboard, screen, settings, time_passed) engine.check_kittens(kittens, sword, scoreboard, screen, settings, time_passed) # If all balloons have disappeared, either through popping or rising, # release a new batch of balloons. if len(balloons) == 0: # If we are not just starting a game, increase the balloon speed and points per balloon, # and increment batches_finished if scoreboard.balloons_popped > 0: # Increase the balloon speed, and other factors, for each new batch of balloons. settings.balloon_speed *= settings.speed_increase_factor settings.kitten_ratio *= settings.speed_increase_factor settings.points_per_balloon = int(round(settings.points_per_balloon * settings.speed_increase_factor)) scoreboard.batches_finished += 1 # If player has completed required batches, increase batch_size if scoreboard.batches_finished % settings.batches_needed == 0 and scoreboard.batches_finished > 0: settings.batch_size += 1 engine.release_batch(screen, settings, balloons, kittens) else: # Game is not active, so... # Show play button play_button.blitme() # Show instructions for first few games. if settings.games_played < 3: instructions.blitme() # If a game has just ended, show Game Over button if settings.games_played > 0: game_over_button.blitme() # Display updated scoreboard scoreboard.blitme() # Show the redrawn screen pygame.display.flip()
def run_game(): # Get access to our game settings settings = Settings() # initialize game pygame.init() screen = pygame.display.set_mode( (settings.screen_width, settings.screen_height), 0, 32) clock = pygame.time.Clock() scoreboard = Scoreboard(screen, settings) play_button = Button( screen, settings.screen_width / 2 - settings.button_width / 2, settings.screen_height / 2 - settings.button_height / 2, settings, "Start Balloon Ninja") game_over_button = Button( screen, play_button.x_position, play_button.y_position - 2 * settings.button_height, settings, "Game Over") instructions = Instructions(screen, settings) # Create a list to hold our balloons, and our kittens balloons = [] #kittens = [] # Create our dagger sword = Sword(screen, settings.scoreboard_height) # Create our game engine, with access to appropriate game parameters: engine = Engine(screen, settings, scoreboard, balloons, sword) # main event loop while True: rcreen, center = ball.getit(settings.screen_width, settings.screen_height) img = pygame.transform.flip( pygame.transform.rotate(pygame.surfarray.make_surface((rcreen)), 270), True, False) time_passed = clock.tick(50) if center is None or center[0] is None: mousex, mousey = pygame.mouse.get_pos()[0], pygame.mouse.get_pos( )[1] else: mousex, mousey = center mouse_x, mouse_y = pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1] engine.check_events(play_button, mouse_x, mouse_y) # Redraw the empty screen before redrawing any game objects screen.blit(img, (0, 0)) if settings.game_active: # Update the sword's position and check for popped or disappeared balloons engine.update_sword(mousex, mousey) engine.check_balloons(time_passed, mousex, mousey) # If all balloons have disappeared, either through popping or rising, # release a new batch of balloons. if len(balloons) == 0: # If we are not just starting a game, increase the balloon speed and points per balloon, # and increment batches_finished if scoreboard.balloons_popped > 0: # Increase the balloon speed, and other factors, for each new batch of balloons. settings.balloon_speed *= settings.speed_increase_factor settings.kitten_ratio *= settings.speed_increase_factor settings.points_per_balloon = int( round(settings.points_per_balloon * settings.speed_increase_factor)) scoreboard.batches_finished += 1 # If player has completed required batches, increase batch_size if scoreboard.batches_finished % settings.batches_needed == 0 and scoreboard.batches_finished > 0: settings.batch_size += 1 engine.release_batch() else: # Game is not active, so... # Show play button play_button.blitme() # Show instructions for first few games. if settings.games_played < 3: instructions.blitme() # If a game has just ended, show Game Over button if settings.games_played > 0: game_over_button.blitme() # Display updated scoreboard scoreboard.blitme() # Show the redrawn screen pygame.display.flip()
def run_game(): # access to settings #width,height=800,600 settings =Settings() #initialize game pygame.init() sound= 'resources/back_sound.mp3' pygame.mixer.init() pygame.mixer.music.load(sound) pygame.mixer.music.play(-1) pygame.event.wait() screen=pygame.display.set_mode((settings.width,settings.height),0,32) pygame.display.set_caption("PopBob") clock =pygame.time.Clock() scoreboard = Scoreboard(screen, settings) play_button = Button(screen, settings.width/2-settings.button_width/2, settings.height/2-settings.button_height/2, settings, "Play") game_over_button = Button(screen,play_button.x_position, play_button.y_position-2*settings.button_height, settings, "Game Over") #balloons=[Balloon(screen, settings.balloon_speed)] # scoreboard_height=50 # game play params #balloon_speed= 0.1 #points_per_hit=10 instructions = Instructions(screen, settings) #scoreboard=Scoreboard(screen, settings.scoreboard_height) balloons = [] fish = [] #spawn_balloon(screen, settings, balloons) # balloons=[Balloon(screen, settings.balloon_speed)] sword=Sword(screen, settings.scoreboard_height) #new_balloon=Balloon(screen) #bg_image=pygame.image.load('resources/back_pop_ga.jpg') engine = Engine(screen, settings, scoreboard, balloons, fish, sword) while 1: time_passed = clock.tick(50) mouse_x,mouse_y = pygame.mouse.get_pos()[0],pygame.mouse.get_pos()[1] engine.check_events(play_button, mouse_x, mouse_y) print (pygame.mouse.get_pos()) screen.blit(settings.bg_image,(0,0)) if settings.game_active: engine.update_sword(mouse_x, mouse_y) engine.check_balloons(time_passed) engine.check_fishes(time_passed) if len(balloons)==0: if scoreboard.balloons_popped >0: settings.balloon_speed *= settings.speed_increase_factor settings.fish_ratio *= settings.speed_increase_factor settings.points_per_hit = int(round(settings.points_per_hit * settings.speed_increase_factor)) scoreboard.batches_finished +=1 if scoreboard.batches_finished % settings.batches_needed == 0 and scoreboard.batches_finished>0: settings.batch_size+=1 engine.release_batch() else : play_button.blitme() if settings.games_played < 3 : instructions.blitme() if settings.games_played >0: game_over_button.blitme() #displaying the scoreboard scoreboard.blitme() pygame.display.flip()
class Pong: def __init__(self): self.settings = Settings() self.screen = None self.screen_rect = None self.clock = None self.background = None self.welcome_board = None self.scoreboard = None self.victory_board = None self.ball = None self.players = None self.player_left = None self.player_right = None self.time_since_left_player_human_input = 0 self.time_since_right_player_human_input = 0 self.initialize_pygame() self.sound_game_start = None self.sound_game_end = None self.sound_you_lose = None self.load_sounds() self.__round = 0 self.__points_max = None self.is_playing = False self.time_since_last_game = 0 self.__ai_difficulty = self.settings.game_default_difficulty_value self.set_max_points(self.settings.game_default_points) self.start_ai_game() self.main_loop() def initialize_pygame(self): print("Initializing Pong game ...") pygame.mixer.pre_init(self.settings.sound_sample_rate, -16, self.settings.sound_channels, 1024) pygame.mixer.init() pygame.init() self.clock = pygame.time.Clock() width = int(self.settings.screen_width * self.settings.screen_resize) height = int(self.settings.screen_height * self.settings.screen_resize) self.screen = pygame.display.set_mode((width, height)) self.screen_rect = self.screen.get_rect() pygame.display.set_caption(self.settings.screen_title) self.background = Background(self.settings, self.screen) self.welcome_board = WelcomeBoard(self.settings, self.screen, self) self.scoreboard = Scoreboard(self.settings, self.screen, self) self.victory_board = VictoryBoard(self.settings, self.screen, self) self.ball = Ball(self.settings, self.screen) self.scoreboard.set_ball(self.ball) self.initialize_players() def initialize_players(self): self.players = [ Player(self.settings, self.screen, self.scoreboard), Player(self.settings, self.screen, self.scoreboard) ] self.player_left = self.players[0] self.player_right = self.players[1] self.scoreboard.set_players(self.player_left, self.player_right) def load_sounds(self): samples_dir = os.path.join("assets", "audio", "samples") self.sound_game_start = Sound( os.path.join(samples_dir, "GameStart.wav")) self.sound_game_end = Sound(os.path.join(samples_dir, "GameEnd.wav")) self.sound_you_lose = Sound(os.path.join(samples_dir, "Sad.wav")) def set_max_points(self, p): self.__points_max = p self.scoreboard.set_max_points(p) def start_ai_game(self): self.start_game() self.player_left.set_is_ai(True) self.player_right.set_is_ai(True) self.welcome_board.activate() def start_game(self): self.is_playing = True self.__round = 0 self.player_left.set_score(0) self.player_left.make_left_side() self.time_since_left_player_human_input = 0 self.player_left.set_is_ai(False) self.player_left.set_ai_difficulty(self.__ai_difficulty) self.player_right.set_score(0) self.player_right.make_right_side() self.time_since_right_player_human_input = 0 self.player_right.set_is_ai(True) self.player_right.set_ai_difficulty(self.__ai_difficulty) self.welcome_board.deactivate() channel = pygame.mixer.find_channel() if channel: channel.play(self.sound_game_start) self.start_round() def end_game(self): self.is_playing = False self.time_since_last_game = 0 self.scoreboard.dirty() #----------------------------------------# if self.player_left.get_score() > self.player_right.get_score(): player_winner = self.player_left else: player_winner = self.player_right self.victory_board.set_winner(player_winner) #----------------------------------------# if not self.welcome_board.is_activated(): self.victory_board.activate() #----------------------------------------# pygame.mixer.stop() channel = pygame.mixer.find_channel() if channel: channel.play(self.sound_game_end) #----------------------------------------# self.welcome_board.activate() def get_round(self): return self.__round def set_ai_difficulty(self, n): self.__ai_difficulty = n def start_round(self): #----------------------------------------# if self.player_left.get_score() >= self.__points_max: self.end_game() return if self.player_right.get_score() >= self.__points_max: self.end_game() return self.__round += 1 #----------------------------------------# for p in self.players: p.prepare_for_round() #----------------------------------------# self.ball.reset() self.ball.init_start_velocity() def main_loop(self): while True: # ----------------------------------------# elapsed_ms = self.clock.tick() # ----------------------------------------# self.handle_game_state(elapsed_ms) self.handle_events() # ----------------------------------------# self.update_players(elapsed_ms) self.update_ball(elapsed_ms) self.update_background(elapsed_ms) self.update_victory_board(elapsed_ms) # ----------------------------------------# self.check_for_win() # ----------------------------------------# self.draw() def handle_game_state(self, elapsed_ms): if self.welcome_board.wants_to_play(True): self.set_max_points(self.welcome_board.get_selected_points_value()) self.set_ai_difficulty( self.welcome_board.get_selected_difficulty_value()) self.start_game() elif self.welcome_board.wants_to_play_ai(True): self.set_max_points(self.welcome_board.get_selected_points_value()) self.set_ai_difficulty( self.welcome_board.get_selected_difficulty_value()) self.start_ai_game() self.welcome_board.deactivate() elif (not self.is_playing) and self.settings.can_start_ai_game: self.time_since_last_game += elapsed_ms if self.time_since_last_game > self.settings.game_auto_ai_game_timeout: self.start_ai_game() def handle_events(self): #----------------------------------------# for event in pygame.event.get(): # ----------------------------------------# if event.type == pygame.QUIT: sys.exit() # ----------------------------------------# elif self.welcome_board.handle_event(event): pass # ----------------------------------------# elif event.type == pygame.KEYUP or event.type == pygame.KEYDOWN: self.handle_keyboard_events(event) def handle_keyboard_events(self, event): if self.is_playing: if not self.welcome_board.is_activated(): self.handle_keyboard_player_events(event) # Q for Quit if event.key == pygame.K_q: sys.exit(0) def handle_keyboard_player_events(self, event): # If there are two players, use W/A/S/D and UP/DOWN/LEFT/RIGHT if (not self.player_left.is_ai()) and (not self.player_right.is_ai()): left_up = pygame.K_w left_down = pygame.K_s left_left = pygame.K_a left_right = pygame.K_d # right_up = pygame.K_UP right_down = pygame.K_DOWN right_left = pygame.K_LEFT right_right = pygame.K_RIGHT else: left_up = pygame.K_UP left_down = pygame.K_DOWN left_left = pygame.K_LEFT left_right = pygame.K_RIGHT right_up = pygame.K_w right_down = pygame.K_s right_left = pygame.K_a right_right = pygame.K_d #----------------------------------------# if event.key == left_up: self.time_since_left_player_human_input = 0 if self.player_left.is_ai(): self.player_left.set_is_ai(False) if event.type == pygame.KEYUP: self.player_left.set_moving_up(False) elif event.type == pygame.KEYDOWN: self.player_left.set_moving_up(True) #----------------------------------------# if event.key == left_down: self.time_since_left_player_human_input = 0 if self.player_left.is_ai(): self.player_left.set_is_ai(False) if event.type == pygame.KEYUP: self.player_left.set_moving_down(False) elif event.type == pygame.KEYDOWN: self.player_left.set_moving_down(True) #----------------------------------------# if event.key == left_left: self.time_since_left_player_human_input = 0 if self.player_left.is_ai(): self.player_left.set_is_ai(False) if event.type == pygame.KEYUP: self.player_left.set_moving_left(False) elif event.type == pygame.KEYDOWN: self.player_left.set_moving_left(True) #----------------------------------------# if event.key == left_right: self.time_since_left_player_human_input = 0 if self.player_left.is_ai(): self.player_left.set_is_ai(False) if event.type == pygame.KEYUP: self.player_left.set_moving_right(False) elif event.type == pygame.KEYDOWN: self.player_left.set_moving_right(True) #----------------------------------------# if event.key == right_up: self.time_since_right_player_human_input = 0 if self.player_right.is_ai(): self.player_right.set_is_ai(False) if event.type == pygame.KEYUP: self.player_right.set_moving_up(False) elif event.type == pygame.KEYDOWN: self.player_right.set_moving_up(True) #----------------------------------------# if event.key == right_down: self.time_since_right_player_human_input = 0 if self.player_right.is_ai(): self.player_right.set_is_ai(False) if event.type == pygame.KEYUP: self.player_right.set_moving_down(False) elif event.type == pygame.KEYDOWN: self.player_right.set_moving_down(True) #----------------------------------------# if event.key == right_left: self.time_since_right_player_human_input = 0 if self.player_right.is_ai(): self.player_right.set_is_ai(False) if event.type == pygame.KEYUP: self.player_right.set_moving_left(False) elif event.type == pygame.KEYDOWN: self.player_right.set_moving_left(True) #----------------------------------------# if event.key == right_right: self.time_since_right_player_human_input = 0 if self.player_right.is_ai(): self.player_right.set_is_ai(False) if event.type == pygame.KEYUP: self.player_right.set_moving_right(False) elif event.type == pygame.KEYDOWN: self.player_right.set_moving_right(True) def update_players(self, elapsed_ms): if not self.is_playing: return self.time_since_left_player_human_input += elapsed_ms self.time_since_right_player_human_input += elapsed_ms #----------------------------------------# if self.player_left.is_ai() != self.player_right.is_ai(): if self.player_left.is_ai(): self.player_left.set_is_ai(False) self.player_right.set_is_ai(True) # for p in self.players: p.update(self.ball, elapsed_ms) def update_ball(self, elapsed_ms): if not self.is_playing: return self.ball.update(self.players, elapsed_ms) def check_for_win(self): if not self.is_playing: return x, y = self.ball.get_position() #----------------------------------------# if x < 0: self.player_wins_round(self.player_right) elif x > self.screen_rect.width: self.player_wins_round(self.player_left) #----------------------------------------# elif not (0 <= y < self.screen_rect.height): print("Ball out of bounds:", (x, y)) half = self.screen_rect.width / 2 if x > half: self.player_wins_round(self.player_left) else: self.player_wins_round(self.player_right) def update_background(self, elapsed_ms): self.background.update(elapsed_ms) def update_victory_board(self, elapsed_ms): self.victory_board.update(elapsed_ms) def player_wins_round(self, player): # print("Player:", player, " wins round!") player.adjust_score(1) self.scoreboard.dirty() channel = pygame.mixer.find_channel() if channel: channel.play(self.sound_you_lose) self.start_round() def draw(self): self.background.blitme() self.scoreboard.blitme() for p in self.players: p.blitme() self.ball.blitme() self.welcome_board.blitme() self.victory_board.blitme() pygame.display.flip()