运行Pygame前,要先安装好Pygame。
import pygame
if __name__ == '__main__':
# 1、初始化Pygame
pygame.init()
2、创建游戏窗口
格式:set_mode((宽度,高度)),里面为元组,单位是像素
screen = pygame.display.set_mode((700, 900))
3、游戏循环
while True:
# 检测事件
for event in pygame.event.get():
# 检测窗口上的关闭按钮是否被点击
if event.type == pygame.QUIT:
# 退出游戏
print('关闭按钮被点击')
exit()
2、显示文字
import pygame
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((600, 400))
设置窗口的背景颜色
screen.fill((255, 255, 100))
1、创建字体对象
创建系统字体
格式: SysFont(name, size, bold=False, italic=False, constructor=None)
name ->字体名
size ->字体大小
bold ->加粗
italic->倾斜
创建自定义字体
Font(字体文件路径,字体大小)
font = pygame.font.Font('./font/aa.ttf', 35)
2、根据字体去创建显示对象(文字)(找内容)
"""
格式:render(self, text, antialias, color, background=None):
text ->要显示的文字内容(str)
antialias -> 是否平滑
color ->计算机三原色(红、绿、蓝)RGB颜色,值范围是0-255
(0, 0, 0)-->黑色
(255,0,0)-->红色
(0,0,255)-->蓝色
(x,x,x)--->灰色
"""
surface = font.render('Cannot write Chinese', True, (0, 255, 0))
3、将内容添加到窗口上(画在纸上)
"""
blit(需要显示的对象,显示位置)
需要显示的对象-->Surface类型的数据
显示位置-->坐标(x, y)
"""
screen.blit(surface, (0, 100))
4、将窗口的内容展示出来
pygame.display.flip()
游戏循环
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
三、显示图片
import pygame
pygame.init()
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((600, 600))
screen.fill((232, 255, 164))
1、获取图片对象
格式:image = pygame.image.load('./font/xx.jpg')
"""
a、获取图片大小
get_size()
"""
image_size = image.get_size() #get_size将产生新的图片,将变量返回才会显示
print(image_size)
"""
b、形变:
transform:形变包含缩放,旋转,平移
scale(缩放对象,新的大小)--->返回一个缩放后的对象
"""
格式: image = pygame.transform.scale(image, (200, 200))
"""
旋转
rotate(旋转对象,旋转角度) ----角度是0-360对应的度数
格式:image = pygame.transform.rotate(image, -60)
"""
旋转缩放
rotozoom(旋转对象,旋转角度,缩放比例)
"""
格式:image = pygame.transform.rotozoom(image, 0, 1.5)
2、将图片对象渲染到窗口上
screen.blit(image, (0, 0))
3、展示在屏幕上
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
四、显示图形
import pygame
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((666, 555))
screen.fill((123, 123, 223))
1、画直线
格式:line(Surface, color, start_pos, end_pos, width=1)
Surface -> 画在哪个地方
color ->线的颜色
start_pos ->起点
end_pos ->终点
width -> 线的宽度
pygame.draw.line(screen, (0, 0, 255), (120, 124), (200, 433), 10)
pygame.draw.line(screen, (0, 222, 255), (10, 124), (200, 433), 20)
多点连线
格式:lines(画线的地方,颜色,closed(是否封闭),坐标点的列表,width=1)
pygame.draw.lines(screen, (0, 123, 234), True, [(29, 150), (200, 250), (3, 400)], 10)
画矩形
Rect ->(x, y, width, height,width= 1(若等于0就填充))
2、画曲线
格式:arc(Surface, color, Rect, start_angle, stop_angle, width=1)
Rect ->(x, y, width, height)矩形
star_angle
stop_angle
"""
from math import pi
pygame.draw.arc(screen, (0,0,0),(100,100,300,300),pi/2,pi,30)
"""
3、画图
格式:circle(位置,颜色,圆心位置,半径,with=0)
"""
pygame.draw.circle(screen,(0, 34, 100),(200,200),100, 40)
"""
画椭圆
格式:ellipse(Surface,color,rect,width=0)
"""
pygame.draw.ellipse(screen,(130,79,90),(22,33,400,222),20)
# 将内容展示在屏幕上,若没有这些代码将不会显示
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()