为了账号安全,请及时绑定邮箱和手机立即绑定

Python 游戏之旅(Pygame)

标签:
Python

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

 

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

 

安装:

pip3 install pygame

 

测试:(可忽略)

python -m pygame.examples.aliens

 

画一个厉害的画玩玩:

 

生成一个最简窗口:

复制代码

 1 import pygame  # 导入pygame库 2 from sys import exit  # 导入sys库中的exit函数 3  4 # 初始化pygame 5 pygame.init() 6  7 #创建一个窗口,参数((宽度,高度)) 8 screen = pygame.display.set_mode((700,450)) 9 # 设置窗口标题10 pygame.display.set_caption("东小东游戏窗口")11 12 #  载入背景图13 background = pygame.image.load('imgx/wa.jpg')14 15 # 事件循环16 while True:17     # 绘制背景18     screen.blit(background, (0, 0))19 20     # 更新屏幕21     pygame.display.update()22 23     # 所有事件的监听,处理24     for event in pygame.event.get():25         # 游戏退出事件26          if event.type == pygame.QUIT:27             # 退出程序28             exit()

复制代码

 

 

画画实现:

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

Windows文字库所在位置:C:\Windows\Fonts

复制代码

 1 import pygame  # 导入pygame库 2 from sys import exit  # 导入sys库中的exit函数 3 from math import pi 4 import pygame.freetype 5  6 # 初始化pygame 7 pygame.init() 8 #创建一个窗口,参数((宽度,高度)) 9 screen = pygame.display.set_mode((700,450))10 #设置窗口标题11 pygame.display.set_caption("东小东游戏窗口")12 13 14 #颜色对象,参数(R,G,B,Alphal)15 co1=pygame.Color(255,255,20,10)16 co2=pygame.Color(255,0,0,255)17 co3=pygame.Color(0,255,0,255)18 cotextbg=pygame.Color(0,255,0,200)19 cotext=pygame.Color(255,255,255,255)20 21 # 填充背景色22 screen.fill(co1)23 24 #显示文字方法1,需要一直刷新25 #绘制文字,参数(字体,默认大小)26 #ftext111=pygame.font.SysFont("arial", 16)27 #参数:(文字,是否取消锯齿,前景色,背景色)28 #textviewx111=ftext111.render("wwww",True,(100,100,50),(0,255,255,50))29 30 #绘制文字222,不能使用中文路径,参数(字体,默认大小),返回矩形对象31 ftext222=pygame.freetype.Font("fontx/fontext.ttf",20)32 #方法233 #参数(窗口,左上角位置,文字,前景,背景,旋转角度[0,359],大小),返回矩形对象34 textrect222=ftext222.render_to(screen,(620,420),"东小东",fgcolor=cotext,bgcolor=cotextbg,rotation=0,size=20)35 #方法3,需要一直刷新,返回viwe和矩形对象36 texview333,texrect333=ftext222.render("东小东2",fgcolor=cotext,bgcolor=cotextbg,rotation=20,size=50)37 38 39 #------ 以下形状都会返回矩形对象  --------40 # 绘制矩形,参数(窗口,颜色,(左上角x,左上角y,w,h),宽度),宽度为0 则填充41 rectx = pygame.draw.rect(screen, (255,255,255), (330, 289, 30, 20), 0)42 rectx = pygame.draw.rect(screen, (255,255,255), (380, 289, 30, 20), 0)43 rectx = pygame.draw.rect(screen, co2, (270, 270, 200, 50), 5)44 # 绘制圆形,参数(窗口,颜色,圆形坐标(x,y),半径,宽度)45 circlex = pygame.draw.circle(screen, co2, (250, 200), 50, 0)46 circlex = pygame.draw.circle(screen, co2, (450, 180), 50, 1)47 48 # 绘制直线,参数(窗口,颜色,起点坐标(x,y),结束点坐标,宽度)49 #linex = pygame.draw.line(screen, co2, (20, 20), (100, 30), 1)50 # 无锯齿线,斜线去掉锯齿51 #aalinex = pygame.draw.aaline(screen, co2, (30, 30), (100, 50), 1)52 53 # 椭圆,使用外切矩形画,参数(窗口,颜色,矩形的(左上角x,左上角y,w,h),宽度54 ell = pygame.draw.ellipse(screen, co2, (100, 40, 500, 350), 1)55 56 # 绘制多线,第一的结束点必然会连接第二的起始点...57 # 列表参数为:第一条直线起始点,第一条直线结束点,第二条.....58 #listline = [(10, 100), (10, 200), (20, 100), (20, 300)]59 # 参数false表示结束点是否要连接最开始的起始点60 #linexss = pygame.draw.lines(screen, co2, False, listline, 2)61 62 # 绘制弧线63 alrectx = pygame.draw.arc(screen, co3, (344, 244, 60, 20), 0 * pi, 1 * pi, 3)64 65 # 事件循环66 while True:67     #显示文字方法168     #screen.blit(textviewx111,(20,20))69     #显示文字方法370     #screen.blit(texview333,(200,200))71 72     # 更新屏幕73     pygame.display.update()74     # 所有事件的监听,处理75     for event in pygame.event.get():76         # 游戏退出事件77          if event.type == pygame.QUIT:78             # 退出程序79             exit()

复制代码

 

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

