def __init__(self, width, height): """ Set up the application. """ super().__init__(width, height) arcade.set_background_color(arcade.color.BLACK) self.shapes = arcade.ShapeElementList() # This is a large rectangle that fills the whole # background. The gradient goes between the two colors # top to bottom. color1 = (215, 214, 165) color2 = (219, 166, 123) points = (0, 0), (SCREEN_WIDTH, 0), (SCREEN_WIDTH, SCREEN_HEIGHT), (0, SCREEN_HEIGHT) colors = (color1, color1, color2, color2) rect = arcade.create_rectangles_filled_with_colors(points, colors) self.shapes.append(rect) # Another rectangle, but in this case the color doesn't change. Just the # transparency. This time it goes from left to right. color1 = (165, 92, 85, 255) color2 = (165, 92, 85, 0) points = (100, 100), (SCREEN_WIDTH - 100, 100), (SCREEN_WIDTH - 100, 300), (100, 300) colors = (color2, color1, color1, color2) rect = arcade.create_rectangles_filled_with_colors(points, colors) self.shapes.append(rect) # Two lines color1 = (7, 67, 88) color2 = (69, 137, 133) points = (100, 400), (SCREEN_WIDTH - 100, 400), (SCREEN_WIDTH - 100, 500), (100, 500) colors = (color2, color1, color2, color1) shape = arcade.create_lines_with_colors(points, colors, line_width=5) self.shapes.append(shape) # Triangle color1 = (215, 214, 165) color2 = (219, 166, 123) color3 = (165, 92, 85) points = (SCREEN_WIDTH // 2, 500), (SCREEN_WIDTH // 2 - 100, 400), (SCREEN_WIDTH // 2 + 100, 400) colors = (color1, color2, color3) shape = arcade.create_triangles_filled_with_colors(points, colors) self.shapes.append(shape) # Ellipse, gradient between center and outside color1 = (69, 137, 133, 127) color2 = (7, 67, 88, 127) shape = arcade.create_ellipse_filled_with_colors(SCREEN_WIDTH // 2, 350, 50, 50, inside_color=color1, outside_color=color2) self.shapes.append(shape)
def setup(window): """ This, and any function with the arcade.decorator.init decorator, is run automatically on start-up. """ window.mountains = [] background = arcade.ShapeElementList() points = (0, 0), (SCREEN_WIDTH, 0), (SCREEN_WIDTH, SCREEN_HEIGHT), (0, SCREEN_HEIGHT) colors = (arcade.color.SKY_BLUE, arcade.color.SKY_BLUE, arcade.color.BLUE, arcade.color.BLUE) rect = arcade.create_rectangles_filled_with_colors(points, colors) background.append(rect) window.mountains.append(background) for i in range(1, 4): color_start = (i * 10, i * 30, i * 10) color_end = (i * 20, i * 40, i * 20) min_y = 0 + 70 * (3 - i) max_y = 120 + 70 * (3 - i) mountain_range = create_mountain_range(min_y, max_y, color_start, color_end) window.mountains.append(mountain_range)
def setup(window): """ This, and any function with the arcade.decorator.init decorator, is run automatically on start-up. """ window.mountains = [] background = arcade.ShapeElementList() color1 = (195, 157, 224) color2 = (240, 203, 163) points = (0, 0), (SCREEN_WIDTH, 0), (SCREEN_WIDTH, SCREEN_HEIGHT), (0, SCREEN_HEIGHT) colors = (color1, color1, color2, color2) rect = arcade.create_rectangles_filled_with_colors(points, colors) background.append(rect) window.mountains.append(background) layer_4 = create_mountain_range([0, 350], [SCREEN_WIDTH, 320], 1.1, 250, 8, (158, 98, 204)) window.mountains.append(layer_4) layer_3 = create_mountain_range([0, 270], [SCREEN_WIDTH, 190], 1.1, 120, 9, (130, 79, 138)) window.mountains.append(layer_3) layer_2 = create_mountain_range([0, 180], [SCREEN_WIDTH, 80], 1.2, 30, 12, (68, 28, 99)) window.mountains.append(layer_2) layer_1 = create_mountain_range([250, 0], [SCREEN_WIDTH, 200], 1.4, 20, 12, (49, 7, 82)) window.mountains.append(layer_1)
def create_wall_shape(self): """ Instead of rendering each wall block, we create a single shape which can be drawn in a single call, rather than a call for each wall block :return: """ self.shape_walls = arcade.ShapeElementList() self.shape_walls.center_x = 0 self.shape_walls.center_y = 0 self.shape_walls.angle = 0 point_list = [] color_list = [] # create the walls into a single shape walls = self.game.walls for wall in walls: points = self.get_entity_dimensions(wall) point_list.append(points[0]) point_list.append(points[1]) point_list.append(points[2]) point_list.append(points[3]) # as we have 4 points for i in range(4): color_list.append(COLOUR_MAP[wall.base_colour]) self.shape_walls.append( arcade.create_rectangles_filled_with_colors( point_list, color_list))
def update_shape_sprite(self, entity: Entity): """ Create/Update the sprite shape for an entity and add/update the entry for it in `self.entities_shapelist` :param entity: :return: """ shape_sprite: ShapeSprite = entity.shape_sprite if entity.id not in self.entities_shapelist: entity_shapelist = arcade.ShapeElementList() # we need to convert from general colours to arcade specific colours entity_shapelist.append( arcade.create_rectangles_filled_with_colors( shape_sprite.point_list, [COLOUR_MAP[x] for x in shape_sprite.color_list])) else: entity_shapelist = self.entities_shapelist[entity.id] entity_shapelist.center_x = shape_sprite.position_x entity_shapelist.center_y = SCREEN_HEIGHT - shape_sprite.position_y entity_shapelist.draw() self.entities_shapelist[entity.id] = entity_shapelist
def setup(self): self.shape_list = arcade.ShapeElementList() # --- Create all the rectangles # We need a list of all the points and colors point_list = [] color_list = [] # Now calculate all the points for x in range(0, SCREEN_WIDTH, SQUARE_SPACING): for y in range(0, SCREEN_HEIGHT, SQUARE_SPACING): # Calculate where the four points of the rectangle will be if # x and y are the center top_left = (x - HALF_SQUARE_WIDTH, y + HALF_SQUARE_HEIGHT) top_right = (x + HALF_SQUARE_WIDTH, y + HALF_SQUARE_HEIGHT) bottom_right = (x + HALF_SQUARE_WIDTH, y - HALF_SQUARE_HEIGHT) bottom_left = (x - HALF_SQUARE_WIDTH, y - HALF_SQUARE_HEIGHT) # Add the points to the points list. # ORDER MATTERS! # Rotate around the rectangle, don't append points caty-corner point_list.append(top_left) point_list.append(top_right) point_list.append(bottom_right) point_list.append(bottom_left) # Add a color for each point. Can be different colors if you want # gradients. for i in range(4): color_list.append(arcade.color.DARK_BLUE) shape = arcade.create_rectangles_filled_with_colors(point_list, color_list) self.shape_list.append(shape)
def create_ceiling(max_width, max_height): color1 = (60, 60, 75, 255) color2 = (0, 0, 0, 0) points = (0, max_height), (max_width, max_height), (max_width, max_height - CEILING_HEIGHT), (0, max_height - CEILING_HEIGHT) colors = (color1, color1, color2, color2) return arcade.create_rectangles_filled_with_colors(points, colors)
def create_mountain_range(start, end, roughness, vertical_displacement, num_of_iterations, color_start): shape_list = arcade.ShapeElementList() layer_1 = midpoint_displacement(start, end, roughness, vertical_displacement, num_of_iterations) layer_1 = fix_points(layer_1) color_list = [color_start] * len(layer_1) lines = arcade.create_rectangles_filled_with_colors(layer_1, color_list) shape_list.append(lines) return shape_list
def _create_shape(self) -> arcade.Shape: """ Create a shape representing the rectangle of this rock. :return: a Arcade shape object. """ colors = [] for i in range(4): colors.append(self.color) return arcade.create_rectangles_filled_with_colors(self.points, colors)
def create_shape_list(self): self.shape_list = arcade.ShapeElementList() point_list = [] color_list = [] colour_codes = [ arcade.color.BLACK, # 0 arcade.color.BLUE, # 1 arcade.color.GREEN, # 2 arcade.color.CYAN, # 3 arcade.color.RED, # 4 arcade.color.MAGENTA, # 5 arcade.color.BROWN, # 6 arcade.color.LIGHT_GRAY, # 7 arcade.color.DARK_GRAY, # 8 arcade.color.LIGHT_BLUE, # 9 arcade.color.LIGHT_GREEN, # 10 arcade.color.LIGHT_CYAN, # 11 arcade.color.LIGHT_RED_OCHRE, # 12 arcade.color.LIGHT_MEDIUM_ORCHID, # 13 arcade.color.WHITE ] for row_index, row_value in enumerate(reversed(self.lines)): for column_index, value in enumerate(row_value): try: value = int(value) except ValueError: continue top_left = (column_index * self.square_size, row_index * self.square_size) top_right = (top_left[0] + self.square_size, top_left[1]) bottom_right = (top_left[0] + self.square_size, top_left[1] + self.square_size) bottom_left = (top_left[0], top_left[1] + self.square_size) point_list.append(top_left) point_list.append(top_right) point_list.append(bottom_right) point_list.append(bottom_left) colour = colour_codes[value] for i in range(4): color_list.append(colour) self.shape_list.append( arcade.create_rectangles_filled_with_colors( point_list, color_list))
def create_shape(self, scale) -> [_arcade.Shape]: """ Create a list of shapes representing the four lines that make up this border. :return: a list of Arcade shapes. """ self._calc_points(scale) colors = [self.color for _ in range(4)] self.shapes = [] for side in [ self.top_points, self.right_points, self.bottom_points, self.left_points ]: self.shapes.append( _arcade.create_rectangles_filled_with_colors(side, colors)) return self.shapes
def update(self, delta_time): color_list = [] point_list = [] damping = 0.9 for y in range(1, ROWS - 1): for x in range(1, COLS - 1): self.buffer_2[y][x] = ( self.buffer_1[y][x - 1] + self.buffer_1[y][x + 1] + self.buffer_1[y + 1][x] + self.buffer_1[y - 1][x]) / 2 - self.buffer_2[y][x] self.buffer_2[y][x] = self.buffer_2[y][x] * damping if self.buffer_2[y][x] <= 0.5: continue for i in range(4): color_list.append( (self.buffer_2[y][x] * 255, self.buffer_2[y][x] * 255, self.buffer_2[y][x] * 255)) new_x = SQUARE_SIZE * x new_y = SQUARE_SIZE * y top_left = (new_x - HALF_SQUARE_WIDTH, new_y + HALF_SQUARE_HEIGHT) top_right = (new_x + HALF_SQUARE_WIDTH, new_y + HALF_SQUARE_HEIGHT) bottom_right = (new_x + HALF_SQUARE_WIDTH, new_y - HALF_SQUARE_HEIGHT) bottom_left = (new_x - HALF_SQUARE_WIDTH, new_y - HALF_SQUARE_HEIGHT) # Add the points to the points list. # ORDER MATTERS! # Rotate around the rectangle, don't append points caty-corner point_list.append(top_left) point_list.append(top_right) point_list.append(bottom_right) point_list.append(bottom_left) shape = arcade.create_rectangles_filled_with_colors( point_list, color_list) self.draw_buffer = arcade.ShapeElementList() self.draw_buffer.append(shape) self.buffer_1, self.buffer_2 = self.buffer_2, self.buffer_1
def create_eye_fills(self, eye_point_list, colour): """ Create the snake body segments (including head/tail). Return a list of vertex buffer objects (VBOs) that can be rendered to the screen efficiently. """ eye_fill_point_list = [] eye_fill_colour_list = [] for point in eye_point_list: eye_fill_point_list.append(point) eye_fill_colour_list.append(colour) # Create the eye group VBO. eye_fills = arcade.create_rectangles_filled_with_colors( eye_fill_point_list, eye_fill_colour_list ) return eye_fills
def __init__(self, map_filename, resolution): width, height = resolution super().__init__(width, height, 'Uciekajacy Kitaszek!', fullscreen=True) self.engine = engine.Engine(map_filename) self.SQUARE_WIDTH = width / self.engine.width self.SQUARE_HEIGHT = height / self.engine.height self.set_mouse_visible(False) self.board_width = width self.board_height = height self.direction = None self.text_color = TEXT_COLOR self.time = 0.0 self.Kitaszek = None self.Zbiggi = arcade.ShapeElementList() self.do_move = None points = [] colors = [] for x, y in self.engine.get_walls(): wall_x = self.SQUARE_WIDTH * x wall_y = self.SQUARE_HEIGHT * y points.append((wall_x, wall_y)) points.append((wall_x + self.SQUARE_WIDTH, wall_y)) points.append( (wall_x + self.SQUARE_WIDTH, wall_y + self.SQUARE_HEIGHT)) points.append((wall_x, wall_y + self.SQUARE_HEIGHT)) colors.append(WALL_COLOR) colors.append(WALL_COLOR) colors.append(WALL_COLOR) colors.append(WALL_COLOR) self.walls = arcade.create_rectangles_filled_with_colors( points, colors) self.setup()
def create_body_segment_fills(self, body_segments): """ Create the snake body segments (including head/tail). Return a list of vertex buffer objects (VBOs) that can be rendered to the screen efficiently. """ body_segment_point_list = [] body_segment_colour_list = [] # Add the head shape points & the head colour. for segment_xy in body_segments[:1]: segment_points = self.get_segment_points(segment_xy) for point in segment_points: body_segment_point_list.append(point) body_segment_colour_list.append(self.head_colour) # Add body segment shape points & the first body segment colour. for segment_xy in body_segments[1::3]: segment_points = self.get_segment_points(segment_xy) for point in segment_points: body_segment_point_list.append(point) body_segment_colour_list.append(self.body_colour_1) # Add body segment shape points & the second body segment colour. for segment_xy in body_segments[2::3]: segment_points = self.get_segment_points(segment_xy) for point in segment_points: body_segment_point_list.append(point) body_segment_colour_list.append(self.body_colour_2) # Add body segment shape points & the third body segment colour. for segment_xy in body_segments[3::3]: segment_points = self.get_segment_points(segment_xy) for point in segment_points: body_segment_point_list.append(point) body_segment_colour_list.append(self.body_colour_3) # Create the entire body VBO. body_segment_fills = arcade.create_rectangles_filled_with_colors( body_segment_point_list, body_segment_colour_list ) return body_segment_fills
def update_shapes(self): """ Updates the shape_list so it reflects the new grid states """ self.shape_list = arcade.ShapeElementList() self.color_list = [] # Iterate over the grid and set cell color according to the cell state for i in range(ROW_COUNT): for j in range(COLUMN_COUNT): if self.grid[i][j] == 0: for k in range(4): self.color_list.append(DEAD_CELL_COLOR) else: for k in range(4): self.color_list.append(ALIVE_CELL_COLOR) # Create the cells's rectangles and add then to the shape list shape = arcade.create_rectangles_filled_with_colors( self.point_list, self.color_list ) self.shape_list.append(shape)
def on_draw(self): """ Called whenever we need to draw the window. """ shapes = arcade.ShapeElementList() pipePoints = [] pipeColors = [] for pipe in self.pipes: pipe.append_points(pipePoints, pipeColors) shapes.append( arcade.create_rectangles_filled_with_colors( pipePoints, pipeColors)) birdPoints = [] birdColors = [] for bird in self.pop.alive: bird.append_points(birdPoints, birdColors) shapes.append( arcade.create_triangles_filled_with_colors(birdPoints, birdColors)) arcade.start_render() shapes.draw() arcade.draw_text('High Score: ' + str(self.highScore), WINDOW_WIDTH // 2 - 40, WINDOW_HEIGHT - FONT_SIZE - 10, arcade.color.TIGERS_EYE, FONT_SIZE)
def get_shape(board, x_offset, y_offset, square_size, square_spacing): point_list = [] color_list = [] for x in range(100): for y in range(100): # Used points instead of squares to draw on screen faster. top_left = (x * square_spacing + x_offset, SCREEN_HEIGHT - y * square_spacing - y_offset) top_right = (top_left[0] + square_size, top_left[1]) bottom_right = (top_right[0], top_right[1] - square_size) bottom_left = (top_left[0], bottom_right[1]) point_list.append(top_left) point_list.append(top_right) point_list.append(bottom_right) point_list.append(bottom_left) for _ in range(4): # Add the color of the block depending on what is in it on the board. color_list.append(COLORS[board[y][x]]) # Return Arcade rectangles. return arcade.create_rectangles_filled_with_colors(point_list, color_list)
def update_map_mode(self, new_map_mode): self.map_mode = new_map_mode self.shape_list = arcade.ShapeElementList() print(self.map_mode) if new_map_mode == 'none': arcade.start_render() self.text_list.append([ 'Welcome', window_width / 2, window_height / 2, arcade.color.BLACK, 20 ]) elif [ 'Welcome', window_width / 2, window_height / 2, arcade.color.BLACK, 20 ] in self.text_list: self.text_list.remove([ 'Welcome', window_width / 2, window_height / 2, arcade.color.BLACK, 20 ]) #self.shape_list.append(shape) #arcade.finish_render() if new_map_mode == 'main': point_list = [] main_color_list = [] max_element = np.amax(self.map) min_element = np.amin(self.map) color_step_0 = math.floor(125 / (max_element)) color_step_1 = math.floor(255 / (max_element)) color_step_2 = math.floor(170 / (max_element)) step_x = math.floor(map_width / size_x) step_y = math.floor(map_height / size_y) for x in range(0, size_x, 1): for y in range(0, size_y, 1): if self.map[x, y] > 0: color_0 = 125 - color_step_0 * (self.map[x, y]) color_1 = 255 - color_step_1 * (self.map[x, y]) color_2 = 170 - color_step_2 * (self.map[x, y]) elif self.map[x, y] > -1: color_0 = 0 color_1 = 171 color_2 = 255 else: color_0 = 0 color_1 = 125 color_2 = 255 for i in range(4): main_color_list.append((color_0, color_1, color_2)) point_list.append((map_width_start + x * step_x, map_height_start + (y + 1) * step_y)) point_list.append((map_width_start + (x + 1) * step_x, map_height_start + (y + 1) * step_y)) point_list.append((map_width_start + (x + 1) * step_x, map_height_start + y * step_y)) point_list.append((map_width_start + x * step_x, map_height_start + y * step_y)) shape = arcade.create_rectangles_filled_with_colors( point_list, main_color_list) self.shape_list.append(shape) if new_map_mode == 'landmass': point_list = [] main_color_list = [] step_x = math.floor(map_width / size_x) step_y = math.floor(map_height / size_y) for x in range(0, size_x, 1): for y in range(0, size_y, 1): if self.map_landmass[x, y] > 0: color_0 = 75 color_1 = 83 color_2 = 32 else: color_0 = 0 color_1 = 127 color_2 = 255 for i in range(4): main_color_list.append((color_0, color_1, color_2)) point_list.append((map_width_start + x * step_x, map_height_start + (y + 1) * step_y)) point_list.append((map_width_start + (x + 1) * step_x, map_height_start + (y + 1) * step_y)) point_list.append((map_width_start + (x + 1) * step_x, map_height_start + y * step_y)) point_list.append((map_width_start + x * step_x, map_height_start + y * step_y)) shape = arcade.create_rectangles_filled_with_colors( point_list, main_color_list) self.shape_list.append(shape)
def make_skyline(width, skyline_height, skyline_color, gap_chance=0.70, window_chance=0.30, light_on_chance=0.5, window_color=(255, 255, 200), window_margin=3, window_gap=2, cap_chance=0.20): """ Make a skyline """ shape_list = arcade.ShapeElementList() # Add the "base" that we build the buildings on shape = arcade.create_rectangle_filled(width / 2, skyline_height / 2, width, skyline_height, skyline_color) shape_list.append(shape) building_center_x = 0 skyline_point_list = [] color_list = [] while building_center_x < width: # Is there a gap between the buildings? if random.random() < gap_chance: gap_width = random.randrange(10, 50) else: gap_width = 0 # Figure out location and size of building building_width = random.randrange(20, 70) building_height = random.randrange(40, 150) building_center_x += gap_width + (building_width / 2) building_center_y = skyline_height + (building_height / 2) x1 = building_center_x - building_width / 2 x2 = building_center_x + building_width / 2 y1 = skyline_height y2 = skyline_height + building_height skyline_point_list.append([x1, y1]) skyline_point_list.append([x1, y2]) skyline_point_list.append([x2, y2]) skyline_point_list.append([x2, y1]) for i in range(4): color_list.append( [skyline_color[0], skyline_color[1], skyline_color[2]]) if random.random() < cap_chance: x1 = building_center_x - building_width / 2 x2 = building_center_x + building_width / 2 x3 = building_center_x y1 = y2 = building_center_y + building_height / 2 y3 = y1 + building_width / 2 shape = arcade.create_polygon([[x1, y1], [x2, y2], [x3, y3]], skyline_color) shape_list.append(shape) # See if we should have some windows if random.random() < window_chance: # Yes windows! How many windows? window_rows = random.randrange(10, 15) window_columns = random.randrange(1, 7) # Based on that, how big should they be? window_height = (building_height - window_margin * 2) / window_rows window_width = (building_width - window_margin * 2 - window_gap * (window_columns - 1)) / window_columns # Find the bottom left of the building so we can start adding widows building_base_y = building_center_y - building_height / 2 building_left_x = building_center_x - building_width / 2 # Loop through each window for row in range(window_rows): for column in range(window_columns): if random.random() < light_on_chance: x1 = building_left_x + column * ( window_width + window_gap) + window_margin x2 = building_left_x + column * ( window_width + window_gap) + window_width + window_margin y1 = building_base_y + row * window_height y2 = building_base_y + row * window_height + window_height * .8 skyline_point_list.append([x1, y1]) skyline_point_list.append([x1, y2]) skyline_point_list.append([x2, y2]) skyline_point_list.append([x2, y1]) for i in range(4): color_list.append( (window_color[0], window_color[1], window_color[2])) building_center_x += (building_width / 2) shape = arcade.create_rectangles_filled_with_colors( skyline_point_list, color_list) shape_list.append(shape) return shape_list
def on_update(self, delta_time): # clear the shape list for the new frame self.shape_list = arcade.ShapeElementList() # self.minimap_shape_list = arcade.ShapeElementList() # set the floor and ceiling colors floor = arcade.create_rectangle( self.screen_width // 2, int(self.screen_height * 0.25), self.screen_width, self.screen_height // 2, self.floor_color ) ceiling = arcade.create_rectangle( self.screen_width // 2, int(self.screen_height * 0.75), self.screen_width, self.screen_height // 2, self.ceiling_color ) # add the floor and ceiling shapes to the shape_list self.shape_list.append(floor) self.shape_list.append(ceiling) # create the point_list and color_list for raycasting point_list = [] color_list = [] # begin raycasting for x in range(0, self.screen_width + 1): # calculate the ray position and direction camera_x = (2 * x / self.screen_width) - 1 if camera_x > 1 or camera_x < -1: print('camera_x is too big or too small!') print(f'camera_x = {camera_x}') ray_dir_x = self.dir_x + self.plane_x * camera_x ray_dir_y = self.dir_y + self.plane_y * camera_x # determine which grid-square of the map we're in map_x = int(self.pos_x) map_y = int(self.pos_y) # length of ray from the current position to the next vertical gridline side_dist_x = None # length of ray from the current position to the next horizontal gridline side_dist_y = None # length of the ray from one horizontal or vertical gridline to the next one try: delta_dist_x = abs(1 / ray_dir_x) except ZeroDivisionError: if ray_dir_y == 0: delta_dist_x = 0 elif ray_dir_x == 0: delta_dist_x = 1 else: delta_dist_x = abs(1 / ray_dir_x) try: delta_dist_y = abs(1 / ray_dir_y) except ZeroDivisionError: if ray_dir_x == 0: delta_dist_y = 0 elif ray_dir_y == 0: delta_dist_y = 1 else: delta_dist_y = abs(1 / ray_dir_y) # the distance to the next perpendicular wall perpendicular_wall_dist = None # which direction to step in the x direction or the y direction (either +1 or -1) step_x = None step_y = None # was a there a wall hit? hit = 0 # was a North/South wall hit or an East/West wall hit? side = None if ray_dir_x < 0: step_x = -1 side_dist_x = (self.pos_x - map_x) * delta_dist_x else: step_x = 1 side_dist_x = (map_x + 1 - self.pos_x) * delta_dist_x if ray_dir_y < 0: step_y = -1 side_dist_y = (self.pos_y - map_y) * delta_dist_y else: step_y = 1 side_dist_y = (map_y + 1 - self.pos_y) * delta_dist_y new_map_x = False new_map_y = False # continually cast the ray out into the distance until it hits a wall while hit == 0: if side_dist_x < side_dist_y: side_dist_x += delta_dist_x map_x += step_x side = 0 else: side_dist_y += delta_dist_y map_y += step_y side = 1 # check if the ray has hit a wall yet if 0 < self.map[map_x][map_y] < 10: hit = 1 if map_x != self.last_map_x: new_map_x = True self.last_map_x = map_x if map_y != self.last_map_y: new_map_y = True self.last_map_y = map_y if not self.minimap[map_x][map_y]: self.minimap[map_x][map_y] = True # top-left self.mm_point_list.append( (MINIMAP_POS_X + map_x * MINIMAP_SIZE, MINIMAP_POS_Y + map_y * MINIMAP_SIZE + MINIMAP_SIZE)) self.mm_color_list.append(arcade.color.BLACK) # top-right self.mm_point_list.append((MINIMAP_POS_X + map_x * MINIMAP_SIZE + MINIMAP_SIZE, MINIMAP_POS_Y + map_y * MINIMAP_SIZE + MINIMAP_SIZE)) self.mm_color_list.append(arcade.color.BLACK) # bottom-right self.mm_point_list.append( (MINIMAP_POS_X + map_x * MINIMAP_SIZE + MINIMAP_SIZE, MINIMAP_POS_Y + map_y * MINIMAP_SIZE)) self.mm_color_list.append(arcade.color.BLACK) # bottom-left self.mm_point_list.append( (MINIMAP_POS_X + map_x * MINIMAP_SIZE, MINIMAP_POS_Y + map_y * MINIMAP_SIZE)) self.mm_color_list.append(arcade.color.BLACK) elif 10 <= self.map[map_x][map_y] < 20: hit = 2 if side == 0: perpendicular_wall_dist = (map_x - self.pos_x + (1 - step_x) / 2) / (ray_dir_x + 0.00000001) else: perpendicular_wall_dist = (map_y - self.pos_y + (1 - step_y) / 2) / (ray_dir_y + 0.00000001) """ ********************************************** MODIFY CODE BELOW FOR ALLOWING PITS/HIGH WALLS ********************************************** """ # the height of the wall at the given pixel column line_height = int(self.screen_height / (perpendicular_wall_dist + 0.00000001)) # the pixel (height) at which to start drawing the wall draw_start = -line_height / 2 + self.screen_height / 2 if draw_start < 0: draw_start = 0 if hit == 1: # if the wall is single-height draw_end = line_height / 2 + self.screen_height / 2 elif hit == 2: # otherwise, if the wall is double-height draw_end = line_height + self.screen_height / 2 if draw_end >= self.screen_height: draw_end = self.screen_height - 1 # set the color with which to draw the given pixel column if side == 0: try: color = self.main_wall_color_list[self.map[map_x][map_y] % 10] except IndexError: color = arcade.color.YELLOW elif side == 1: try: color = self.dark_wall_color_list[self.map[map_x][map_y] % 10] except IndexError: color = arcade.color.DARK_YELLOW draw_start_pos = (x, draw_start) draw_end_pos = (x, draw_end) point_list.append(draw_start_pos) point_list.append(draw_end_pos) if new_map_x or new_map_y: color = arcade.color.BLACK for i in range(2): color_list.append(color) shape = arcade.create_line_generic_with_colors(point_list, color_list, 3) self.shape_list.append(shape) minimap_background = arcade.create_rectangle_filled_with_colors(self.mm_bg_points, self.mm_bg_colors) self.shape_list.append(minimap_background) minimap_shape = arcade.create_rectangles_filled_with_colors(self.mm_point_list, self.mm_color_list) self.shape_list.append(minimap_shape) ppos_point_list = [] ppos_color_list = [] # top-left ppos_point_list.append( (MINIMAP_POS_X + self.pos_x * MINIMAP_SIZE, MINIMAP_POS_Y + self.pos_y * MINIMAP_SIZE + MINIMAP_SIZE)) ppos_color_list.append(arcade.color.BLUE) # top-right ppos_point_list.append((MINIMAP_POS_X + self.pos_x * MINIMAP_SIZE + MINIMAP_SIZE, MINIMAP_POS_Y + self.pos_y * MINIMAP_SIZE + MINIMAP_SIZE)) ppos_color_list.append(arcade.color.RED) # bottom-right ppos_point_list.append( (MINIMAP_POS_X + self.pos_x * MINIMAP_SIZE + MINIMAP_SIZE, MINIMAP_POS_Y + self.pos_y * MINIMAP_SIZE)) ppos_color_list.append(arcade.color.BLUE) # bottom-left ppos_point_list.append((MINIMAP_POS_X + self.pos_x * MINIMAP_SIZE, MINIMAP_POS_Y + self.pos_y * MINIMAP_SIZE)) ppos_color_list.append(arcade.color.BLUE) player_shape = arcade.create_rectangle_filled_with_colors(ppos_point_list, ppos_color_list) self.shape_list.append(player_shape) self.old_time = self.time self.time += delta_time # frame_time is the amount of time this frame spent on screen (in seconds) self.frame_time = (self.time - self.old_time) FPS = 1 / self.frame_time """ ******************************************************** HERE IS WHERE THE CODE FOR AUTO QUALITY ADJUST SHOULD GO ******************************************************** """ if FPS < 60 and self.render_resolution > 2: self.render_resolution -= 1 if FPS > 60: self.render_resolution += 1 self.move_speed = self.frame_time * self.constant_move_speed self.rotation_speed = self.frame_time * self.constant_rotation_speed # print(f'constant rotation speed: {self.constant_rotation_speed}\nframe time: {frame_time}\nadjusted rotation speed: {self.rotation_speed}') self.rotation_speed *= (self.rotate_x_magnitude / 100) if self.move_forward: if not self.map[int(self.pos_x + self.dir_x * self.move_speed)][int(self.pos_y)]: self.pos_x += self.dir_x * self.move_speed if not self.map[int(self.pos_x)][int(self.pos_y + self.dir_y * self.move_speed)]: self.pos_y += self.dir_y * self.move_speed elif self.move_backward: if not self.map[int(self.pos_x - self.dir_x * self.move_speed)][int(self.pos_y)]: self.pos_x -= self.dir_x * self.move_speed if not self.map[int(self.pos_x)][int(self.pos_y - self.dir_y * self.move_speed)]: self.pos_y -= self.dir_y * self.move_speed if self.strafe_left: if not self.map[int(self.pos_x - self.dir_y * self.move_speed)][int(self.pos_y)]: self.pos_x -= self.dir_y * self.move_speed if not self.map[int(self.pos_x)][int(self.pos_y + self.dir_x * self.move_speed)]: self.pos_y += self.dir_x * self.move_speed elif self.strafe_right: if not self.map[int(self.pos_x + self.dir_y * self.move_speed)][int(self.pos_y)]: self.pos_x += self.dir_y * self.move_speed if not self.map[int(self.pos_x)][int(self.pos_y - self.dir_x * self.move_speed)]: self.pos_y -= self.dir_x * self.move_speed if self.rotate_left: # both camera direction and camera plane must be rotated old_dir_x = self.dir_x self.dir_x = self.dir_x * math.cos(self.rotation_speed) - self.dir_y * math.sin(self.rotation_speed) self.dir_y = old_dir_x * math.sin(self.rotation_speed) + self.dir_y * math.cos(self.rotation_speed) old_plane_x = self.plane_x self.plane_x = self.plane_x * math.cos(self.rotation_speed) - self.plane_y * math.sin(self.rotation_speed) self.plane_y = old_plane_x * math.sin(self.rotation_speed) + self.plane_y * math.cos(self.rotation_speed) elif self.rotate_right: # both camera direction and camera plane must be rotated old_dir_x = self.dir_x self.dir_x = self.dir_x * math.cos(-self.rotation_speed) - self.dir_y * math.sin(-self.rotation_speed) self.dir_y = old_dir_x * math.sin(-self.rotation_speed) + self.dir_y * math.cos(-self.rotation_speed) old_plane_x = self.plane_x self.plane_x = self.plane_x * math.cos(-self.rotation_speed) - self.plane_y * math.sin(-self.rotation_speed) self.plane_y = old_plane_x * math.sin(-self.rotation_speed) + self.plane_y * math.cos(-self.rotation_speed) if self.mouse_look: self.mouse_look = False self.rotate_right = False self.rotate_left = False
def on_draw(self): arcade.start_render() arcade.create_rectangles_filled_with_colors(self.point_list, self.color_list).draw()
def get_new_background(max_width, max_height): color1 = rand_color() color2 = rand_color() points = (0, 0), (max_width, 0), (max_width, max_height), (0, max_height) colors = (color1, color1, color2, color2) return arcade.create_rectangles_filled_with_colors(points, colors)