def gameLoop(): player = Player(50, 280, 100) autofireTimer = 14 enemyFireTimer = 0 enemies = Enemies() enemies.spawnWave() projectiles = Projectiles() enemies.enemies[0].fireProjectile(projectiles, player.posx, player.posy) moon = pygame.font.Font(pygame.font.match_font('moon'), 16) while 1: dtime = clock.tick(60) for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() pressed = pygame.key.get_pressed() autofireTimer = processInput(pressed, player, projectiles, autofireTimer) if player.posx < 0: player.posx = 0 elif player.posx > windowWidth - 80: player.posx = windowWidth - 80 if player.posy > windowHeight - 70: player.posy = windowHeight - 70 elif player.posy < 0: player.posy = 0 player.checkCollisions(projectiles, enemies) projectiles.update() enemyFireTimer += 1 enemies.update(projectiles) #checking for collisions happens here enemies.checkAndSpawn() enemies.checkAndFire(projectiles, player.posx, player.posy, enemyFireTimer) if player.lives <= 0: break with Profiler() as p: drawInterface(screen, player, enemies, moon) player.draw(screen) projectiles.draw(screen) enemies.draw(screen) pygame.display.flip() 1000 // dtime
def start(display, settings): """This is the main body of the game logic""" counter = 0 boss_alive = False if settings.settings["Music"] == "On": pygame.mixer.music.load('assets/main_game.wav') pygame.mixer.music.play(-1) player_img, enemy_img, capital_ship_img = "assets/player_ship.png", "assets/alienship.png", "assets/capital_ship.png" bullet, fired_bullets, on_screen_enemies, turn_timer = False, [], 1, 0 enemy_list = [Enemy(enemy_img, display) for i in range(on_screen_enemies)] player = Player(player_img, display) FPS = settings.settings["Framerate"] num_stars = 800 stars = [ Stars(settings.settings["Resolution"]["X"], settings.settings["Resolution"]["Y"]) for i in range(num_stars) ] # Game loop while True: display.fill(colour.black) # Draw stars for star in stars: star.draw(display) if star.y > settings.settings["Resolution"]["Y"]: stars.remove(star) new_height = 1 # Add new stars - we want this to be somewhat random new_stars = [ Stars(settings.settings["Resolution"]["X"], new_height) for i in range(int(random.uniform(0.25, 1.25))) ] for new_star in new_stars: stars.append(new_star) if turn_timer > 500: on_screen_enemies += 1 for e in [ Enemy(enemy_img, display) for i in range(on_screen_enemies) ]: enemy_list.append(e) turn_timer = 0 for e in enemy_list: e.draw() if e.load_weapons < 1: e.load_weapons = 100 fired_bullets.append(e.fire()) e.load_weapons -= 1 # If bullet has been fired, draw it and assess whether it hits the target or not for b in fired_bullets: # Remove the bullets at the bottom so you don't move into them if b.y > 1080: fired_bullets.remove(b) b.draw() player_status = player.hit(b) if player_status == "hit": # Handle edge case where bullet goes off screen but still has causal power try: fired_bullets.remove(b) except: pass elif player_status == "dead": try: fired_bullets.remove(b) except: pass # End game return True, player for e in enemy_list: hit_result = e.hit(b, player) if hit_result == "dead": fired_bullets.remove(b) enemy_list.remove(e) elif hit_result == "hit": fired_bullets.remove(b) player.draw() draw_text(display, player) if (player.score % 4 == 0 and player.score != 0) and boss_alive == False: new_boss = True boss_alive = True counter = 100 enemy_list.append(create_boss(capital_ship_img, display)) if counter > 0 and new_boss == True: draw_boss_text(display, settings) counter -= 1 else: counter = 120 new_boss = False # Checks what events have been created and takes them off the queue for event in pygame.event.get(): if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE): terminate() elif (event.type == KEYDOWN and event.key == K_a): player.x -= 25 elif (event.type == KEYDOWN and event.key == K_d): player.x += 25 elif (event.type == KEYDOWN and event.key == K_SPACE): fired_bullets.append(player.fire()) turn_timer += 1 # update() draws Surface object to screen pygame.display.update() pygame.time.Clock().tick(FPS)
Player.speed = -10 if event.type == pygame.KEYUP: if event.key == pygame.K_SPACE: Player.speed = 5 screen.fill() # Logica do background screen.backgroung_position -= 2 if screen.backgroung_position * -1 == screen.backgroung_limit: screen.backgroung_position = 0 # Criando o jogador player = Player(45, 45) player.draw(screen.obj) Player.y += Player.speed # Criando os muros image_wallup = pygame.image.load('img/img_wallup.png') image_walldown = pygame.image.load('img/img_walldown.png') upper_wall = Wall(0) upper_wall.draw(screen.obj, image_wallup) bottom_wall = Wall(Wall.height + Wall.distance) bottom_wall.draw(screen.obj, image_walldown) # Criando o score score.draw(screen.obj)
def game_loop(game_level): room = pygame.image.load("Living RoomWithStuff.png").convert() # Living Room.jpg") menu = pygame.image.load("BoxWithElements.png").convert_alpha() # Menu is 1025 x 196 timer_offset = 0 menu_timer_fill = pygame.image.load("Timer.png").convert() menu_timer_background = pygame.image.load("WhiteBox.png").convert() menu_objective = pygame.image.load("Controls.png").convert() start_time = pygame.time.get_ticks() # Time from the beginning of execution cycle_time = pygame.time.get_ticks() # Time of each cycle or loop iteration time_counter = 0 #ghost_guy = ghost("boo.png") # Player Sprite declaration ############## player_position = vector2(352, 114) player_sprite = Player("WalkingSheet.png", player_position) # Object declaration, spray ID = 01, tape ID = 02, dog_toy ID = 03 spray = Objects("SprayDarkLine.png", vector2(820, 444), 01) tape = Objects("Object_Tape.png", vector2(630, 280), 02) dog_toy = Objects("Object_DogToy.png", vector2(400, 530), 03) dog_bags = Objects("Object_DogBag.png", vector2(160, 308), 04) trash_can = Objects("Object_TrashCan.png", vector2(0, 450), 05) # Stores all objects into object_list #object_list = [spray, tape, dog_toy, dog_bags, trash_can] object_list = [spray, dog_bags] # Path declaration, 6 points for dog to travel too path_list = [Path(vector2(750, 510), vector2(60, 40)), Path(vector2(160, 528), vector2(60, 40)), Path(vector2(350, 400), vector2(60, 40)), Path(vector2(550, 340), vector2(60, 40)), Path(vector2(620, 500), vector2(60, 40)), Path(vector2(250, 340), vector2(60, 40))] # Puppy declaration and setup puppy_position = path_list[0].pos puppy_speed = vector2(0, 0) puppy_sprite = Puppy("dogresized.png", puppy_position, puppy_speed) # Pup clone: Better to start together or sep? puppy_position2 = path_list[2].pos puppy_speed2 = vector2(0, 0) puppy_sprite2 = Puppy("dogresized.png", puppy_position, puppy_speed2) pup_list = [puppy_sprite] if game_level >= 2: pup_list.append(puppy_sprite2) # Sets object status as active for first puppy path for pup in pup_list: pup.change_path(path_list) # Game loop setup frame_clock = pygame.time.Clock() FPS = 60 game_running = True while game_running: frame_clock.tick(FPS) # get user events pygame.event.pump() for evt in pygame.event.get(): # print(evt) # Prints all key and mouse events if evt.type == pygame.QUIT: pygame.quit() sys.exit() sprinting = False # Not sure if this is correct ######## if evt.type == pygame.KEYDOWN: if evt.key == pygame.K_ESCAPE: pygame.quit sys.exit() if evt.key == pygame.K_w: # Up player_sprite.moving = True elif evt.key == pygame.K_s: player_sprite.moving = True elif evt.key == pygame.K_a: player_sprite.moving = True elif evt.key == pygame.K_d: player_sprite.moving = True elif evt.key == pygame.K_SPACE: sprinting = True # Equip items tree if evt.key == pygame.K_j: player_sprite.looking_for_item = True else: player_sprite.looking_for_item = False else: player_sprite.moving = False # Pressed keys are assigned an action keys_pressed = pygame.key.get_pressed() # Movement speed increase with 'sprint' if keys_pressed[pygame.K_w]: player_sprite.moving = True player_sprite.timer = 0 player_sprite.side = 3 player_sprite.move(0, -4, sprinting) if keys_pressed[pygame.K_s]: player_sprite.moving = True player_sprite.timer = 0 player_sprite.side = 0 player_sprite.move(0, 4, sprinting) if keys_pressed[pygame.K_a]: player_sprite.moving = True player_sprite.timer = 0 player_sprite.side = 2 player_sprite.move(-4, 0, sprinting) if keys_pressed[pygame.K_d]: player_sprite.moving = True player_sprite.timer = 0 player_sprite.side = 1 player_sprite.move(4, 0, sprinting) if keys_pressed[pygame.K_k]: player_sprite.cleanup = True else: player_sprite.cleanup = False # SIMULATION --------------------------- # UPDATE end = pygame.time.get_ticks() delta_time = end - cycle_time cycle_time = end # Game Timer functions counting_time = pygame.time.get_ticks() - start_time counting_seconds = int(counting_time % 60000 / 1000) # print(counting_seconds) # Using get_ticks, counting seconds currently times game if counting_seconds != time_counter: time_counter += 1 timer_offset += 5 # If the timer has exceeded 59 seconds, game over screen if counting_seconds == 59: game_running = False return path_list # Checks if player is touching any objects, works for obj in object_list: player_sprite.update(obj, delta_time) for pup in pup_list: # Moves towards active target, returns True if reached if pup.move(delta_time): # Set new active area when destination reached print("Reached point, in Main loop") for path in path_list: path.update(pup) pup.change_path(path_list) # Is the player in the path area for cleanup for path in path_list: if path.status == 'warning': path.update(player_sprite) # DRAW SECTION (from back to front) Room, paths, player, Pup, menu, objects################## screen.blit(room, (0, 0)) # Draws each object to room for path in path_list: path.draw(screen) # draws the unequipped items for obj in object_list: if not obj.item_held: obj.draw(screen) player_sprite.draw(screen) for pup in pup_list: pup.draw(screen) screen.blit(menu_timer_background, (5, 584)) screen.blit(menu_timer_fill, (5 - timer_offset, 594)) # 305 x 146 # 1 sec 5 pixel screen.blit(menu, (0, 572)) screen.blit(menu_objective, (782, 628)) #draws equipped items for obj in object_list: if obj.item_held: obj.draw(screen) pygame.display.flip()
def game_loop(): room = pygame.image.load("Living RoomWithStuff.png") # Living Room.jpg") menu = pygame.image.load("BoxWithElements.png").convert_alpha() # Menu is 1025 x 196 ########################### start_time = pygame.time.get_ticks( ) # Time from the beginning of execution cycle_time = pygame.time.get_ticks( ) # Time of each cycle or loop iteration time_counter = 0 # Player Sprite declaration ############## player_position = vector2(352, 114) player_sprite = Player("WalkingSheet.png", player_position) # Object declaration spray = Objects("Object_Spray.png", vector2(200, 430)) tape = Objects("Object_Tape.png", vector2(300, 330)) dog_toy = Objects("Object_DogToy.png", vector2(400, 530)) # Stores all objects into object_list object_list = [spray, tape, dog_toy] # Path declaration, 6 points for dog to travel too path_list = [ Path(vector2(750, 300), vector2(40, 40)), Path(vector2(110, 370), vector2(40, 40)), Path(vector2(304, 420), vector2(40, 40)), Path(vector2(750, 300), vector2(40, 40)), Path(vector2(620, 100), vector2(40, 40)), Path(vector2(250, 250), vector2(40, 40)) ] # Sets object status as active for first puppy path change_point(path_list) # Puppy declaration and setup puppy_position = path_list[0].pos puppy_speed = vector2(0, 0) puppy_sprite = Puppy("DogSpriteSpreadfinal.png", puppy_position, puppy_speed) # Game loop setup frame_clock = pygame.time.Clock() FPS = 60 game_running = True while game_running: frame_clock.tick(FPS) # get user events pygame.event.pump() for evt in pygame.event.get(): # print(evt) # Prints all key and mouse events if evt.type == pygame.QUIT: pygame.quit() sys.exit() elif evt.type == pygame.KEYDOWN and evt.key == pygame.K_ESCAPE: pygame.quit sys.exit() sprinting = False player_sprite.moving = False # Not sure if this is correct ######## if evt.type == pygame.KEYDOWN: if evt.key == pygame.K_w: # Up player_sprite.moving = True elif evt.key == pygame.K_s: player_sprite.moving = True elif evt.key == pygame.K_a: player_sprite.moving = True elif evt.key == pygame.K_d: player_sprite.moving = True elif evt.key == pygame.K_SPACE: sprinting = True # Pressed keys are assigned an action keys_pressed = pygame.key.get_pressed() # Movement speed increase with 'sprint' if keys_pressed[pygame.K_w]: player_sprite.timer = 0 player_sprite.side = 3 player_sprite.move(0, -4, sprinting) if keys_pressed[pygame.K_s]: player_sprite.timer = 0 player_sprite.side = 0 player_sprite.move(0, 4, sprinting) if keys_pressed[pygame.K_a]: player_sprite.timer = 0 player_sprite.side = 2 player_sprite.move(-4, 0, sprinting) if keys_pressed[pygame.K_d]: player_sprite.timer = 0 player_sprite.side = 1 player_sprite.move(4, 0, sprinting) # SIMULATION --------------------------- # UPDATE simulation end = pygame.time.get_ticks() delta_time = end - cycle_time cycle_time = end # Checks if player is touching any objects for obj in object_list: player_sprite.update(obj, delta_time) # Finds active spot for puppy to travel too for path in path_list: if path.status == 'active': puppy_sprite.target = path.pos path.status = 'idle' path.color = object_color_idle break # Moves towards active target if puppy_sprite.move(delta_time): # Set new active area when destination reached print("Reached point") change_point(path_list) # Game Timer functions counting_time = pygame.time.get_ticks() - start_time counting_seconds = int(counting_time % 60000 / 1000) # print(counting_seconds) # Using get_ticks, counting seconds currently times game if counting_seconds != time_counter: time_counter += 1 # If the timer has exceeded 59 seconds, game over screen if counting_seconds == 59: game_running = False game_over(path_list) # DRAW SECTION (from back to front)################## screen.blit(room, (0, 0)) # Draws each object to room for path in path_list: path.draw(screen) for obj in object_list: obj.draw(screen) # Puppy sprite is not allowing the room to be redrawn puppy_sprite.draw(screen) player_sprite.draw(screen) screen.blit(menu, (0, 572)) pygame.display.flip()