加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python 游戏之旅(Pygame)

发布时间:2020-12-16 23:58:10 所属栏目:Python 来源:网络整理
导读:Pygame是跨平台Python模块,专为电子游戏设计,包含图像、声音。建立在SDL基础上,允许实时电子游戏研发而无需被低级语言(如机器语言和汇编语言)束缚。基于这样一个设想,所有需要的游戏功能和理念都(主要是图像方面)都完全简化为游戏逻辑本身,所有的资

Pygame是跨平台Python模块,专为电子游戏设计,包含图像、声音。建立在SDL基础上,允许实时电子游戏研发而无需被低级语言(如机器语言和汇编语言)束缚。基于这样一个设想,所有需要的游戏功能和理念都(主要是图像方面)都完全简化为游戏逻辑本身,所有的资源结构都可以由高级语言提供。

Pygame的编程其实可以理解为循环加事件实现。

安装:

pip3 install pygame

测试:(可忽略)

python -m pygame.examples.aliens

画一个厉害的画玩玩:

生成一个最简窗口:

pygame sys exit screen = pygame.display.set_mode((700,450 pygame.display.set_caption( background = pygame.image.load( event event.type == exit()

??

画画实现:

实现文字显示、事件监听、画大部分图形,其中显示文字时需要中文字体库支持,可直接将对应的字体拷贝到目录下,方便程序的移植,而不要固定写Windows字体库的路径,所以相对路径是最好的解决方法。

Windows文字库所在位置:C:WindowsFonts

pygame sys exit math screen = pygame.display.set_mode((700,450 pygame.display.set_caption( co1=pygame.Color(255,255,20,10 co2=pygame.Color(255,255 co3=pygame.Color(0,255 cotextbg=pygame.Color(0,200 cotext=pygame.Color(255,255 ftext222=pygame.freetype.Font(,20 textrect222=ftext222.render_to(screen,(620,420),,fgcolor=cotext,bgcolor=cotextbg,rotation=0,size=20 texview333,texrect333=ftext222.render(,rotation=20,size=50 rectx = pygame.draw.rect(screen,(255,255),(330,289,30,20 rectx = pygame.draw.rect(screen,(380,0) rectx = pygame.draw.rect(screen,co2,(270,270,200,5 circlex = pygame.draw.circle(screen,(250,200),50 circlex = pygame.draw.circle(screen,(450,180),50,1 ell = pygame.draw.ellipse(screen,40,500,350),1 alrectx = pygame.draw.arc(screen,co3,(344,244,60,0 * pi,1 * pi,3 event event.type == exit()

壁球小游戏基本实现及Pygame的其他内容补充:

pygame sys exit winWidth=700 winHeight=450 ballX=1 ballY=1 fpsx=19 timefpsx= winRoot= winWidth= winHeight= mod=pygame.RESIZABLE screen = pygame.display.set_caption( titleiconx=pygame.image.load( backgroundx = pygame.image.load( ballimgx=pygame.image.load( ballrectx= ballrectx= ballrectx.left<0 ballrectx.right> ballX=- ballrectx.top<0 ballrectx.bottom> ballY=- event event.type == event.type == (,event.pos, event.type == ( event.type == (,,event.rel, event.type == (,event.key, event.key==27 event.key == 273 ballY =-1* event.key == 274 ballY= event.key ==276 ballX=-1* event.key ==275 ballX= event.key == 32: screen = event.type == ( event.type == ( winWidth= winHeight=event.size[1 screen = pygame.display.set_mode((winWidth,mod)

自定义事件处理:

可绑定标准事件,但传递参数不一定需要标准,可在标准事件捕获到自定义事件:

eventx=pygame.event.Event(pygame.KEYDOWN,{:33 event event.type ==pygame.KEYDOWN: (event.ww)

??


Pygame官网:

参考:嵩天教授的Python游戏开发教程(pygame)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读