def game(): pygame.init() clock = pygame.time.Clock() canvas = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption('Traffic Simulation') FONT = pygame.font.SysFont("Bizarre-Ass Font Sans Serif", 20) renderEngine = RenderEngine(canvas, pygame, BLOCK_SIZE, WIDTH, HEIGHT, FONT) game_map = Map(renderEngine, ROAD_MAP, BLOCK_SIZE, DEBUG) while True: for ev in pygame.event.get(): if ev.type == pygame.QUIT: pygame.quit() if not config['USE_TEXTURES']: canvas.fill(BACKGROUND_COLOR) Veichle.dt = clock.get_time() / 1000 game_map.update() message = 'Time: ' + str(int(pygame.time.get_ticks() / 1000)) renderEngine.drawText(message, 20, 20) renderEngine.drawText('Fps: ' + str(int(clock.get_fps())), 20, 40) pygame.display.update() pygame.display.flip() clock.tick(180)
def __init__(self): """ Инициализация новой игровой сессии """ self.players: Player = [] self.players_count = 0 self.map = Map(20, 20)
def main(): map1 = Map(x_size=10, y_size=10) map1.generate_map() # x_min = map1.map_start_coordinates[0] - 2 # y_min = map1.map_start_coordinates[1] - 2 # x_max = map1.map_start_coordinates[0] + 2 # y_max = map1.map_start_coordinates[1] + 2 x_min = 0 y_min = 0 x_max = map1.x_size - 1 y_max = map1.y_size - 1 map1.print_map(x_min, y_min, x_max, y_max) player = Player(name='Brian', coordinates=map1.map_start_coordinates) # game loop while True: current_room = map1.rooms[player.coordinates] print(f'player.coordinates = {player.coordinates}') print(f'room.description = "{current_room.description}"') print(f'room.doors = "{current_room.doors}"') command = input('> ').upper() print(f"'{command}'") break
def __init__(self, canvas): self.canvas = canvas self.game_map = Map() self.hero = Hero(0, 1) self.boss = Boss(0, 5) self.skeleton = Skeleton(6, 8) self.skeleton2 = Skeleton(0, 7) self.skeleton3 = Skeleton(4, 5) self.stat = Stat(self.hero, self.boss) self.monster_position()
def test_init(self): size = 100 treasures = 10 traps = 11 mgame_map = Map(Position(size, size), treasures, traps) mmap = mgame_map.game_map treasuresCount = 0 trapsCount = 0 for line in mmap: treasuresCount += line.count("!") trapsCount += line.count("x") self.assertEqual(treasuresCount, treasures) self.assertEqual(traps, trapsCount)
def __init__(self): """ Initializes player, game map. """ mapSquareSize = random.randint(10, 20) mapSize = Position(mapSquareSize, mapSquareSize) startPos = \ Position(random.randint(0, mapSquareSize - 1), random.randint(0, mapSquareSize - 1)) self.player = Player(position=startPos, hp=3, bag_counter=0) self.enemy = Enemy(3, 1, Position(1, 1), Position(0, 0), Position(mapSize.x, mapSize.y)) self.level = Map(size=mapSize, treasures=6, traps=6) self.actions = { "Up": "w", "Down": "s", "Left": "a", "Right": "d", "Save": "save", "Load": "load", "Help": "help", "Exit": "exit" } self.game_status = GameState.INGAME self.__ready_to_end = False self.mrlock = threading.RLock() def enemy_update(enemy, player): while not self.ready_to_end: time.sleep(3) enemy.update(player) self.enemyThread = threading.Thread(target=enemy_update, args=(self.enemy, self.player)) self.enemyThread.start()
from itertools import cycle map_data = None def is_valid_pos(cur_pos): x, y = cur_pos.x, cur_pos.y return 0 <= x < 100 and 0 <= y < 100 and map_data[y][x] if __name__ == "__main__": map_data = map_generator.generate_map(100, 100, seed=25, obstacle_density=0.35) game_map = Map(map_data) start = Point(2, 2) end = Point(25, 65) rel_pos = [ Point(-1, 1), Point(1, 1), Point(0, 0), Point(-1, -1), Point(1, -1), ] units_pos = [start + pos for pos in rel_pos] units = [] for i, unit_pos in enumerate(units_pos): unit = FORMATION.copy(start + rel_pos[i]) unit.add_to_formation(
def __init__(self, name, gender): self.player = Player(name, gender) self.map = Map('farm_map.txt', 'farm_descriptions.txt', 'farm_items.txt', 'farm_items_map.txt') self.item_fns = ItemFns(self.map, self.player)
room["outside"].add_item(Lightsource("torch", "Should be enough to see...")) room["foyer"].add_item( Item("sword", "Not the sharpest..."), Treasure("spoon", "I think it's worth something.", 10, False)) room["overlook"].add_item(Treasure("ruby", "Shines bright...", 20, False)) room["treasure"].add_item( Treasure("coin", "At least it's something.", 15, False)) # Nothin in narrow and treasure # # Main # # Make a new player object that is currently in the 'outside' room. player = Player(room["outside"]) map = Map() print(player.current_room.items[0].name) print( f"\n\nWelcome adventurer, you are currently in {player.current_room.name}. {player.current_room.descr}\n" ) print(""" XXXXXXXX XXX XXXXX XXXX XX XXXXXXXX XX XXXXXX XXXX XXXXXXXXXX XXXX XXXX XX XXXXX X +----------------+ | Overlook | +------+ +-------+ | | | | +------------+ | | | Treasure |
def start_game(config): # Loads game data, setups first map of the game then runs the main game loop. # Load all game data. tiles_data, generation_data, actor_data, item_data, animation_data = load_data( ) # Initialize map then generate a new map. current_map = Map(100, 100) start_x, start_y = generate_cave_map(current_map, generation_data, tiles_data, actor_data, item_data) # Initialize camera. camera = Camera(0, 0, config['camera_width'], config['camera_height'], 0, 0) # Spawn player. player_stats = { "hp": 100, "mp": 100, "starting_ap": 2, "max_ap": 10, "ap_recovery": 2, "base_damage": "1d4", "ac": 10, "hit": 4 } player = Actor(start_x, start_y, 'Player', '@', [255, 255, 255, 255]) player_inventory = Inventory(player) player.alive = Alive(player, player_stats, inventory=player_inventory) # Initialize FOV. fov = FOV(player, 5, current_map) under_mouse = None debug = False game_state = GameState.players_turn previous_state = None # Setup presets/containers for the game loop. actor_queue = PriorityQueue() actor_turns_left = False animations = [] no_block_animations = [] widgets = [] targeting_action = None terminal.refresh() while True: terminal.clear() if not debug: if current_map.tiles[player.x][player.y].is_stairs: player.x, player.y = generate_cave_map(current_map, generation_data, tiles_data, actor_data, item_data) current_map.depth += 1 render_all(player, current_map, fov, camera, under_mouse, animations, no_block_animations, widgets, targeting_action, game_state, config, animation_data) # Parse player input. action = handle_input(player, current_map, camera, fov, under_mouse, widgets, targeting_action, animations, no_block_animations, animation_data, game_state) under_mouse = get_under_mouse(player, current_map.tiles, current_map.actors, current_map.items, widgets, camera, game_state) # If an action was returned, complete that action. if isinstance(action, str): if action == 'restart': start_game(config) elif action == 'end-turn': if player.alive.check_in_combat(current_map.actors, fov): game_state = GameState.actors_turn elif action == 'toggle-inventory': if game_state == GameState.show_inventory: game_state = previous_state widgets = [] else: previous_state = game_state game_state = GameState.show_inventory fill_widgets(player, widgets, config, game_state) elif action == 'toggle-targeting': game_state = previous_state targeting_action = None elif action == 'toggle-map': if game_state == GameState.show_full_map: game_state = previous_state else: previous_state = game_state game_state = GameState.show_full_map elif action: if len(animations) == 0: if game_state == GameState.show_inventory: game_state = previous_state widgets = [] if action.item.targeting: previous_state = game_state game_state = GameState.targeting targeting_action = action elif game_state == GameState.targeting: game_state = previous_state targeting_action = None if game_state == GameState.players_turn: action.perform(animations, no_block_animations, animation_data) # Check if camera was moved, if it was, recalculate FOV. camera_moved = camera.move_camera(player.x, player.y, current_map) if camera_moved: fov.fov_recompute() # If player is in combat and out of ap, end the players turn. if player.alive.check_in_combat(current_map.actors, fov): if player.alive.get_ap(False) <= 0: game_state = GameState.actors_turn # Only continue if all animations have finished. if len(animations) == 0: # Go through all actors and let them take turns until # they are out of ap or decide not to take any actions. if game_state == GameState.actors_turn: # Fill the queue with actors that still need to take turns. if actor_queue.empty(): for actor in current_map.actors: if actor.alive and not actor.alive.turn_over and fov.in_fov( actor): actor_queue.put(actor, actor.alive.get_ap(False)) else: actor = actor_queue.get() if actor.alive: action = actor.alive.get_action( player, current_map, fov) if action: action.perform(animations, no_block_animations, animation_data) actor_turns_left = True else: actor.alive.turn_over = True # Player recovers ap at the end of the actors turn. if actor_queue.empty(): if not actor_turns_left: player.alive.recover_ap() game_state = GameState.players_turn for actor in current_map.actors: if actor.alive: actor.alive.turn_over = False actor_turns_left = False else: debug_render(player, current_map) # Parse player input. action = handle_input(player, current_map, camera) if action == 'restart': start_game(config) if action == 'debug': debug = not debug terminal.set("window: size='40x20', cellsize='24x24'") terminal.refresh()