def start_the_game(): clock = pygame.time.Clock() score = Score(display) rocketa = hero.Hero() # enemy = rocket.Rocket(rocketa, display, 1000, 700) # enemy2 = rocket.Rocket(rocketa, display, 100, 100) coin = bonus.Bonus(display, rocketa, 10) coin_list = [] enemy_list = [] coin_counter = 0 time_start = pygame.time.get_ticks() run = True while run: if pygame.time.get_ticks() % 1500 == 0: enemy_list.append( rocket.Rocket(rocketa, display, random.randint(-200, const.width_display + 50), random.randint(-200, const.height_display + 50))) clock.tick(const.FPS) rocketa.change_xy() for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() keys = pygame.key.get_pressed() display.blit(const.bg, (0, 0)) if keys[pygame.K_LEFT]: rocketa.go_left() rocketa.rotate_left() elif keys[pygame.K_RIGHT]: rocketa.go_right() rocketa.rotate_right() else: rocketa.rect() if keys[pygame.K_ESCAPE]: menu.Pause() if coin.check(): coin_counter += 1 # enemy.is_collision() # enemy2.is_collision() for i in enemy_list: i.enemy_change_xy() i.is_collision() coin.update() score.score_update(coin_counter) # enemy.enemy_change_xy() # enemy2.enemy_change_xy() pygame.display.flip()
def loop(self): #record the high scores #print('level loop') # curframe = inspect.currentframe() # calframe = inspect.getouterframes(curframe, 2) # print('caller name:', calframe[1][3]) # stack = inspect.stack() # the_class = stack[1][0].f_locals["self"].__class__.__name__ # the_method = stack[1][0].f_code.co_name # # print("I was called by {}.{}()".format(the_class, the_method)) self.game.high = max(self.game.high, self.game.score) if self.status != '_first': # start up some new sprites ... r = self.get_border(INIT_BORDER) self.run_codes( pygame.Rect(r.x, r.y, r.w, TH)) #top #MYNOTE this is where init normal enemy? self.run_codes(pygame.Rect(r.right - TW, r.y, TW, r.h)) #right self.run_codes(pygame.Rect(r.x, r.bottom - TH, r.w, TH)) #bottom self.run_codes(pygame.Rect(r.x, r.y, TW, r.h)) #left # grab the current existing sprites # doing this avoids a few odd situations sprites = self.sprites[:] #MYNOTE first time self.sprites = []?? may be tile_to_sprite when print sprites it is living things #print(sprites) # mark off the previous rect for s in sprites: s.prev = pygame.Rect(s.rect) # let the sprites do their thing for s in sprites: if s.loop == None: continue s.loop(self, s) # re-calculate the groupings groups = {} for s in sprites: for g in s.groups: if g not in groups: groups[g] = [] groups[g].append(s) # hit them sprites! w-tsh! for s1 in sprites: for g in s1.hit_groups: if g not in groups: continue for s2 in groups[g]: if not s1.rect.colliderect(s2.rect): continue s1.hit(self, s1, s2) # hit walls and junk like that for s in sprites: if not len(s.groups): continue r = s.rect hits = [] for y in xrange(r.top - r.top % TH, r.bottom, TH): for x in xrange(r.left - r.left % TW, r.right, TW): t = self.layer[y / TH][x / TW] if t == None: continue if not t.hit_groups.intersection(s.groups): continue dist = abs(t.rect.centerx - s.rect.centerx) + abs(t.rect.centery - s.rect.centery) hits.append([dist, t]) hits.sort() for dist, t in hits: if not t.rect.colliderect(s.rect): continue t.hit(self, t, s) # remove inactive sprites border = self.get_border(DEINIT_BORDER) for s in sprites: if s.auto_gc and not border.colliderect(s.rect): s.active = False if not s.active: self.sprites.remove( s) # this removes 'em from the real list! if hasattr(s, 'deinit'): s.deinit(self, s) if hasattr(s, '_code'): if s._code not in self.codes: print 'error in code GC', s._code continue del self.codes[s._code] #pan the screen if self.player != None: self.player.pan(self, self.player) # printing tiles position self.tilesData = [] for l in self.layer: for t in l: if t is not None: #if not hasattr(t,'standable'): break #if t.standable != 1: break tileData = [t.rect.centerx, t.rect.centery, t.hit_groups] self.tilesData.append(tileData) # screenView = self.view # printing sprites rect position MYCODE self.bossSprite = None self.playerSprite = None self.playerPos = [0, 0] self.spritesData = [] for s in self.sprites: spriteData = [s.rect.centerx, s.rect.centery] if hasattr(s, 'type'): spriteData.append(s.type) if s.type == 'player': self.playerSprite = s if s.type == 'boss': self.bossSprite = s self.spritesData.append(spriteData) # agentConnect.getScreen(tilesData, spritesData,screenView.left,screenView.top) if self.playerSprite != None: self.playerPos = [ self.playerSprite.rect.centerx, self.playerSprite.rect.centery ] if self.bossSprite != None: self.bossStrength = self.bossSprite.strength else: self.bossStrength = 6 self.currentFitness = agentConnect.fitnessF(self.playerPos, self.title, self.bossStrength) if self.currentFitness > self.bestFitness: self.bestFitness = self.currentFitness self.notImproved = 0 else: if self.title == "Boss" or self.title == "boss_1.tga": self.notImproved += 0.5 else: self.notImproved += 1 if self.currentFitness % 1 == 0: agentConnect.doAction('up') #print self.status #MYCODE random control # if self.frame % 180 < 120: # agentConnect.doAction('right') # else: # agentConnect.doAction('left') # agentConnect.doAction('jump') # agentConnect.doAction('bubble') # more frames self.frame += 1 if (self.frame % FPS) == 0: pass #print self.player.rect.bottom #print '' #print 'frame:',self.frame #print 'sprites:',len(self.sprites) #print 'codes:',len(self.codes) #handle various game status' if self.status == '_first': if self.player.exploded: self.player.loop(self, self.player) else: self.status = 'first' # elif self.status == 'first': # self.status = None # return menu.Pause(self.game,'get ready',self) elif self.status == 'exit': self.game.lcur = (self.game.lcur + 1) % len(levels.LEVELS) if self.game.lcur == 0: # you really won!!! # self.game.music_play('finish') # next = menu.Transition(self.game,self.parent) # return menu.Pause(self.game,'CONGRATULATIONS!',next) next = menu.Transition(self.game, self.parent) return menu.Pause(self.game, 'GAME END', next) else: # self.game.music_play('lvlwin',1) # l2 = Level(self.game,self.fname,self.parent) # next = menu.Transition(self.game,l2) # return menu.Pause(self.game,'good job!',next) next = menu.Transition(self.game, self.parent) return menu.Pause(self.game, 'GAME END', next) elif self.status == 'dead': if self.agentCon is not None: self.agentCon.setGameEnd() # if self.game.lives: # self.game.lives -= 1 # return menu.Transition(self.game,Level(self.game,self.fname,self.parent)) # else: # next = menu.Transition(self.game,self.parent) # return menu.Pause(self.game,'game over',next) next = menu.Transition(self.game, self.parent) return menu.Pause(self.game, 'GAME END', next) elif self.status == 'transition': self.status = None return menu.Transition(self.game, self)
def is_collision(self): if (self.rocketa_rect.left > self.kosmolet.rocketa_rect.left) and ( self.rocketa_rect.right < self.kosmolet.rocketa_rect.right ) and (self.rocketa_rect.top > self.kosmolet.rocketa_rect.top) and ( self.rocketa_rect.bottom < self.kosmolet.rocketa_rect.bottom): menu.Pause()
def loop(self): #record the high scores self.game.high = max(self.game.high, self.game.score) if self.status != '_first': # start up some new sprites ... r = self.get_border(INIT_BORDER) self.run_codes(pygame.Rect(r.x, r.y, r.w, TH)) #top self.run_codes(pygame.Rect(r.right - TW, r.y, TW, r.h)) #right self.run_codes(pygame.Rect(r.x, r.bottom - TH, r.w, TH)) #bottom self.run_codes(pygame.Rect(r.x, r.y, TW, r.h)) #left # grab the current existing sprites # doing this avoids a few odd situations sprites = self.sprites[:] # mark off the previous rect for s in sprites: s.prev = pygame.Rect(s.rect) # let the sprites do their thing for s in sprites: if s.loop == None: continue s.loop(self, s) # re-calculate the groupings groups = {} for s in sprites: for g in s.groups: if g not in groups: groups[g] = [] groups[g].append(s) # hit them sprites! w-tsh! for s1 in sprites: for g in s1.hit_groups: if g not in groups: continue for s2 in groups[g]: if not s1.rect.colliderect(s2.rect): continue s1.hit(self, s1, s2) # hit walls and junk like that for s in sprites: if not len(s.groups): continue r = s.rect hits = [] for y in xrange(r.top - r.top % TH, r.bottom, TH): for x in xrange(r.left - r.left % TW, r.right, TW): t = self.layer[y / TH][x / TW] if t == None: continue if not t.hit_groups.intersection(s.groups): continue dist = abs(t.rect.centerx - s.rect.centerx) + abs(t.rect.centery - s.rect.centery) hits.append([dist, t]) hits.sort() for dist, t in hits: if not t.rect.colliderect(s.rect): continue t.hit(self, t, s) # remove inactive sprites border = self.get_border(DEINIT_BORDER) for s in sprites: if s.auto_gc and not border.colliderect(s.rect): s.active = False if not s.active: self.sprites.remove( s) # this removes 'em from the real list! if hasattr(s, 'deinit'): s.deinit(self, s) if hasattr(s, '_code'): if s._code not in self.codes: print 'error in code GC', s._code continue del self.codes[s._code] #pan the screen if self.player != None: self.player.pan(self, self.player) # more frames self.frame += 1 if (self.frame % FPS) == 0: pass #print self.player.rect.bottom #print '' #print 'frame:',self.frame #print 'sprites:',len(self.sprites) #print 'codes:',len(self.codes) #handle various game status' if self.status == '_first': if self.player.exploded: self.player.loop(self, self.player) else: self.status = 'first' elif self.status == 'first': self.status = None return menu.Pause(self.game, 'get ready', self) elif self.status == 'exit': self.game.lcur = (self.game.lcur + 1) % len(levels.LEVELS) if self.game.lcur == 0: # you really won!!! self.game.music_play('finish') next = menu.Transition(self.game, self.parent) return menu.Pause(self.game, 'CONGRATULATIONS!', next) else: self.game.music_play('lvlwin', 1) l2 = Level(self.game, self.fname, self.parent) next = menu.Transition(self.game, l2) return menu.Pause(self.game, 'good job!', next) elif self.status == 'dead': if self.game.lives: self.game.lives -= 1 return menu.Transition( self.game, Level(self.game, self.fname, self.parent)) else: next = menu.Transition(self.game, self.parent) return menu.Pause(self.game, 'game over', next) elif self.status == 'transition': self.status = None return menu.Transition(self.game, self)
def run_game(self, game: Game) -> None: """Runs the game""" pg.display.set_caption("Treasure Hunt game!") line_color = pg.Color('#8c8c91') # Initializes game loop booleans draw_general_path = False draw_all_path = False draw_path = False draw_grid = True show_all = False exit_game = False game_start = False player_set = False is_paused = False paths_import = False treasures_copied = False treasures_copy = [] # Create Game Menu Objects name_entry = menu.NameEntry(self.screen_size, self.screen) settings_menu = menu.Settings(self.screen_size, self.screen) main_menu = menu.MainMenu(self.screen_size, self.screen) pause = menu.Pause(self.screen_size, self.screen) # Initializes player rect object rect_size = (8, 8) rect_pos = (0, 0) player_rect = pg.Rect(rect_pos, rect_size) map_paths = [] general_path = Path((0, 0)) # Sets default map and path game.set_map(1) game.reset_path() # Initializes empty path list for the current map paths_colors = [pg.Color('#390ac0'), pg.Color('#188418'), pg.Color('#b81118'), pg.Color('#cb02a6'), pg.Color('#1584af'), pg.Color('#2bacc4'), pg.Color('#ac83b1'), pg.Color('#4ca13b'), pg.Color('#9c7463')] # Initialize music and sound pg.mixer.music.load('music/background.mp3') pg.mixer.music.set_volume(0.03) pg.mixer.music.play(-1) # river_sound = rock_sound = pg.mixer.Sound('music/rock.mp3') rock_sound.set_volume(0.1) treasure_sound = pg.mixer.Sound('music/treasure.wav') treasure_sound.set_volume(0.1) fragment_sound = pg.mixer.Sound('music/fragment.mp3') fragment_sound.set_volume(0.1) shortest_path_rect = [] while not exit_game: name_on = not player_set settings_on = False name_input = name_entry.display(name_on) if not player_set: game.player = Player(name_input) game.player.set_vision_radius(20) vision_radius = game.player.get_vision_radius() player_set = True if game_start: menu_on = False else: menu_on = True main_option = main_menu.display(menu_on) if main_option == 'Start': game.reset_path() rect_pos = game.path.initial_pos player_rect = pg.Rect(rect_pos, rect_size) game_start = True main_menu.return_option = '' elif main_option == 'Settings': settings_on = True elif main_option == 'Quit': exit_game = True else: pass settings_option = settings_menu.display(settings_on) if settings_option == 'logout': player_set = False settings_menu.option = '' map_id = settings_menu.map_id current_mode = settings_menu.mode # Sets game map id given by settings game.set_map(map_id) game.path.set_map(map_id) # Defines player movement step size h_step, v_step = game.game_map.get_step() if not paths_import: map_paths = [] for path_ in game.path_list: if path_.get_map() == map_id: map_paths.append(path_) paths_import = True # Generate the general path for the current map. This may slow down performance general_path = Path(initial_pos=(int(self.screen_size[0] / 40 - rect_size[0] / 2), int(self.screen_size[1] / 2 - rect_size[1] / 2))) general_path.set_map(map_id) general_path.set_general_paths(game.map_list[map_id-1], rect_size) # Obtains game objects from the map obstacle_list_type = [(x[0], x[1]) for x in game.game_map.get_obstacles()] obstacle_list = [x[0] for x in game.game_map.get_obstacles()] treasure_list = game.game_map.get_treasures() if not treasures_copied: treasures_copy = copy.deepcopy(treasure_list) treasures_copied = True fragment_list = game.game_map.get_fragments() # Retrieves object types and their relevant information object_type = game.game_map.get_object_types() # Change in position according to movement event dir_key = {K_LEFT: (-h_step, 0), K_RIGHT: (h_step, 0), K_UP: (0, -v_step), K_DOWN: (0, v_step)} # Assign keys to movement names dir_name = {K_LEFT: 'left', K_RIGHT: 'right', K_UP: 'up', K_DOWN: 'down'} # Assign keys to those for opposite direction. This is for reversing a move dir_opposite = {K_LEFT: K_RIGHT, K_RIGHT: K_LEFT, K_UP: K_DOWN, K_DOWN: K_UP} # Game window if game_start: # Checks for possible movements given obstacles in the current map possible_movements = ['left', 'right', 'up', 'down'] margins = [0, 800] available_movements = [] for move in possible_movements: new_pos = next_pos(rect_pos, move, h_step, v_step) center_pos = [new_pos[0] + rect_size[0] / 2, new_pos[1] + rect_size[1] / 2] new_rect = pg.Rect(new_pos, rect_size) if new_rect.collidelist(obstacle_list) == -1 and \ all(x not in margins for x in center_pos): available_movements.append(move) treasure_obtainable = True for event in pg.event.get(): if event.type == QUIT: exit_game = True pg.quit() if current_mode == 'Shortest Path': if player_rect.collidelist(treasures_copy) == -1: shortest_path = [] # Utilizes Shortest Path function if event.type == MOUSEBUTTONDOWN: init_pos = player_rect.topleft for fragment in fragment_list: if fragment.collidepoint(pg.mouse.get_pos()): final_pos = (int(fragment.centerx - rect_size[0] / 2), int(fragment.centery - rect_size[1] / 2)) shortest_path = general_path.shortest_path(init_pos, final_pos) for pos in shortest_path: path_rect_pos = (pos[0] + 2, pos[1] + 2) path_rect = pg.Rect(path_rect_pos, (4, 4)) shortest_path_rect.append(path_rect) pos_change = (pos[0] - init_pos[0], pos[1] - init_pos[1]) rect_pos = tuple(map(sum, zip(rect_pos, pos_change))) player_rect.move_ip(pos_change) game.path.update_path((int(rect_pos[0]), int(rect_pos[1]))) init_pos = rect_pos if event.type == KEYDOWN: if event.key in dir_key and dir_name[event.key] not in available_movements: rock_sound.play() # Assign rectangle movements according to key event if movement is valid if event.key in dir_key and dir_name[event.key] in available_movements: event_key = event.key pos_change = dir_key[event.key] rect_pos = tuple(map(sum, zip(rect_pos, pos_change))) player_rect.move_ip(pos_change) # Checks for fragment and treasure collision # Treasure Collision treasure_collision_index = player_rect.collidelist(treasure_list) if treasure_collision_index != -1: # With at least 3 fragments on treasure collision if game.player.backpack['fragments'] >= 3: treasure_sound.play() # Remove collided treasure from list del treasure_list[treasure_collision_index] game.player.update_backpack('treasures', 1) game.player.update_backpack('fragments', -3) # Not enough fragments on treasure collision else: treasure_obtainable = False # Moves player back to their last position pos_change = dir_key[dir_opposite[event_key]] rect_pos = tuple(map(sum, zip(rect_pos, pos_change))) player_rect.move_ip(pos_change) # Prints info message self.not_enough_fragment() if treasure_obtainable: game.path.update_path((int(rect_pos[0]), int(rect_pos[1]))) if event.key == K_f: show_all = not show_all if event.key == K_ESCAPE: is_paused = not is_paused if event.key == K_g: draw_grid = not draw_grid if event.key == K_p: draw_path = not draw_path if event.key == K_a: draw_all_path = not draw_all_path if event.key == K_h: draw_general_path = not draw_general_path # Fragment Collision fragment_collision_index = player_rect.collidelist(fragment_list) if fragment_collision_index != -1: fragment_sound.play() # remove collided fragment from list del fragment_list[fragment_collision_index] game.player.update_backpack('fragments', 1) # Fills screen self.screen.fill((248, 186, 182)) # Sets color for fragment and treasures treasure_color = pg.Color('#fdcc33') fragment_color = pg.Color('#f25805') if show_all: # Draws all game objects onto screen if show_all is True for o in obstacle_list_type: pg.draw.rect(self.screen, object_type[o[1]][0], o[0]) for treasure in treasure_list: pg.draw.rect(self.screen, treasure_color, treasure) for fragment in fragment_list: pg.draw.rect(self.screen, fragment_color, fragment) else: # Otherwise, utilizes vision field function path_pos = game.path.all_pos vision_rect_size = (rect_size[0] + 2 * vision_radius, rect_size[1] + 2 * vision_radius) vision_rect_pos = [(pos[0] - vision_radius, pos[1] - vision_radius) for pos in path_pos] vision_rects = [pg.Rect(pos, vision_rect_size) for pos in vision_rect_pos] for o in obstacle_list_type: if o[0].collidelist(vision_rects) != -1: pg.draw.rect(self.screen, object_type[o[1]][0], o[0]) for treasure in treasure_list: if treasure.collidelist(vision_rects) != -1: pg.draw.rect(self.screen, treasure_color, treasure) for fragment in fragment_list: if fragment.collidelist(vision_rects) != -1: pg.draw.rect(self.screen, '#ea4915', fragment) # Adds grid to the screen if draw_grid: self.draw_grid(40) for path_rect in shortest_path_rect: pg.draw.rect(self.screen, (35, 25, 25), path_rect) # Draws player rectangle object onto screen pg.draw.rect(self.screen, (255, 255, 255), player_rect) if pause.display(is_paused) == 'exit': player_rect.topleft = game.path.initial_pos game.reset_path() game.player.reset() game.game_map.reset() game_start = False paths_import = False treasures_copied = False shortest_path_rect.clear() pause.reset() is_paused = False if draw_all_path: # Draws all paths for the current map from previous games for i in range(len(map_paths)): if i < len(paths_colors) - 1: path_vertices = map_paths[i].get_graph().get_vertices().values() for vertex in path_vertices: pos = vertex.pos neighbours = vertex.neighbours for neighbour in neighbours: init_pos = (pos[0] + rect_size[0] / 2, pos[1] + rect_size[1] / 2) end_pos = (neighbour.pos[0] + rect_size[0] / 2, neighbour.pos[1] + rect_size[1] / 2) pg.draw.line(self.screen, paths_colors[i], init_pos, end_pos) if draw_general_path: # Draws all possible paths for the current game path_vertices = general_path.get_graph().get_vertices().values() for vertex in path_vertices: pos = vertex.pos neighbours = vertex.neighbours for neighbour in neighbours: init_pos = (pos[0] + rect_size[0] / 2, pos[1] + rect_size[1] / 2) end_pos = (neighbour.pos[0] + rect_size[0] / 2, neighbour.pos[1] + rect_size[1] / 2) pg.draw.line(self.screen, paths_colors[0], init_pos, end_pos) # Showing the path of the player if draw_path: path_vertices = game.path.get_graph().get_vertices().values() for vertex in path_vertices: pos = vertex.pos neighbours = vertex.neighbours for neighbour in neighbours: init_pos = (pos[0] + rect_size[0] / 2, pos[1] + rect_size[1] / 2) end_pos = (neighbour.pos[0] + rect_size[0] / 2, neighbour.pos[1] + rect_size[1] / 2) pg.draw.line(self.screen, line_color, init_pos, end_pos) # On winning game if game.player.backpack['treasures'] == game.game_map.get_difficulty(): # Keeps record of move_count move_count = copy.deepcopy(game.path.move_count) # Reset player position player_rect.topleft = game.path.initial_pos # Saves current game score to player data and resets player game.player.update_data(game.game_map.get_difficulty()) game.player.reset() # Saves the current path to file game.path.write_path() # The reset path function is part of the Game class, contrary to GameMap and Player # This is path requires a given map and player, which are obtained from game. game.reset_path() # Resets the game objects in the current GameMap game.game_map.reset() shortest_path_rect.clear() treasures_copied = False self.game_end(move_count) # Reread from files game = Game() game.read() game.set_map(map_id) paths_import = False game_start = False # Display current number of fragments and treasures the player has found self.show_score(game) pg.display.flip() self.clock.tick(60) pg.quit()