python中的pygame
直接上代码(实用型模块)
- 显示文本
import pygame
# 显示文字
if __name__ == '__main__':
# 初始化pygame组件
pygame.init()
# 设置pygame的显示窗口尺寸
screen = pygame.display.set_mode((1000,600))
# 设置窗口背景色
screen.fill((255, 255, 255))
# 设置显示字体的字体类型,大小
font = pygame.font.Font('./font/aa.ttf',50) # HYShangWeiShouShuW.ttf
# 设置要显示的字体内容,平滑允许值,颜色
surface = font.render('阿萨德', True, (255, 0, 0),(0,255,0))
# 设置文字显示在画布上的坐标
screen.blit(surface,(40,40))
while True:
# 监测窗口事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
# 刷新窗口
pygame.display.flip()
- 显示图片
import pygame
import random
显示图片
if __name__ == '__main__':
# 初始化pygame组件
pygame.init()
# 设置pygame的显示窗口尺寸
screen = pygame.display.set_mode((1000, 600))
# 设置窗口背景色
screen.fill((255, 255, 255))
# 获取图片对象
image = pygame.image.load("./image/timg.jpg")
# 获取图片大小
size_image = image.get_size()
print(size_image)
# 形变
"""
transform:形变包含缩放,旋转,平移
"""
# 修改图片尺寸 scale
trans_image = pygame.transform.scale(image, (1000, 600))
# 旋转图片 rotate (旋转对象,旋转角度) 逆时针
trans_image = pygame.transform.rotate(image,180)
# 旋转+缩放 rotozoom (旋转对象,旋转角度,缩放比例)
trans_image = pygame.transform.rotozoom(image,90,0.5)
# 设置图片为窗口图标
pygame.display.set_icon(image)
# 设置图片显示在画布上的坐标
screen.blit(trans_image, (0, 0))
n = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
pygame.display.flip()
- 绘制图形
import pygame
import math
import random
if __name__ == '__main__':
# 初始化pygame组件
pygame.init()
# 设置pygame的显示窗口尺寸
screen = pygame.display.set_mode((1000, 600))
# 设置窗口背景色
screen.fill((255, 255, 255))
"""
1.画直线
line(Surface ->画图位置,color ->颜色,start_pos ->起点,end_pos ->终点,width=1 ->线宽)
lines(画线的位置,颜色,closed,点列表,宽度width=1)
"""
line = pygame.draw.line(screen,(0,0,0),(20,20),(400,600),2)
pygame.draw.lines(screen,(0,0,0),True,[(20,20),(200,500),(40,80)])
"""
2.画图形
矩形:rect(画图位置,颜色,(x,y,长,宽),width)
曲线:arc(画图位置,颜色,矩形属性(左上顶点坐标x,y,长,宽),width)
"""
q = (50,50,600,100)
pygame.draw.rect(screen,(0,255,0),q,1)
pygame.draw.arc(screen,(0,0,0),q,math.pi/2,math.pi*2,2)
"""
3.画圆
circle(位置,颜色,圆心位置,半径,width)
"""
pygame.draw.circle(screen,
(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)),
(500, 300),
200, 0)
"""
4.画椭圆
ellipse(位置,颜色,rect,width)
"""
pygame.draw.ellipse(screen,(0,0,0),(100,200,200,100),0)
# 从此次往下为窗口控制变色小球的上下左右移动,键盘控制方式
sumadd_m = 20
sumadd_n = 300
add_m = 0
add_n = 0
while True:
screen.fill((255, 255, 255))
pygame.draw.circle(screen, (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)), (sumadd_m, sumadd_n),20, 0)
# 刷新窗口
pygame.display.flip()
# 监测窗口事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
add_m = 1
elif event.key == pygame.K_LEFT:
add_m = -1
elif event.key == pygame.K_UP:
add_n = -1
elif event.key == pygame.K_DOWN:
add_n = 1
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
add_m = 0
elif event.key == pygame.K_LEFT:
add_m = 0
elif event.key == pygame.K_UP:
add_n = 0
elif event.key == pygame.K_DOWN:
add_n = 0
sumadd_m += add_m
sumadd_n += add_n
# pygame.time.wait(10) # 控制程序单次循环运行时间,作用使程序每次循环暂停10毫秒