room = room.backward enemy_group = room.enemy_group obstacle_group = room.obstacle_group player.x = 400 player.y = upper_bound + 30 count += 1 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.KEYDOWN: # Player Movement if event.key == pygame.K_a: player.x_change = -player.xSpeed player.moving = True player.dirX = "left" player.facing = "left" f.write("Player moving left\n") #TC1 if event.key == pygame.K_d: player.x_change = player.xSpeed player.moving = True player.dirX = "right" player.facing = "right" f.write("Player moving right\n") #TC1 if event.key == pygame.K_w: player.y_change = -player.ySpeed player.moving = True player.dirY = "up" f.write("Player moving up\n") #TC1 if event.key == pygame.K_s:
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()