def game_play(screen, screen_rect): playing = True game_state = GameState.STARTING number_of_laps = 2 # Load images, assign to sprite classes img = fileUtils.load_image('rider1-placeholder.png') Player.images = [img, pygame.transform.flip(img, 1, 0)] # decorate the game window # icon = pygame.transform.scale(Rider.images[0], (32, 32)) # pygame.display.set_icon(icon) # pygame.display.set_caption('Pygame Aliens') # pygame.mouse.set_visible(0) # create the background, tile the bgd image background = fileUtils.load_image('pumptrack.png') background_rect = background.get_rect() screen.blit(background, background_rect) pygame.display.flip() background_in_alpha = pygame.Surface( (screen_rect.size[0], screen_rect.size[1])) background_in_alpha.set_alpha(128) background_in_alpha.fill(colors.black) # load the sound effects # shoot_sound = load_sound('car_door.wav') """if pygame.mixer: music = os.path.join(main_dir, 'data', 'house_lo.wav') pygame.mixer.music.load(music) pygame.mixer.music.play(-1)""" # fileUtils.load_music("Lame_Drivers_-_01_-_Frozen_Egg.mp3") # pygame.mixer.music.play(-1) # Initialize Game Groups aliens = pygame.sprite.Group() all = pygame.sprite.RenderUpdates() lastalien = pygame.sprite.GroupSingle() # assign default groups to each sprite class Player.containers = all Rider.containers = aliens, all, lastalien TimeCounter.containers = all # Create Some Starting Values clock = pygame.time.Clock() # initialize our starting sprites player = Player(screen_rect, way_points_pump_track, True) time_counter = TimeCounter() time_countdown = CountDown() game_over = GameOverScreen() pause_menu = PauseScreen() if pygame.font: all.add(time_counter) button_down = False while playing: # get input for event in pygame.event.get(): game_over_state = game_over.update_input(event) if game_over_state == GameOverState.RESTART: game_state = GameState.RESTART elif game_over_state == GameOverState.QUIT: return quit_game = pause_menu.update_input(event) if quit_game: return if event.type == pygame.QUIT \ or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE and game_state == GameState.PLAYING): game_state = GameState.PAUSED if event.type == pygame.KEYDOWN \ and event.key == pygame.K_SPACE \ and button_down is False \ and game_state == GameState.PLAYING: button_down = True all.clear(screen, background) all.update() # player.update_state(game_state) time_counter.set_state(game_state) time_countdown.set_state(game_state) game_over.set_state(game_state) time_countdown.update() if time_countdown.countdown <= 0 and game_state == GameState.STARTING: game_state = GameState.PLAYING screen.blit(background, background_rect) pygame.display.flip() if game_state == GameState.PLAYING: player.pump() if button_down is True: player.add_pump() button_down = False if player.lap >= number_of_laps: game_state = GameState.GAME_OVER game_over.set_final_time(time_counter.time_in_millis) if game_state == GameState.STARTING: time_countdown.draw(screen, screen_rect, background, background_in_alpha) pygame.display.update() if game_state == GameState.PAUSED: pause_menu.draw(screen, screen_rect, background, background_in_alpha) pygame.display.update() if game_state == GameState.GAME_OVER: game_over.draw(screen, screen_rect, background, background_in_alpha) pygame.display.update() if game_state == GameState.RESTART: clock = pygame.time.Clock() player = Player(screen_rect, way_points_pump_track, True) time_counter = TimeCounter() time_countdown = CountDown() game_state = GameState.STARTING dirty = all.draw(screen) pygame.display.update(dirty) clock.tick(30)
class World: keys1 = { 'right': pygame.K_RIGHT, 'left': pygame.K_LEFT, 'up': pygame.K_UP, 'down': pygame.K_DOWN, 'space': pygame.K_SPACE, 'attack1': pygame.K_m, 'attack2': pygame.K_COMMA, 'attack3': pygame.K_PERIOD, 'attack4': pygame.K_SLASH } keys2 = { 'right': pygame.K_d, 'left': pygame.K_a, 'up': pygame.K_w, 'down': pygame.K_s, 'space': pygame.K_f, 'attack1': pygame.K_e, 'attack2': pygame.K_r, 'attack3': pygame.K_y, 'attack4': pygame.K_y } def __init__(self): # Players group used to draw multiple players self.player = Sumilidon(90, 200, 1256, keys=self.keys1) self.player2 = Metabee(290, 300, 1257, keys=self.keys2) self.players = {} self.add_player(self.player) self.add_player(self.player2) arenas = arena_info() self.rooms = [Room(arenas[0][0] + ".txt"), Room(arenas[1][0] + ".txt")] self.current_room = 0 self.countdown = CountDown() # Main Game Loop variables self.done = False self.fps = 60.0 self.clock = pygame.time.Clock() self.screen = None pygame.font.init() # you have to call this at the start, # if you want to use this module. self.game_font = pygame.font.SysFont('times', 20) self.game_over = False self.winner = "" def add_player(self, player): self.players[player.unique_id] = player def run(self, screen): self.screen = screen while not self.done: self.process_events() self.update() self.draw() pygame.display.update() self.clock.tick(self.fps) def process_events(self): keys = pygame.key.get_pressed() self.player.process_keys(keys) # Remove these line for networking self.player2.process_keys(keys) for event in pygame.event.get(): if event.type == pygame.QUIT: self.done = True def update(self): # Will only update count down timer until it's done if not self.countdown.should_countdown(): self.countdown.update() else: # --- Game Logic --- self.player.set_room(self.rooms[self.current_room]) self.player.update(self.players) # Remove these line for networking self.player2.set_room(self.rooms[self.current_room]) self.player2.update(self.players) if (self.player.damage_taken >= 100 or self.player2.damage_taken >= 100) and not self.game_over: self.game_over = True if self.player.damage_taken < 100: self.winner = "P1Wins.png" elif self.player2.damage_taken < 100: self.winner = "P2Wins.png" pygame.mixer.music.stop() pygame.mixer.music.load(get_path_name("media", "winner.mp3")) pygame.mixer.music.play(0) def draw_countdown(self): if not self.countdown.should_countdown(): self.countdown.draw(self.screen) def draw_players(self): pos = 0 for player in self.players: position = (pos, len(self.players)) self.players[player].draw(self.screen, self.game_font, position) pos += 1 def draw_game_over(self): self.countdown.draw_game_over(self.screen, self.winner) def draw(self): self.screen.fill(BACKGROUND_COLOR) self.rooms[self.current_room].draw(self.screen) self.draw_players() self.draw_countdown() if self.game_over: self.draw_game_over()