def reset(self): #resetting the game to the start self.is_playing = False self.is_finished = False self.current_level = 0 self.death_count = [0] self.pos_mouse = (0, 0) self.menus[1] = screens.Scores() self.levels = [ levels.Level1(self.char_id), levels.Level2(self.char_id), levels.Level3(self.char_id), levels.Level4(self.char_id, self.enemy_id), levels.Level5(self.char_id, self.enemy_id), levels.Level6(self.char_id, self.enemy_id), levels.Level7(self.char_id, self.enemy_id), levels.Level8(self.char_id), levels.Level9(self.char_id), levels.Level10(self.char_id) ] self.credits = screens.Credits() self.time_init = 0 self.time_game = 0 self.score = 0
def __init__(self, dim_x, dim_y): self.window = pygame.display.set_mode((dim_x, dim_y)) self.music = pygame.mixer.music.load('Files/SoundTrack.ogg') self.is_open = True self.is_playing = False self.is_in_menu = False self.is_in_scores = False self.is_finished = False self.current_level = 0 self.death_count = [0] self.pos_mouse = (0, 0) self.char_id = 1 self.enemy_id = 2 self.menus = [screens.MainMenu(), screens.Scores(), screens.Settings()] self.levels = [ levels.Level1(self.char_id), levels.Level2(self.char_id), levels.Level3(self.char_id), levels.Level4(self.char_id, self.enemy_id), levels.Level5(self.char_id, self.enemy_id), levels.Level6(self.char_id, self.enemy_id), levels.Level7(self.char_id, self.enemy_id), levels.Level8(self.char_id), levels.Level9(self.char_id), levels.Level10(self.char_id) ] self.credits = screens.Credits() self.time_init = 0 self.time_game = 0 self.score = 0 self.is_paused = False pygame.display.set_caption('I Keep Having This Dream')
def main(winstyle=0): #initialize pygame pygame.init() #initialize display variables #TODO look into possibly moving to main loop surface = pygame.display.set_mode(constants.Game.SCREENRECT.size, pygame.SRCALPHA) surface.fill(constants.Colors.BLUE) #initialize clock variables clock = pygame.time.Clock() lvl1 = levels.Level1() while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() #call levels test = 0 lvl1.update(surface, clock) pygame.time.wait(1000)
def run_game(): # Inicializa o pygame, as configurações e o objeto screen pygame.init() pygame.mixer.init() pygame.font.init() ai_settings = Settings() level = levels.Level1() pygame.display.set_icon(ai_settings.icon_image) screen = pygame.display.set_mode( [ai_settings.screen_width, ai_settings.screen_height]) pygame.display.set_caption(ai_settings.display_name) pygame.mixer.music.load(ai_settings.set_music) pygame.mixer.music.play(-1, 0.0) for num_level in range(1, levels.numlevels + 1): if num_level == 1: is_clearance = gf.start_level_game(level, screen, ai_settings.font_small) if num_level == levels.numlevels: gf.show_text(screen, ai_settings.font_big, is_clearance, True) else: gf.show_text(screen, ai_settings.font_big, is_clearance)
def main(): pygame.init() fps_clock = pygame.time.Clock() screen_surf = pygame.display.set_mode( (win_width, win_height)) # create display Surface pygame.display.set_caption('Python in Space!') # dictionary with all flags representing if key is pressed which are checked by game to control players' ship ship_cntrl = { 'pressed_w': False, 'pressed_a': False, 'pressed_s': False, 'pressed_d': False, 'pressed_space': False } all_sprites = pygame.sprite.Group() all_ship_prj = pygame.sprite.Group() all_enemy_prj = pygame.sprite.Group() all_enemies = pygame.sprite.Group() player = player_ship.Player(screen_surf, ship_cntrl, all_enemies, all_enemy_prj) # create ship object game_text = pygame.freetype.Font(None) all_sprites.add(player) current_lvl = levels.Level1(screen_surf, player, all_enemies, all_enemy_prj, all_sprites) # set current level while True: # game loop for event in pygame.event.get(): # event loop if event.type == pygame.locals.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: # check if any button is currently pressed if event.key == pygame.K_w: ship_cntrl.update({'pressed_w': True}) elif event.key == pygame.K_a: ship_cntrl.update({'pressed_a': True}) elif event.key == pygame.K_s: ship_cntrl.update({'pressed_s': True}) elif event.key == pygame.K_d: ship_cntrl.update({'pressed_d': True}) elif event.key == pygame.K_SPACE: ship_cntrl.update({'pressed_space': True}) new_projectile = player_ship.ShipStandardPrj( player, all_enemies ) # create new projectile if space is pressed all_ship_prj.add(new_projectile) all_sprites.add(new_projectile) logging.debug( F'Active ship projectiles - {len(all_ship_prj)}' ) # check amount of active players' bullets elif event.key == pygame.K_9: # spawns test ship on 9 new_enemy = enemy_ships.Boss1(screen_surf, player, all_enemy_prj, all_sprites, ('top', 85), [(85, 15), (15, 15), 'repeat']) all_enemies.add(new_enemy) all_sprites.add(new_enemy) logging.info( F"Spawned new ship at {new_enemy.rect.center}, total number of enemies {len(all_enemies)}" ) elif event.key == pygame.K_DELETE: for enemy in all_enemies: enemy.kill() elif event.type == pygame.KEYUP: # check for release of the keys if event.key == pygame.K_w: ship_cntrl.update({'pressed_w': False}) elif event.key == pygame.K_a: ship_cntrl.update({'pressed_a': False}) elif event.key == pygame.K_s: ship_cntrl.update({'pressed_s': False}) elif event.key == pygame.K_d: ship_cntrl.update({'pressed_d': False}) elif event.key == pygame.K_SPACE: ship_cntrl.update({'pressed_space': False}) logging.debug( F'Total amount of active sprites: {len(all_sprites.sprites())}, enemies: {len(all_enemies.sprites())}' ) current_lvl.update() all_sprites.update() all_sprites.draw(screen_surf) game_text.render_to(screen_surf, (10, 10), F'Deaths: {player.deaths}', fgcolor=c_white, size=30) if current_lvl.level_completed is True: text_surf, text_rect = game_text.render( F'Level 1 completed! And you only died {player.deaths} times!', fgcolor=c_white, size=35) text_rect.center = (win_width / 2, win_height / 2) screen_surf.blit(text_surf, text_rect) pygame.display.update() fps_clock.tick(fps)
import cutscenes import spriteSheet import drops import enemies # Setup pygame.init() pygame.display.set_caption("Bloo") Screen = pygame.display.set_mode((800, 600), 0, 32) # Load spritesheets platforms.load() weapons.load() drops.load() enemies.load() # Current level lvl = levels.Level1() currentLvl = "Level1" mc = player.Player() # GUI digit1 = gui.Number(1, 1) digit2 = gui.Number(2, 2) digit3 = gui.Number(3, 3) digit4 = gui.Number(4, 4) value, value1, value2, value3, value4 = [0, 0, 0, 0, 0] coinIcon = gui.Coin() bg = pygame.image.load("colored_land.png").convert() bg = pygame.transform.scale(bg, (800, 600)) menuButton = gui.MenuButton() # Title screen title = True titleLogo = pygame.image.load("Bloo.png").convert_alpha()
for event in pygame.event.get(): if event.type == pygame.QUIT: done = True if event.type == pygame.MOUSEBUTTONDOWN: try: if level.time < 60: laser_ogg.play() level.shot(pygame.mouse.get_pos()) elif level.time > 65: level.time = 65 except: if level.time == "Menu": laser_ogg.play() nav = level.shot(pygame.mouse.get_pos()) if nav == 1 and level.level1.status != "Locked": level = levels.Level1() elif nav == 2 and level.level2.status != "Locked": level = levels.Level2() elif nav == 3 and level.level3.status != "Locked": level = levels.Level3() elif nav == 4 and level.level4.status != "Locked": level = levels.Level4() elif nav == 5: level = selection.PlayerSelect() # write game logic here pos = pygame.mouse.get_pos() level.update(pos) if level.time == -1: #Clear Pop Timer threads for obj in gc.get_objects():
class Game: LEVELS = [ levels.Level7(), levels.Level6(), levels.Level5(), levels.Level1(), levels.Level3(), levels.Level8(), levels.Level4(), levels.Level9(), levels.Level2() ] MENU_WIDTH = 200 def __init__(self, width, height, fpsLock): self.width = width self.height = height self.fpsLock = fpsLock self.level = 0 self.life = 3 self.fps = 0 self.timeTot = 0 self.time = 0 self.state = STATE.INITIAL self.gamefield = pygame.Surface((width - self.MENU_WIDTH, height)) self.gamefield = self.gamefield.convert() self.menufield = pygame.Surface((self.MENU_WIDTH, height)) self.menufield = self.menufield.convert() self.addButtons() def addButtons(self): self.fpsMessage = Message((self.MENU_WIDTH-25, 15), str(self.fps)) self.lifeMessage = Message((self.MENU_WIDTH/2, 60), "life: {}".format(self.life)) self.levlMessage = Message((self.MENU_WIDTH/2, 90), "level: {}".format(self.level)) self.timeMessage = Message((self.MENU_WIDTH/2, 120), "time: {0}/{1}".format( self.time / self.fpsLock, self.timeTot / self.fpsLock)) self.endMessage = Message(((self.width - self.MENU_WIDTH)/2, self.height/2), "GAME OVER", size=60, color=(250,0,0)) self.winMessage = Message(((self.width - self.MENU_WIDTH)/2, self.height/2), "WIN!", size=60, color=(247, 167, 111)) pos, w, h = (self.MENU_WIDTH/2, 350), self.MENU_WIDTH*0.75, 40 self.restartButton = Button(pos, w, h, "restart") pos, w, h = (self.MENU_WIDTH/2, 400), self.MENU_WIDTH*0.75, 40 self.addNewMButton = Button(pos, w, h, "new missile") pos, w, h = (self.MENU_WIDTH/2, 200), self.MENU_WIDTH*0.75, 40 self.nextLvlButton = Button(pos, w, h, "next level") pos, w, h = (self.MENU_WIDTH/2, 250), self.MENU_WIDTH*0.75, 40 self.prevLvlButton = Button(pos, w, h, "prev. level") pos, w, h = (self.MENU_WIDTH/2, 300), self.MENU_WIDTH*0.75, 40 self.pauseButton = Button(pos, w, h, "pause") def pause(self): if self.state == STATE.WAIT_UNTILL_STOP: self.state = STATE.PAUSE self.pauseButton.message.update("continue") elif self.state == STATE.PAUSE: self.state = STATE.WAIT_UNTILL_STOP self.pauseButton.message.update("pause") def lose(self): self.initLevel() self.life -= 1 self.time = 0 if self.life == 0: self.state = STATE.LOSS else: self.state = STATE.INITIAL def win(self): self.state = STATE.WIN def restart(self): self.initLevel() self.time = 0 self.life = 3 self.state = STATE.INITIAL def goToNextLevel(self): self.level = min(len(self.LEVELS) - 1, self.level + 1) self.restart() def goToPreviousLevel(self): self.level = max(0, self.level - 1) self.restart() def checkMenuSelection(self, pos): return pos[0] > self.width-self.MENU_WIDTH def checkButtonsSelection(self, pos): x, y = pos[0] - self.width + self.MENU_WIDTH, pos[1] self.restartButton.checkSelected((x,y)) self.addNewMButton.checkSelected((x,y)) self.nextLvlButton.checkSelected((x,y)) self.prevLvlButton.checkSelected((x,y)) self.pauseButton.checkSelected((x,y)) def checkButtonsActivation(self, pos): x, y = pos[0] - self.width + self.MENU_WIDTH, pos[1] if self.restartButton.checkActivated((x,y)): self.restart() if self.addNewMButton.checkActivated((x,y)) and self.state not in (STATE.LOSS, STATE.WIN): self.state = STATE.INITIAL if self.nextLvlButton.checkActivated((x,y)): self.goToNextLevel() if self.prevLvlButton.checkActivated((x,y)): self.goToPreviousLevel() if self.pauseButton.checkActivated((x,y)): self.pause() def display(self, screen): self.gamefield.fill((116, 208, 242)) self.menufield.fill((0, 109, 193)) self.fpsMessage.update(str(self.fps)) self.fpsMessage.display(self.menufield) self.lifeMessage.update("life: {}".format(self.life)) self.lifeMessage.display(self.menufield) self.timeMessage.update("time: {0:.1f}/{1}".format( self.time / float(self.fpsLock), self.timeTot / self.fpsLock)) self.timeMessage.display(self.menufield) self.levlMessage.update("level: {}".format(self.level + 1)) self.levlMessage.display(self.menufield) self.restartButton.display(self.menufield) self.addNewMButton.display(self.menufield) self.nextLvlButton.display(self.menufield) self.prevLvlButton.display(self.menufield) self.pauseButton.display(self.menufield) self.world.display(self.gamefield) if self.state == STATE.LOSS: self.endMessage.display(self.gamefield) if self.state == STATE.WIN: self.winMessage.display(self.gamefield) screen.blit(self.menufield, (self.width-self.MENU_WIDTH, 0)) screen.blit(self.gamefield, (0, 0)) def initLevel(self): width, height = self.width - self.MENU_WIDTH, self.height world = World(width, height) level = self.LEVELS[self.level] level.initialize(world) self.timeTot = level.MAX_TIME self.world = world def run(self, screen, fpsClock): self.initLevel() while True: if self.time == self.timeTot: self.lose() pos = pygame.mouse.get_pos() self.checkButtonsSelection(pos) if self.state == STATE.INITIAL: self.world.canon.setDirection(pos) elif self.state == STATE.CHOOSE_POWER: self.world.canon.setSpeed(pos) elif self.state == STATE.WAIT_UNTILL_STOP: self.world.canon.setDirection(pos) self.world.move(self.win, self.lose) self.time += 1 for event in pygame.event.get(): if event.type == pygame.QUIT: return elif event.type == pygame.MOUSEBUTTONDOWN: if self.checkMenuSelection(pos): self.checkButtonsActivation(pos) else: if self.state == STATE.INITIAL: self.state = STATE.CHOOSE_POWER self.world.canon.state = 1 elif self.state == STATE.CHOOSE_POWER: self.state = STATE.WAIT_UNTILL_STOP self.world.canon.state = 2 self.world.addMissile() elif self.state == STATE.LOSS: self.restart() continue elif self.state == STATE.WIN: self.goToNextLevel() continue self.display(screen) pygame.display.update() self.fps = fpsClock.tick(self.fpsLock)
def run(self): pygame.mixer.music.play(-1) while self.is_open: for event in pygame.event.get(): if event.type == pygame.QUIT: self.is_open = False break if event.type == pygame.MOUSEBUTTONDOWN: self.pos_mouse = pygame.mouse.get_pos() break if self.is_playing: try: if event.type == pygame.KEYDOWN: if event.key == pygame.K_RIGHT or event.key == pygame.K_d: self.levels[ self.current_level].player.velocity_x = 7 if event.key == pygame.K_LEFT or event.key == pygame.K_a: self.levels[ self.current_level].player.velocity_x = -7 if event.key == pygame.K_UP or event.key == pygame.K_w: if not self.levels[ self.current_level].player.is_jumping: self.levels[ self. current_level].player.velocity_y = -15 self.levels[ self. current_level].player.is_jumping = True if event.key == pygame.K_r: self.levels[ self.current_level].player.was_hurt = True self.death_count[0] += 1 if event.type == pygame.KEYUP: if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT or \ event.key == pygame.K_d or event.key == pygame.K_a: self.levels[ self.current_level].player.velocity_x = 0 if event.key == pygame.K_UP or event.key == pygame.K_w: self.levels[ self.current_level].player.velocity_y = 0 except IndexError: pass if not self.is_playing and not self.is_in_menu and not self.is_in_scores: self.menus[0].draw(self.window) self.menus[0].update(self.window) if self.menus[0].elements[1].is_pressed( self.menus[0].element_locations[1], self.pos_mouse): self.is_playing = True self.time_init = time.time() self.is_in_menu = self.menus[0].elements[2].is_pressed( self.menus[0].element_locations[2], self.pos_mouse) self.is_in_scores = self.menus[0].elements[3].is_pressed( self.menus[0].element_locations[3], self.pos_mouse) self.is_open = not self.menus[0].elements[4].is_pressed( self.menus[0].element_locations[4], self.pos_mouse) elif self.is_finished: self.credits.draw(self.window) self.credits.update() if self.credits.have_rolled(): self.is_playing = False self.reset() elif self.is_in_menu: self.menus[2].draw(self.window) if self.menus[2].elements[0].is_pressed( self.menus[2].element_locations[0], self.pos_mouse): self.is_in_menu = False self.pos_mouse = (0, 0) if self.menus[2].elements[1].is_pressed( self.menus[2].element_locations[1], self.pos_mouse): if not self.is_paused: self.is_paused = True pygame.mixer.music.pause() else: self.is_paused = False pygame.mixer.music.unpause() self.pos_mouse = (0, 0) if self.menus[2].elements[2].is_pressed( self.menus[2].element_locations[2], self.pos_mouse): open('Files/scoreboard.txt', 'w').close() self.pos_mouse = (0, 0) self.menus[1] = screens.Scores() if self.menus[2].elements[3].is_pressed( self.menus[2].element_locations[3], self.pos_mouse): if self.char_id == 1: self.char_id = 2 else: self.char_id = 1 self.levels = [ levels.Level1(self.char_id), levels.Level2(self.char_id), levels.Level3(self.char_id), levels.Level4(self.char_id, self.enemy_id), levels.Level5(self.char_id, self.enemy_id), levels.Level6(self.char_id, self.enemy_id), levels.Level7(self.char_id, self.enemy_id), levels.Level8(self.char_id), levels.Level9(self.char_id), levels.Level10(self.char_id) ] self.pos_mouse = (0, 0) if self.menus[2].elements[4].is_pressed( self.menus[2].element_locations[4], self.pos_mouse): if self.enemy_id == 1: self.enemy_id = 2 else: self.enemy_id = 1 self.levels = [ levels.Level1(self.char_id), levels.Level2(self.char_id), levels.Level3(self.char_id), levels.Level4(self.char_id, self.enemy_id), levels.Level5(self.char_id, self.enemy_id), levels.Level6(self.char_id, self.enemy_id), levels.Level7(self.char_id, self.enemy_id), levels.Level8(self.char_id), levels.Level9(self.char_id), levels.Level10(self.char_id) ] self.pos_mouse = (0, 0) elif self.is_in_scores: self.menus[1].draw(self.window) if self.menus[1].elements[0].is_pressed( self.menus[1].element_locations[0], self.pos_mouse): self.is_in_scores = False self.pos_mouse = (0, 0) else: try: self.levels[self.current_level].draw(self.window) deaths = objects.Text(str(self.death_count[0]), 24) deaths.draw(self.window, [10, 450]) self.levels[self.current_level].update(self.death_count) if self.levels[self.current_level].is_finished(): self.current_level += 1 pygame.time.wait(1000) except IndexError: self.time_game = time.time() - self.time_init self.score = 10000 - self.time_game - ( self.death_count[0] * 50) self.is_finished = True scoreboard = open('Files/scoreboard.txt', 'a') scoreboard.write( time.strftime("%d/%m/%Y") + ',' + str(int(self.death_count[0])) + ',' + str(int(self.time_game)) + ',' + str(int(self.score)) + '\n') scoreboard.close() pygame.display.update()