def main(): pygame.init() pygame.running = True running = True routes = None # window for drawing on window_width = config.window_width window_height = config.window_height grid_spacing = config.grid_spacing win = pygame.display.set_mode( (window_width, window_height)) #width and height of window pygame.display.set_caption("Pokémon Crisis") bg = pygame.image.load('sprites/background.jpg').convert() oak = pygame.image.load('sprites/oak.png') new_map = Map() #use all objects, set specific number of grass tree and water objects new_map.generate_map(grass=20, trees=20, water=2) clock = pygame.time.Clock() ash = Player(choice(tuple(new_map.nodes))) font = pygame.font.SysFont('verdana', 10, False, True) path = Pathfinding(new_map.nodes, new_map.movement_cost_area) def redraw_gamewindow(): win.blit(bg, (0, 0)) #put picutre on screen (background) pygame.draw.rect(win, (200, 200, 200), (0, 0, window_width, window_height)) # # draw grid # for x in range(0,window_width,grid_spacing): #col # for y in range(0,window_height,grid_spacing): #row # pygame.draw.rect(win, (125,125,125), (x,y,grid_spacing,grid_spacing),1) Map.draw(win) if routes: # draw bfs route for x, y in routes['BFS']: pygame.draw.rect(win, (0, 0, 255), (x, y, grid_spacing, grid_spacing)) #draw dfs route for x, y in routes['DFS']: pygame.draw.rect(win, (0, 0, 100), (x, y, grid_spacing, grid_spacing)) # draw dijkstra route for x, y in routes['GBFS']: pygame.draw.rect(win, (255, 0, 0), (x, y, grid_spacing, grid_spacing)) #draw DA route for x, y in routes['DA']: pygame.draw.rect(win, (0, 255, 0), (x, y, grid_spacing, grid_spacing)) # draw A* route for x, y in routes['A*']: pygame.draw.rect(win, (100, 100, 100), (x, y, grid_spacing, grid_spacing)) # re-position oak if oob oak_x = target_x oak_y = target_y if oak_x > window_width - oak.get_width(): oak_x -= grid_spacing elif oak_y > window_height - oak.get_height(): oak_y -= grid_spacing win.blit(oak, (oak_x, oak_y)) ash.draw(win) pygame.display.update() #main event loop while running: clock.tick(9) #9 FPS for event in pygame.event.get( ): #get mouse positions, keyboard clicks etc if event.type == pygame.QUIT: #we pressed the exit button running = False if event.type == pygame.MOUSEBUTTONDOWN: target_x, target_y = event.pos[0], event.pos[1] routes = path.compute_all_paths(ash.x, ash.y, target_x, target_y) #move amongst available nodes #(no movement out of bounds and in object coordinates) #movement cost in grass / water ash.move(new_map.objs_area, new_map.movement_cost_area) redraw_gamewindow() pygame.quit()
class StartGame: '''main game class''' def __init__(self, win, net, username): self.menu = False self.grid = False self.win = win self.net = net #generate and save map # new_map = Map() # new_map.generate_map('random', False) self.bg = pygame.image.load('sprites/background.jpg').convert() #load first shuffled map from server self.current_map = 0 Map.load(self.net.maps[self.current_map]) #create client player object and assign colour based on ID, username from input, map from server self.ash = Player( RandomNode(Map.nodes).node, self.net.playerID, username, self.current_map) #other player objects p2, p3, p4, p5 = None, None, None, None self.players = [p2, p3, p4, p5] self.bikes = [Bike() for i in range(config.bikes)] self.mushrooms = [Mushroom() for i in range(config.mushrooms)] WeaponStatus.set_locations(self.bikes, self.mushrooms) music = pygame.mixer.music.load(config.theme) pygame.mixer.music.set_volume(0.5) pygame.mixer.music.play(-1) self.ash.ID = self.net.playerID def redraw_gamewindow(self): '''draw objects onto the screen: background, grid, players, weapons''' self.win.blit(self.bg, (0, 0)) #put picutre on screen (background) # draw grid if self.grid: for x in range(0, config.window_width, config.grid_spacing): #col for y in range(0, config.window_height, config.grid_spacing): #row pygame.draw.rect( self.win, (125, 125, 125), (x, y, config.grid_spacing, config.grid_spacing), 1) Map.draw(self.win) for bike in self.bikes: bike.draw(self.win) for mushroom in self.mushrooms: mushroom.draw(self.win) self.ash.draw(self.win) #draw all players for p in self.players: if p and p.ID != None: p.draw(self.win) #note that we are 20px behind (last position doesnt show... this is temp fix) for bullet in p.inventory: bullet_sprite = pygame.image.load( 'sprites/objects/pokeball.png').convert_alpha() x, y, _dir, start_x, start_y = bullet if _dir == 'L': x += 20 elif _dir == 'R': x -= 20 elif _dir == 'U': y += 20 elif _dir == 'D': y -= 20 self.win.blit(bullet_sprite, (x, y)) self.ash.check_trample(p) #pressed z if self.menu: Menu(self.win, [self.ash.stats, self.ash.username], [[p.stats, p.username] for p in self.players if p]) for bullet in self.ash.inventory: self.ash.draw_bullet(self.win, bullet) for p in self.players: if p and p.ID != None: self.ash.check_kill(bullet, p) pygame.display.update() def change_map(self): '''host (ID=0) may change map i.e. go to next shuffled map from server or back to 0''' if self.ash.map < len(self.net.maps) - 1: self.ash.map += 1 else: self.ash.map = 0 Map.load(self.net.maps[self.ash.map]) WeaponStatus.set_locations(self.bikes, self.mushrooms) def check_keyboard_input(self, event): '''check for Z,X,C''' if event.type == pygame.KEYUP: if event.key == pygame.K_SPACE: self.ash.space_up = True if event.key == pygame.K_z: #z for menu self.menu = True if not self.menu else False if event.key == pygame.K_x: #a for grid self.grid = True if not self.grid else False if event.key == pygame.K_c and self.ash.ID == 0: #host can press c to change map self.change_map() def fetch_data(self): '''get data from server, player positions, stats, kill status etc''' Multiplayer.get_player_data(self.ash, self.net, self.players, self.bikes, self.mushrooms) Multiplayer.check_death_status(self.ash, self.players) def check_collisions_and_pickups(self): WeaponStatus(self.ash) self.ash.move(Map.objs_area, Map.movement_cost_area, self.bikes, self.mushrooms)