绘制简单的图形:
语法格式:Pygame.draw.XXX(*params)
矩形: rect(Surface, color, Rect, width=0) (Rect是范围,边框=0(填充),1边框大小(向外扩展边框))
多边形: polygan(Surface, color, pointlist, width=0)
圆形: circle(Surface, color, pos, radius, width=0) (pos圆心位置, radius 半径)
椭圆: ellipse(Surface, color, Rect, width=0)(Rect表示一个限定矩形)
弧线: arc(Surface, color, Rect, start_angle, stop_angle, width=1) (不能填充)
线段: line(Surface, color, start_pos, end_pos, widht=1) (只能表示宽度)
Lines(Surface, color, closed, pointlist, width=1) (closed表示是否闭合,不能填充)
抗锯齿: aaline(Surface, color, startpos, endpos, blend=1)
aalines(Surface, color, closed, pointlist, blend=1) (blend表示是否开启抗锯齿,pygame2.2后默认都是true)
EG: 绘制一条小鱼儿:
import pygame
import sys
from pygame.locals import *
import math
size = width, height = 800, 600
bg = (255, 255, 255)
clock = pygame.time.Clock()
screen = pygame.display.set_mode(size)
pygame.display.set_caption('Simple Figures')
pos = (50,251)
moving = False
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
if event.type == MOUSEBUTTONDOWN:
if event.button == 1:
moving = True
if event.type == MOUSEBUTTONUP:
if event.button == 1:
moving = False
screen.fill(bg)
# drawing a fish
pygame.draw.rect(screen, (0, 255, 0), (250, 150, 300, 200), 0)
pygame.draw.aalines(screen, (255,0,0),1,((80,250),(250,150),(250,350)), 1)
pygame.draw.polygon(screen, (0,0,255),((550,150),(650,350),(650,150),(550,350)), 0)
pygame.draw.ellipse(screen, (255,0,0), (110, 210, 75, 75), 0)
pygame.draw.arc(screen, (125,125,125), (250, 150, 30, 200), -math.pi/2, -3 * math.pi / 2, 2 )
# a moving ball that you can move with left button of mouse
if moving:
pos = pygame.mouse.get_pos()
pygame.draw.circle(screen, (200, 200, 0), pos, 20.0, 0)
pygame.display.flip()
clock.tick(120)