def draw_field(self): if self.background is not None: arcade.draw_lrwh_rectangle_textured(0, 0, self.box.box_sizes[0], self.box.box_sizes[1], self.background) else: arcade.cleanup_texture_cache() if self.box.field is None: return gx = numpy.arange(0, self.box.box_sizes[0], 50) gy = numpy.arange(0, self.box.box_sizes[1], 50) for x in gx: for y in gy: position = self.box.center.copy() position[0] = x position[1] = y value = 10 * self.box.field.equation(position=position) try: arcade.draw_line(x, y, x + value[0], y + value[1], [255, 255, 255], 1) arcade.draw_circle_filled(x + value[0], y + value[1], 2, [255, 255, 255]) except Exception: pass self.background = arcade.Texture("background", arcade.get_image(0, 0))
def on_draw(self): """ Render the screen. """ if self.draw_counter % 10 == 0: image_bytes = arcade.get_image().tobytes() width = arcade.get_image().width height = arcade.get_image().height self.ndiImgSender.send_image(image_bytes, width, height) # This command has to happen before we start drawing arcade.start_render() self.shape_list.draw() self.draw_counter += 1
def _get_image(self): print(self.black_background) self.black_background.draw() self._draw_pipes() self._draw_players() image = arcade.get_image(0, 0, self.width, self.height) # image = image.convert('RGB') # image.show() # image = numpy.array(image) # gray = cv2.cvtColor(image[30:490, 0:400], cv2.COLOR_BGR2GRAY) # PIL.Image.fromarray(gray1).show() # gray = cv2.cvtColor(image[30:490, 0:400], cv2.COLOR_RGB2GRAY) return image
def on_draw(self): """ Render the screen. """ # Clear the screen to the background color arcade.start_render() # Draw our sprites self.roads_list.draw() self.trees_list.draw() self.buildings_list.draw() image = arcade.get_image() image.save('screenshot.png', 'PNG')
def _draw(self): arcade.start_render() if self._cached_background is None: _render_background(self.width, self.height) _render_map(self.viewport, self.map) self._cached_background = arcade.Texture("bg", arcade.get_image(), hit_box_algorithm="None") arcade.draw_lrwh_rectangle_textured(0, 0, self.width, self.height, self._cached_background) if self.tracks is not None and self._track_index < len(self.tracks): frame = self.tracks[self._track_index] _render_obstacles(self.viewport, frame) self._track_index += 1
def update(self, delta_time): # Start update timer self.performance_timing.start_timer('update') # Stop timing the update self.performance_timing.stop_timer('update') # Figure out if we need more coins if self.performance_timing.target_n > len(self.coin_list): new_coin_amount = self.performance_timing.target_n - len( self.coin_list) self.add_coins(new_coin_amount) # End the program run if self.performance_timing.end_run(): # Save screenshot image = arcade.get_image() image.save(RESULTS_IMAGE, 'PNG') self.close()
def on_key_press(self, key, key_modifiers): """ Called whenever a key on the keyboard is pressed. For a full list of keys, see: http://arcade.academy/arcade.key.html """ if key == arcade.key.DOWN: self.cannon_angle -= 5 self.cannon_angle = max(self.cannon_angle, 0) if key == arcade.key.UP: self.cannon_angle += 5 self.cannon_angle = min(self.cannon_angle, 90) if key == arcade.key.SPACE: self.fire_cannonball() if key == arcade.key.P: image = arcade.get_image() path = f"screenshot_{MyGame.i}.png" image.save(path) print("Saved a screenshot at path: ", path)
def update(self, delta_time): # Start update timer self.performance_timing.start_timer('update') self.player_list.update() if self.player.center_x < 0 and self.player.change_x < 0: self.player.change_x *= -1 if self.player.center_y < 0 and self.player.change_y < 0: self.player.change_y *= -1 if self.player.center_x > SCREEN_WIDTH and self.player.change_x > 0: self.player.change_x *= -1 if self.player.center_y > SCREEN_HEIGHT and self.player.change_y > 0: self.player.change_y *= -1 coin_hit_list = arcade.check_for_collision_with_list( self.player, self.coin_list) for coin in coin_hit_list: coin.center_x = random.randrange(SCREEN_WIDTH) coin.center_y = random.randrange(SCREEN_HEIGHT) # Stop timing the update self.performance_timing.stop_timer('update') # Figure out if we need more coins if self.performance_timing.target_n > len(self.coin_list): new_coin_amount = self.performance_timing.target_n - len( self.coin_list) self.add_coins(new_coin_amount) # End the program run if self.performance_timing.end_run(): # Save screenshot image = arcade.get_image() image.save(RESULTS_IMAGE, 'PNG') self.close() import pyglet pyglet.app.exit()
def on_update(self, dt): """ Move everything """ # Start update timer self.performance_timing.start_timer('update') for shape in self.shape_list: shape.move() # Stop timing the update self.performance_timing.stop_timer('update') # Figure out if we need more coins if self.performance_timing.target_n > len(self.shape_list): new_coin_amount = self.performance_timing.target_n - len(self.shape_list) self.add_shapes(new_coin_amount) # End the program run if self.performance_timing.end_run(): # Save screenshot image = arcade.get_image() image.save(self.results_image, 'PNG') self.close()
def draw_field(self): if self.background is not None: arcade.draw_lrwh_rectangle_textured(0, 0, self.box.box_sizes[0], self.box.box_sizes[1], self.background) else: arcade.cleanup_texture_cache() if self.box.field == None: return gx = numpy.arange(0, self.box.box_sizes[0], 80) gy = numpy.arange(0, self.box.box_sizes[1], 80) for x in gx: for y in gy: speed = 100 * self.box.field.getvalue([x, y]) try: arcade.draw_line(x, y, x + speed[0], y + speed[1], [255, 255, 255], 1) arcade.draw_circle_filled(x + speed[0], y + speed[1], 2, [255, 255, 255]) except: pass self.background = arcade.Texture("background", arcade.get_image(0, 0))
def main(): model = Dql() for episode in range(0, EPISODES): current_sequence = collections.deque() game = GameWindow() game.initial_display_state() current_sequence.append(image_preprocess(arcade.get_image(), 0)) epsilon = 0.3 for t in range(1, TIMESTEPS): print("Timestep{}".format(t)) action_t = None if random.random() < epsilon or len(current_sequence) < 4: action_t = random.choice(list(AgentActions)) else: current_t = np.array([ current_sequence[0], current_sequence[1], current_sequence[2], current_sequence[3] ]) current_t = current_t.reshape(1, 4, 100, 100, 1) result_t = model.prediction(current_t) print(np.argmax(result_t)) action_t = AgentActions(np.argmax(result_t)) game.on_key_press(action_t, None) reward_t = game.on_update(1) game.on_draw() print("DequeLen{}".format(len(current_sequence))) if len(current_sequence) == 4: previous_sequence = current_sequence.copy() current_sequence.popleft() current_sequence.append(image_preprocess( arcade.get_image(), t)) model.store_transition(previous_sequence, action_t, reward_t, current_sequence) if (reward_t == Reward.NEG): break #Random minibatch n = model.get_storage_pos() batch_input = np.array([]).reshape(0, 4, 100, 100, 1) batch_output = np.array([]).reshape(0, 3) for i in range(0, min(n, 20)): random_sample = np.random.randint(low=n) s_prev, s_curr, s_tns = model.get_transition(random_sample) if (s_tns[1] == Reward.NEG): y = Reward.NEG else: s_curr = s_curr.reshape(1, 4, 100, 100, 1) y = s_tns[1] + GAMMA * np.max(model.prediction(s_curr)) s_prev = s_prev.reshape(1, 4, 100, 100, 1) target = np.array([0, 0, 0]) target[s_tns[0]] = y batch_output = np.vstack((batch_output, target)) batch_input = np.vstack((batch_input, s_prev)) model.fit(batch_input, batch_output) else: current_sequence.append(image_preprocess( arcade.get_image(), t)) if (reward_t == Reward.NEG): break arcade.close_window()
def on_update(self, delay_time: float): """TODO: Update game 60 times per min. :returns: Obversition, reward, terminal """ #self.action = Amazing_brick.get_action() #print(action) #self.action_update_game(self.action) # update physics engine self.physics_engine.update() reward = 0.0 # get reward form cal score score, max_score, reward = self.cal_score() # action update game #self.action_update_game(self.action) # Restart the game when collode if (self.player.collides_with_list(self.pipe_sprites) or self.player.collides_with_list(self.enemy_sprites) or self.player.bottom < 0 or self.view_bottom < 0): # time.sleep(0.5) # self.setup() self.TOTAL_GAME_NUM += 1 self.is_game_running = False # Set reward reward = -1 # if self.player.left < 0: # self.player.left = 0 # if self.player.right > SCREEN_WIDTH: # self.player.right = SCREEN_WIDTH # Manage Scrolling changed = False # Scrolling up top_boundary = self.view_bottom + SCREEN_HEIGHT - TOP_VIEW_MARGIN if self.player.top > top_boundary: self.view_bottom += self.player.top - top_boundary changed = True if changed: # Only scroll to integers. -> Make sure we end up with pixels that # don't line up on the screen self.view_bottom = int(self.view_bottom) # Do the scrolling arcade.set_viewport(0, SCREEN_WIDTH, self.view_bottom, SCREEN_HEIGHT + self.view_bottom) # Create pipe sprites if they are already into the screen and delete pipe sprites if they are no langer on the screen. # If all sprites in the same position are delated, then flag = True flag = False pipe_list = self.get_pipe_y() for pipe in self.pipe_sprites: pipe_new = pipe.center_y + PIPE_TWO_DISTANCE if (top_boundary - pipe.center_y < PIPE_TWO_DISTANCE ) and pipe_new not in pipe_list and self.is_add_pipe: self.create_pipe_and_enemy( int(pipe.center_y + PIPE_TWO_DISTANCE)) if pipe.center_y < self.view_bottom: if (top_boundary - pipe.center_y < PIPE_TWO_DISTANCE) and pipe_new not in pipe_list: self.create_pipe_and_enemy( int(pipe.center_y + PIPE_TWO_DISTANCE)) self.pipe_sprites.remove(pipe) pipe_list = self.get_pipe_y() flag = True # Make sure destory all pipe in the same position if pipe.center_y in pipe_list: flag = False if flag: self.is_add_pipe = True for enemy in self.enemy_sprites: if enemy.center_y < self.view_bottom: enemy.kill() # print('num of pipe sprites:',len(self.pipe_sprites)) # print('num of enemy sprites:',len(self.enemy_sprites)) image = np.array( arcade.get_image(0, 0, width=int(SCREEN_WIDTH * RETINA_SCALING), height=int(SCREEN_HEIGHT * RETINA_SCALING))) # image.save(image_name,'PNG') #print('save',type(image), 'succeed') #return image #image = np.array(image) #image = DQNagent.preprocess(image) # record the action and corrsponding reward self.agent.record(self.action, reward, score, max_score, self.is_game_running, image) # make decision self.action = self.agent.NextAction(reward, self.TOTAL_GAME_NUM) # Update game with action self.player.update(self.action) self.all_sprites.update()
def on_draw(self): """ Render the screen. """ # Start the render process. This must be done before any drawing commands. arcade.start_render() # Draw a grid # Draw vertical lines every 120 pixels for x in range(0, 601, 120): arcade.draw_line(x, 0, x, 600, arcade.color.BLACK, 2) # Draw horizontal lines every 200 pixels for y in range(0, 601, 200): arcade.draw_line(0, y, 800, y, arcade.color.BLACK, 2) # Draw a point arcade.draw_text("draw_point", 3, 405, arcade.color.BLACK, 12) arcade.draw_point(60, 495, arcade.color.RED, 10) # Draw a set of points arcade.draw_text("draw_points", 123, 405, arcade.color.BLACK, 12) point_list = ((165, 495), (165, 480), (165, 465), (195, 495), (195, 480), (195, 465)) arcade.draw_points(point_list, arcade.color.ZAFFRE, 10) # Draw a line arcade.draw_text("draw_line", 243, 405, arcade.color.BLACK, 12) arcade.draw_line(270, 495, 300, 450, arcade.color.WOOD_BROWN, 3) # Draw a set of lines arcade.draw_text("draw_lines", 363, 405, arcade.color.BLACK, 12) point_list = ((390, 450), (450, 450), (390, 480), (450, 480), (390, 510), (450, 510)) arcade.draw_lines(point_list, arcade.color.BLUE, 3) # Draw a line strip arcade.draw_text("draw_line_strip", 483, 405, arcade.color.BLACK, 12) point_list = ((510, 450), (570, 450), (510, 480), (570, 480), (510, 510), (570, 510)) arcade.draw_line_strip(point_list, arcade.color.TROPICAL_RAIN_FOREST, 3) arcade.draw_line_strip(point_list, arcade.color.BEIGE) # Draw a polygon arcade.draw_text("draw_polygon_outline", 3, 207, arcade.color.BLACK, 9) point_list = ((30, 240), (45, 240), (60, 255), (60, 285), (45, 300), (30, 300)) arcade.draw_polygon_outline(point_list, arcade.color.SPANISH_VIOLET, 3) # Draw a filled in polygon arcade.draw_text("draw_polygon_filled", 123, 207, arcade.color.BLACK, 9) point_list = ((150, 240), (165, 240), (180, 255), (180, 285), (165, 300), (150, 300)) arcade.draw_polygon_filled(point_list, arcade.color.SPANISH_VIOLET) # Draw an outline of a circle arcade.draw_text("draw_circle_outline", 243, 207, arcade.color.BLACK, 10) arcade.draw_circle_outline(300, 285, 18, arcade.color.WISTERIA, 3) arcade.draw_circle_outline(350, 285, 18, arcade.color.WISTERIA) # Draw a filled in circle arcade.draw_text("draw_circle_filled", 363, 207, arcade.color.BLACK, 10) arcade.draw_circle_filled(420, 285, 18, arcade.color.GREEN) # Draw an ellipse outline, and another one rotated arcade.draw_text("draw_ellipse_outline", 483, 207, arcade.color.BLACK, 10) arcade.draw_ellipse_outline(540, 273, 15, 36, arcade.color.AMBER, 3) arcade.draw_ellipse_outline(540, 336, 15, 36, arcade.color.BLACK_BEAN, 3, 45) # Draw a filled ellipse, and another one rotated arcade.draw_text("draw_ellipse_filled", 3, 3, arcade.color.BLACK, 10) arcade.draw_ellipse_filled(60, 81, 15, 36, arcade.color.AMBER) arcade.draw_ellipse_filled(60, 144, 15, 36, arcade.color.BLACK_BEAN, 45) # Draw an arc, and another one rotated arcade.draw_text("draw_arc/filled_arc", 123, 3, arcade.color.BLACK, 10) arcade.draw_arc_outline(150, 81, 15, 36, arcade.color.BRIGHT_MAROON, 90, 360) arcade.draw_arc_filled(150, 144, 15, 36, arcade.color.BOTTLE_GREEN, 90, 360, 45) # Draw an rectangle outline arcade.draw_text("draw_rect", 243, 3, arcade.color.BLACK, 10) arcade.draw_rectangle_outline(295, 100, 45, 65, arcade.color.BRITISH_RACING_GREEN) arcade.draw_rectangle_outline(295, 160, 20, 45, arcade.color.BRITISH_RACING_GREEN, 3, 45) # Draw a filled in rectangle arcade.draw_text("draw_filled_rect", 363, 3, arcade.color.BLACK, 10) arcade.draw_rectangle_filled(420, 100, 45, 65, arcade.color.BLUSH) arcade.draw_rectangle_filled(420, 160, 20, 40, arcade.color.BLUSH, 45) # Load and draw an image to the screen # Image from kenney.nl asset pack #1 arcade.draw_text("draw_bitmap", 483, 3, arcade.color.BLACK, 12) texture = arcade.load_texture("images/playerShip1_orange.png") scale = .6 arcade.draw_texture_rectangle(540, 120, scale * texture.width, scale * texture.height, texture, 0) arcade.draw_texture_rectangle(540, 60, scale * texture.width, scale * texture.height, texture, 45) color = arcade.get_pixel(100, 100) assert color == (255, 255, 255) image = arcade.get_image()
def __init__(self, title, track_file): temp_track_data = [] start_pos = '' track_sprite_dir = "sprites/track-structure/" track_sprite_ext = ".bmp" with open(track_file) as track_data: GRAPHICS_SCALE = float(track_data.readline()) TILE_SIZE = int(ORG_TILE_SIZE * GRAPHICS_SCALE) start_pos = track_data.readline().split(',') temp_track_data = track_data.readlines() self.track_sprites = arcade.SpriteList() for i, track_line in enumerate(reversed(temp_track_data)): track_line = track_line.split(',') for j, tile in enumerate(track_line): if isValidTrackTile(tile): track_sprite = arcade.Sprite( track_sprite_dir + tile + track_sprite_ext, GRAPHICS_SCALE) track_sprite.top = (i + 1) * TILE_SIZE track_sprite.left = j * TILE_SIZE self.track_sprites.append(track_sprite) self.window_width = len(track_line) * TILE_SIZE self.window_height = len(temp_track_data) * TILE_SIZE super().__init__(self.window_width, self.window_height, title) self.track_sprites.draw() self.track_shape = arcade.get_image() if not DEBUG: track_sprite_dir = "sprites/track-texture/" track_sprite_ext = ".png" self.track_sprites = arcade.SpriteList() for i, track_line in enumerate(reversed(temp_track_data)): track_line = track_line.split(',') for j, tile in enumerate(track_line): if isValidTrackTile(tile): track_sprite = arcade.Sprite( track_sprite_dir + tile + track_sprite_ext, GRAPHICS_SCALE) track_sprite.top = (i + 1) * TILE_SIZE track_sprite.left = j * TILE_SIZE self.track_sprites.append(track_sprite) arcade.set_background_color(arcade.color.AMAZON) else: self.distance_lines = [] arcade.set_background_color(arcade.color.WHITE) self.car = Car( int(start_pos[1]) * TILE_SIZE - (TILE_SIZE / 2), (len(temp_track_data) - int(start_pos[0])) * TILE_SIZE - (TILE_SIZE / 2), int(start_pos[2]), "sprites/car.png", GRAPHICS_SCALE) self.distance_angles = [-90, -60, -30, 0, 30, 60, 90] self.end = False self.steer_dir = 0 self.steer_speed = 0 self.data_file = open( "NN_data/" + datetime.datetime.now().strftime("%Y_%m_%d_%H_%M") + ".txt", "w") joysticks = arcade.get_joysticks() if joysticks: self.joystick = joysticks[0] self.joystick.open() """ else:
def on_draw(self): """ Render the screen. """ # Start the render process. This must be done before any drawing commands. arcade.start_render() # Draw a grid # Draw vertical lines every 120 pixels for x in range(0, 601, 120): arcade.draw_line(x, 0, x, 600, arcade.color.BLACK, 2) # Draw horizontal lines every 200 pixels for y in range(0, 601, 200): arcade.draw_line(0, y, 800, y, arcade.color.BLACK, 2) # Draw a point arcade.draw_text("draw_point", 3, 405, arcade.color.BLACK, 12) arcade.draw_point(60, 495, arcade.color.RED, 10) # Draw a set of points arcade.draw_text("draw_points", 123, 405, arcade.color.BLACK, 12) point_list = ((165, 495), (165, 480), (165, 465), (195, 495), (195, 480), (195, 465)) arcade.draw_points(point_list, arcade.color.ZAFFRE, 10) # Draw a line arcade.draw_text("draw_line", 243, 405, arcade.color.BLACK, 12) arcade.draw_line(270, 495, 300, 450, arcade.color.WOOD_BROWN, 3) # Draw a set of lines arcade.draw_text("draw_lines", 363, 405, arcade.color.BLACK, 12) point_list = ((390, 450), (450, 450), (390, 480), (450, 480), (390, 510), (450, 510)) arcade.draw_lines(point_list, arcade.color.BLUE, 3) # Draw a line strip arcade.draw_text("draw_line_strip", 483, 405, arcade.color.BLACK, 12) point_list = ((510, 450), (570, 450), (510, 480), (570, 480), (510, 510), (570, 510)) arcade.draw_line_strip(point_list, arcade.color.TROPICAL_RAIN_FOREST, 3) arcade.draw_line_strip(point_list, arcade.color.BEIGE) # Draw a polygon arcade.draw_text("draw_polygon_outline", 3, 207, arcade.color.BLACK, 9) point_list = ((30, 240), (45, 240), (60, 255), (60, 285), (45, 300), (30, 300)) arcade.draw_polygon_outline(point_list, arcade.color.SPANISH_VIOLET, 3) # Draw a filled in polygon arcade.draw_text("draw_polygon_filled", 123, 207, arcade.color.BLACK, 9) point_list = ((150, 240), (165, 240), (180, 255), (180, 285), (165, 300), (150, 300)) arcade.draw_polygon_filled(point_list, arcade.color.SPANISH_VIOLET) # Draw an outline of a circle arcade.draw_text("draw_circle_outline", 243, 207, arcade.color.BLACK, 10) arcade.draw_circle_outline(300, 285, 18, arcade.color.WISTERIA, 3) arcade.draw_circle_outline(350, 285, 18, arcade.color.WISTERIA) # Draw a filled in circle arcade.draw_text("draw_circle_filled", 363, 207, arcade.color.BLACK, 10) arcade.draw_circle_filled(420, 285, 18, arcade.color.GREEN) # Draw an ellipse outline, and another one rotated arcade.draw_text("draw_ellipse_outline", 483, 207, arcade.color.BLACK, 10) arcade.draw_ellipse_outline(540, 273, 15, 36, arcade.color.AMBER, 3) arcade.draw_ellipse_outline(540, 336, 15, 36, arcade.color.BLACK_BEAN, 3, 45) # Draw a filled ellipse, and another one rotated arcade.draw_text("draw_ellipse_filled", 3, 3, arcade.color.BLACK, 10) arcade.draw_ellipse_filled(60, 81, 15, 36, arcade.color.AMBER) arcade.draw_ellipse_filled(60, 144, 15, 36, arcade.color.BLACK_BEAN, 45) # Draw an arc, and another one rotated arcade.draw_text("draw_arc/filled_arc", 123, 3, arcade.color.BLACK, 10) arcade.draw_arc_outline(150, 81, 15, 36, arcade.color.BRIGHT_MAROON, 90, 360) arcade.draw_arc_filled(150, 144, 15, 36, arcade.color.BOTTLE_GREEN, 90, 360, 45) # Draw an rectangle outline arcade.draw_text("draw_rect", 243, 3, arcade.color.BLACK, 10) arcade.draw_rectangle_outline(295, 100, 45, 65, arcade.color.BRITISH_RACING_GREEN) arcade.draw_rectangle_outline(295, 160, 20, 45, arcade.color.BRITISH_RACING_GREEN, 3, 45) # Draw a filled in rectangle arcade.draw_text("draw_filled_rect", 363, 3, arcade.color.BLACK, 10) arcade.draw_rectangle_filled(420, 100, 45, 65, arcade.color.BLUSH) arcade.draw_rectangle_filled(420, 160, 20, 40, arcade.color.BLUSH, 45) # Load and draw an image to the screen # Image from kenney.nl asset pack #1 arcade.draw_text("draw_bitmap", 483, 3, arcade.color.BLACK, 12) texture = arcade.load_texture( ":resources:images/space_shooter/playerShip1_orange.png") scale = .6 # arcade.draw_texture_rectangle(540, 120, scale * texture.width, # scale * texture.height, texture, 0) # arcade.draw_texture_rectangle(540, 60, scale * texture.width, # scale * texture.height, texture, 45) # # Overlapping, with transparency test # Draw arcade.draw_rectangle_filled(650, 100, 50, 50, (255, 0, 0)) arcade.draw_rectangle_filled(670, 100, 50, 50, (0, 255, 0, 127)) import sys # TODO: Fix. See https://github.com/pvcraven/arcade/issues/539 if sys.platform != "darwin": # Test colors color = arcade.get_pixel(635, 100) assert color == (255, 0, 0) color = arcade.get_pixel(670, 100) assert color == (128, 127, 0) color = arcade.get_pixel(690, 100) assert color == (128, 255, 128) # Test this other thing color = arcade.get_pixel(100, 100) assert color == (255, 255, 255) # Run the get image. Ideally we'd test the output arcade.get_image()