Petlja为初学者提供的PyGame工具箱
PygameBg的Python项目详细描述
pygamebg是一个小型python包,旨在减少简单pygame程序中的样板代码,主要是初始化代码和主循环。
pygamebg应该使pygame的学习曲线对于初学者来说更为渐进,但不会失去纯粹的pygame api的关注。
当我们将python与类似c的编程语言进行比较时,我们通常提到的一个积极的特性是一行“hellow world!“示例:
print('Hellow World!')
在这里,Python游戏还不够。正确的“绘制圆”程序如下所示:
import pygame as pg pg.init() surface = pg.display.set_mode((400,400)) pg.display.set_caption("Blue circle") pg.draw.circle(surface, pg.Color("blue"), (200,200), 100) pg.display.update() while pg.event.wait().type != pg.QUIT: pass pg.quit()
本例中的中心代码行是:
pg.draw.circle(surface, pg.Color("blue"), (200,200),100)
我们可以说前三行(不包括import)打开了一个窗口,最后四行等待用户退出,但我们不想让初学者负担这些样板语句的细节。
下面是一个使用patljabg的等效示例:
import pygame as pg import pygamebg surface = pygamebg.open_window(400, 400, "Blue circle") pg.draw.circle(surface, pg.Color("blue"), (200,200), 100) pygamebg.wait_loop()
对于初学者来说,这是一个可读性更强的例子,并且更容易解释:我们打开窗口,然后画蓝色圆圈,然后等待用户退出。
除了wait_loop,pygamebg还支持frame_loop和event_loop。
下面是使用frame_loop:
的示例import pygame as pg import pygamebg surface = pygamebg.open_window(300, 300, "Read keyboard state") x, y = 150, 150 def update(): global x, y surface.fill(pg.Color("white")) pressed = pg.key.get_pressed() if pressed[pg.K_RIGHT]: x += 1 if pressed[pg.K_LEFT]: x -= 1 if pressed[pg.K_DOWN]: y += 1 if pressed[pg.K_UP]: y -= 1 pg.draw.circle(surface , pg.Color("red"), (x, y), 30) pygamebg.frame_loop(30, update)
因此,帧循环每帧调用一次update函数,并可以选择调用事件处理程序:
import pygame as pg import pygamebg width, height = 500, 300 surface = pygamebg.open_window(width, width, "Increasing and decreasing speed") pg.key.set_repeat(10,10) fps = 30 x, y = 150, 150 vx, vy = 0, 0 def update(): global x,y x = (x + vx/fps) % width y = (y + vy/fps) % height surface.fill(pg.Color("white")) color = pg.Color("red") pg.draw.circle(surface, color, (int(x), int(y)), 30) def handle_event(d): global vx, vy if d.type == pg.KEYDOWN: if d.key == pg.K_RIGHT: vx += 1 elif d.key == pg.K_LEFT: vx -= 1 elif d.key == pg.K_DOWN: vy += 1 elif d.key == pg.K_UP: vy -= 1 pygamebg.frame_loop(fps, update, handle_event)
我们还可以使用dictionary参数为特定的事件类型指定事件处理程序:
def keydown(e): global vx, vy if e.key == pg.K_RIGHT: vx += 1 elif e.key == pg.K_LEFT: vx -= 1 elif e.key == pg.K_DOWN: vy += 1 elif e.key == pg.K_UP: vy -= 1 pygamebg.frame_loop(fps, update, {pg.KEYDOWN: keydown})
帧循环可以处理事件,但它始终是帧驱动的:它在每个帧上更新,并在每次更新之前处理挂起的事件。
纯事件循环在事件发生时立即处理它们,并在需要时触发重新绘制(当事件处理程序返回True):
import pygame as pg import pygamebg surface = pygamebg.open_window(500, 500, "Keyboard and mouse events") pg.key.set_repeat(10,10) x, y = 150, 150 def handle_event(e): global x, y if e.type == pg.MOUSEBUTTONDOWN: x,y = e.pos return True if e.type == pg.KEYDOWN: if e.key == pg.K_RIGHT: x += 1 elif e.key == pg.K_LEFT: x -= 1 elif e.key == pg.K_DOWN: y += 1 elif e.key == pg.K_UP: y -= 1 else: return False return True return False def paint(): surface.fill(pg.Color("white")) pg.draw.circle(surface, pg.Color("blue"), (x, y), 50) pygamebg.event_loop(paint, handle_event)
dictionary参数还可用于指定特定事件类型的事件处理程序:
import pygame as pg import pygamebg surface = pygamebg.open_window(500, 500, "Keyboard and mouse events") pg.key.set_repeat(10,10) x, y = 150, 150 def clicked(e): global x, y x,y = e.pos return True def keypressed(e): global x,y if e.key == pg.K_RIGHT: x += 1 elif e.key == pg.K_LEFT: x -= 1 elif e.key == pg.K_DOWN: y += 1 elif e.key == pg.K_UP: y -= 1 else: return False return True def paint(): surface.fill(pg.Color("white")) pg.draw.circle(surface, pg.Color("blue"), (x, y), 50) pygamebg.event_loop(paint, {pg.MOUSEBUTTONDOWN:clicked, pg.KEYDOWN:keypressed})
所有示例的源文件都可用here。
如何安装pygamebg
使用pip安装pygamebg:
pip3 install pygamebg
如果您使用Windows而上一个命令不起作用,请尝试:
py -3 -m pip install pygamebg