學了這么長時間的Pygame,一直想寫個游戲?qū)崙?zhàn)一下??雌饋砗芎唵蔚挠螒?,寫其來怎么這么難。最初想寫個俄羅斯方塊,想了很長時間如何實現(xiàn),想來想去,也沒寫出來,于是干脆下載別人的代碼來讀。后來,要想寫一個幫助記憶的挖寶箱的游戲,結(jié)果也沒完成。唯一完成了就是下面這個小人接金幣的游戲,超級簡單,通過左右鍵控制小人移動去接空中下來的金幣,接住金幣得5分,接不住游戲結(jié)束,金幣速度會隨著level的關(guān)數(shù)而越來越快。完成這段代碼后,我依然覺得這段代碼寫得很差,確實也是自己對pygame只是掌握了皮毛,對surface、sprite這些理解的還不透徹。這里把代碼寫出來,有時間的大牛們可以幫助指點一下,讓我也有所提高。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
# -*- coding: cp936 -*- ''' 一個超級簡單的游戲 左右鍵控制小人移動去接空中下來的金幣,接住金幣得5分,接不住游戲結(jié)束,金幣速度會隨著level的關(guān)數(shù) 而越來越快 ''' import pygame,sys,os,random pygame.init() class rect(): #畫出小人 def __init__( self ,filename,initial_position): self .image = pygame.image.load(filename) self .rect = self .image.get_rect() self .rect.topleft = initial_position class goldrect(pygame.sprite.Sprite): #繪出金幣 def __init__( self ,gold_position,speed): pygame.sprite.Sprite.__init__( self ) self .image = pygame.image.load( 'image\gold.png' ) self .rect = self .image.get_rect() self .rect.topleft = gold_position self .speed = speed def move( self ): self .rect = self .rect.move( self .speed) def drawback(): #繪出背景圖片 my_back = pygame.image.load( 'image\qi3.jpg' ) bakscreen.blit(my_back,[ 0 , 0 ]) def loadtext(levelnum,score,highscore): #繪出成績、level、最高分等 my_font = pygame.font.SysFont( None , 24 ) levelstr = 'Level:' + str (levelnum) text_screen = my_font.render(levelstr, True , ( 255 , 0 , 0 )) bakscreen.blit(text_screen, ( 650 , 50 )) highscorestr = 'Higescore:' + str (highscore) text_screen = my_font.render(highscorestr, True , ( 255 , 0 , 0 )) bakscreen.blit(text_screen, ( 650 , 80 )) scorestr = 'Score:' + str (score) text_screen = my_font.render(scorestr, True , ( 255 , 0 , 0 )) bakscreen.blit(text_screen, ( 650 , 110 )) def loadgameover(scorenum,highscore): #繪出GAME OVER my_font = pygame.font.SysFont( None , 50 ) levelstr = 'GAME OVER' over_screen = my_font.render(levelstr, True , ( 255 , 0 , 0 )) bakscreen.blit(over_screen, ( 300 , 240 )) highscorestr = 'YOUR SCORE IS ' + str (scorenum) over_screen = my_font.render(highscorestr, True , ( 255 , 0 , 0 )) bakscreen.blit(over_screen, ( 280 , 290 )) if scorenum> int (highscore): #寫入最高分 highscorestr = 'YOUR HAVE GOT THE HIGHEST SCORE!' text_screen = my_font.render(highscorestr, True , ( 255 , 0 , 0 )) bakscreen.blit(text_screen, ( 100 , 340 )) highfile = open ( 'highscore' , 'w' ) highfile.writelines( str (scorenum)) highfile.close() def gethighscore(): #讀取最高分 if os.path.isfile( 'highscore' ): highfile = open ( 'highscore' , 'r' ) highscore = highfile.readline() highfile.close() else : highscore = 0 return highscore bakscreen = pygame.display.set_mode([ 800 , 600 ]) bakscreen.fill([ 0 , 160 , 233 ]) pygame.display.set_caption( 'Dig!Dig!' ) drawback() levelnum = 1 #level scorenum = 0 #得分 highscore = gethighscore() #最高分 ileft = 1 #記錄向左移動步數(shù),用來控制圖片 iright = 10 #記錄向右移動步數(shù),用來控制圖片 x = 100 y = 450 filename = 'image\1.png' backimg_ren = rect(filename,[x,y]) bakscreen.blit(backimg_ren.image,backimg_ren.rect) loadtext(levelnum,scorenum,highscore) goldx = random.randint( 50 , 580 ) speed = [ 0 ,levelnum] mygold = goldrect([goldx, 100 ],speed) pygame.display.update() while True : if scorenum> 0 and scorenum / 50.0 = = int (scorenum / 50.0 ): #當?shù)梅质?0的倍數(shù)時修改level levelnum = scorenum / 50 + 1 speed = [ 0 ,levelnum] for event in pygame.event.get(): if event. type = = pygame.QUIT: sys.exit() #make gold pressed_keys = pygame.key.get_pressed() if pressed_keys[pygame.K_LEFT]: #按下左鍵 drawback() loadtext(levelnum,scorenum,highscore) if iright > 14 :iright = 10 iright = iright + 1 filename = 'image\'+str(iright)+' .png' if x< 50 : x = 50 else : x = x - 10 backimg_surface = rect(filename,[x,y]) bakscreen.blit(backimg_surface.image,backimg_surface.rect) if pressed_keys[pygame.K_RIGHT]: #按下右鍵 drawback() loadtext(levelnum,scorenum,highscore) if ileft > 4 :ileft = 0 ileft = ileft + 1 filename = 'image\'+str(ileft)+' .png' if x> 560 : x = 560 else : x = x + 10 backimg_surface = rect(filename,[x,y]) bakscreen.blit(backimg_surface.image,backimg_surface.rect) drawback() loadtext(levelnum,scorenum,highscore) mygold.move() bakscreen.blit(mygold.image,mygold.rect) backimg_surface = rect(filename,[x,y]) bakscreen.blit(backimg_surface.image,backimg_surface.rect) if mygold.rect.top> 600 : #判斷金幣是否著地,一但著地,游戲結(jié)束 loadgameover(scorenum,highscore) if mygold.rect.colliderect(backimg_surface.rect): #判斷金幣是否與小人碰撞,如果碰撞表示小人接到金幣 scorenum + = 5 loadtext(levelnum,scorenum,highscore) goldx = random.randint( 50 , 580 ) mygold = goldrect([goldx, 100 ],speed) pygame.display.update() |
程序中用到的資源可從這里下載:文件名:gold.7z, 訪問地址:http://www.kuaipan.cn/file/id_16699292408348719.htm