Article directory
five graphics
In addition to straight lines, pygame provides a variety of graphics drawing functions. In addition to the necessary drawing window, color and line width placed at the end, their parameters are as shown in the following table
function | graphics | Parameter Type |
---|---|---|
rect | rectangle | Rect |
ellipse | oval | Rect |
arc | elliptical arc | Rect, st, ed |
circle | round | center, radius |
polygon | polygon | points |
It can be seen from the parameter categories that the drawing logic of rectangles and ellipses is consistent. It can be understood that the ellipse created with rectangular constraints is the inscribed ellipse of the rectangle; and the elliptical arc is nothing more than an additional starting and ending angle.
This is not the case with circles and polygons, where the former is based on a center and radius, and the latter is based on a set of points. The following focuses on showing the basic usage of these drawing functions.
import pygame
from pygame.draw import *
pygame.init()
screen = pygame.display.set_mode((640, 360))
pts = [(400, 0), (450, 100), (500, 0), (600, 100),
(620, 300), (450, 350)]
while True:
if pygame.QUIT in [e.type for e in pygame.event.get()]:
pygame.quit()
break
screen.fill("purple")
rect(screen, "green", pygame.Rect(10, 30, 100, 300))
ellipse(screen, "red", pygame.Rect(120, 30, 100, 300))
arc(screen, "black", pygame.Rect(230, 30, 100, 300), 0, 4.5)
circle(screen, "blue", pygame.Vector2(360, 180), 40)
polygon(screen, "pink", pts)
pygame.display.flip()
The effect is as follows
rectangle
Rectangle is a very basic graphic in pygame, and there are many usage scenarios. Its advantage is that it can very conveniently determine whether two objects collide. Its complete parameter list is as follows
rect(surface, color, rect, width=0, border_radius=0, border_top_left_radius=-1, border_top_right_radius=-1, border_bottom_left_radius=-1, border_bottom_right_radius=-1)
Among them, the five additional parameters ending with radius can be used to set the rounded corners of the rectangle. Examples are as follows
pygame.init()
screen = pygame.display.set_mode((640, 360))
while True:
if pygame.QUIT in [e.type for e in pygame.event.get()]:
pygame.quit()
break
screen.fill("purple")
rect(screen, "green", pygame.Rect(50, 30, 200, 300), border_radius=10)
rect(screen, "red", pygame.Rect(280, 30, 300, 300),
border_top_left_radius=10, border_top_right_radius=40, border_bottom_left_radius=80, border_bottom_right_radius=160)
pygame.display.flip()
The effect is as follows
round
Circles are similar to rectangles. In addition to basic drawing parameters, 4 customizable parameters are also provided.
circle(surface, color, center, radius, width=0, draw_top_right=None, draw_top_left=None, draw_bottom_left=None, draw_bottom_right=None)
These four parameters are all of Boolean type and are used to control the four corners of the circle. If none are specified, there will be no effect. If one of the parameters is specified as True, only a quarter arc in this direction will be drawn. The example is as follows.
pygame.init()
screen = pygame.display.set_mode((640, 360))
while True:
if pygame.QUIT in [e.type for e in pygame.event.get()]:
pygame.quit()
break
screen.fill("purple")
circle(screen, (1, 136, 225), pygame.Vector2(320, 180), 150,
draw_top_left=True, draw_bottom_right=True)
circle(screen, "white", pygame.Vector2(320, 180), 150,
draw_top_right=True, draw_bottom_left=True)
pygame.display.flip()