Python游戏编程之旅(1):初识pygame

#一、pygame简介

Pygame 是一组用来开发游戏软件的 Python 程序模块,基于 SDL 库的基础上开发。允许你在 Python 程序中创建功能丰富的游戏和多媒体程序,Pygame 是一个高可移植性的模块可以支持多个操作系统。用它来开发小游戏非常适合。

可以去http://www.pygame.org/hifi.html 下载并安装使用pygame。

#二、pygame使用

使用pygame的第一步是将pygame库导入到python程序中,以便来使用它


import pygame

然后需要引入pygame中的所有常量。


from pygame.locals import *

再经过初始化以后我们就可以尽情地使用pygame了。初始化pygame:


pygame.init()

通常来说我们需要先创建一个窗口,方便我们与程序的交互。下面创建了一个600 x 500的窗口


screen  =  pygame.display.set_mode((600,500))

##1.打印字体

pygame支持使用pygame.font将文打印到窗口。要打印文本的话首先需要创建一个文字对象


myfont  =  pygame.font.Font(None,60)

这个文本绘制进程是一个重量级的进程,比较耗费时间,常用的做法是先在内存中创建文本图像,然后将文本当作一个图像来渲染。


white  =  255,255,255

blue  =  0,0,200

textImage  =  myfont.render("Hello Pygame",  True,  white)

textImage 对象可以使用screen.blit()来绘制。上面代码中的render函数第一个参数是文本,第二个参数是抗锯齿字体,第三个参数是一个颜色值(RGB值)。

要绘制本文,通常的过程是清屏,绘制,然后刷新。


screen.fill(blue)

screen.blit(textImage,  (100,100))

pygame.display.update()

如果此时运行程序的话,会出现一个窗口一闪而过。为了让它长时间的显示,我们需要将它放在一个循环中。


1  import pygame

2  from pygame.locals import *

3

4  white  =  255,255,255

5  blue  =  0,0,200

6

7  pygame.init()

8  screen  =  pygame.display.set_mode((600,500))

9

10  myfont  =  pygame.font.Font(None,60)

11  textImage  =  myfont.render("Hello Pygame",  True,  white)

12

13  while  True:

14 for  event in  pygame.event.get():

15 if  event.type in  (QUIT,  KEYDOWN):

16 sys.exit()

17

18 screen.fill(blue)

19 screen.blit(textImage,  (100,100))

20 pygame.display.update()

image

pygame除了打印字体,还有绘制各种常见图形的常见功能。(使用pygame.draw())

##2.绘制一个圆形。

使用pygame.draw.circle()方法,该方法需要传递圆的大小,颜色和位置参数。


1  color  =  255,255,0

2  position  =  300,250

3  radius  =  100

4  width  =  10

5  pygame.draw.circle(screen,  color,  position,  radius,  width)

##3.绘制一个矩形。

为了增添一些乐趣,咱们这次绘制一个可以移动的矩形,而不只是单单的在屏幕中间绘制。

首先,需要设置pos_x, pos_y 两个变量来记录矩形的位置信息,然后在创建一对速度变量(vel_x,vel_y),在while循环内不断的更新矩形的位置。当矩形到达屏幕边缘的时候,将速度变量取反,这样就可以产生碰撞的效果了。


1  import pygame

2  from pygame.locals import *

3  pygame.init()

4  screen  =  pygame.display.set_mode((600,500))

5  pygame.display.set_caption("Drawing Rectangles")

6

7  pos_x  =  300

8  pos_y  =  250

9  vel_x  =  2

10  vel_y  =  1

11

12  while  True:

13 for  event in  pygame.event.get():

14 if  event.type in  (QUIT,  KEYDOWN):

15 pygame.quit()

16 sys.exit()

17

18 screen.fill((0,0,200))

19    

20 #移动矩形

21 pos_x  +=  vel_x

22 pos_y  +=  vel_y

23    

24 #使矩形保持在窗口内

25 if  pos_x  >  500  or  pos_x  0:

26 vel_x  =  -vel_x

27 if  pos_y  >  400  or  pos_y  0:

28 vel_y  =  -vel_y        

29    

30 #绘制矩形

31 color  =  255,255,0

32 width  =  0  #solid fill

33 pos  =  pos_x,  pos_y,  100,  100

34 pygame.draw.rect(screen,  color,  pos,  width)

35    

36 pygame.display.update()

image

##4.绘制线条

使用pygame.draw.line()方法,该方法,需要传递起始点和终点,还有线条的颜色和宽度


#绘制线条

color  =  255,255,0

width  =  8

pygame.draw.line(screen,  color,  (100,100),  (500,400),  width)

image

##5.绘制弧形。

弧形是圆的一部分,可以使用pygame.draw.arc方法来绘制它,由于这个形状比较复杂,所以它比前几个函数需要跟更多的参数。

首先,需要一个矩形来表示弧形的边界。(需提供矩形左上角的位置,宽度和高度。)弧形就绘制在这个矩形当中。

然后需要提供弧形的起始角度和结束角度。平时在生活中我们一般都是用度为单位来衡量一个角度,但是在几何三角学中,通常使用的是弧度单位。

