def draw_checkerboard(self): for i in range(self.cell_amt): for j in range(self.cell_amt): if (i % 2 == 0) ^ (j % 2 == 0): cr = Rect((i * self.cell_size[0], j * self.cell_size[1]), self.cell_size) draw_rect(self.play_surf, self.cell_colour, cr)
def draw(self): surface = BasicFrame.draw(self) if not self.pressed: draw_rect(surface, self.dark, Rect((0, 0), self.size), self.thick) else: draw_rect(surface, self.light, Rect((0, 0), self.size), self.thick) return surface
def draw(self, window): window.blit(self.image, (self.x, self.y)) if self.activated: draw_rect(window, (0, 0, 0), (self.x, self.y, self.image.get_width(), self.image.get_height()), 2) else: draw_rect(window, (150, 150, 150), (self.x, self.y, self.image.get_width(), self.image.get_height()), 2) window.blit(self.small_block_image, ((self.x + self.image.get_width() // 2) - self.small_block_image.get_width() // 2, (self.y + self.image.get_height() // 2) - self.small_block_image.get_height() // 2))
def draw(self): surface = BasicFrame.draw(self) if self.pressed: draw_rect(surface, self.dark, Rect((0, 0), self.size), self.thick+2) elif self.hovered: draw_rect(surface, self.dark, Rect((0, 0), self.size), self.thick) return surface
def loop_hook(self): if self.mouse_pressed: if self.point_buffer % 1 == 0: self.points[self.point_buffer] = Vector2(mouse_pos()) self.quad.insert_point(self.point_buffer, self.points[self.point_buffer]) self.point_buffer += 1 if self.rect_selection is not None: self.select_points() rect_size = Vector2(mouse_pos()) - self.rect_selection draw_rect( self.canvas, (20, 250, 40), Rect(self.rect_selection.x, self.rect_selection.y, rect_size.x, rect_size.y), 1) # self.canvas.lock() self.quad.draw(self.canvas) for point in self.points: if point in self.selected_points: color = (220, 255, 100) else: color = (120, 120, 120) circle(self.canvas, color, self.points[point], 3) # self.canvas.lock() print('pts: %i | sel. pts: %i | FPS: %.2f' % (len( self.points), len(self.selected_points), self.clock.get_fps()))
def stretched_circle(surface: Surface, rect: Rect, colour: Colour): rounded_rect(surface, rect, colour, min(rect.width, rect.height) * .5) r = rect.copy() r.width -= r.height r.x += r.height / 2 r.height += 2 r.y -= 1 draw_rect(surface, colour, r)
def draw(self, canvas: Surface): draw_rect( canvas, (255, 255, 255), Rect(self.top_left.x, self.top_left.y, self.width, self.height), 1) if self.subdivided: self.top_left_quad.draw(canvas) self.bottom_left_quad.draw(canvas) self.top_right_quad.draw(canvas) self.bottom_right_quad.draw(canvas)
def image(self): width = self.parent.rect.width height = Constants.HP_BAR_HEIGHT img = Surface((width, height)) img.fill(Constants.INERT) damage = int(self.hit_points / self.max * width) draw_rect(img, Constants.DAMAGE, Rect(0, 0, damage, height)) health = int(self.parent.hit_points / self.max * width) draw_rect(img, Constants.HP, Rect(0, 0, health, height)) return img
def loop(self): DEBUG = config.getboolean('display', 'debug') draw_interval = 1 / float(self.target_fps) tick_fps = self.target_fps draw_timer = 0 tick = self.clock.tick main_surface = self.main_surface fps_display_acc = 0 get_fps = self.clock.get_fps poll_event = event.poll while len(self.scene_stack) > 0: events = list() e = poll_event() while e: events.append(e) if e.type == QUIT: while len(self.scene_stack) > 0: self.pop_scene() sys.exit() e = poll_event() delta = tick(tick_fps) fps = get_fps() fps_display_acc += delta if fps_display_acc >= 10000: set_caption("FPS ::: %.4f" % fps) fps_display_acc = 0 self.current_scene.update_events() self.current_scene.update(delta, events) draw_timer += delta if draw_timer >= draw_interval: if DEBUG: main_surface.fill((0, 0, 0)) draw_timer -= draw_interval self.current_scene.clear(main_surface) dirty = self.current_scene.draw(main_surface) if DEBUG: for rect in dirty: draw_rect(main_surface, (0, 255, 0), rect, 1) flip() else: update(dirty)
def _redraw_screen(self): """Redraws the screen.""" # fill background with window color self._screen.fill(WINDOW_COLOR) # draw label self._screen.blit(self._label, SHOTS_LABEL_POSITION) # draw paddle and ball draw_rect(self._screen, self._paddle.color, self._paddle) draw_circle(self._screen, self._ball.color, self._ball.center, self._ball.radius) # update whole screen flip_screen()
def _draw_game_over_screen(self): """Draws the game over screen.""" # fill background with window color self._screen.fill(WINDOW_COLOR) # draw label self._screen.blit(self._label, SHOTS_LABEL_POSITION) # draw game over label self._screen.blit(GAME_OVER_LABEL, GAME_OVER_LABEL_POSITION) # draw paddles draw_rect(self._screen, self._paddle.color, self._paddle) # update whole screen flip_screen()
def draw(self,surface,color): """ Draw a series of rectangles on a Surface. """ def get_green(i): val = 30 # the color should never go below 10 green val = max(val, 255-255*(i)) # the color should start bright green, and decrease # in intensity with each segment return val for i, seg in enumerate(self._render()): # get an index and a Rectangle representing a Segment fraction = i/len(self.segments) color = (0, get_green(fraction), 0) # get the color we want to use on this Segment draw_rect(surface, color, seg)
def display(self): pygame.event.pump() screen = self.screen borders = [pygame.Rect(b[0], b[1], b[2], b[3]) for b in self.m.borders] pellets = [pygame.Rect(p[0], p[1], p[2], p[3]) for p in self.m.pellets] b = self.m.mybox myrect = pygame.Rect(b[0], b[1], b[2], b[3]) screen.fill((0, 0, 64)) # dark blue for player in self.m.players.items(): name = player[0] if name != self.m.myname: draw_rect(screen, (255, 0, 0), player[1]) # red else: draw_rect(screen, (0, 191, 255), player[1]) # deep sky blue [pygame.draw.rect(screen, (255, 192, 203), p) for p in pellets] # pink [pygame.draw.rect(screen, (0, 191, 255), b) for b in borders] # red pygame.display.update()
def render(self, target): target.fill(self.bg_colour) lerped_fill_colour = self.fill_colour_min.lerp(self.fill_colour_max, self.lerp_n) lerped_circ_colour = self.circ_colour_min.lerp(self.circ_colour_max, self.lerp_n) lerped_rect_colour = self.circ_colour_min.lerp(self.circ_colour_max, 1 - self.lerp_n) if self.circs_are_close(): target.fill((255, 0, 0)) else: target.fill(lerped_fill_colour) actual_circ_colour = ((255, 0, 0) if self.circs_are_close() else lerped_circ_colour) actual_rect_colour = ((255, 0, 0) if self.circs_are_close() else lerped_rect_colour) target.blit(self.play_surf, self.play_rect) self.fruit.render(target) circ_pos_tuple = tuple(self.get_lerped_circ_pos()) neg_circ_pos = (circ_pos_tuple[0], self.circ_pos_max[1] - circ_pos_tuple[1]) draw_circle(target, actual_circ_colour, circ_pos_tuple, self.circ_radius) draw_rect(target, actual_rect_colour, Rect((circ_pos_tuple[0] - self.circ_radius // 2, circ_pos_tuple[1] - self.circ_radius // 2), (self.circ_radius, self.circ_radius))) draw_circle(target, actual_circ_colour, neg_circ_pos, self.circ_radius) draw_rect(target, actual_rect_colour, Rect((neg_circ_pos[0] - self.circ_radius // 2, neg_circ_pos[1] - self.circ_radius // 2), (self.circ_radius, self.circ_radius))) target.blit(self.text, (90, 90))
def draw(self, window): self.time_2 = int(time()) draw_rect(window, self.color, (self.x, self.y, self.width, self.height)) y = self.y for line in self.lines: line_render = self.font.render(line, True, (200, 200, 200)) window.blit(line_render, ( self.x, y + (self.letter_example.get_height() - line_render.get_height()))) y += self.letter_example.get_height() self.draw_line = self.time_2 - self.time_1 in range(1) if self.draw_line: draw_line(window, (255, 255, 255), (self.x + line_render.get_width(), y - self.letter_example.get_height() + 3), (self.x + line_render.get_width(), (y - self.letter_example.get_height() + 3) + 35), 2) else: if not self.time_2 - self.time_1 in range(1, 2): self.time_1 = int(time())
def _redraw_screen(self): """Redraws the screen.""" # fill background with window color self._screen.fill(WINDOW_COLOR) # draw labels self._screen.blit(self._label1, SCORE1_LABEL_POSITION) self._screen.blit(self._label2, SCORE2_LABEL_POSITION) # draw net draw_dashed_line(self._screen, NET_COLOR, (NET_X, 0), (NET_X, WINDOW_HEIGHT)) # draw paddles and ball draw_rect(self._screen, self._paddle1.color, self._paddle1) draw_rect(self._screen, self._paddle2.color, self._paddle2) draw_circle(self._screen, self._ball.color, self._ball.center, self._ball.radius) # update whole screen flip_screen()
def _draw_game_over_screen(self): """Draws the game over screen.""" # fill background with window color self._screen.fill(WINDOW_COLOR) # draw labels self._screen.blit(self._label1, SCORE1_LABEL_POSITION) self._screen.blit(self._label2, SCORE2_LABEL_POSITION) # draw game over label winner = '1' if self._score1 > self._score2 else '2' winner_label = FONT.render('PLAYER {} WON'.format(winner), True, FONT_COLOR) self._screen.blit(winner_label, WINNER_LABEL_POSITION) # draw paddles draw_rect(self._screen, self._paddle1.color, self._paddle1) draw_rect(self._screen, self._paddle2.color, self._paddle2) # update whole screen flip_screen()
def draw_piece(surf, color, left, top, width, height, size): padding_factor = 0.025 shadow_factor = 0.085 margin_factor = 0.05 base_color = color margin_color = darken_color(color, 0.8) bottom_color = darken_color(color, 0.4) # Applying padding padding = int(size * padding_factor) left, top = left + padding, top + padding width, height = width - 2 * padding, height - 2 * padding size = size - 2 * padding # Applying shadow effect shadow = int(size * shadow_factor) top_rect = (left, top, width - shadow, height - shadow) bottom_rect = (left + shadow, top + shadow, width - shadow, height - shadow) draw_rect(surf, bottom_color, bottom_rect) draw_rect(surf, base_color, top_rect) # Draw margins draw_rect(surf, margin_color, top_rect, int(size * margin_factor))
def display(self): pygame.event.pump() screen = self.screen borders = [pygame.Rect(b[0], b[1], b[2], b[3]) for b in self.m.borders] pellets = [pygame.Rect(p[0], p[1], p[2], p[3]) for p in self.m.pellets] if self.m.players: b = self.m.players[self.m.myname] myrect = pygame.Rect(b[0], b[1], b[2], b[3]) screen.fill((0, 0, 64)) # dark blue if self.m.players: pygame.draw.rect(screen, (0, 191, 255), myrect) # Deep Sky Blue [pygame.draw.rect(screen, (255, 192, 203), p) for p in pellets] # pink [pygame.draw.rect(screen, (0, 191, 255), b) for b in borders] # red if self.m.players: for name, p in self.m.players.items(): if name != self.m.myname: draw_rect(screen, (255, 0, 0), p) # red if self.m.myname: draw_rect(screen, (0, 191, 255), self.m.players[self.m.myname]) # deep sky blue pygame.display.update()
def rounded_rect(surface: Surface, rect: Rect, colour: Colour, corner_radius: int): ''' Draw a rectangle with rounded corners. Would prefer this: pygame.draw.rect(surface, colour, rect, border_radius=corner_radius) but this option is not yet supported in my version of pygame so do it ourselves. We use anti-aliased circles to make the corners smoother ''' if rect.width < 2 * corner_radius or rect.height < 2 * corner_radius: raise ValueError( f"Both height (rect.height) and width (rect.width) must be > 2 * corner radius ({corner_radius})" ) corner_radius = int(corner_radius) # need to use anti aliasing circle drawing routines to smooth the corners circle(surface, rect.left + corner_radius, rect.top + corner_radius, corner_radius, colour) circle(surface, rect.right - corner_radius - 1, rect.top + corner_radius, corner_radius, colour) circle(surface, rect.left + corner_radius, rect.bottom - corner_radius - 1, corner_radius, colour) circle(surface, rect.right - corner_radius - 1, rect.bottom - corner_radius - 1, corner_radius, colour) rect_tmp = Rect(rect) rect_tmp.width -= 2 * corner_radius rect_tmp.center = rect.center draw_rect(surface, colour, rect_tmp) rect_tmp.width = rect.width rect_tmp.height -= 2 * corner_radius rect_tmp.center = rect.center draw_rect(surface, colour, rect_tmp)
def display(self): self.screen.fill((0, 0, 64)) # dark blue [draw_rect(self.screen, (0, 191, 255), b) for b in model.borders] # deep sky blue [draw_rect(self.screen, (255, 192, 203), p) for p in model.pellets] # shrimp for name, p in model.players.items(): if name != model.myname: draw_rect(self.screen, (255, 0, 0), p) # red if model.myname: draw_rect(self.screen, (0, 191, 255), model.players[model.myname]) # deep sky blue update_pygame_display() self.clock.tick(50) # frames per second, independent of server frame rate
def draw(self): draw_rect(self.filed.surface, self.obj.color if self.obj else CELLS_COLOR, (self.x, self.y, self.width, self.height))
def draw(self, surface, color): """ Draw this obstacle on a Surface. """ draw_rect(surface, color, self.render())
client.do_send({'msg_type': 'die'}) else: # eat if bigger players[name] = None # None until server sends his new box client.do_send({'msg_type': 'eat_player', 'target': name}) # check for collisions with pellets if mybox: # may have been Noned by colliding with another player pe_idx = mybox.collidelist(pellets) if pe_idx != -1: # ate a pellet: grow, and replace a pellet client.do_send({'msg_type': 'eat_pellet', 'pellet_index': pe_idx}) # draw everything screen.fill((0, 0, 64)) # dark blue [draw_rect(screen, (0, 191, 255), b) for b in borders] # deep sky blue [draw_rect(screen, (255, 192, 203), p) for p in pellets] # shrimp for name, hisbox in players.items(): # draw other players if name != myname and hisbox: if mybox and hisbox.width < mybox.width: color = 0, 255, 0 # smaller than me: green else: color = 255, 0, 0 # bigger or same size: red draw_rect(screen, color, hisbox) # anti-aliased, black text = font.render(str(hisbox.width), 1, (0, 0, 0)) screen.blit(text, hisbox) if mybox: # draw me if I'm alive draw_rect(screen, (0, 191, 255), mybox) # Deep Sky Blue text = font.render(str(mybox.width), 1, (0, 0, 0)) # anti-alias, black screen.blit(text, mybox)
def show(self, window): """Paints a black box at x, y to sx, sy""" # probably needs to be overloaded, just paints a black box draw_rect(window, colors.black, (self.x, self.y, self.sx, self.sy))
def draw_racket(self): self.racket = draw_rect(self.screen, self.color, self.rect)
def __init__(self, screen, color, pos_x, pos_y): self.screen = screen self.color = color self.rect = Rect(pos_x, pos_y, self.size_x, self.size_y) self.move_step = self.move_step self.racket = draw_rect(self.screen, self.color, self.rect)
client.do_send({'msg_type': 'die'}) else: # eat if bigger players[name] = None # None until server sends their new box client.do_send({'msg_type': 'eat_player', 'target': name}) # check for collisions with pellets if mybox: # may have been Noned by colliding with another player pe_idx = mybox.collidelist(pellets) if pe_idx != -1: # ate a pellet: grow, and replace a pellet client.do_send({'msg_type': 'eat_pellet', 'pellet_index': pe_idx}) # draw everything screen.fill((0, 0, 64)) # dark blue [draw_rect(screen, (0, 191, 255), b) for b in borders] # deep sky blue [draw_rect(screen, (255, 192, 203), p) for p in pellets] # shrimp for name, theirbox in players.items(): # draw other players if name != myname and theirbox: if mybox and theirbox.width < mybox.width: color = 0, 255, 0 # smaller than me: green else: color = 255, 0, 0 # bigger or same size: red draw_rect(screen, color, theirbox) # anti-aliased, black text = font.render(str(theirbox.width), 1, (0, 0, 0)) screen.blit(text, theirbox) if mybox: # draw me if I'm alive draw_rect(screen, (0, 191, 255), mybox) # Deep Sky Blue text = font.render(str(mybox.width), 1, (0, 0, 0)) # anti-alias, black screen.blit(text, mybox)
def draw(self, canvas): draw_rect(canvas, self.color, (self.x, self.y, self.brush_size, self.brush_size))
if key == K_ESCAPE: exit() elif key in valid_inputs: if key == K_UP: dx, dy = 0, -1 elif key == K_DOWN: dx, dy = 0, 1 elif key == K_LEFT: dx, dy = -1, 0 elif key == K_RIGHT: dx, dy = 1, 0 msg = {'input': valid_inputs[key]} client.do_send(msg) # draw everything screen.fill((0, 0, 64)) # dark blue [draw_rect(screen, (0, 191, 255), b) for b in borders] # deep sky blue [draw_rect(screen, (255, 192, 203), p) for p in pellets] # shrimp for name, p in players.items(): if name != myname: draw_rect(screen, (255, 0, 0), p) # red if myname and myname in players.keys(): if counter % 2 == 0: players[myname].move_ip(dx, dy) # update position draw_rect(screen, (0, 191, 255), players[myname]) # deep sky blue update_pygame_display() counter += 1 clock.tick(50) # frames per second, independent of server frame rate
def draw(self, toolbar): draw_rect(toolbar, self.color, (self.x, self.y, self.width, self.height)) draw_circle(toolbar, (self.color[0] - 40, self.color[1] - 40, self.color[2] - 40), (self.circle_x, self.circle_y), self.circle_radius)
while 1: poll() # push and pull network messages # send valid inputs to the server for event in get_pygame_events(): if event.type == QUIT: exit() if event.type == KEYDOWN: key = event.key if key == K_ESCAPE: exit() elif key in valid_inputs: msg = {'input': valid_inputs[key]} client.do_send(msg) # draw everything screen.fill((0, 0, 64)) # dark blue [draw_rect(screen, (0, 191, 255), b) for b in borders] # deep sky blue [draw_rect(screen, (255, 192, 203), p) for p in pellets] # shrimp for name, p in players.items(): if name != myname: draw_rect(screen, (255, 0, 0), p) # red if myname: draw_rect(screen, (0, 191, 255), players[myname]) # deep sky blue update_pygame_display() clock.tick(50) # frames per second, independent of server frame rate