前言
Python3使用Pygame画窗口
安装Pygame包
引入包
1 2
| import pygame from pygame.locals import *
|
绘制窗口
<weight>
:窗口宽度
<height>
:窗口高度
1
| canvas = pygame.display.set_mode((<width>, <height>))
|
填充颜色
<color_red>
:RGB的红色值,范围0255
<color_green>
:RGB的绿色值,范围0255
<color_blue>
:RGB的蓝色值值,范围0~255
<color_name>
:颜色英文名
1 2 3
| canvas.fill((<color_red>, <color_green>, <color_blue>))
canvas.fill(THECOLORS["<color_name>"])
|
加载图片
加载到内存
<file>
:图片文件路径
1
| img = pygame.image.load("<file>.png")
|
绘制到窗口
img
:加载到内存的图片变量名
<x>
:位置横坐标
<y>
:位置纵坐标
1
| canvas.blit(img, (<x>, <y>))
|
加载声音
加载到内存
<file>
:声音文件路径
1
| sound = pygame.mixer.music.load("<file>.wav")
|
开始播放
1
| pygame.mixer.music.play()
|
加载文字
加载字体到内存
1
| font = pygame.font.Font(r"./Consolas.ttf", 20)
|
通过字体创建文字对象
<text>
:文本内容
(255, 0, 0)
:文字颜色RGB值
<x>
:文字位置横坐标
<y>
:文字位置纵坐标
1 2
| text = font.render("<text>", True, (255, 0, 0)) text_rect = text.get_rect(center=(<x>, <y>))
|
绘制文字对象
1
| canvas.blit(text, text_rect)
|
绘画
画矩形
canvas
:窗口对象
<x>
:矩形位置横坐标
<y>
:矩形位置纵坐标
<weight>
:矩形宽度
<weight>
:矩形高度
<line>
:如果不为0,表示线宽;如果为0,表示填充
1 2 3
| pygame.draw.rect(canvas, (<red>, <green>, <blue>), [<x>, <y>, <width>, <hexight>], <line>)
pygame.draw.rect(canvas, THECOLORS["<color_name>"], [<x>, <y>, <width>, <hexight>], <line>)
|
画圆形
canvas
:窗口对象
<x>
:圆形位置横坐标
<y>
:圆形位置纵坐标
<r>
:圆的半径
<line>
:如果不为0,表示线宽;如果为0,表示填充
1 2 3
| circle(canvas, (<red>, <green>, <blue>), (<x>, <y>), <r>, <line>)
circle(canvas, THECOLORS["<color_name>"], (<x>, <y>), <r>, <line>)
|
画弧形
- 因为弧形涉及到角度,所以通常引入math包用于计算角度
canvas
:窗口对象
<x>
:弧形位置横坐标
<y>
:弧形位置纵坐标
<width>
:弧形宽度
<width>
:弧形高度
<angle_start>
:弧形起始角度
<angle_end>
:弧形结尾角度
<line>
:如果不为0,表示线宽;如果为0,表示填充
1 2 3 4 5
| import math
pygame.draw.arc(canvas, (<red>, <green>, <blue>), (<x>, <y>, <width>, <height>), <angle_start>, <angle_end>, <line>)
pygame.draw.arc(canvas, THECOLORS["<color_name>"], (<x>, <y>, <width>, <height>), <angle_start>, <angle_end>, <line>)
|
画椭圆形
canvas
:窗口对象
<x>
:椭圆形位置横坐标
<y>
:椭圆形位置纵坐标
<width>
:椭圆形宽度
<width>
:椭圆形高度
<line>
:如果不为0,表示线宽;如果为0,表示填充
1 2 3
| ellipse(canvas, (<red>, <green>, <blue>), (<left>, <top>, <width>, <height>), <line>)
ellipse(canvas, THECOLORS["<color_name>"], (<left>, <top>, <width>, <height>), <line>)
|
画曲线
- aalines是绘制抗锯齿的线
- Pygame中没有直接用来画曲线的函数,所以可以使用抗锯齿的线函数,采用多个__1像素__的__圆形点__或__矩形__点拼接为曲线
False
:首位不相连
[[<x1>, <y1>], [<x2>, <y2>], ...]
:包含一组点的横纵坐标列表
<line>
:因为是画线,所以只能是线宽
1 2 3
| pygame.draw.aalines(canvas, (<red>, <green>, <blue>), False, [[<x1>, <y1>], [<x2>, <y2>], ...], <line>)
pygame.draw.aalines(canvas, THECOLORS["<color_name>"], False, [[<x1>, <y1>], [<x2>, <y2>], ...], <line>)
|
截取当前窗口为图片
<file>
:图片文件路径
1
| pygame.image.save(canvas, "<file>.jpg")
|
事件判定
关闭按钮事件
1 2 3 4
| for event in pygame.event.get(): if event.type == QUIT: ... pass
|
键盘事件
键盘按下事件
<num>
:键码值,详情见键码值对照表
1 2 3 4
| for event in pygame.event.get(): if event.type == KEYDOWN and e.key == <num>: ... pass
|
鼠标事件
鼠标按下事件
1 2 3 4
| for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: ... pass
|
鼠标移动事件
x
:拆包后得到的鼠标当前横坐标
y
:拆包后得到的鼠标当前纵坐标
1 2 3
| for event in pygame.event.get(): if event.type == MOUSEMOTION: x, y = e.pos
|
刷新屏幕
关闭窗口
退出程序
完成
参考文献
知乎——Blueawn
知乎——Blueawn
知乎——Blueawn