def create_obstacles(role_settings, screen, obstacles): obstacle = Obstacle(role_settings, screen) obstacle.width = obstacle.obstacle_width available_space_x = role_settings.screen_width - 2 * obstacle.width number__obstacle_x = int(available_space_x / (2 * obstacle.width)) for alien_number in range(number__obstacle_x): obstacle = Obstacle(role_settings, screen)
def run_game(): pygame.init() #创建一群障碍物 obstacles = Group() #角色设置的实例化 role_settings = Settings() screen = pygame.display.set_mode( (role_settings.screen_width, role_settings.screen_height), ) screen = pygame.display.set_mode((2000, 1000)) pygame.display.set_caption("天天跑酷") #创建一个背景 backend = BackendImage(screen) #创建一个实例化的人物 ship = Ship(screen, role_settings) #创建障碍物的实例 obstacle = Obstacle(screen) #创建移动背景的实例 while True: #调用game_functions模块的check_events实现对事件的监听 gf.check_events(ship, obstacle) gf.update_screen(role_settings, screen, ship, backend, obstacle, obstacles) gf.create_obstacles(role_settings, screen, obstacles)
def __init__(self, size): super(Field, self).__init__() w, h = size self._map = {} self.size = size self.pos = w / 2., h / 2. for x in xrange(1, w - 1): self.add_sprite(Obstacle(pos=(x, 0), orient=(0, 1))) self.add_sprite(Obstacle(pos=(x, h - 1), orient=(0, -1))) for y in xrange(1, h - 1): self.add_sprite(Obstacle(pos=(0, y), orient=(1, 0))) self.add_sprite(Obstacle(pos=(w - 1, y), orient=(-1, 0))) self.add_sprite(ObstacleCorner(pos=(0, 0), orient=(1, 1))) self.add_sprite(ObstacleCorner(pos=(w - 1, 0), orient=(-1, 1))) self.add_sprite(ObstacleCorner(pos=(w - 1, h - 1), orient=(-1, -1))) self.add_sprite(ObstacleCorner(pos=(0, h - 1), orient=(1, -1))) self.init_texture()
def __init__(self): Level.__init__(self, 'test level 2', (12, 11)) A = self.add A(Player(pos=(4, 7))) A(Emitter(pos=(2, 9), orient=(0, -1))) A(Emitter(pos=(9, 8), orient=(-1, 0))) A(Obstacle(pos=(6, 1))) A(Obstacle(pos=(7, 1))) A(Obstacle(pos=(8, 1))) A(Mirror(pos=(2, 2), orient=(1, 1))) A(Mirror(pos=(9, 2), orient=(-1, 1))) A(Mirror(pos=(9, 6), orient=(-1, -1))) A(Mirror(pos=(4, 3), orient=(0, 1))) A(Goal(pos=(5, 0), orient=(0, 1))) self.collect()
def new(self): """For things that should happen once, at the start of a new game""" # Load map from file self.map_folder = path.join(self.game_folder, 'data/maps') # self.map = Map(path.join(game_folder, 'map.txt')) self.map = TiledMap(self, path.join(self.map_folder, MAP_NAME)) self.map_img = self.map.make_map() self.map_rect = self.map_img.get_rect() # self.paths = breadth_first_search(self.map, self # initialize all variables and do all the setup for a new game self.all_sprites = pg.sprite.LayeredUpdates() self.walls = pg.sprite.Group() self.mobs = pg.sprite.Group() self.bullets = pg.sprite.Group() self.items = pg.sprite.Group() # For Loading map from tmx data for tile_object in self.map.tmxdata.objects: obj_center = vec(tile_object.x + tile_object.width / 2, tile_object.y + tile_object.height / 2) if tile_object.name == 'player': self.player = Player(self, obj_center.x, obj_center.y) if tile_object.name == 'zombie': Mob(self, obj_center.x, obj_center.y) # For EM all entities must have unique id if tile_object.name == 'wall': Obstacle(self, tile_object.x, tile_object.y, tile_object.width, tile_object.height) if tile_object.name in ['health', 'shotgun', 'pistol']: Item(self, obj_center, tile_object.name) # self.EM = AI.EntityManager() for mob in self.mobs: # self.EM.add_entity(mob.id) #All entities must have a unique id mob.SM.current_state = zombie_states.Idle(self, mob) mob.SM.global_state = zombie_states.ZombieGlobalState(self, mob) assert self.player is not None self.camera = Camera(self.map.width, self.map.height) # Give camera total size of map # Flags self.draw_debug = False self.paused = False self.night = False self.effects_sounds['level_start'].play()
def create_walls(self): tp = self.tmxdata.get_tile_properties_by_gid for layer in self.tmxdata.visible_layers: if layer.name == 'Walls': for x, y, gid, in layer: tile = tp(gid) if tile: try: if tile['type'] == 'wall': Obstacle( self.game, x * self.tmxdata.tilewidth * TILE_SIZE_MULTIPLIER, y * self.tmxdata.tilewidth * TILE_SIZE_MULTIPLIER, self.tmxdata.tilewidth * TILE_SIZE_MULTIPLIER, self.tmxdata.tilewidth * TILE_SIZE_MULTIPLIER) except: pass
def new(self): """ Creates a new game :return: None """ self.all_sprites = pg.sprite.LayeredUpdates() self.walls = pg.sprite.Group() self.bullets = pg.sprite.Group() self.mobs = pg.sprite.Group() self.items = pg.sprite.Group() self.swingAreas = pg.sprite.Group() self.camera = Camera(self.map.width, self.map.height) self.paused = False self.running = True self.pathfinder = Pathfinder() self.game_graph = WeightedGraph() mob_positions = [] wall_positions = [] for row, tiles in enumerate(self.map.data): for col, tile in enumerate(tiles): if tile == '1': wall_positions.append((col * TILESIZE, row * TILESIZE)) if tile == 'P': self.player = Player(self, col * TILESIZE, row * TILESIZE) if tile == 'E': mob_positions.append((col * TILESIZE, row * TILESIZE)) if tile == 'W': WeaponPickup(self, (col * TILESIZE, row * TILESIZE)) for position in wall_positions: Obstacle(self, position[0], position[1]) for position in mob_positions: Mob(self, position[0], position[1]) self.game_graph.walls = [(int(wall[0] // TILESIZE), int(wall[1] // TILESIZE)) for wall in wall_positions] self.mob_idx = 0 self.last_queue_update = 0 g.run()
def new(self): # initialize all variables and do all the setup for a new game self.draw_debug = False self.paused = False self.night = True # Sprite groups self.all_sprites = pg.sprite.LayeredUpdates() self.obstacles = pg.sprite.Group() self.mobs = pg.sprite.Group() self.bullets = pg.sprite.Group() self.items = pg.sprite.Group() # Map self.map = TiledMap(path.join(self.map_folder, '01.tmx')) self.map_img = self.map.make_map() self.map_rect = self.map_img.get_rect() # Map Objects for tile_object in self.map.tmxdata.objects: # For spawning in center of tiled object obj_center = vec(tile_object.x + tile_object.width / 2, tile_object.y + tile_object.height / 2) if tile_object.name == 'player': self.player = Player(self, obj_center.x, obj_center.y) if tile_object.name == 'zombie': Mob(self, obj_center.x, obj_center.y) if tile_object.name == 'wall': Obstacle(self, tile_object.x, tile_object.y, tile_object.width, tile_object.height) if tile_object.name in ['health', 'shotgun']: Item(self, obj_center, tile_object.name) # Camera object self.camera = Camera(self.map.width, self.map.height) # Start a new level sound self.effects_sounds['level_start'].play()