def tutorial(): tutplayer = Player('Tutorial') tutmap = Map(tut=True) tutplayer.linkMap(tutmap) while True: clearScreen() tutmap.update() choice = userInput() if choice == 'q': break elif choice in 'wasd': tutplayer.move(choice) del tutmap, tutplayer
menu = 0 # game loop while mc_gyver.alive: pygame.time.Clock().tick(30) # checking the events for event in pygame.event.get(): if event.type == QUIT: quit() if event.type == KEYDOWN: old_pos = mc_gyver.x, mc_gyver.y if event.key == K_DOWN: new_pos = (mc_gyver.x, mc_gyver.y + sprite) mc_gyver.move(old_pos, new_pos, map) if event.key == K_UP: new_pos = (mc_gyver.x, mc_gyver.y - sprite) mc_gyver.move(old_pos, new_pos, map) if event.key == K_LEFT: new_pos = (mc_gyver.x - sprite, mc_gyver.y) mc_gyver.move(old_pos, new_pos, map) if event.key == K_RIGHT: new_pos = (mc_gyver.x + sprite, mc_gyver.y) mc_gyver.move(old_pos, new_pos, map) # Printing the floor, walls, Mc Gyver, guardian and objects window.blit(floor, (0, 0)) [window.blit(wall, pos) for pos in list(map.wall_pos)] window.blit(guardian.image, (guardian.x, guardian.y)) window.blit(mc_gyver.image, (mc_gyver.x, mc_gyver.y))
def main(): # Initialise pygame. pygame.init() # Set up screen. screen_width = 800 screen_height = 640 screen_surface = pygame.display.set_mode([screen_width, screen_height]) # Set up sprites: SPR_TREE = pygame.image.load('Sprites\\tree.png').convert_alpha() SPR_PLAYER = pygame.image.load('Sprites\\player.png').convert_alpha() SPR_ORC = pygame.image.load('Sprites\\orc.png').convert_alpha() sprites = {"player": SPR_PLAYER, "tree": SPR_TREE, "orc": SPR_ORC} # Set up view port constants for the area which will display the game map. HUD dimensions are derived from this. view_port_width = 800 view_port_height = 480 view_port_x_offset = 0 view_port_y_offset = 50 # Calculate a simple map which is double the screen size to test scrolling. map_width, map_height = display_to_map(screen_width * 2, screen_height * 2) # Map size in 16px by 16px tiles # Create player and map objects. player_stats = StatBlock(h=100, m=0, s=10, d=10) player = Player("Player", map_x=randint(1, map_width - 1), map_y=randint(1, map_height - 1), colour=(0, 255, 0), sprite=SPR_PLAYER, stats=player_stats) game_map = GameMap(map_width, map_height) # Create a game map. # Create some random noise in the map. for x, y in game_map: if randint(1, 10) == 1: game_map.blocked[x, y] = True # List to store all the game entities. Populate with player. entities = list() entities.append(player) # Add some basic monsters. for mon in range(10): name = "Orc " + str(mon + 1) mon_stats = StatBlock(h=10, m=0, s=12, d=8) mon = Monster(name, map_x=randint(1, map_width - 1), map_y=randint(1, map_height - 1), colour=(255, 0, 0), game_map=game_map, sprite=SPR_ORC, stats=mon_stats) entities.append(mon) # Set the first turn as the player. current_turn = Turn.player # Main game loop. running = True while running: # Create a map chunk for iteration based on the rect boundaries. visible_map_chunk = get_visible_map_chunk(player, game_map, view_port_width, view_port_height) # Render the various screen elements. The placement of this determines whether player or enemies movement lag.. render_all(screen_surface, screen_width, screen_height, view_port_width, view_port_height, view_port_x_offset, view_port_y_offset, game_map, player, entities, visible_map_chunk, sprites) # Start Player Turn if current_turn == Turn.player: # Get inputs and terminate loop if necessary. user_input, running = get_inputs(running) if not user_input: continue # If no input continue with game loop. # Process actions else: # Process user input and get actions. action = handle_keys(user_input) # Action categories. move = action.get("move") quit_game = action.get("quit") if quit_game: # Triggered when ESC key is pressed. running = False if move: # If movement keys are pressed move player. player_map_x, player_map_y = player.get_map_position() dx, dy = move # Pull relative values from action. # Calculate potential new coordinates destination_x = player_map_x + dx destination_y = player_map_y + dy if not game_map.blocked[ destination_x, destination_y]: # Check if the tiles are walkable. attack_target = get_blocking_entities( entities, destination_x, destination_y ) # Is there a monster at the destination? # if there is an entity at the location... if attack_target: if isinstance(attack_target, Monster): # ... and it's a monster player.attack(attack_target) # Attack it. else: player.move( dx, dy ) # If the cell is empty, move player into it. current_turn = Turn.monster # Set turn state to monster. # Start Monster Turn if current_turn == Turn.monster: # Iterate through all entities. for entity in entities: if isinstance(entity, Monster): # If the entity is a Monster # Can the monster see the player (and vice versa). if (entity.map_x, entity.map_y) in visible_map_chunk: if not entity.target: # If the monster doesn't have a target, set it to the player. entity.target = player # Process monster turn ai entity.take_turn(game_map, entities) current_turn = Turn.player # Set to player's turn again. # If the main game loop is broken, quit the game. pygame.quit()
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()
# Register mouse onclick click = False # Used to manage how fast the screen updates clock = pygame.time.Clock() # -------- Main Program Loop ----------- while not done: # --- Main event loop for event in pygame.event.get(): if event.type == pygame.QUIT: done = True keys = pygame.key.get_pressed() # player movement if keys[pygame.K_a]: player.move(-1,0) if keys[pygame.K_d]: player.move(1,0) if keys[pygame.K_w]: player.move(0,-1) if keys[pygame.K_s]: player.move(0,1) if keys[pygame.K_a] and keys[pygame.K_j] and stamina > 1: # player stamina player.move(-4,0) stamina = stamina - 2 if keys[pygame.K_d] and keys[pygame.K_j] and stamina > 1: player.move(4,0) stamina = stamina - 2 if keys[pygame.K_w] and keys[pygame.K_j] and stamina > 1: player.move(0,-4)
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()