1.pygame
1.1初始化
pygame.init()
1.2.创建窗口
set_mode((宽度,高度))
screen = pygame.display.set_mode((600,400))
1.3.游戏循环
while True:
#检测窗口上的按钮是否被点击
for event in pygame.event.get():
if event.type ==pygame.QUIT
colse()
2、显示文字
2.1创建字体
创建系统字体
sysfont(name, size, blod == Flase, italic==False)
italic:倾斜
font =pygame.font.sysFont('times,68)
创建自定义字体
Font(字体文件路径,字体大小)
font = pygame.font.Font(‘./aa/aa.tff
2.1根据字体创建显示对象(文字)(找内容)
render(self,text,antialias,color,background = None)
antialias:-->是否平滑
surface = font.render('hello,python'True,(0,255,0))
2.2将内容画到纸上
bilt(需要显示的对象,显示位置)
screen.blit(surface,(100,100))
2.3将窗口位置上的内容展示出来
pygame.display.flip()
3.显示图片
3.1 获取图片对象
image = pygame.image.load(图片地址)
3.2 将图片对象渲染到窗口上
screen.blit(image,(0,0))
3.3 将图片展示到屏幕上
pygame.display.filp()
获取图片大小
image_size = image.get_size()
图片缩放
transform:形变包括缩放、平移、旋转
scale(缩放对象,新的大小)--->返回一个缩放后的新对象
image = pygame.transform.scale(image,(600,400))
rotate(旋转角度,旋转角度)--->角度是0-360对应的度数
image = pygame.transform.orate(image,90)
rotozoom(旋转对象,旋转角度,缩放比例)
image = pygame.transform.rotozoom(image,-90,0.4)
4.显示图形
画矩形
** rect(位置,颜色,(x,y,with,height))**
from math import pi
pygame.draw.arc(screen,(0, 0, 0), (0, 0,200, 200), pi+pi/4, pi*2-pi/4)
画圆
circle(位置,颜色,圆心位置,半径,width=0)
import random
pygame.draw.circle(screen,\
(random.randint(0,255),random.randint(0,255),random.randint(0,255)),\
(400,200),100)
画椭圆
ellipse(Surface,color,Rect,width = 0)
画直线
line(Surface ,colors,start_pos,end_pos,width)
Surface-->画在哪个地方
color-->线的颜色
star_pos——>起点
end_pos-->终点
width-->线的宽度
pygame.draw.line(screen, (255, 0, 0), (78, 59), (100, 100), 2)
lines(画线的位置,颜色,closed,点的列表,width=1)
pygame.draw.lines(screen,(0,0,255),True,[(10,10),(200,50),(100,100)])
画曲线
arc(Surface ,colors,start_pos,end_pos,width)