def on_update(self, delta_time): """ Movement and game logic """ # Reset collision data total = 0 for ball in self.ball_list: ball.hit = False if ball.status != Status.VULNERABLE: total += 1 self.totals.append(total) n = len(self.totals) self.red_points.append((5 + WINDOW_WIDTH + n * 0.4, (total * 6))) self.blue_points.append( (5 + WINDOW_WIDTH + n * 0.4, ((100 - total) * 6))) self.red_line_strip = arcade.create_line_strip(self.red_points, arcade.color.RED, 2) self.blue_line_strip = arcade.create_line_strip( self.blue_points, arcade.color.BLUE, 2) for ball in self.ball_list: ball.x += ball.change_x ball.y += ball.change_y if ball.status == Status.INFECTIOUS: ball.infectious -= 1 if ball.infectious == 0: ball.immune() # Collision with walls if ball.x < ball.size: ball.change_x *= -1 if ball.y < ball.size: ball.change_y *= -1 if ball.x > WINDOW_WIDTH - ball.size: ball.change_x *= -1 if ball.y > WINDOW_HEIGHT - ball.size: ball.change_y *= -1 # Collision with another ball for other in self.ball_list: if other.id == ball.id: continue if other.hit: continue if (-ball.size < (ball.x - other.x) < ball.size) and (-ball.size < (ball.y - other.y) < ball.size): (ball.change_x, other.change_x) = (other.change_x, ball.change_x) (ball.change_y, other.change_y) = (other.change_y, ball.change_y) other.hit = True if ball.status == Status.INFECTIOUS: other.infect() if other.status == Status.INFECTIOUS: ball.infect() ball.hit = True break
def __setup_draw(self): self.road_back.append( arcade.create_line_strip(self.points, arcade.color.DARK_GRAY, self.thickness)) self.road_front.append( arcade.create_line_strip(self.points, arcade.color.BLACK, 1)) for e in self.points: self.draw_points.append( arcade.create_ellipse(e.x, e.y, 1, 1, arcade.color.YELLOW)) for b in self.turn_boundaries: self.draw_boundaries.append(b.draw_line(20))
def __init__(self, width, height, title): """ Set up the application. """ super().__init__(width, height, title) self.shape_list = arcade.ShapeElementList() point_list = ((0, 50), (10, 10), (50, 0), (10, -10), (0, -50), (-10, -10), (-50, 0), (-10, 10), (0, 50)) colors = [ getattr(arcade.color, color) for color in dir(arcade.color) if not color.startswith("__") ] for i in range(200): x = SCREEN_WIDTH // 2 - random.randrange(SCREEN_WIDTH) y = SCREEN_HEIGHT // 2 - random.randrange(SCREEN_HEIGHT) color = random.choice(colors) points = [(px + x, py + y) for px, py in point_list] my_line_strip = arcade.create_line_strip(points, color, 5) self.shape_list.append(my_line_strip) self.shape_list.center_x = SCREEN_WIDTH // 2 self.shape_list.center_y = SCREEN_HEIGHT // 2 self.shape_list.angle = 0 arcade.set_background_color(arcade.color.BLACK)
def primitive_create(self): circle1 = arcade.create_ellipse_filled(self.x1, self.y1, self.mass1, self.mass1, self.color) circle2 = arcade.create_ellipse_filled(self.x2, self.y2, self.mass2, self.mass2, self.color) line1 = arcade.create_line(self.offsetX, self.offsetY, self.x1, self.y1, self.color, self.linewidth) line2 = arcade.create_line(self.x1, self.y1, self.x2, self.y2, self.color, self.linewidth) trail = arcade.create_line_strip(self.traillist, self.color, self.trailwidth) self.circle1 = arcade.ShapeElementList() self.circle1.append(circle1) self.circle2 = arcade.ShapeElementList() self.circle2.append(circle2) self.line1 = arcade.ShapeElementList() self.line1.append(line1) self.line2 = arcade.ShapeElementList() self.line2.append(line2) self.trail = arcade.ShapeElementList()
def __init__(self, width, height, title): """ Set up the application. """ super().__init__(width, height, title) self.shape_list = arcade.ShapeElementList() self.shape_list.center_x = SCREEN_WIDTH // 2 self.shape_list.center_y = SCREEN_HEIGHT // 2 self.shape_list.angle = 0 point_list = ((0, 50), (10, 10), (50, 0), (10, -10), (0, -50), (-10, -10), (-50, 0), (-10, 10), (0, 50)) colors = [ getattr(arcade.color, color) for color in dir(arcade.color) if not color.startswith("__") ] for i in range(5): x = SCREEN_WIDTH // 2 - random.randrange(SCREEN_WIDTH - 50) y = SCREEN_HEIGHT // 2 - random.randrange(SCREEN_HEIGHT - 50) color = random.choice(colors) points = [(px + x, py + y) for px, py in point_list] my_line_strip = arcade.create_line_strip(points, color, 5) self.shape_list.append(my_line_strip) point_list = ((-50, -50), (0, 40), (50, -50)) for i in range(5): x = SCREEN_WIDTH // 2 - random.randrange(SCREEN_WIDTH - 50) y = SCREEN_HEIGHT // 2 - random.randrange(SCREEN_HEIGHT - 50) points = [(px + x, py + y) for px, py in point_list] triangle_filled = arcade.create_triangles_filled_with_colors( points, random.sample(colors, 3)) self.shape_list.append(triangle_filled) point_list = ((-50, -70), (-50, 70), (50, 70), (50, -70)) for i in range(5): x = SCREEN_WIDTH // 2 - random.randrange(SCREEN_WIDTH - 50) y = SCREEN_HEIGHT // 2 - random.randrange(SCREEN_HEIGHT - 50) points = [(px + x, py + y) for px, py in point_list] rect_filled = arcade.create_rectangle_filled_with_colors( points, random.sample(colors, 4)) self.shape_list.append(rect_filled) point_list = ((100, 100), (50, 150), (100, 200), (200, 200), (250, 150), (200, 100)) poly = arcade.create_polygon(point_list, (255, 10, 10)) self.shape_list.append(poly) ellipse = arcade.create_ellipse(20, 30, 50, 20, (230, 230, 0)) self.shape_list.append(ellipse) arcade.set_background_color(arcade.color.BLACK) self.offscreen = self.ctx.framebuffer( color_attachments=self.ctx.texture((SCREEN_WIDTH, SCREEN_HEIGHT), wrap_x=gl.GL_CLAMP_TO_EDGE, wrap_y=gl.GL_CLAMP_TO_EDGE)) self.glow = postprocessing.BloomEffect((SCREEN_WIDTH, SCREEN_HEIGHT))
def __init__(self, width, height): """ Initializer """ super().__init__(width, height) arcade.set_background_color(arcade.color.WHITE) point_list = ([0, 100], [100, 100], [100, 300], [300, 300]) self.line_strip = arcade.create_line_strip(point_list, arcade.csscolor.BLACK, 10)
def __init__(self, world_map, size_px): self.world_map = world_map self.size_px = size_px width, height = size_px self.face_size_px = (width // 5, height // 3) face_width, face_height = self.face_size_px cells_on_edge = self.world_map.cells_on_edge self.cell_size_px = (face_width // cells_on_edge, face_height // cells_on_edge) self._initialize_default_cells_colors() self._color_mode = 'common' # ShapeElementList object for borders drawing self._borders = arcade.ShapeElementList() face_width, face_height = self.face_size_px x_list = [face_width / 2 * i for i in range(11)] y_list_1 = itertools.cycle([0 * face_height, 1 * face_height]) y_list_2 = itertools.cycle([2 * face_height, 3 * face_height]) point_list_1 = tuple(zip(x_list, y_list_1)) point_list_2 = tuple(zip(x_list, y_list_2)) # Borders drawing border_1 = arcade.create_line_strip(point_list_1, arcade.color.WHITE, 2) border_2 = arcade.create_line_strip(point_list_2, arcade.color.WHITE, 2) self._borders.append(border_1) self._borders.append(border_2) triangles = map(self._get_triangle_vertices, self.world_map.cells) triangles_vertices = itertools.chain.from_iterable(triangles) self._cells_vertices = list(triangles_vertices) # ShapeElementList object for cells drawing. See _update_cells_colors() self._cells_grid_container = None self._update_cells_colors()
def _create_shape(self, scale) -> Shape: """ Create a shape representing this lake. :return: a Arcade shape object. """ if self.hole is not None: points = self._create_points(scale) return create_line_strip(points, self.color, self.border_width * scale) return create_ellipse_filled(self.center_x, self.center_y, self.outer_radius * scale, self.outer_radius * scale, self.color)
def __init__(self, width, height): """ Set up the application. """ super().__init__(width, height) self.shape_list = arcade.ShapeElementList() self.shape_list.center_x = SCREEN_WIDTH // 2 self.shape_list.center_y = SCREEN_HEIGHT // 2 self.shape_list.angle = 0 point_list = ((0, 50), (10, 10), (50, 0), (10, -10), (0, -50), (-10, -10), (-50, 0), (-10, 10), (0, 50)) colors = [ getattr(arcade.color, color) for color in dir(arcade.color) if not color.startswith("__") ] for i in range(5): x = SCREEN_WIDTH // 2 - random.randrange(SCREEN_WIDTH - 50) y = SCREEN_HEIGHT // 2 - random.randrange(SCREEN_HEIGHT - 50) color = random.choice(colors) points = [(px + x, py + y) for px, py in point_list] my_line_strip = arcade.create_line_strip(points, color, 5) self.shape_list.append(my_line_strip) point_list = ((-50, -50), (0, 40), (50, -50)) for i in range(5): x = SCREEN_WIDTH // 2 - random.randrange(SCREEN_WIDTH - 50) y = SCREEN_HEIGHT // 2 - random.randrange(SCREEN_HEIGHT - 50) points = [(px + x, py + y) for px, py in point_list] triangle_filled = arcade.create_triangles_filled_with_colors( points, random.sample(colors, 3)) self.shape_list.append(triangle_filled) point_list = ((-50, -70), (-50, 70), (50, 70), (50, -70)) for i in range(5): x = SCREEN_WIDTH // 2 - random.randrange(SCREEN_WIDTH - 50) y = SCREEN_HEIGHT // 2 - random.randrange(SCREEN_HEIGHT - 50) points = [(px + x, py + y) for px, py in point_list] rect_filled = arcade.create_rectangle_filled_with_colors( points, random.sample(colors, 4)) self.shape_list.append(rect_filled) point_list = ((100, 100), (50, 150), (100, 200), (200, 200), (250, 150), (200, 100)) poly = arcade.create_polygon(point_list, (255, 10, 10), 5) self.shape_list.append(poly) ellipse = arcade.create_ellipse(20, 30, 50, 20, (230, 230, 0)) self.shape_list.append(ellipse) arcade.set_background_color(arcade.color.BLACK)
def _create_shape(self) -> Shape: """ Create a shape representing this lake. :return: a Arcade shape object. """ if self.large_sim_type: return arcade.create_line_strip(self.points, self.color, self.border_width) else: color_list = [self.color] + [self.color] * (32 + 1) return arcade.create_line_generic_with_colors( self.points, color_list, 6)
def create_full_path(self): """ Creates a looping path through all way points and back to the starting position """ self.path = [] self.pathidx = 0 for waypoint1, waypoint2 in zip( [self.position] + self.waypoints, self.waypoints + [self.position] ): for point in self.calculate_path( center_of_tile(*waypoint1), center_of_tile(*waypoint2) ): self.path.append(center_of_tile(*point)) self.pathshape = arcade.create_line_strip( self.path + self.path[:1], self.pathcolor, 2 )
def __init__(self, width, height): """ Set up the application. """ super().__init__(width, height) self.shape_list = arcade.ShapeElementList() point_list = ((0, 50), (10, 10), (50, 0), (10, -10), (0, -50), (-10, -10), (-50, 0), (-10, 10), (0, 50)) my_line_strip = arcade.create_line_strip(point_list, arcade.color.RED, 5) self.shape_list.append(my_line_strip) self.shape_list.center_x = 100 self.shape_list.center_y = 100 arcade.set_background_color(arcade.color.BLACK)
def render(self, et, dt): if self.shapes is None: self.shapes = arcade.ShapeElementList() l = None for e in self.waypoints.points: n = self.map.project(e[0]) if l is not None: self.shapes.append( arcade.create_line_strip((l, n), (0, 40, 150, 64), 3)) rx, ry = self.map.project_radius(e[0], self.waypoints.radius) self.shapes.append( arcade.create_ellipse_filled(*n, rx, ry, (0, 40, 150, 32), 0, 18)) self.shapes.append( arcade.create_ellipse_filled(*n, 5, 5, (0, 40, 150, 128), 0, 9)) l = n self.shapes.draw()
def calculate_and_draw_path(self, current): self.path_shape_list = arcade.ShapeElementList() path = [] points_list = [] if current: temp = current point = pymunk.Vec2d(temp.x * G_W + G_W / 2, temp.y * G_H + G_H / 2) points_list.append((point.x, point.y)) path.append(point) while temp.parent: temp = temp.parent point = pymunk.Vec2d(temp.x * G_W + G_W / 2, temp.y * G_H + G_H / 2) points_list.append((point.x, point.y)) path.append(point) if len(points_list) > 2: line_strip = arcade.create_line_strip( points_list, arcade.color.BAKER_MILLER_PINK, 5) self.path_shape_list.append(line_strip) self.final_path = path.copy()
def draw_line(self, length): line_dir = pymunk.Vec2d(1, self.slope).normalized() line_centre = self.point_on_line1 return arcade.create_line_strip((line_centre - line_dir * length / 2, line_centre + line_dir * length / 2), arcade.color.BAKER_MILLER_PINK)
def make_objects(): shape_list = arcade.ShapeElementList() center_x = 0 center_y = 0 width = 20 height = 20 shape = arcade.create_ellipse_filled(center_x, center_y, width, height, arcade.color.WHITE) shape_list.append(shape) center_x += 40 shape = arcade.create_ellipse_outline(center_x, center_y, width, height, arcade.color.RED, border_width=1) shape_list.append(shape) center_x += 40 shape = arcade.create_ellipse_outline(center_x, center_y, width, height, arcade.color.DARK_RED, border_width=1) shape_list.append(shape) shape = arcade.create_line(0, 0, 80, 0, arcade.color.BLUE, line_width=1) shape_list.append(shape) shape = arcade.create_line(0, 0, 80, 0, arcade.color.LIGHT_BLUE, line_width=1) shape_list.append(shape) center_x = 0 center_y = 50 width = 20 height = 20 outside_color = arcade.color.AERO_BLUE inside_color = arcade.color.AFRICAN_VIOLET tilt_angle = 45 shape = arcade.create_ellipse_filled_with_colors(center_x, center_y, width, height, outside_color, inside_color, tilt_angle) shape_list.append(shape) center_x = 0 center_y = -50 width = 20 height = 20 shape = arcade.create_rectangle_filled(center_x, center_y, width, height, arcade.color.WHITE) shape_list.append(shape) shape = arcade.create_rectangle_outline(center_x, center_y, width, height, arcade.color.BLACK, border_width=1) shape_list.append(shape) shape = arcade.create_rectangle_outline(center_x, center_y, width, height, arcade.color.AMERICAN_ROSE, border_width=1) shape_list.append(shape) color1 = (215, 214, 165) color2 = (219, 166, 123) points = (70, 70), (150, 70), (150, 150), (70, 150) colors = (color1, color1, color2, color2) shape = arcade.create_rectangle_filled_with_colors(points, colors) shape_list.append(shape) points = (0, 0), (150, 150), (0, 150), (0, 250) shape = arcade.create_line_strip(points, arcade.color.AFRICAN_VIOLET) shape_list.append(shape) points = (0, 0), (75, 90), (60, 150), (90, 250) shape = arcade.create_line_generic(points, arcade.color.ALIZARIN_CRIMSON, gl.GL_TRIANGLE_FAN) shape_list.append(shape) return shape_list
def on_draw(self): """ Render the screen. """ # This command has to happen before we start drawing arcade.start_render() self._frames += 1 # arcade.draw_text("Test", 50, 50) if self.box.field is not None and self.box.field.equation != self.box.field.nofield: self.draw_field() if self.left_mouse_down: arcade.draw_line(self.mouse_x, self.mouse_y, self.mouse_dx, self.mouse_dy, arcade.color.WHITE, 1) # draw avg impuls vector start = self.box.box_sizes / 2 avg_impuls = self.box.avg_momentum() end = start + avg_impuls arcade.draw_line(*start[:2], *end[:2], color=arcade.color.WHITE, line_width=1) self.arrow_list = None self.arrow_list = arcade.ShapeElementList() # if self.box.trail > 0: self.trail_list = None self.trail_list = arcade.ShapeElementList() while self.box.delete_particles: ball = self.box.delete_particles.pop() ball.object.kill() while self.box.merged_particles: ball = self.box.merged_particles.pop() ball.object.kill() # +D_SPRITE_RADIUS ball.object = arcade.SpriteCircle( int(ball.radius) + D_SPRITE_RADIUS, ball.color, True) self.ball_list.append(ball.object) for _, ball in enumerate(self.box.particles): #arcade.draw_circle_filled(ball.position[0], ball.position[1], ball.radius, ball.color) end = ball.position + ball.speed # arcade.draw_line(ball.position[0], ball.position[1], end[0], end[1], arcade.color.GRAY_ASPARAGUS, 2) arrow = arcade.create_line(ball.position[0], ball.position[1], end[0], end[1], arcade.color.GRAY_ASPARAGUS, 2) self.arrow_list.append(arrow) if ball.positions: positions = [(p[0], p[1]) for p in ball.positions] trail = arcade.create_line_strip(positions, arcade.color.GRAY, 1) self.trail_list.append(trail) output = "" if ball.charge < 0: output = "-" elif ball.charge > 0: output = "+" if ball.charge != 0 and len( output) > 0 and self.box.interaction != 0: arcade.draw_text(output, ball.position[0] + 5, ball.position[1] - 10, arcade.color.WHITE, 20, font_name="Calibri Bold") # output = str(i) # arcade.draw_text(output, ball.position[0]-0, ball.position[1]+0, arcade.color.WHITE, 8, font_name="Calibri Bold") ball.object.center_x = ball.position[0] ball.object.center_y = ball.position[1] if self.box.dimensions > DSIZE: ball.object.scale = ball.position[DSIZE] / self.box.box_sizes[ DSIZE] if self.box.dimensions > DALPHA: ball.object.alpha = 255 * (ball.position[DALPHA] / self.box.box_sizes[DALPHA]) % 255 # draw springs for _, spring in enumerate(self.box.springs): v = MAXCOLOR + 1 / ((spring.dlength() / 10000) - INVMAXCOLOR) color = self.getcolor(v, 0, 255) if self.box.dimensions > DALPHA: pos = (spring.p1.position[DALPHA] + spring.p2.position[DALPHA]) / 2 alpha = 255 * (pos / self.box.box_sizes[DALPHA]) % 255 color.append(alpha) arcade.draw_line(*spring.p1.position[:2], *spring.p2.position[:2], color=color, line_width=1) # dpos = self.box.displacement(spring.p1.position, spring.p2.position) # # start = spring.p1.position # # end = spring.p1.position + dpos # start = self.box.center # end = self.box.center + dpos # arcade.draw_line(*start[:2], *end[:2], color=(255,0,0), line_width=2) # dpos = self.box._displacement(spring.p1.position, spring.p2.position) # # start = spring.p1.position # # end = spring.p1.position + dpos # start = self.box.center # end = self.box.center + dpos # arcade.draw_line(*start[:2], *end[:2], color=(0,255,0), line_width=2) # draw rods for _, rod in enumerate(self.box.rods): arcade.draw_line(*rod.p1.position[:2], *rod.p2.position[:2], color=arcade.color.GRAY, line_width=2) self.arrow_list.draw() self.ball_list.draw() self.trail_list.draw() if self._draw_planes: self.plane_list.draw() self.hole_list.draw() #self.sprite_list.draw_hit_boxes((255,255,255), 2) if self.text: # Put the text on the screen. charge = sum(p.charge for p in self.box.particles) output = f"Ticks: {self.box.ticks}, Dimensions: {self.box.dimensions}, Balls: {len(self.box.particles)}, Charge: {charge}" arcade.draw_text(output, 10, 20, arcade.color.WHITE, 14) if self._frames < TEXT_REFRESH or self._frames % TEXT_REFRESH == 0: KE = self.box.energy["KE"] self._output["KE"] = f"Kinetic energy: {KE:.2f}" PE = self.box.energy["PE"] self._output["PE"] = f"Potential Energy: {PE:.2f}" SE = self.box.energy["SE"] self._output["SE"] = f"Spring Energy: {SE:.2f}" TE = KE + PE + SE self._output["TE"] = f"Total Energy: {TE:.2f}" try: arcade.draw_text(self._output["KE"], 10, 50, arcade.color.WHITE, 14) arcade.draw_text(self._output["PE"], 10, 80, arcade.color.WHITE, 14) arcade.draw_text(self._output["SE"], 10, 120, arcade.color.WHITE, 14) arcade.draw_text(self._output["TE"], 10, 150, arcade.color.WHITE, 14) # arcade.draw_text(self._output["R"], 10, 180, arcade.color.WHITE, 14) except KeyError: pass # output = "Avg Impuls: {}".format(self.box.avg_momentum()) # arcade.draw_text(output, 10, 80, arcade.color.WHITE, 14) # P = self.box.pressure() # output = "pressure: {:}".format(P) # arcade.draw_text(output, 10, 110, arcade.color.WHITE, 14) # PV = self.box.pressure() * self.box.volume() # output = "PV: {:.2f}".format(PV) # arcade.draw_text(output, 10, 150, arcade.color.WHITE, 14) # try: # output = "PV/nE: {}".format(PV/(E*len(self.box.particles))) # arcade.draw_text(output, 10, 180, arcade.color.WHITE, 14) # except: # raise # output = "Avg position: {}".format(self.box.avg_position()) # arcade.draw_text(output, 10, 110, arcade.color.WHITE, 14) #play sound if self.bounced and not self.quiet: arcade.play_sound(self.sound, 0.1)