def __init__(self, position=None, color=None): sf.Drawable.__init__(self) self.shape = sf.CircleShape(settings.ENTITY_SIZE/2) r = random.randrange(56, 256) g = random.randrange(56, 256) b = random.randrange(56, 256) self.shape.fill_color = sf.Color(r, g, b, 50) self.shape.outline_color = sf.Color(r, g, b, 200) self.shape.outline_thickness = 1 v_x = random.randrange(-max_speed, max_speed + 1) v_y = random.randrange(-max_speed, max_speed + 1) self.position = position self.velocity = sf.Vector2(v_x, v_y) self.line = sf.VertexArray(sf.PrimitiveType.LINES, 2) self.line[0].color = sf.Color(r, g, b, 200) self.line[1].color = sf.Color(r, g, b, 50) self.centre_of_mass = sf.Vector2() self.average_velocity = sf.Vector2() self.num_nearby_entities = 0
def __init__(self, width, height, cell_size): sf.Drawable.__init__(self) self.width = width self.height = height self.cell_size = cell_size self.cols = int(math.ceil((self.width / cell_size))) self.rows = int(math.ceil((self.height / cell_size))) self.cells = [[Cell() for _ in range(self.rows)] for _ in range(self.cols)] # Grid lines for drawing self.lines = sf.VertexArray(sf.PrimitiveType.LINES, 2*self.rows+2*self.cols) for x in range(self.cols): self.lines[2*x].position = (x*self.cell_size, 0) self.lines[2*x+1].position = (x*self.cell_size, self.height) self.lines[2*x].color = sf.Color(220, 220, 100, 50) self.lines[2*x+1].color = sf.Color(220, 220, 100, 50) for y in range(self.rows): self.lines[2*(y+self.cols)].position = (0, y*self.cell_size) self.lines[2*(y+self.cols)+1].position = (self.width, y*self.cell_size) self.lines[2*(y+self.cols)].color = sf.Color(220, 220, 100, 50) self.lines[2*(y+self.cols)+1].color = sf.Color(220, 220, 100, 50)
def draw_cartesian_point(x, y, color=sf.Color.BLUE): # Convert to screen coordinates screen_x = (radius + x) * window_scale screen_y = (radius - y) * window_scale # Graphics library does not allow drawing of single pixel. # Instead, a vertex array must be created # that contains only a single pixel. point = sf.VertexArray(sf.PrimitiveType.POINTS, 1) point[0].position = (screen_x, screen_y) point[0].color = color # Then the vertex array is drawn to the screen window.draw(point) window.display()
def __init__(self, frametime=sf.seconds(0.2), paused=False, looped=True): super(AnimatedSprite, self).__init__() self.animation = None self.frametime = frametime self.paused = paused self.looped = looped self.current_time = None self.current_frame = 0 self.texture = None self.vertices = sf.VertexArray(sf.PrimitiveType.QUADS, 4)
def __init__(self, WindowSize, rate=15, debug=False): self.debug = debug self.window = Window(WindowSize, WindowSize) self.line = sf.VertexArray(sf.PrimitiveType.LINES_STRIP, 2) self.circle = sf.CircleShape() self.circle.fill_color = sf.Color.TRANSPARENT self.circle.radius = 2 self.window.view.reset((0, 0, WindowSize, WindowSize)) self.clear = self.window.clear self.draw = self.window.draw self.display = self.window.display self.window.framerate_limit = rate # draw speed limiter self.scale = self.window.view.size.x / self.window.size.x self.pointClicked = sf.Vector2(0, 0) self.hideStuff = True self.panFrom = None
def __init__(self, w, h, cs): self.GW = w self.GH = h self.CS = cs self.prev_idx = 1 self.curr_idx = 0 self.pad = 1 if cs > 2 else 0 # if cells are 2x2 px or smaller, disable padding self.ALIVE_COLOR = sf.Color(255, 128, 0) self.DEAD_COLOR = sf.Color(32, 32, 32) self.quads = sf.VertexArray( sf.PrimitiveType.QUADS ) self.cells = [] self.init_cells() self.randomize_cells()
def __init__(self, pos, width, height, color, input): super().__init__(pos, input) self.width = width self.height = height self.center = sf.Vector2(width / 2, height / 2) self.vertices = sf.VertexArray(sf.PrimitiveType.QUADS, 4) self.vertices[0].position = sf.Vector2(pos.x, pos.y) self.vertices[1].position = sf.Vector2(pos.x + self.width, pos.y) self.vertices[2].position = sf.Vector2(pos.x + self.width, pos.y + self.height) self.vertices[3].position = sf.Vector2(pos.x, pos.y + self.height) for i in range(0, 4): self.vertices[i].color = color self.local_bounds = sf.Rectangle(pos, sf.Vector2(width, height)) self.mouse_state = 'up'
def load(self, size): self.v_array = sf.VertexArray( sf.PrimitiveType.POINTS, size ) p_list = [] for i in xrange(size): x = randint(-100, 100) y = randint(-100, 100) lifetime = randint(1, LIFETIME) p_info = ParticleInfo(sf.Vector2(x, y), lifetime) p_list.append(p_info) self.particles = dict( zip(self.v_array, p_list) )