导语
想起来好久没更这个系列的文章了,周末过来补一波好了。本期我们将利用python制作一个打砖块小游戏,废话不多说,让我们愉快地开始吧~
开发工具
Python版本:3.6.4
相关模块:
pygame模块;
以及一些Python自带的模块。
环境搭建
安装Python并添加到环境变量,pip安装需要的相关模块即可。
效果展示
在cmd窗口运行Game18.py文件即可。
效果如下:
视频链接
原理简介
游戏规则(摘自维基百科):
打砖块是一种电子游戏。屏幕上部有若干层砖块,一个球在屏幕上方的砖块和墙壁、屏幕下方的移动短板和两侧墙壁之间来回弹,当球碰到砖块时,球会反弹,而砖块会消失。玩家要控制萤幕下方的板子,让“球”通过撞击消去所有的“砖块”,球碰到萤幕底边就会消失,所有的球消失则游戏失败。把砖块全部消去就可以破关。
板子操作方式:按“→”向右,按“←”向左。
逐步实现:
游戏实现起来其实非常简单。首先,我们根据游戏规则定义3个游戏精灵类,分别是:
- 板子类;
- 球类;
- 砖块类。
先定义游戏精灵的好处是方便后续游戏精灵类之间的碰撞检测以及游戏精灵的操作管理等。具体而言,对于板子类,其应当具有根据玩家操作而移动等功能,其代码实现如下:
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
|
'''板子''' class Paddle(pygame.sprite.Sprite): def __init__( self , x, y, width, height, SCREENWIDTH, SCREENHEIGHT, * * kwargs): pygame.sprite.Sprite.__init__( self ) self .init_state = [x, y, width, height] self .rect = pygame.Rect(x, y, width, height) self .base_speed = 10 self .SCREENWIDTH = SCREENWIDTH self .SCREENHEIGHT = SCREENHEIGHT '''移动板子''' def move( self , direction): if direction = = 'left' : self .rect.left = max ( 0 , self .rect.left - self .base_speed) elif direction = = 'right' : self .rect.right = min ( self .SCREENWIDTH, self .rect.right + self .base_speed) else : raise ValueError( 'Paddle.move.direction unsupport <%s>...' % direction) return True '''绑定到屏幕上''' def draw( self , screen, color): pygame.draw.rect(screen, color, self .rect) return True '''重置''' def reset( self ): self .rect = pygame.Rect( self .init_state[ 0 ], self .init_state[ 1 ], self .init_state[ 2 ], self .init_state[ 3 ]) return True |
对于球类,则由电脑控制其移动方式(比如撞到墙自动换方向等),其代码实现如下:
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
|
'''球''' class Ball(pygame.sprite.Sprite): def __init__( self , x, y, radius, SCREENWIDTH, SCREENHEIGHT, * * kwargs): pygame.sprite.Sprite.__init__( self ) self .init_state = [x, y, radius * 2 , radius * 2 ] self .rect = pygame.Rect(x, y, radius * 2 , radius * 2 ) self .base_speed = [ 5 , 5 ] self .direction = [random.choice([ 1 , - 1 ]), - 1 ] self .radius = radius self .SCREENWIDTH = SCREENWIDTH self .SCREENHEIGHT = SCREENHEIGHT '''移动球''' def move( self ): self .rect.left + = self .direction[ 0 ] * self .base_speed[ 0 ] self .rect.top + = self .direction[ 1 ] * self .base_speed[ 1 ] if self .rect.left < = 0 : self .rect.left = 0 self .direction[ 0 ] = - self .direction[ 0 ] elif self .rect.right > = self .SCREENWIDTH: self .rect.right = self .SCREENWIDTH self .direction[ 0 ] = - self .direction[ 0 ] if self .rect.top < = 0 : self .rect.top = 0 self .direction[ 1 ] = - self .direction[ 1 ] elif self .rect.bottom > = self .SCREENHEIGHT: return False return True '''改变运动速度和方向(与拍相撞时)''' def change( self ): self .base_speed = [random.choice([ 4 , 5 , 6 ]), random.choice([ 4 , 5 , 6 ])] self .direction = [random.choice([ 1 , - 1 ]), - 1 ] return True '''绑定到屏幕上''' def draw( self , screen, color): pygame.draw.circle(screen, color, ( self .rect.left + self .radius, self .rect.top + self .radius), self .radius) return True '''重置''' def reset( self ): self .rect = pygame.Rect( self .init_state[ 0 ], self .init_state[ 1 ], self .init_state[ 2 ], self .init_state[ 3 ]) return True |
对于砖块类,则比较简单,其代码实现如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
'''砖块''' class Brick(pygame.sprite.Sprite): def __init__( self , x, y, width, height, * * kwargs): pygame.sprite.Sprite.__init__( self ) self .init_state = [x, y, width, height] self .rect = pygame.Rect(x, y, width, height) '''绑定到屏幕上''' def draw( self , screen, color): pygame.draw.rect(screen, color, self .rect) return True '''重置''' def reset( self ): self .rect = pygame.Rect( self .init_state[ 0 ], self .init_state[ 1 ], self .init_state[ 2 ], self .init_state[ 3 ]) return True |
然后和以前一样,多弄几个关卡,每个关卡地图都用一个.level文件来设计定义,例如这样:
其中B代表砖块所在的位置。
OK,接下来可以考虑实现游戏主循环了。基本的逻辑就是:
即每关结束判断一下是过关了还是GG了,过关了就进入下一关,否则就直接进入结束界面。当然最后一关是个例外,因为结束之后肯定要进入结束界面。具体而言,主逻辑代码实现如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
def run( self ): while True : self .__startInterface() for idx, levelpath in enumerate ( self .cfg.LEVELPATHS): state = self .__runLevel(levelpath) if idx = = len ( self .cfg.LEVELPATHS) - 1 : break if state = = 'win' : self .__nextLevel() else : break if state = = 'fail' : self .__endInterface( False ) else : self .__endInterface( True ) |
到此这篇关于Python Pygame实战之打砖块游戏的实现的文章就介绍到这了,更多相关Python Pygame打砖块内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://mp.weixin.qq.com/s/9tNVTA06dFthdugNs3TePA