def run_game(): pygame.init() clock = pygame.time.Clock() screen = pygame.display.set_mode((globals['SCREEN_WIDTH'], globals['SCREEN_HEIGHT']), 0, 32) currently_held_keys = [] #debug stuff lvl = Level("testlevel.txt", screen) playerCam = Camera((85,85), globals['SCREEN_WIDTH'], globals['SCREEN_HEIGHT']) originCam = Camera((0,0), globals['SCREEN_WIDTH'], globals['SCREEN_HEIGHT']) player = Player("resources/robodude.png", screen, (85,85), playerCam) currentCam = player.camera entities = [] entities.append(player) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: exit_game() if event.type == pygame.KEYDOWN: currently_held_keys.append(event.key) if event.type == pygame.KEYUP: currently_held_keys.remove(event.key) currentCam = player.camera do_render_light = True for keydown in currently_held_keys: if keydown in globals['WASD_KEYS']: player.movement_handler(keydown) #debug stuff if keydown == pygame.K_1: currentCam = originCam if keydown == pygame.K_2: do_render_light = False time_passed = clock.tick(50) screen.fill(globals['BG_COLOR']) # debug stuff pygame.display.set_caption('Lightgame : %d fps' % clock.get_fps()) corners = currentCam.get_corners() lvl.draw_visible_level(corners[0], corners[1]) player.update(time_passed, lvl, currentCam) player.draw(currentCam) lights_per_entity = [entity.lights for entity in entities] all_lights = [light for lights in lights_per_entity for light in lights] if do_render_light: currentCam.render_light(screen, all_lights) pygame.display.flip()
class Game(Scene): def __init__(self): self.c = Constants() self.player = Player(self.c.screen_width / 2, self.c.screen_height / 2, 300) def draw(self, sm, screen): screen.fill(self.c.black) utils.draw_text(screen, "Game: [M] to Menu") self.player.draw(screen) def input(self, sm): for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit(0) if event.type == pygame.KEYDOWN: if event.key == pygame.K_m: sm.pop() sm.push( FadeTransitionScene(self, None) ) # We pass none as we are not transitiong to a *new* scene. if event.key == pygame.K_w: self.player.moving_up = True if event.key == pygame.K_s: self.player.moving_down = True if event.key == pygame.K_a: self.player.moving_left = True if event.key == pygame.K_d: self.player.moving_right = True if event.type == pygame.KEYUP: if event.key == pygame.K_w: self.player.moving_up = False if event.key == pygame.K_s: self.player.moving_down = False if event.key == pygame.K_a: self.player.moving_left = False if event.key == pygame.K_d: self.player.moving_right = False def update(self, sm, dt): self.player.update(dt) def exit(self): print("Leaving Game.") def enter(self): print("Entering Game.")
def main(): bg_x = bg.get_width() bg_y = bg.get_height() font = pygame.font.SysFont('helvetica', 20) player = Player(bg_x // 2, bg_y // 2) enemies = [Minion(random.randrange(0, bg_x), random.randrange(0, bg_y)) for _ in range(10)] + \ [Runner(random.randrange(0, bg_x), random.randrange(0, bg_y)) for _ in range(3)] + \ [Tank(random.randrange(0, bg_x), random.randrange(0, bg_y))] projectiles = [] projectile_tick = 0 score = 0 run = True while run: clock.tick(27) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False pygame.quit() player.move(bg_x, bg_y, width, height) if projectile_tick == 0: if pygame.mouse.get_pressed()[0]: projectiles.extend(player.shoot(w2, h2)) projectile_tick = 1 elif projectile_tick < player.gun.cooldown: projectile_tick += 1 else: projectile_tick = 0 win.fill((0, 0, 0)) win.blit(bg, (w2 - player.x, h2 - player.y)) player.draw(win, player, w2, h2) for projectile in projectiles: projectile.draw(win, player, w2, h2) projectile.move() if projectile.is_offscreen(player, width, height): projectiles.remove(projectile) for enemy in enemies: if enemy.is_hit(projectile): projectiles.remove(projectile) enemy.hit(projectile) if enemy.is_dead(): score += enemy.points enemies.remove(enemy) break for enemy in enemies: if not enemy.is_offscreen(player, width, height): if player.is_hit(enemy) and not player.was_hit: player.was_hit = True player.curr_health -= 1 if player.curr_health == 0: run = False pygame.quit() enemy.move(bg_x, bg_y, player) enemy.draw(win, player, w2, h2) text = font.render(f'Score: {score}', True, (255, 255, 255)) win.blit(text, (0, 0)) pygame.display.update()