def start_game(level_name: str = 'main_menu'): """ Starts a game from the beginning. :param level_name: a level name to start with. ('first' by def.) (for all level names, see level.py) """ global \ GAME_OVER, \ GAME_WIN, \ current_level, \ current_score, \ total_score, \ enemy_count, \ GAME_MODE GAME_MODE = True pygame.mixer.stop() if level_name == 'end': end_game() return if level_name == 'first': total_score = 0 if level_name == 'main_menu': GAME_MODE = False sound.play_main_menu_theme() total_score += current_score current_score = 0 GAME_OVER = False GAME_WIN = False current_level = level.levels[level_name] level.load_level(level_name, window)
def __init__(self, levelName, maingame): super(Game, self).__init__(enclosing_game_window=maingame) # Assign game constants self.lives = LIVES self.score = SCORE # Init groups self.all = pygame.sprite.RenderUpdates() self.blocks = pygame.sprite.Group() self.balls = pygame.sprite.Group() self.paddles = pygame.sprite.Group() self.texts = pygame.sprite.Group() # Assign groups Ball.containers = self.all, self.balls Paddle.containers = self.all, self.paddles BaseBlock.containers = self.all, self.blocks # Init messages self.messages = WIN_LOSS_MESSAGES # Init sprites self.level = load_level(levelName) for block in self.level.blocks: self.blocks.add(block) self.all.add(block) self.ball = Ball(game_window=self) self.paddle = Paddle(game_window=self) # Init background self.background = self.level.background self.screen.blit(self.background, (0, 0))
def __init__(self, levelName, maingame): super(Game, self).__init__(enclosing_game_window=maingame) #Assign game constants self.lives = LIVES self.score = SCORE #Init groups self.all = pygame.sprite.RenderUpdates() self.blocks = pygame.sprite.Group() self.balls = pygame.sprite.Group() self.paddles = pygame.sprite.Group() self.texts = pygame.sprite.Group() #Assign groups Ball.containers = self.all, self.balls Paddle.containers = self.all, self.paddles BaseBlock.containers = self.all, self.blocks #Init messages self.messages = WIN_LOSS_MESSAGES #Init sprites self.level = load_level(levelName) for block in self.level.blocks: self.blocks.add(block) self.all.add(block) self.ball = Ball(game_window=self) self.paddle = Paddle(game_window=self) #Init background self.background = self.level.background self.screen.blit(self.background, (0, 0))
def main(): # Set linux terminal input mode to allow character level input tty_settings = termios.tcgetattr(sys.stdin) tty.setcbreak(sys.stdin) level, player, monsters = l.load_level("level.txt") draw_level(level, player, monsters) while True: action = read_player_input() if action == QUIT: break elif action is not None: level, player, monsters, should_quit = perform_player_action( level, player, monsters, action) if should_quit: break level, player, monsters = move_monsters( level, player, clean_up_monsters(monsters)) if p.is_dead(player): print("You lose!") break draw_level(level, player, monsters) # restore linux terminal settings termios.tcsetattr(sys.stdin, termios.TCSADRAIN, tty_settings)
def Init(lvl): global W,H,speed_down,speed_up,speed_x,walls,enemies,level,xE,damage,fuel speed_down = 0 speed_up = 0 speed_x = 0 walls = [] enemies = [] level = load_level(lvl) damage = 192 fuel = 192 main()
def start_new_game(filename): while True: os.system('clear') number_of_players = input("Number of players (1 or 2): ") if number_of_players.isdigit(): if int(number_of_players) in [1, 2]: break player_names = [] for i in range(int(number_of_players)): while True: player_name = input("Name of Player {}: ".format(i + 1)) if player_name != "": player_names.append(player_name) break else: print("Please input a name!") level.load_level(sys.path[0] + '/maps/level1.inf', int(number_of_players), player_names) error = globalLogic.main() return error
def load_game_menu(): os.system('clear') in_load_menu = True saves = sorted(level.get_save_files()) list_stage = 0 print_load_menu(saves, list_stage) while in_load_menu: key = keyboard.getch() if key == "A": if list_stage > 0: list_stage -= 1 os.system('clear') print_load_menu(saves, list_stage) elif key == "B": if list_stage < int((len(saves) - 1) / 9): list_stage += 1 os.system('clear') print_load_menu(saves, list_stage) elif key == "l": try: level_to_load = input("Insert filename without extension: ") level.load_level(sys.path[0] + SAVE_GAME_PATH + level_to_load + ".sav") error = globalLogic.main() except FileNotFoundError: print_load_menu(saves, list_stage) print("{}.sav does not exist".format(sys.path[0] + SAVE_GAME_PATH + level_to_load)) elif key in [str(i) for i in range(10)]: if int(key) == 0: in_load_menu = False if int(key) > 0: if int(key) <= len(saves) - list_stage * 9: os.system('clear') level.load_level(sys.path[0] + SAVE_GAME_PATH + saves[int(key) - 1 + 9 * list_stage]) error = globalLogic.main() in_load_menu = False
def Init( ): # funcion que reinicia las constantes importantes a cada cambio de mapa global W, H, speed_y, walls, enemies, level, xE, damage, fuel, speed_x, lvl speed_y = 0 walls = [] enemies = [] level = load_level(lvl) damage = 192 fuel = 192 speed_x = 0 speed_y = 0 main()
def reset(self): for char in self.zombies.sprites() + [self.player]: char.reset() # TODO: readd zombies level, deco, self.objects, level_rect, cam_origin = load_level() self.fixed_sprites = level + deco + self.objects del self.blocks[:] self.blocks.extend([block.rect for block in level]) if cam_origin: self.camera.reset() self.camera.move(*cam_origin) self.camera.pos.bottom = min(self.camera.pos.bottom, level_rect.bottom) else: self.camera.pos.center = level_rect.center self.camera.pos.bottom = level_rect.bottom self.level_rect = level_rect
# Módulos import sys, pygame from pygame.locals import * from level import init_level, load_level # Constantes W = 1024 H = 640 speed_down = 0 speed_up = 0 speed_x = 0 WHITE = (255, 255, 255) GREEN = (64, 239, 20) walls = [] enemies = [] level = load_level(1) xE = 1 # Clases # --------------------------------------------------------------------- class Player(object): def __init__(self, imagen, transp, posX, posY): self.image = load_image(imagen, transp) self.rect = self.image.get_rect() self.rect.centerx = posX self.rect.centery = posY def Mover_Nave(self, x, y): self.rect.centerx += x
def __init__(self, level_path): self.game_over = False self.win = False self.camera_pos = Vec2d(0,0) self.on_screen = [] self.particles = [] self.shape_map = {} self.pan_left = False self.pan_right = False self.pan_up = False self.pan_down = False self.move_left = False self.move_right = False self.jump = False self.jump_time = 0 self.swap_lr = False self.set_keys_later = False #Editor events self.mode_edit=False self.del_mode=False self.cmd_x=0 self.cmd_y=0 self.pos_start=None self.pos_end=None self.pos_mouse=None self.snap_radius=5.0 self.dec_snap_radius=0 self.inc_snap_radius=0 #Load Sounds self.music = pygame.mixer.Sound(os.path.join(RES, 'music.ogg')) self.win_sound = pygame.mixer.Sound(os.path.join(RES, 'win.ogg')) self.win_sound.set_volume(0.6) self.lose_sound = pygame.mixer.Sound(os.path.join(RES, 'lose.ogg')) self.coin_sound = pygame.mixer.Sound(os.path.join(RES, 'coin.ogg')) self.music.play(-1) #PHYSICS!!!! pm.init_pymunk() self.space = pm.Space() self.space.gravity = Vec2d(0.0, 0.0) # Load level self.level_path = level_path try: self.level = level.load_level(self.level_path) except: self.level = level.Level() if not hasattr(self.level, 'coins'): self.level.coins = [] for line in self.level.lines: self.shape_map[line.shape] = line self.space.add_static(line.shape) for gvol in self.level.gvols: self.shape_map[gvol.shape] = gvol self.space.add_static(gvol.shape) if self.level.goal: self.shape_map[self.level.goal.shape] = self.level.goal self.space.add_static(self.level.goal.shape) for cn in self.level.coins: self.shape_map[cn.shape] = cn self.space.add_static(cn.shape) #gravity polygons self.mode_gvol=False self.mode_grav_vec=False self.mode_grav_poly=False#check whether we've used this poly yet in list self.grav_set=False self.grav_vec=None self.poly_verts=[] #the player self.player = Player(self) self.space.add(self.player.body, self.player.shape) self.shape_map[self.player.shape] = self.player #the mouse self.mouse_body=pm.Body(pm.inf,pm.inf) mouse_shape=pm.Circle(self.mouse_body,3,Vec2d(0,0)) mouse_shape.collision_type=COLLTYPE_MOUSE self.space.add(self.mouse_body,mouse_shape) self.space.add_collision_handler(COLLTYPE_DEFAULT, COLLTYPE_MOUSE, self.del_line,None,None,None) self.space.add_collision_handler(COLLTYPE_GRAVITY, COLLTYPE_MOUSE, self.del_gvol,None,None,None) self.space.add_collision_handler(COLLTYPE_LETHAL, COLLTYPE_MOUSE, self.del_line,None,None,None) self.space.add_collision_handler(COLLTYPE_COIN, COLLTYPE_MOUSE, self.del_coin,None,None,None) #The screen to collide with what we need to draw self.screen_body = pm.Body(pm.inf, pm.inf) self.screen_shape = None self.set_screen_shape() self.space.set_default_collision_handler(None, self.ignore_collision, None, None) self.space.add_collision_handler(COLLTYPE_DEFAULT, COLLTYPE_PLAYER, None, self.collect_player_collisions, None, None) self.space.add_collision_handler(COLLTYPE_GRAVITY, COLLTYPE_PLAYER, self.enter_gravity_volume, self.handle_gvol_collision, None, self.leave_gravity_volume) self.space.add_collision_handler(COLLTYPE_GRAVITY, COLLTYPE_PARTICLE, None, self.handle_gvol_collision, None, None) self.space.add_collision_handler(COLLTYPE_LETHAL, COLLTYPE_PLAYER, None, self.handle_lethal_collision, None, None) self.space.add_collision_handler(COLLTYPE_GOAL, COLLTYPE_PLAYER, None, self.handle_goal_collision, None, None) self.space.add_collision_handler(COLLTYPE_COIN, COLLTYPE_PLAYER, None, self.handle_coin_collision, None, None)
import flags from run_manager import RunManager from character import Character import level ################################# flags.run_manager = RunManager()# flags.character = Character() # ################################# level.load_level(flags.run_manager.display_manager.map) flags.run_manager.start_game()