一个基于sprite的轻量级游戏和图形框架
ggame的Python项目详细描述
ggame
布莱森服务器的简单精灵和游戏平台(PyGame,Tkinter跟随?).
GGAME代表两件事:“好游戏”(当然!)还有“git游戏”或“github游戏” 因为它的设计与Brython Server配合使用 Github作为后端文件存储。
ggame是not旨在成为一个功能齐全的游戏api,具有各种功能。Ggame是 主要作为计算机程序设计的教学工具,认识到 对于许多有进步的学生来说,创建有吸引力的互动游戏是一个强大的动力。 因此,可以合理实现的任何功能或性能增强 由用户留作练习。
请访问 detailed documentation page for ggame。 这是从ggame源自动生成的。
功能目标
ggame库的目的是简单易用。例如:
fromggameimportApp,ImageAsset,Sprite# Create a displayed object at 100,100 using an image assetSprite(ImageAsset("bunny.png"),(100,100))# Create the app, with a default stageapp=App()# Run the appapp.run()
另一个例子
下面的示例演示了更常见的用例,其中 类,sprite和app,作为bunny和demoapp的子类和给定的事件处理程序 以及步骤(即投票)功能。
fromggameimportApp,ImageAsset,Sprite,MouseEventfromrandomimportrandom,randintclassBunny(Sprite):asset=ImageAsset("bunny.png")def__init__(self,position):super().__init__(Bunny.asset,position)# register mouse eventsApp.listenMouseEvent(MouseEvent.mousedown,self.mousedown)App.listenMouseEvent(MouseEvent.mouseup,self.mouseup)App.listenMouseEvent(MouseEvent.mousemove,self.mousemove)self.dragging=Falsedefstep(self):""" Every now and then a bunny hops... """ifrandom()<0.01:self.x+=randint(-20,20)self.y+=randint(-20,20)defmousedown(self,event):# capture any mouse down within 50 pixelsself.deltax=event.x-(self.x+self.width//2)self.deltay=event.y-(self.y+self.height//2)ifabs(self.deltax)<50andabs(self.deltay)<50:self.dragging=True# only drag one bunny at a time - consume the eventevent.consumed=Truedefmousemove(self,event):ifself.dragging:self.x=event.x-self.deltax-self.width//2self.y=event.y-self.deltay-self.height//2event.consumed=Truedefmouseup(self,event):ifself.dragging:self.dragging=Falseevent.consumed=TrueclassDemoApp(App):def__init__(self):super().__init__()foriinrange(10):Bunny((randint(50,self.width),randint(50,self.height)))defstep(self):""" Override step to perform action on each frame update """forbunnyinself.spritelist:bunny.step()# Create the appapp=DemoApp()# Run the appapp.run()
安装ggame
在将ggame与github上的python源存储库一起使用之前,可以添加ggame源 树到您的存储库。如果在http://runpython.com中执行代码,则当前 ggame存储库已添加到导入搜索路径,无需安装。
对ggame的贡献
环境
使用python 3.7+设置开发环境。例如,使用 this procedure。
使用venv创建一个虚拟环境并激活它:
$ python3.7 -m venv venv
$ source venv/bin/activate
安装要求:
$ pip install -r requirements-headless.txt
通过运行测试来测试您的环境:
$ scripts/run_tests.sh
代码质量
ggame中的python源应该通过black
传递。例如:
$ black ggame/app.py
python源代码也应该使用pylint
进行测试。例如:
$ python3 -m pylint -r n ggame/app.py
你可以用狮身人面像来完成所有这些检查 通过执行脚本:
$ scripts/run_tests.sh