1.绘制矩形:pygame.draw.rect()
rect(Surface, color, Rect, width=0) ->Rect
import pygame
import sys
from pygame.locals import *
#初始化Pygame
pygame.init()
#宏定义
WHITE = (255,255,255)
BLACK = (0,0,0)
size = width,height = 640,480 #实际上是元组
screen = pygame.display.set_mode(size)
pygame.display.set_caption("木尧-pygame-简单图形绘制")
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.fill(WHITE)
pygame.draw.rect(screen,BLACK,(50,50,150,50),0)
pygame.draw.rect(screen,BLACK,(250,50,150,50),1)
pygame.draw.rect(screen,BLACK,(450,50,150,50),10)
#把内存中的画面翻到屏幕上
pygame.display.flip()
#调整帧率 1s绘制10帧
clock.tick(10)
2.绘制多边形:pygame.draw.polygon()
polygon(Surface, color, pointlist, width=0) -> Rect
#多边形
pointlist = [(200,75),(300,25),(400,75),(450,25),(450,125),(400,75),(300,125)]
pygame.draw.polygon(screen,BLACK,pointlist,0)
3.绘制圆形:pygame.draw.circle()
circle(Surface, color, pos, radius, width=0) -> Rect
Aim:绘制一个同心圆,鼠标点击或者拖拽时他跟着动
import pygame
import sys
from pygame.locals import *
#初始化Pygame
pygame.init()
#宏定义
WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
size = width,height = 640,480 #实际上是元组
screen = pygame.display.set_mode(size)
pygame.display.set_caption("木尧-pygame-简单图形绘制")
position = size[0]//2,size[1]//2
moving = False
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == MOUSEBUTTONDOWN:
if event.button == 1:
moving = True
if event.type == MOUSEBUTTONUP:
if event.button == 1:
moving = False
if moving:
position = pygame.mouse.get_pos()
screen.fill(WHITE)
#圆形
pygame.draw.circle(screen,RED,position,25,25)
pygame.draw.circle(screen,GREEN,position,75,25)
pygame.draw.circle(screen,BLUE,position,125,25)
#把内存中的画面翻到屏幕上
pygame.display.flip()
#调整帧率 1s绘制120帧
clock.tick(120)
效果图:
4.绘制椭圆:pygame.draw.ellipse()
ellipse(Surface, color, Rect, width=0) -> Rect
#椭圆
pygame.draw.ellipse(screen,RED,(100,100,400,100),0)
pygame.draw.ellipse(screen,RED,(100,100,300,300),2)
5.绘制弧线:pygame.draw.arc()
arc(Surface, color, Rect, start_angle, stop_angle, width=1) -> Rect
pygame.draw.arc(screen,RED,(100,100,300,100),0,math.pi,5)
pygame.draw.arc(screen,RED,(220,50,200,200),math.pi,math.pi*2,5)
6.绘制直线
pygame.draw.
line
()
pygame.draw.
lines
()
pygame.draw.
aaline
()
pygame.draw.
aalines
()
pygame.draw.line(screen,GREEN,(100,100),(300,400),1)
pygame.draw.lines(screen, BLACK, False, [[0, 80], [50, 90], [200, 80], [220, 30]], 5)
pygame.draw.aaline(screen, GREEN, [0, 50],[50, 80], True)