复制代码

  1 import pygame  # 导入pygame库  2 from sys import exit  # 导入sys库中的exit函数  3 # 初始化pygame  4 pygame.init()  5   6   7 #定义窗体的宽高  8 winWidth=700  9 winHeight=450 10  11 #定义图片每次移动的x,y轴像素 12 ballX=1 13 ballY=1 14  15 #刷新最大帧速率,while循环的速度 16 fpsx=19 17  18 #初始化帧速率对象 19 timefpsx=pygame.time.Clock() 20  21 #全屏显示需调用函数 22 #获取到电脑屏幕的大小并赋值给窗口大小 23 def setfull(): 24     global winHeight,winWidth 25     winRoot=pygame.display.Info() 26     winWidth=winRoot.current_w 27     winHeight=winRoot.current_h 28  29  30  31 #创建一个窗口,参数((宽度,高度),显示模式) 32 #显示模式 33  34 mod=pygame.RESIZABLE #屏幕大小可调,需监听pygame.VIDEORESIZE事件 35  36 #mod=pygame.NOFRAME #无边框 37  38 #mod=pygame.FULLSCREEN #全屏,需打开下面setfull函数 39 #setfull() 40  41 screen = pygame.display.set_mode((winWidth,winHeight),mod) 42  43 # 设置窗口标题及标题内容 44 pygame.display.set_caption("东小东游戏窗口--壁球") 45 #设置标题图片 46 titleiconx=pygame.image.load("imgx/yzm.jpg") 47 pygame.display.set_icon(titleiconx) 48  49 #  载入背景图 50 backgroundx = pygame.image.load('imgx/wa.jpg') 51  52 #载入需要移动的图片 53 ballimgx=pygame.image.load("imgx/ball.png") 54 #生成与对象外切的矩形,生成的矩形对象可以获取到x,y,宽高的信息 55 ballrectx=ballimgx.get_rect() 56  57  58 # 事件循环 59 while True: 60     #设置帧速率 61     timefpsx.tick(fpsx) 62     # 绘制背景 63     screen.blit(backgroundx, (0, 0)) 64  65     #判断窗口是否被显示,如果最小化则为false 66     if pygame.display.get_active(): 67        #移动图片,参数(x轴每次移动像素,y轴每次移动像素) 68        ballrectx=ballrectx.move(ballX,ballY) 69  70     #判断图片是否移动到边缘,达到边缘时将移动的X或Y方向取反 71     if ballrectx.left<0 or ballrectx.right>winWidth: 72         ballX=-ballX 73     if ballrectx.top<0 or ballrectx.bottom>winHeight: 74         ballY=-ballY 75  76     #绘制移动的图片 77     screen.blit(ballimgx,ballrectx) 78  79     # 更新屏幕 80     pygame.display.update() 81  82  83     # 所有事件的监听(事件队列只能缓存128个事件),处理 84     for event in pygame.event.get(): 85         # 游戏退出事件 86         if event.type == pygame.QUIT: 87             # 退出程序 88             exit() 89  90         #鼠标事件 91         #鼠标按下事件 92         if event.type == pygame.MOUSEBUTTONDOWN: 93             print("鼠标按下,坐标为:",event.pos,",鼠标按下键为:",event.button) 94         #鼠标抬起事件 95         if event.type == pygame.MOUSEBUTTONUP: 96             print("鼠标抬起") 97         #鼠标移动 98         if event.type ==pygame.MOUSEMOTION: 99             print("鼠标移动中......当前坐标:",event.pos,"相对位置:",event.rel,"鼠标三键状态:",event.buttons)100 101         #键盘按键事件,字母只会输出小写的编码102         #键盘按下按键事件103         if event.type ==pygame.KEYDOWN:104             print("键盘按下键为:",event.key,"按键按下的模式标志为:",event.mod)105             #如果按下ESC键则退出程序106             if event.key==27:107                 exit()108             #游戏处理:上(273)、下(274)、左(276)、右(275)109             #实现按下哪个方向按键即向哪个方向移动110             if event.key == 273:111                 ballY =-1*abs(ballY)112             elif event.key == 274:113                 ballY=abs(ballY)114             elif event.key ==276:115                 ballX=-1*abs(ballY)116             elif  event.key ==275:117                 ballX=abs(ballX)118             if event.key == 32:#空格键,使无边框119                 screen = pygame.display.set_mode((winWidth, winHeight),pygame.NOFRAME)120 121         if event.type == pygame.KEYUP:122             print("键盘按键抬起:",event.key)123 124 125 126         if event.type ==pygame.VIDEORESIZE:127             print("屏幕大小有改变:",event.size)128             winWidth=event.size[0]129             winHeight=event.size[1]130             screen = pygame.display.set_mode((winWidth, winHeight), mod)

复制代码

 

自定义事件处理:


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

复制代码

1 eventx=pygame.event.Event(pygame.KEYDOWN,{"ww":33})2 pygame.event.post(eventx)3 4 # 所有事件的监听,处理5 for event in pygame.event.get():6      if event.type ==pygame.KEYDOWN:#键盘按键事件7          print(event.ww) #输出33

复制代码

 

 

 

 


Pygame官网:https://www.pygame.org/docs/

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

原文出处:https://www.cnblogs.com/dongxiaodong/p/10015451.html  

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消