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()

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()

4.繪製線條

使用pygame.draw.line()方法,該方法,需要傳遞起始點和終點,還有線條的顏色和寬度


#繪製線條

color  =  255,255,0

width  =  8

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

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

最後我們通過一個非常簡單的小實例來鞏固和複習一下上面所學到的知識。

三、畫大餅遊戲。

當玩家按下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入門——超級瑪麗界面搭建

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章