将角度转化为弧度需要的是math.radians()方法,它包含在math库中,因此使用之前一定不要忘了先引入math库.


1  import math

2  import pygame

3  from pygame.locals import *

4  pygame.init()

5  screen  =  pygame.display.set_mode((600,500))

6  pygame.display.set_caption("Drawing Arcs")

7

8  while  True:

9 for  event in  pygame.event.get():

10 if  event.type in  (QUIT,  KEYDOWN):

11 pygame.quit()

12 sys.exit()

13

14 screen.fill((0,0,200))

15    

16 #绘制弧形的代码

17 color  =  255,0,255

18 position  =  200,150,200,200

19 start_angle  =  math.radians(0)

20 end_angle  =  math.radians(180)

21 width  =  8

22 pygame.draw.arc(screen,  color,  position,  start_angle,  end_angle,  width)

23    

24 pygame.display.update()

25

image

最后我们通过一个非常简单的小实例来巩固和复习一下上面所学到的知识。

#三、画大饼游戏。

当玩家按下1、2、3、4相应的按键时,就会在程序中绘制相应的饼块,当整个饼块都被绘制完成的时候,颜色会变为亮绿色。


1  import math

2  import pygame

3  from pygame.locals import *

4  pygame.init()

5  screen  =  pygame.display.set_mode((600,500))

6  pygame.display.set_caption("The Pie Game - Press 1,2,3,4")

7  myfont  =  pygame.font.Font(None,  60)

8

9  color  =  200,  80,  60

10  width  =  4

11  x  =  300

12  y  =  250

13  radius  =  200

14  position  =  x-radius,  y-radius,  radius*2,  radius*2

15

16  piece1  =  False

17  piece2  =  False

18  piece3  =  False

19  piece4  =  False

20

21  while  True:

22 for  event in  pygame.event.get():

23 if  event.type  ==  QUIT:

24 exit()

25 elif event.type  ==  KEYUP:

26 if  event.key  ==  pygame.K_ESCAPE:

27 sys.exit()

28 elif event.key  ==  pygame.K_1:

29 piece1  =  True

30 elif event.key  ==  pygame.K_2:

31 piece2  =  True

32 elif event.key  ==  pygame.K_3:

33 piece3  =  True

34 elif event.key  ==  pygame.K_4:

35 piece4  =  True

36                

37 #清屏

38 screen.fill((0,0,200))

39    

40 #绘制4个数字

41 textImg1  =  myfont.render("1",  True,  color)

42 screen.blit(textImg1,  (x+radius/2-20,  y-radius/2))

43 textImg2  =  myfont.render("2",  True,  color)

44 screen.blit(textImg2,  (x-radius/2,  y-radius/2))

45 textImg3  =  myfont.render("3",  True,  color)

46 screen.blit(textImg3,  (x-radius/2,  y+radius/2-20))

47 textImg4  =  myfont.render("4",  True,  color)

48 screen.blit(textImg4,  (x+radius/2-20,  y+radius/2-20))

49

50

51 #判断是否绘制饼

52 if  piece1:

53 start_angle  =  math.radians(0)

54 end_angle  =  math.radians(90)

55 pygame.draw.arc(screen,  color,  position,  start_angle,  end_angle,  width)

56 pygame.draw.line(screen,  color,  (x,y),  (x,y-radius),  width)

57 pygame.draw.line(screen,  color,  (x,y),  (x+radius,y),  width)

58 if  piece2:

59 start_angle  =  math.radians(90)

60 end_angle  =  math.radians(180)

61 pygame.draw.arc(screen,  color,  position,  start_angle,  end_angle,  width)

62 pygame.draw.line(screen,  color,  (x,y),  (x,y-radius),  width)

63 pygame.draw.line(screen,  color,  (x,y),  (x-radius,y),  width)

64 if  piece3:

65 start_angle  =  math.radians(180)

66 end_angle  =  math.radians(270)

67 pygame.draw.arc(screen,  color,  position,  start_angle,  end_angle,  width)

68 pygame.draw.line(screen,  color,  (x,y),  (x-radius,y),  width)

69 pygame.draw.line(screen,  color,  (x,y),  (x,y+radius),  width)

70 if  piece4:

71 start_angle  =  math.radians(270)

72 end_angle  =  math.radians(360)

73 pygame.draw.arc(screen,  color,  position,  start_angle,  end_angle,  width)

74 pygame.draw.line(screen,  color,  (x,y),  (x,y+radius),  width)

75 pygame.draw.line(screen,  color,  (x,y),  (x+radius,y),  width)

76        

77 #是否4个饼都被绘制完成

78 if  piece1 and  piece2 and  piece3 and  piece4:

79 color  =  0,255,0

80

81 pygame.display.update()

现在我们已经了解了一些pygame的基本操作,Pygame入门——超级玛丽界面搭建

作为一个开发者,有一个学习的氛围跟一个交流圈子特别重要,这是一个我的Python交流群:795225532,不管你是小白还是大牛欢迎入驻,分享Python零基础入门全套资料、面试经验,讨论技术, 大家一起交流学习成长!希望帮助开发者少走弯路。

猜你喜欢

转载自blog.csdn.net/weixin_43649691/article/details/88869832