def init_game(self): # initialize all variables and do all the setup for a new game self.reset() # fire button self.button_x = self.GRIDWIDTH + 60 self.button_y = self.GRIDHEIGHT - 100 self.button_width = 93 self.button_height = 45 self.button_fire = Button(self.screen, self.button_x, self.button_y, self.button_width, self.button_height, text='Fire', fontSize=50, margin=20, inactiveColour=(0, 255, 0), hoverColour=(255, 255, 255), pressedColour=(255, 0, 0), radius=5, onRelease=lambda: self.fire_shoot_event())
class Resize(AnimationBase): def __init__(self, widget, timeout, width, height): super().__init__(widget, timeout, width=width, height=height) if __name__ == '__main__': def animate(): resize.start() translate.start() pygame.init() win = pygame.display.set_mode((600, 600)) button = Button(win, 100, 100, 300, 150) resize = Resize(button, 3, 200, 200) translate = Translate(button, 3, 200, 200) button.setOnClick(animate) run = True while run: events = pygame.event.get() for event in events: if event.type == pygame.QUIT: pygame.quit() run = False quit() win.fill((255, 255, 255))
def init(simulation, resolution=[1080, 720], enable_menu=True, enable_metrics=True, fps=20, sync=True): global _simulation _simulation = simulation global _resolution _resolution = resolution # Menu flag global _menu _menu = False global _stop _stop = False global _fps _fps = fps global _sync _sync = sync global _last_render _last_render = 0 pygame.init() pygame.display.set_caption("Bad Boids 4 Life Simulator") global _screen _screen = pygame.display.set_mode(resolution) global _clustering_method _clustering_method = "LARS_CLUSTERING" button_data = [ # Button rectangle function TEXT [[0, _screen.get_height() - 60, 60, 60], toggle_menu, "menu"], [[_screen.get_width() - 130, _screen.get_height() - 60, 60, 60], save, "SAVE"], [[_screen.get_width() - 60, _screen.get_height() - 60, 60, 60], load, "LOAD"], [[_screen.get_width() - 200, _screen.get_height() - 60, 60, 60], change_clustering, _clustering_method] ] # buttons global _buttons _buttons = [] for rect, func, text in button_data: button = Button(_screen, rect[0], rect[1], rect[2], rect[3], text=text, onClick=func) _buttons.append(button) # time global _clock _clock = pygame.time.Clock() global _menu_enabled _menu_enabled = enable_menu global _metrics_enabled _metrics_enabled = enable_metrics global _sliders _sliders = [] if enable_menu: for n, (par, max_value) in enumerate(SLIDABLE_PARAMETERS): slider = LabeledSlider(_screen, 10, resolution[1] - 60 - n * 40, par, initial=_simulation.pars[par], min=0, max=max_value) _sliders.append(slider) return _screen, _clock
grid_sprite_group.add(obj) # 创建 超级人物 super_man = MySprite() super_man.load_img("img/super_man.png", 120, 120, 4) shadow_man = MySprite() shadow_man.load_img("img/fox.png", 85, 113, 4) super_man.rect.midbottom = grid_sprite_group.sprites()[0].rect.centerx, \ grid_sprite_group.sprites()[0].rect.bottom shadow_man.rect.midbottom = super_man.rect.midbottom is_sample = False # 出牌开关 # 创建按钮 button = Button(screen, 195, 388, 150, 64, text = "Click", \ font=pygame.font.Font("font/songti.otf", 40,), fontSize = 30, \ textColour = pygame.Color('green'), onClick = buttonOnClick,\ margin = 6, textVAlign = "left", pressedColour = pygame.Color('red'), \ hoverColour = pygame.Color('blue'), \ onClickParams = [is_sample, ], ) # button.setImage(image = pygame.image.load("img/button.png").convert_alpha()) # 创建筛子 local_var = locals() sieve_group = pygame.sprite.Group() for i in range(1, 7): obj = local_var["num_0" + str(i)] = Ele_Sprite("img/" + str(i) * 2 + ".png") obj.num = i obj.rect.center = 670, 300 sieve_group.add(obj) show_obj = sieve_group.sprites()[0] # 所要显示的筛子对象 swing_num = 0 # 摇筛子的 次数 random_num = 0 # 筛子的点数 random_switch = False # 摇筛子的开关
class BoatGame: def __init__(self, on_play_mode): pg.init() self.clock = pg.time.Clock() self.ON_GAME_PRINT_MAP_DURING_STEPS = ON_GAME_PRINT_MAP_DURING_STEPS self.ON_GAME_PRINT_INFO_DURING_STEPS = ON_GAME_PRINT_INFO_DURING_STEPS self.SETTING_WIDTH = SETTING_WIDTH self.SETTING_HEIGHT = SETTING_HEIGHT self.TILESIZE = TILESIZE self.GRIDWIDTH = GRIDWIDTH self.GRIDHEIGHT = GRIDHEIGHT self.HEIGHT = HEIGHT self.WIDTH = WIDTH self.MIN_START_DISTANCE_PLAYER_ENEMY = MIN_START_DISTANCE_PLAYER_ENEMY self.MIN_START_DISTANCE_PLAYER_EXIT = MIN_START_DISTANCE_PLAYER_EXIT self.NUM_EXITS = NUM_EXITS self.MIN_NUM_POWERUP = MIN_NUM_POWERUP self.MAX_NUM_POWERUP = MAX_NUM_POWERUP self.MIN_NUM_OBSTACLE = MIN_NUM_OBSTACLE self.MAX_NUM_OBSTACLE = MAX_NUM_OBSTACLE self.MIN_LENGHT_LINE = MIN_LENGHT_LINE self.MAX_LENGHT_LINE = MAX_LENGHT_LINE self.MIN_LINES_FOR_OBSTACLES = 1 self.MAX_LINES_FOR_OBSTACLES = 2 self.SIMULTAING_ENVIROMENT = SIMULTAING_ENVIROMENT self.ON_SE_MANUAL_INPUT = ON_SIMULATING_ENV_MANUAL_INPUT_AGENT self.ON_SE_MANUAL_INPUT_PLAYER = ON_SIMULATING_ENV_MANUAL_INPUT_PLAYER self.ENEMY_TURN_EVENT = ENEMY_TURN_EVENT self.PLAYER_SHOOT_EVENT = PLAYER_SHOOT_EVENT self.PLAYER_GAME_OVER = PLAYER_GAME_OVER self.count_resetting = 0 self.max_game_range_powerup = 4 self.on_play_mode = on_play_mode self.nums_values_channel = nums_values_channel[0] self.ON_TRAINING_PRINT_MAP_DURING_STEPS = ON_TRAINING_PRINT_MAP_DURING_STEPS self.ON_TRAINING_PRINT_INFO_DURING_STEPS = ON_TRAINING_PRINT_INFO_DURING_STEPS if on_play_mode: self.screen = pg.display.set_mode((self.WIDTH, self.HEIGHT)) pg.display.set_caption(TITLE) pg.key.set_repeat(500, 100) state = DenseEmbeddingState( scale_global_view=scale_global_view, scales_local_views=scales_local_views, num_channels=num_channels_map, scales_property_views=scales_property_views, nums_values_channel=nums_values_channel, nums_values_property=nums_values_property, num_actions=num_actions, with_property_embedding=with_property_embedding) # Create the net and the baseline net = Net(embedding_mode='dense_embedding', num_local_views=num_local_views, num_property_views=num_property_views, num_actions=num_actions, with_property_embedding=with_property_embedding) baseline = Baseline( embedding_mode='dense_embedding', num_local_views=num_local_views, num_property_views=num_property_views, num_actions=num_actions, with_property_embedding=with_property_embedding) # Create the NPC self.npc = NPC(action=dict(type='int', num_values=6), state=state.get_state_dict(), net=net, baseline=net, name='BoatGame NPC') self.npc.get_npc_configuration() self.npc.load_model() def init_game(self): # initialize all variables and do all the setup for a new game self.reset() # fire button self.button_x = self.GRIDWIDTH + 60 self.button_y = self.GRIDHEIGHT - 100 self.button_width = 93 self.button_height = 45 self.button_fire = Button(self.screen, self.button_x, self.button_y, self.button_width, self.button_height, text='Fire', fontSize=50, margin=20, inactiveColour=(0, 255, 0), hoverColour=(255, 255, 255), pressedColour=(255, 0, 0), radius=5, onRelease=lambda: self.fire_shoot_event()) def fire_shoot_event(self): pg.event.post(pg.event.Event(PLAYER_SHOOT_EVENT)) def reset(self, map_dim=None): # print("my_reset " + str(self.count_resetting)) self.count_resetting += 1 # sprites self.all_sprites = pg.sprite.Group() self.feasible_moves_enemy = pg.sprite.Group() self.feasible_moves_player = pg.sprite.Group() self.fire_xs = pg.sprite.Group() self.characters_sprites = pg.sprite.Group() self.exit_sprites = pg.sprite.Group() self.power_ups_sprites = pg.sprite.Group() self.walls_sprites = pg.sprite.Group() self.powerups_visualization_sprites = pg.sprite.Group() self.powerups_visualization_text_sprites = pg.sprite.Group() Powerups_visualization.sy = 0 self.enemy = None self.walls = [] self.power_ups = [] # walls self.generateMap() if map_dim is not None: self.resize_map(map_dim) # player rand_px, rand_py = random.randint( 1, self.GRIDWIDTH / self.TILESIZE - 2), random.randint( 1, self.GRIDHEIGHT / self.TILESIZE - 2) count = 200 threshold = 1.2 while any( isClose(rand_px, w.x, threshold) and isClose(rand_py, w.y, threshold) for w in self.walls): rand_px, rand_py = random.randint( 1, self.GRIDWIDTH / self.TILESIZE - 2), random.randint( 1, self.GRIDHEIGHT / self.TILESIZE - 2) count -= 1 if count < 0: threshold = 0.5 self.player = Player(self, rand_px, rand_py, self.on_play_mode) # enemies rand_ex, rand_ey = random.randint( 1, self.GRIDWIDTH / self.TILESIZE - 2), random.randint( 1, self.GRIDHEIGHT / self.TILESIZE - 2) count = 200 threshold = 1.2 try: while any( isClose(rand_ex, w.x, threshold) and isClose(rand_ey, w.y, threshold) for w in self.walls + [self.player] ): #and np.linalg.norm((int(rand_ex), int(rand_ey)), (px, py) )> MIN_START_DISTANCE_PLAYER_ENEMY: rand_ex, rand_ey = random.randint( 1, self.GRIDWIDTH / self.TILESIZE - 2), random.randint( 1, self.GRIDHEIGHT / self.TILESIZE - 2) count -= 1 if count < 0: threshold = 0.5 except Exception as e: print("Something else went wrong 1") self.enemy = Enemy(self, rand_ex, rand_ey, self.on_play_mode) self.player.set_nemesi(self.enemy) # exit self.exit = [] if self.NUM_EXITS != 0: rand_exit_x, rand_exit_y = random.randint( 0, self.GRIDWIDTH / self.TILESIZE - 1), random.randint( 0, self.GRIDHEIGHT / self.TILESIZE - 1) try: while any( isClose(rand_exit_x, w.x, 0.5) and isClose(rand_exit_y, w.y, 0.5) for w in self.walls + [self.player] + [self.enemy] ): # and np.linalg.norm((int(rand_exit_x), int(rand_exit_y)), (int(self.player.getX()), int(self.player.getY()))) > MIN_START_DISTANCE_PLAYER_EXIT: rand_exit_x, rand_exit_y = random.randint( 0, self.GRIDWIDTH / self.TILESIZE - 1), random.randint( 0, self.GRIDHEIGHT / self.TILESIZE - 1) except Exception as e: print("Something else went wrong 2") self.exit.append(Exit(self, rand_exit_x, rand_exit_y)) # power ups num_powerup = random.randint(self.MIN_NUM_POWERUP, self.MAX_NUM_POWERUP) effects = [0, 1, 2] random.shuffle(effects) for i in range(num_powerup): rand_pow_x, rand_pow_y = random.randint( 0, self.GRIDWIDTH / self.TILESIZE - 1), random.randint( 0, self.GRIDHEIGHT / self.TILESIZE - 1) rand_effect = effects[i] while any( isClose(rand_pow_x, w.x, 0.5) and isClose(rand_pow_y, w.y, 0.5) for w in self.walls + [self.player] + [self.enemy] + self.power_ups): rand_pow_x, rand_pow_y = random.randint( 0, self.GRIDWIDTH / self.TILESIZE - 1), random.randint( 0, self.GRIDHEIGHT / self.TILESIZE - 1) self.power_ups.append( PowerUps(self, rand_pow_x, rand_pow_y, rand_effect)) if self.ON_TRAINING_PRINT_MAP_DURING_STEPS: print("Map generated:") self.print_map() return self.calc_observation() # reinforcement learning framework functions def step(self, actions): # agent perform the action is_possible_action = self.enemy.step_turn(actions) # random action by the agent's enemy if self.ON_SE_MANUAL_INPUT_PLAYER and self.SIMULTAING_ENVIROMENT: print("Choose action for the player") player_random_action = int(input()) else: player_random_action = self.player.random_action() self.player.step_turn(player_random_action) for powerup in self.power_ups: if self.enemy.getX() == powerup.x and self.enemy.getY( ) == powerup.y: powerup.acquired_by(self.enemy) if self.player.getX() == powerup.x and self.player.getY( ) == powerup.y: powerup.acquired_by(self.player) # update feasible moves and fire_xs self.player.update_shoot_and_feasible_moves() self.enemy.update_shoot_and_feasible_moves() # update last action self.enemy.previus_action = actions # calc next state observation = self.calc_observation() done = self.is_game_over() reward = self.calc_reward(done, is_possible_action) if self.ON_TRAINING_PRINT_INFO_DURING_STEPS: print("Next state: ") self.print_state(observation, actions, done, reward) if self.ON_TRAINING_PRINT_MAP_DURING_STEPS: self.print_map() return observation, reward, done def calc_observation(self): observation = {} global_view = self.calculate_global_observation_matrix() observation["global_view"] = Game.to_one_hot_static( global_view, self.nums_values_channel) observation["local_view_0"] = Game.to_one_hot_static( self.calculate_local_observation_matrix(global_view, 3), self.nums_values_channel) observation["local_view_1"] = Game.to_one_hot_static( self.calculate_local_observation_matrix(global_view, 5), self.nums_values_channel) observation["property_view_0"] = self.calculate_vector_properties( self.enemy) observation["property_view_1"] = self.calculate_vector_properties( self.player) observation["prev_action"] = Game.to_one_hot_static( np.array(self.enemy.previus_action), self.enemy.num_actions) return observation def calculate_global_observation_matrix(self): # Map Observations # 0 = free 5 = fire_x_player # 1 = wall 6 = fire_x_agent/enemie # 2 = player 7 = power-up_1 more range # 3 = agent/enemie 8 = power-up_2 front cannon # 4 = exit 9 = power-up_3 back diagonal cannon w, h = int(self.GRIDWIDTH / self.TILESIZE), int(self.GRIDHEIGHT / self.TILESIZE) global_matrix = [[0 for y in range(h)] for x in range(w)] # walls for wall in self.walls: global_matrix[wall.x][wall.y] = 1 # exits for ex in self.exit: global_matrix[ex.getX()][ex.getY()] = 4 # fire xs for fire_x_p in self.player.fire_shoots: if 0 <= fire_x_p.getX( ) <= self.GRIDWIDTH / self.TILESIZE and 0 <= fire_x_p.getY( ) <= self.GRIDHEIGHT / self.TILESIZE: global_matrix[fire_x_p.getX()][fire_x_p.getY()] = 5 for fire_x_p in self.enemy.fire_shoots: if 0 <= fire_x_p.getX( ) <= self.GRIDWIDTH / self.TILESIZE and 0 <= fire_x_p.getY( ) <= self.GRIDHEIGHT / self.TILESIZE: global_matrix[fire_x_p.getX()][fire_x_p.getY()] = 6 # player global_matrix[self.player.getX()][self.player.getY()] = 2 # self agent global_matrix[self.enemy.getX()][self.enemy.getY()] = 3 # powerups for powerup in self.power_ups: if powerup.effect == 0: global_matrix[powerup.x][powerup.y] = 7 elif powerup.effect == 1: global_matrix[powerup.x][powerup.y] = 8 elif powerup.effect == 2: global_matrix[powerup.x][powerup.y] = 9 return np.array(global_matrix) def calculate_local_observation_matrix(self, global_matrix, n): # print("local observation map:") matricx_local_nxn = [[0 for x in range(n)] for y in range(n)] cells = [int(i - (n - 1) / 2) for i in range(n)] delta_m = int((n - 1) / 2) for y in cells: for x in cells: mx, my = self.enemy.getX() + x, self.enemy.getY( ) + y # just 1 enemy if 0 <= mx < self.GRIDWIDTH / self.TILESIZE and 0 <= my < self.GRIDHEIGHT / self.TILESIZE: matricx_local_nxn[x + delta_m][y + delta_m] = global_matrix[mx][my] else: matricx_local_nxn[x + delta_m][y + delta_m] = 1 return np.array(matricx_local_nxn) def calculate_vector_properties(self, character): # hp [0,1,2] [ 0 1 2 ] # direction -----------------> [ 8 x 4 ] # range [0,1,2,3,4] [ 7 6 5 ] # flag_enemy_on_range[0,1] soglia_1 = character.max_hp + 1 soglia_2 = soglia_1 + character.max_direction_code + 1 soglia_3 = soglia_2 + self.max_game_range_powerup + 1 soglia_4 = soglia_3 + 1 + 1 soglia_5 = soglia_4 + 1 + 1 soglia_6 = soglia_5 + 1 + 1 hp = character.hp direction = soglia_1 + table_feasible_directions[str( character.direction)] range_fire = soglia_2 + character.range_fire flag_enemy_on_range = soglia_3 + character.is_enemy_on_range() powerup0 = soglia_4 + character.has_powerup(0) powerup1 = soglia_5 + character.has_powerup(1) powerup2 = soglia_6 + character.has_powerup(2) return [ hp, direction, range_fire, flag_enemy_on_range, powerup0, powerup1, powerup2 ] def is_game_over(self): if self.player.hp <= 0 or self.enemy.hp <= 0: return True else: if not any(x.check_not_collision() for x in self.enemy.feasible_move): return True return False def calc_reward(self, done, is_possible_action): rew = -0.01 if done and self.player.hp <= 0: rew += 10 * self.enemy.hp elif not is_possible_action: rew = -0.1 return rew # generate map def generateMap(self): # walls num_obstacles = random.randint(self.MIN_NUM_OBSTACLE, self.MAX_NUM_OBSTACLE) # print("wall generating:") for o in range(num_obstacles): rand_center_x = random.randint(3, self.GRIDWIDTH / self.TILESIZE - 3) rand_center_y = random.randint(3, self.GRIDHEIGHT / self.TILESIZE - 3) num_lines = random.randint(self.MIN_LINES_FOR_OBSTACLES, self.MAX_LINES_FOR_OBSTACLES) for l in range(num_lines): rand_x = int(np.random.normal(scale=3)) + rand_center_x rand_y = int(np.random.normal(scale=3)) + rand_center_y direct = random.randint(0, 3) lenght = random.randint(self.MIN_LENGHT_LINE, self.MAX_LENGHT_LINE) for x in range(lenght): pos_x, pos_y = 0, 0 if direct == 0: pos_x, pos_y = rand_x + x - int( lenght / 2), rand_y - int(lenght / 2) elif direct == 1: pos_x, pos_y = rand_x + int( lenght / 2), rand_y + x + int(lenght / 2) elif direct == 2: pos_x, pos_y = rand_x + x + int( lenght / 2), rand_y + x + int(lenght / 2) elif direct == 3: pos_x, pos_y = rand_x - x + int( lenght / 2), rand_y + x + int(lenght / 2) if 0 < pos_x < int(self.GRIDWIDTH / self.TILESIZE) and 0 < pos_y < int( self.GRIDHEIGHT / self.TILESIZE): # print(" " + str(pos_x) + " " + str(pos_y)) self.walls.append(Wall(self, pos_x, pos_y)) def resize_map(self, map_dim): map_width = map_dim[0] map_height = map_dim[1] full_map_width = int(self.GRIDWIDTH / self.TILESIZE) full_map_height = int(self.GRIDHEIGHT / self.TILESIZE) # x diff_x = full_map_width - map_width if diff_x % 2 == 0: edge_x1 = edge_x2 = int(diff_x / 2) else: edge_x1 = int(diff_x / 2) + 1 edge_x2 = int(diff_x / 2) for y in range(full_map_height): x1 = edge_x1 for x_ in range(x1): self.walls.append(Wall(self, x_, y)) x2 = full_map_width - edge_x2 for x_ in range(x2, full_map_width): self.walls.append(Wall(self, x_, y)) # y diff_y = full_map_height - map_height if diff_y % 2 == 0: edge_y1 = edge_y2 = int(diff_y / 2) else: edge_y1 = int(diff_y / 2) + 1 edge_y2 = int(diff_y / 2) for x in range(full_map_width): y1 = edge_y1 for y_ in range(y1): self.walls.append(Wall(self, x, y_)) y2 = full_map_height - edge_y2 for y_ in range(y2, full_map_height): self.walls.append(Wall(self, x, y_)) # online game functions def run(self): # game loop - set self.playing = False to end the game self.playing = True while self.playing: self.dt = self.clock.tick(FPS) / 1000 self.events() self.update() self.draw() def close(self): pg.quit() sys.exit() def step_function_testing(self): # just for testing if self.ON_SE_MANUAL_INPUT: action = int(input("Insert agent action: ")) else: action = self.enemy.random_action() self.step(action) pg.time.delay(400) self.player.update_shoot_and_feasible_moves() self.enemy.update_shoot_and_feasible_moves() def events(self): if self.SIMULTAING_ENVIROMENT: # just for testing self.step_function_testing() return 0 # check hover mouse events = pg.event.get() self.button_fire.listen(events) for block in self.player.feasible_move: if block.rect.collidepoint(pg.mouse.get_pos()): block.image.fill(GREEN) else: block.image.fill(LIGHTGREY) # catch all events here for event in events: # quit if event.type == pg.QUIT: self.close() if event.type == pg.KEYDOWN: if event.key == pg.K_ESCAPE: self.close() # player turn if event.type == pg.MOUSEBUTTONUP: # check feasible player move for block in self.player.feasible_move: if block.rect.collidepoint(pg.mouse.get_pos()): dx = int(block.rect.x / self.TILESIZE) - self.player.x dy = int(block.rect.y / self.TILESIZE) - self.player.y self.player.move(dx, dy) # check powerup for powerup in self.power_ups: if self.player.x == powerup.x and self.player.y == powerup.y: powerup.acquired_by(self.player) # check win for exit in self.exit: if self.player.x == exit.x and self.player.y == exit.y: print("win") if event.type == self.PLAYER_SHOOT_EVENT: self.player.shoot_fire() # enemy turn if event.type == self.ENEMY_TURN_EVENT: pg.time.delay(100) state = self.calc_observation() # print current state # self.print_map() # self.print_state(state, "?", "?", "?") # enemy_action = self.enemy.random_action() enemy_action, probability_distribution = self.npc.select_action( state) # perform agent action is_possible_action = self.enemy.step_turn(enemy_action) # update agent's previus action self.enemy.previus_action = enemy_action # check powerup acquisition for powerup in self.power_ups: if self.enemy.getX() == powerup.x and self.enemy.getY( ) == powerup.y: powerup.acquired_by(self.enemy) # print next state if self.ON_GAME_PRINT_INFO_DURING_STEPS: done = self.is_game_over() reward = self.calc_reward(done, is_possible_action) self.print_state(state, enemy_action, done, reward) if self.ON_GAME_PRINT_MAP_DURING_STEPS: self.print_map() if event.type == self.PLAYER_GAME_OVER: print("Game over") self.reset() # draw funcions def update(self): # update portion of the game loop self.all_sprites.update() self.feasible_moves_enemy.update() self.feasible_moves_player.update() self.fire_xs.update() self.characters_sprites.update() self.exit_sprites.update() self.power_ups_sprites.update() self.walls_sprites.update() self.powerups_visualization_sprites.update() self.powerups_visualization_text_sprites.update() def draw_grid(self): for x in range(0, self.GRIDWIDTH + self.TILESIZE, self.TILESIZE): pg.draw.line(self.screen, LIGHTGREY, (x, 0), (x, self.GRIDHEIGHT)) for y in range(0, self.GRIDHEIGHT + self.TILESIZE, self.TILESIZE): pg.draw.line(self.screen, LIGHTGREY, (0, y), (self.GRIDWIDTH, y)) def draw(self): self.screen.fill(BGCOLOR) pg.draw.rect( self.screen, SETTING_COLOR, (self.GRIDWIDTH, 0, self.SETTING_WIDTH, self.SETTING_HEIGHT)) self.draw_grid() self.all_sprites.draw(self.screen) self.walls_sprites.draw(self.screen) self.feasible_moves_enemy.draw(self.screen) self.feasible_moves_player.draw(self.screen) self.exit_sprites.draw(self.screen) self.power_ups_sprites.draw(self.screen) self.fire_xs.draw(self.screen) self.characters_sprites.draw(self.screen) self.powerups_visualization_sprites.draw(self.screen) self.powerups_visualization_text_sprites.draw(self.screen) self.button_fire.draw() pg.display.flip() def print_state(self, observation, actions, done, reward): soglia_1 = self.enemy.max_hp + 1 soglia_2 = soglia_1 + self.enemy.max_direction_code + 1 soglia_3 = soglia_2 + self.max_game_range_powerup + 1 soglia_4 = soglia_3 + 1 + 1 soglia_5 = soglia_4 + 1 + 1 soglia_6 = soglia_5 + 1 + 1 print("L'azione: " + str(actions) + " ha prodotto un reward di Reward: " + str(reward)) print(" Done= " + str(done)) print(" Agent previus action= " + str(observation["prev_action"])) print(" Property view: ") print(" agent hp=" + str(observation["property_view_0"][0])) print(" agent direction=" + str(observation["property_view_0"][1] - soglia_1)) print(" agent range=" + str(observation["property_view_0"][2] - soglia_2)) print(" agent flag_nemesi_in_range=" + str(observation["property_view_0"][3] - soglia_3)) print(" agent has powerup 0 (range)=" + str(observation["property_view_0"][4] - soglia_4)) print(" agent has powerup 1 (front cannon)=" + str(observation["property_view_0"][5] - soglia_5)) print(" agent has powerup 2 (back diagonal cannon)=" + str(observation["property_view_0"][6] - soglia_6)) print() print(" enemy hp=" + str(observation["property_view_1"][0])) print(" enemy direction=" + str(observation["property_view_1"][1] - soglia_1)) print(" enemy range=" + str(observation["property_view_1"][2] - soglia_2)) print(" enemy flag_nemesi_in_range=" + str(observation["property_view_1"][3] - soglia_3)) print(" agent has powerup 0 (range)=" + str(observation["property_view_1"][4] - soglia_4)) print(" agent has powerup 1 (front cannon)=" + str(observation["property_view_1"][5] - soglia_5)) print(" agent has powerup 2 (back diagonal cannon)=" + str(observation["property_view_1"][6] - soglia_6)) print() print(" full property view_agent=" + str(observation["property_view_0"])) print(" full property view_player=" + str(observation["property_view_1"])) def print_map(self): glb_m = self.calculate_global_observation_matrix() glb_m = glb_m.T df = pandas.DataFrame(glb_m) df[self.player.getX()][self.player.getY()] = "#p#" df[self.enemy.getX()][self.enemy.getY()] = "!e!" print(tabulate(df, headers='keys', tablefmt='fancy_grid')) def add_player_powerup_visualization(self, effect): Powerups_visualization(self, effect)
if event.key == pygame.K_g: generate_walls(lambda: draw(win, grid, ROWS, width), grid, len(grid), start, end) # THESE ARE THE PYGAME_WIDGETS MODULE ELEMENTS # THE BUTTON SLIDER AND TEXTBOX ARE ALL FROM THAT LIBRARY # I JUST INITIALIZED THEM WITH MY OWN VALLUEs button = Button(WIN, 260, 250, 270, 80, text='START', fontSize=50, margin=20, inactiveColour=(255, 0, 0), pressedColour=(0, 255, 0), radius=20, onClick=main, onClickParams=(WIN, WIDTH)) slider = Slider(WIN, 250, 180, 180, 40, min=5, max=40, step=1, handleColour=(255, 0, 0),
35, font='Calibri', fontSize=20, textColour=YELLOW, radius=10, borderThickness=2, borderColour=LIGHT_GREY, colour=DARK_BLUE, onSubmit=read_textbox) # button to activate and deactivate control button1 = Button( screen, 360, Y_VALUE, 90, 30, text='CONTROL', font=pg.font.SysFont('Calibri', 15), inactiveColour=button1_colour, radius=10, onClick=button1_clicked, ) # button to activate and deactivate mission button2 = Button( screen, 455, Y_VALUE, 90, 30, text='MISSION', font=pg.font.SysFont('Calibri', 15), inactiveColour=button2_colour,
BLACK = (0,0,0) GREEN = ("#39FF14") RED = (255,7,58) INIT_EPC = 20 pg.init() fps = 60 clock = pg.time.Clock() screen = pg.display.set_mode((WIDTH,HEIGHT)) pg.display.set_caption('Fourier Series') pg.mouse.set_cursor(*pg.cursors.tri_left) slider = Slider(screen,WIDTH-340,HEIGHT-50,170,16,min=1, max=50, step=1,color=WHITE,handleColor=BLACK,handleRadius=8,initial=INIT_EPC) textBox = TextBox(screen, WIDTH-140,HEIGHT-70,40,40,fontSize=15,colour=BLACK,textColour=GREEN) on_button = Button(screen, WIDTH-150, 20, 50, 30, text="ON", fontSize=15, margin=20, inactiveColour=GREEN, hoverColour=(128,128,128), pressedColour=WHITE, radius=2, onClick=lambda:True) off_button = Button(screen, WIDTH-80, 20, 50, 30, text="OFF", fontSize=15, margin=20, inactiveColour=RED, hoverColour= (128,128,128), pressedColour=WHITE, radius=2, onClick=lambda:True) font1 = pg.font.Font('freesansbold.ttf', 16) text = font1.render('Epicycles : ', True, GREEN, BLACK) button_text = font1.render("Circles : ", True, GREEN, BLACK) text_rect = text.get_rect() button_rect = button_text.get_rect() text_rect.center = [WIDTH-420,HEIGHT-40] button_rect.center = [WIDTH-200, 40]
def end_game(): global score_count, best_score, new_game pygame.mixer.music.pause() game_over_sound.play() root.fill(root_bg_color) if int(score_count) > int(best_score): best_score = score_count game_over_message = game_over_font.render('GAME OVER', True, (0, 0, 0)) game_over_score = game_over_score_font.render(f'SCORE : {score_count:04}', True, (0, 0, 0)) game_best_score = game_over_score_font.render(f'BEST : {best_score:04}', True, (0, 0, 0)) root.blit(game_over_message, [80, 150]) root.blit(game_over_score, [200, 250]) root.blit(game_best_score, [210, 310]) restart_button = Button(root, 80, 400, 200, 40, text='New Game', font=pygame.font.Font("fonts/atari.ttf", 15), fontSize=30, inactiveColour=(0, 0, 0), pressedColour=(0, 255, 0), textColour=(255, 255, 255), onClick=lambda: game_restart()) exit_button = Button(root, 320, 400, 200, 40, text='Exit Game', font=pygame.font.Font("fonts/atari.ttf", 15), fontSize=30, inactiveColour=(0, 0, 0), pressedColour=(0, 255, 0), textColour=(255, 255, 255), onClick=lambda: pygame.quit()) run = True while run: events = pygame.event.get() restart_button.listen(events) exit_button.listen(events) restart_button.draw() exit_button.draw() if new_game: root.fill(root_bg_color) new_game = False run = False pygame.mixer.music.play() pygame.display.update()
handleColour=(255, 255, 255), handleRadius=15, curved=True) ytextbox = TextBox(root, screen_size.get_width() - 100, 50, 100, 40, fontSize=15, borderColour=(91, 100, 103)) quitButton = Button(root, screen_size.get_width() - 100, screen_size.get_height() - 80, 100, 60, text='Quit', fontSize=20, margin=20, inactiveColour=(220, 174, 150), radius=20, onClick=lambda: pygame.quit()) fileButton = Button(root, screen_size.get_width() - 100, screen_size.get_height() - 150, 100, 60, text='File Select', fontSize=20, margin=10, inactiveColour=(220, 174, 150), radius=20)
class Recolour(AnimationBase): def __init__(self, widget, timeout, colour): super().__init__(widget, timeout, colour=colour) if __name__ == '__main__': from pygame_widgets import Button def animate(): resize.start() translate.start() pygame.init() win = pygame.display.set_mode((600, 600)) button = Button(win, 100, 100, 300, 150, inactiveColour=(0, 200, 0), hoverColour=(0, 200, 0)) resize = Resize(button, 3, 200, 200) translate = Recolour(button, 5, (0, 100, 100)) button.setOnClick(animate) run = True while run: events = pygame.event.get() for event in events: if event.type == pygame.QUIT: pygame.quit() run = False quit() win.fill((255, 255, 255))
def mute_music(): global music_is_playing if music_is_playing: pygame.mixer.music.pause() music_is_playing = False else: pygame.mixer.music.unpause() music_is_playing = True # buttons mute_image = pygame.image.load('images/Audio-mute.png') mute_image = pygame.transform.scale(mute_image, (17, 17)) mute = Button(screen, 20, 160, 17, 17, image=mute_image, onClick=mute_music) def calculate_distance(x1, y1, x2, y2): dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2) return dist class Background: def __init__(self): self.bg_image = pygame.image.load('images/background.png') self.bg_image = pygame.transform.scale(self.bg_image, (screen_width, screen_height)) self.bg_image_rect = self.bg_image.get_rect() self.bgY1 = 0 self.bgX1 = 0
def intro(): choiceRect = choice.get_rect() choiceRect.center = ((SCREEN_WIDTH // 2, SCREEN_WIDTH // 2 - 100)) hintRect = hint.get_rect() hintRect.center = ((SCREEN_WIDTH // 2, SCREEN_HEIGHT - 100)) xButton = Button(screen, (SCREEN_WIDTH - 50) // 2 - 50, (SCREEN_HEIGHT - 50) // 2, 50, 50, text='X', fontSize=30, margin=20, font=GAME_FONT_30, textColour=PINK, inactiveColour=BACKGROUND_COLOR, pressedColour=PINK, radius=5, onClick=lambda: chooseIcon('X')) oButton = Button(screen, (SCREEN_WIDTH - 50) // 2 + 50, (SCREEN_HEIGHT - 50) // 2, 50, 50, text='O', fontSize=30, margin=20, font=GAME_FONT_30, textColour=PINK, inactiveColour=BACKGROUND_COLOR, pressedColour=PINK, radius=5, onClick=lambda: chooseIcon('O')) screen.blit(choice, choiceRect) screen.blit(hint, hintRect) while USER is '': events = pygame.event.get() for event in events: if event.type == pygame.QUIT: pygame.quit() oButton.listen(events) xButton.listen(events) oButton.draw() xButton.draw() pygame.display.update() clearTheScreen()
flockDisplay = pygame.display.set_mode(displaySize) pygame.display.set_caption('Flocking of Birds') pygame.mouse.set_cursor(*pygame.cursors.tri_left) p1_cam = pygame.Rect(0, 0, (div*width)//1, height) p2_cam = pygame.Rect((div*width)//1, 0, width-(div*width)//1, height) canvas1 = pygame.Surface(p1_cam[2:]) canvas2 = pygame.Surface(p2_cam[2:]) pygame.draw.rect(flockDisplay, black, p1_cam,width=4) pygame.draw.rect(flockDisplay, black, p2_cam,width=1) forceNames = ["Alignment","Cohesion","Separation"] params = ["Perception Radius", "Force Weights"] reset_button = Button( flockDisplay, p2_cam.x+150, 40+(6+1)*70, 70, 40, text='Reset', fontSize=20, margin=20, inactiveColour=(128, 128, 128), hoverColour=(255,255,255), pressedColour=(153, 255, 255), radius=2, onClick=lambda:True ) font1 = pygame.font.Font('freesansbold.ttf', 16) head_texts, head_rects, side_texts, side_rects = [], [], [], [] k=0 for i,param in enumerate(params): head_texts.append(font1.render(param, True, white, black)) head_rects.append(head_texts[0].get_rect()) head_rects[-1].center = [p2_cam.x+120,20+250*i] font2 = pygame.font.Font('freesansbold.ttf', 10) for j,force in enumerate(forceNames): force = force + " : " side_texts.append(font2.render(force, True, white, black))
def start(): global MOVE MOVE = not MOVE sliders = [ Slider(win, 700, 50, 70, 20, min=1, max=L, step=0.1, initial=1), Slider(win, 700, 100, 70, 20, min=30, max=350, step=0.1, initial=30), Slider(win, 700, 150, 70, 20, min=1, max=5000, step=0.1, initial=9.81) ] button_s = Button(win, 10, 10, 100, 50, text='Start/Stop', fontSize=30, margin=20, inactiveColour=green, pressedColour=red, radius=50, onClick=start) def redraw_win(): win.fill(white) pendulum.draw(win) texts = [ FONT.render(f"Amplitude {A}", True, black), FONT.render(f"Length {L}", True, black), FONT.render(f"Gravity {G}", True, black) ]
def __init__(self): self.board = board.Board() self.white = players.Human self.black = players.Human self.history = deque() self.moves = 0 self.state = self.menu self.result = '' self.difficulty = 2 self.menuButtons = [ ButtonArray(win, 80, 200, 720, 200, (2, 1), topBorder=20, bottomBorder=20, leftBorder=100, rightBorder=100, separationThickness=200, borderRadius=20, inactiveColours=(WHITE, BLACK), pressedColours=(LIGHT_GREY, GREY), images=(HUMAN_ICON, HUMAN_ICON), radii=(20, 20), hoverColours=(LIGHT_GREY, GREY), onReleases=(self.selectComputer, self.selectComputer), onReleaseParams=((WHITE, ), (BLACK, ))), ButtonArray(win, 80, 500, 720, 100, (4, 1), topBorder=10, bottomBorder=10, leftBorder=80, rightBorder=80, separationThickness=80, borderRadius=20, texts=('1', '2', '3', '4'), fontSizes=(40, 40, 40, 40), inactiveColours=(RED, GREEN, RED, RED), pressedColours=(RED, GREEN, RED, RED), hoverColours=(DARK_RED, DARK_GREEN, DARK_RED, DARK_RED), onClicks=(self.selectDifficulty, self.selectDifficulty, self.selectDifficulty, self.selectDifficulty), onClickParams=((1, ), (2, ), (3, ), (4, )), radii=(40, 40, 40, 40)), Button(win, 30, 700, 820, 150, inactiveColour=ORANGE, hoverColour=DARK_ORANGE, onClick=self.startGame, text='Start', font=START, radius=30) ] self.endButtons = [ Button(win, 30, 700, 820, 150, inactiveColour=ORANGE, hoverColour=DARK_ORANGE, pressedColour=DARK_ORANGE, onRelease=self.showMenu, text='Play Again?', font=START, radius=30) ]