稍微看了看pygame,这个东西不能用来做大型商业游戏,它最大的用处应该在与验证逻辑
0基础阅读本文+敲代码实现小程序 总共耗时约3小时
pygame 2D 3D 验证逻辑
pip install pygame
python -m pygame.examples.aliens #这是一个pygame自带的小游戏
# 引入
import pygame, sys
# 初始化
pygame.init()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption("GameofLiYuxi")
# 无限循环 事件和响应
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# 刷新
pygame.display.update()
https://python123.io/PY15/PYG02-ball.gif
import pygame, sys
pygame.init()
size = width, height = 600, 400
speed = [1, 1]
BLACK = 0, 0, 0
screen = pygame.display.set_mode(size)
pygame.display.set_caption("ball_game")
ball = pygame.image.load("PYG02-ball.gif")
ballrect = ball.get_rect() #Surface对象
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
ballrect = ballrect.move(speed[0], speed[1])
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]
screen.fill(BLACK)
screen.blit(ball, ballrect)
pygame.display.update()
import pygame, sys
pygame.init()
size = width, height = 600, 400
speed = [1, 1]
BLACK = 0, 0, 0
screen = pygame.display.set_mode(size)
pygame.display.set_caption("ball_game")
ball = pygame.image.load("PYG02-ball.gif")
ballrect = ball.get_rect() #Surface对象
fps = 300 #Frames per Second 每秒帧率
fclock = pygame.time.Clock() #操作时间
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
ballrect = ballrect.move(speed[0], speed[1])
#保证小球始终处于框内,触框反弹
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]
screen.fill(BLACK)
screen.blit(ball, ballrect)
pygame.display.update()
fclock.tick(fps) #刷新帧率控制
import pygame, sys
pygame.init()
size = width, height = 600, 400
speed = [1, 1]
BLACK = 0, 0, 0
screen = pygame.display.set_mode(size)
pygame.display.set_caption("ball_game")
ball = pygame.image.load("PYG02-ball.gif")
ballrect = ball.get_rect() #Surface对象
fps = 300 #Frames per Second 每秒帧率
fclock = pygame.time.Clock() #操作时间
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
speed[0] = speed[0] if speed[0] == 0 else(abs(speed[0]) - 1)*int(speed[0]/abs(speed[0]))
elif event.key == pygame.K_RIGHT:
speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1
elif event.key == pygame.K_UP:
speed[1] = speed[1] + 1 if speed[1] > 0 else speed[1] - 1
elif event.key == pygame.K_DOWN:
speed[1] = speed[1] if speed[1] == 0 else(abs(speed[1]) - 1)*int(speed[1]/abs(speed[1]))
ballrect = ballrect.move(speed[0], speed[1])
#保证小球始终处于框内,触框反弹
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]
screen.fill(BLACK)
screen.blit(ball, ballrect)
pygame.display.update()
fclock.tick(fps) #刷新帧率控制
屏幕控制需求
全屏
屏幕大小
边框有无
标题栏
图标
#屏幕尺寸和模式
pygame.display.set_mode() 设置屏幕模式
pygame.display.Info() 生成屏幕信息
#窗口标题和图标
pygame.display.set_caption() 设置标题
pygame.display.set_icon 设置图标
pygame.display.get_caption()
#窗口感知和刷新
pygame.display.get_active()
pygame.display.flip()
pygame.display.update()
openGL 和 硬件加速
pygame.display.set_mode(r = (0, 0), flags = 0)
r是游戏屏幕的分辨率,采用(width, height)方式输入
flags用来控制显示类型,可用|组合,常见的有以下三类:
pygame.RESIZABLE #窗口大小可调
pygame.NOFRAME #窗口无边界
pygame.FULLSCREEN#窗口全屏显示
pygame.display.Info() 生成屏幕信息
pygame.display.set_mode() 设置屏幕模式
pygame.display.Info() 生成屏幕信息
current_w current_h
pygame.display.set_caption(title, icontitle=None)
pygame.display.get_caption() 返回(title, icontitle)
pygame.display.set_icon(Surface)
事件处理需求
响应键鼠外设
响应屏幕尺寸和模式变化
响应游戏情节的特定触发条件
事件队列 缓存并派发所有事件
pygame.event.KEYDOWN
返回值
event.unicode 按键的unicode码(不推荐)
event.key 按键的常量名称
event.mod 案件修饰符的组合值
pygame.event.KEYUP
返回值
event.key 按键的常量名称
event.mod 案件修饰符的组合值
pygame.event.MOUSEMOTION 鼠标移动事件
返回值
event.pos 鼠标当前坐标值(x, y),相对于窗口左上角
event.rel 鼠标相对运动距离(X, Y),相对于上次事件
event.buttons 鼠标按钮状态(a, b, c),对应于鼠标的三个键。 鼠标移动时,这三个键处于按下状态,对应的位置值为1,反之则为0
pygame.event.MOUSEBUTTONUP 鼠标键释放事件
返回值
event.pos 鼠标当前坐标值(x, y),相对于窗口左上角
event.button 鼠标按下键编号n 取值0/1/2 分别对应三个键
pygame.event.MOUSEBUTTONDOWN 鼠标按下事件
返回值
event.pos 鼠标当前坐标值(x, y),相对于窗口左上角
event.button 鼠标按下键编号n,取值为整数,左键为1,右键为3,设备相关
# 引入
import pygame, sys
# 初始化
pygame.init()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption("GameofLiYuxi")
# 无限循环 事件和响应
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.unicode == "":
print("[KEYDOWN]:", "#", event.key, event.mod)
else:
print("[KEYDOWN]:", event.unicode, event.key, event.mod)
elif event.type == pygame.MOUSEMOTION:
print("[MOUSEMOTION]:", event.pos, event.rel, event.buttons)
elif event.type == pygame.MOUSEBUTTONUP:
print("[MOUSEBUTTONUP]:", event.pos, event.button)
elif event.type == pygame.MOUSEBUTTONDOWN:
print("[MOUSEBUTTONDWON]:", event.pos, event.button)
# 刷新
pygame.display.update()
import pygame, sys
pygame.init()
#设置图标
icon = pygame.image.load("PYG02-ball.gif")
pygame.display.set_icon(icon)
#获取本机屏幕分辨率信息
vInfo = pygame.display.Info()
# size = width, height = vInfo.current_w, vInfo.current_h
size = width, height = 600, 400
speed = [1, 1]
BLACK = 0, 0, 0
screen = pygame.display.set_mode(size, pygame.RESIZABLE)
# screen = pygame.display.set_mode(size, pygame.NOFRAME)
# screen = pygame.display.set_mode(size, pygame.FULLSCREEN)
pygame.display.set_caption("ball_game")
ball = pygame.image.load("PYG02-ball.gif")
ballrect = ball.get_rect() #Surface对象
fps = 300 #Frames per Second 每秒帧率
fclock = pygame.time.Clock() #操作时间
still = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
speed[0] = speed[0] if speed[0] == 0 else(abs(speed[0]) - 1)*int(speed[0]/abs(speed[0]))
elif event.key == pygame.K_RIGHT:
speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1
elif event.key == pygame.K_UP:
speed[1] = speed[1] + 1 if speed[1] > 0 else speed[1] - 1
elif event.key == pygame.K_DOWN:
speed[1] = speed[1] if speed[1] == 0 else(abs(speed[1]) - 1)*int(speed[1]/abs(speed[1]))
elif event.key == pygame.K_ESCAPE:
sys.exit()
elif event.type == pygame.VIDEORESIZE: #使游戏适配屏幕的大小改变
size = width, height = event.w, event.h
screen = pygame.display.set_mode(size, pygame.RESIZABLE)
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
still = True
elif event.type == pygame.MOUSEBUTTONUP:
still = False
if event.button == 1:
ballrect = ballrect.move(event.pos[0] - ballrect.left, event.pos[1] - ballrect.top)
elif event.type == pygame.MOUSEMOTION:
if event.buttons[0] == 1:
ballrect = ballrect.move(event.pos[0] - ballrect.left, event.pos[1] - ballrect.top)
if pygame.display.get_active() and not still:
ballrect = ballrect.move(speed[0], speed[1])
#保证小球始终处于框内,触框反弹
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.right > width and ballrect.right + speed[0] > ballrect.right:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]
if ballrect.bottom > height and ballrect.bottom + speed[1] > ballrect.bottom:
speed[1] = -speed[1]
screen.fill(BLACK)
screen.blit(ball, ballrect)
pygame.display.update()
fclock.tick(fps) #刷新帧率控制
处理事件
pygame.event.get()
pygame的事件队列同时存储128个事件
pygame.event.poll()
pygame.event.clear()
操作事件队列
pygame.event.set_blocked()
pygame.event.get_blocked()
pygame.event.set_allowed()
生成事件
pygame.event.post()
pygame.event.Event()
import pygame, sys
pygame.init()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Pygame事件处理")
fps = 1
fclock = pygame.time.Clock()
num = 1
while True:
uevent = pygame.event.Event(pygame.KEYDOWN, {"unicode": 123, "key": pygame.K_SPACE, "mod": pygame.KMOD_ALT})
pygame.event.post(uevent)
num = num + 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.unicode == "":
print("[KEYDOWN {}]:".format(num), "#", event.key, event.mod)
else:
print("[KEYDOWN {}]:".format(num), event.unicode, event.key, event.mod)
pygame.display.update()
fclock.tick(fps)
颜色
pygame.color类
RGBA
图形
pygame.draw(Surface, color, Rect, width=0)
pygame.draw.circle
pygame.draw.ellipse
pygame.draw.polygon()
pygame.draw.arc(Surface, color, Rect, start_angle, stop_angle, width=0)
pygame.draw.line(Surface, color, start_pos, end_pos,width=1)
pygame.draw.lines(Surface, color, closed, pointlist, width=1)
文字
pygame.freetype