pygame.draw.rect(Surface,color,Rect,width=0)
Surface:指定矩形将绘制在哪个Surface对象上
color:指定矩形的颜色
Rect:要绘制的矩形,位置和尺寸
width:指定矩形边框的大小,使用width值时,边线将在rect的原始边界之外生长
import pygame
import sys
from pygame.locals import *
pygame.init()
size = width,height = 700,400
WHITE = (255,255,255)
BLACK = (0,0,0)
clock = pygame.time.Clock()
screen = pygame.display.set_mode(size)
pygame.display.set_caption("初次见面,请大家多多关照")
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()
clock.tick(10)
pygame.draw.polygon(surface,color,points,width = 0)
points(元组(坐标)或列表(坐标)构成多边形顶点的3个或更多(x,y)坐标的序列,
序列中的每个坐标必须是元组/列表/
例如[(x1, y1), (x2, y2), (x3, y3)]
import pygame
import sys
from pygame.locals import *
pygame.init()
size = width,height = 700,400
WHITE = (255,255,255)
BLACK = (0,0,0)
GREEN = (0,255,0)
points = [(200,75),(300,25),(400,75),(450,25),(450,125),(400,75),(300,125)]
clock = pygame.time.Clock()
screen = pygame.display.set_mode(size)
pygame.display.set_caption("初次见面,请大家多多关照")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.fill(WHITE)
pygame.draw.polygon(screen,GREEN,points,0)
# 更新界面,达到显示的效果
pygame.display.flip()
clock.tick(10)
pygame.draw.circle(Surface,color,pos,radius,width=0)
pos:圆的中心点
radius:圆的半径
import pygame
import sys
from pygame.locals import *
pygame.init()
size = width,height = 600,400
WHITE = (255,255,255)
BLACK = (0,0,0)
GREEN = (0,255,0)
RED = (255,0,0)
BLUE = (0,0,255)
clock = pygame.time.Clock()
screen = pygame.display.set_mode(size)
pygame.display.set_caption("初次见面,请大家多多关照")
moving = False
position = size[0]//2,size[1]//2
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,30,1)
pygame.draw.circle(screen,GREEN,position,60,1)
pygame.draw.circle(screen,BLUE,position,90,1)
# 更新界面,达到显示的效果
pygame.display.flip()
clock.tick(10)
pygame.draw.ellipse(Surface,color,Rect,width=0)
Rect:用于指示椭圆的位置和尺寸的矩形,椭圆将在矩形内居中并由其限定
import pygame
import sys
from pygame.locals import *
pygame.init()
size = width,height = 600,400
WHITE = (255,255,255)
BLACK = (0,0,0)
clock = pygame.time.Clock()
screen = pygame.display.set_mode(size)
pygame.display.set_caption("初次见面,请大家多多关照")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.fill(WHITE)
pygame.draw.ellipse(screen,BLACK,(100,100,440,100),1)
pygame.draw.ellipse(screen,BLACK,(220,50,200,200),1)
# 更新界面,达到显示的效果
pygame.display.flip()
clock.tick(10)
pygame.draw.arc(Surface,color,Rect,start_angle,stop_angle,width=1)
Rect:矩形表示弧将基于的椭圆的位置和尺寸,椭圆将在矩形内居中
start_angle(float):以弧度为单位的弧的起始角度
stop_angle(float):用弧度停止弧的角度
import pygame
import sys
import math # 这里会用到 π,所以需要引入 math 包
from pygame.locals import *
pygame.init()
size = width,height = 600,400
WHITE = (255,255,255)
BLACK = (0,0,0)
clock = pygame.time.Clock()
screen = pygame.display.set_mode(size)
pygame.display.set_caption("初次见面,请大家多多关照")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.fill(WHITE)
pygame.draw.arc(screen,BLACK,(100,100,440,100),0,math.pi,1)
pygame.draw.arc(screen,BLACK,(220,50,200,200),math.pi,math.pi * 2,1)
# 更新界面,达到显示的效果
pygame.display.flip()
clock.tick(10)
pygame.draw.line(Surface,color,start_pos,end_pos,width=1)
绘制一条线段
start_pos:行的起始位置,(x,y)
end_pos:行的结束位置,(x,y)
width(int):(可选)用于线条粗细
if width> = 1,用于线条粗细(默认为1)如果宽度<1,则不会绘制任何内容
pygame.draw.lines(Surface,color,closed,pointlist,width=1)
绘制多条线段
closed(bool):若True在points序列中的第一个和最后一个点之间绘制一个额外的线段,也就是会闭合
points:2个或更多(x,y)坐标的序列,依次绘制连接各点
开启抗锯齿之后,画面会有质的飞越
pygame.draw.aaline(Surface,color,startpos,endpos,blend=1)
绘制抗锯齿直线,比原型光滑
blend:是否开启抗锯齿,不开启,就相当于原型
pygame.draw.aalines(Surface,color,closed,pointlist,blend=1)
绘制多条抗锯齿直线
import pygame
import sys
from pygame.locals import *
pygame.init()
size = width,height = 640,640
WHITE = (255,255,255)
BLACK = (0,0,0)
GREEN = (0,255,0)
points1 = [(200,75),(300,25),(400,75),(450,25),(450,125),(400,75),(300,125)]
points2 = [(200,350),(300,300),(400,350),(450,300),(450,400),(400,350),(300,400)]
clock = pygame.time.Clock()
screen = pygame.display.set_mode(size)
pygame.display.set_caption("初次见面,请大家多多关照")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.fill(WHITE)
pygame.draw.lines(screen,GREEN,1,points1,1)
pygame.draw.line(screen,BLACK,(100,150),(540,200),1)
pygame.draw.aaline(screen,BLACK,(100,250),(540,300),1)
pygame.draw.aalines(screen,BLACK,1,points2,1)
# 更新界面,达到显示的效果
pygame.display.flip()
clock.tick(10)