基于Pierre Dellacherie算法实现俄罗斯方块的人工智能(python实现)《二》
本文章将会说明,把俄罗斯方块从面向过程到面向对象的改进,以及个性化功能的添加
个性化功能包括:
面对对象的设计我主要分为三个类:Wall
,HouseWorker
,和Brick
下面一个一个类来说明
1. Wall类
有些简单的功能我只提一提函数名,需要讲解的话请评论
class Wall () :
global GRID_NUM_WIDTH
global LINE_COLOR
global HEIGHT
global HEIGHT
def __init__ (self) :
pass
def draw_grids (self) :
pass
def draw_matrix (self) :
pass
def remove_full_line (self) :
pass
def show_welcome (self) :
pass
展示文字,上一章说到这个函数我修改了 ,是因为我有时候也想显示文字的背景色
def show_text (self, text, size, x, y, color=(0xff , 0xff , 0xff ) ,bgColor = None) :
fontObj = pygame.font.Font('font/font.ttc' , size)
textSurfaceObj = fontObj.render(text, True , color,bgColor)
textRectObj = textSurfaceObj.get_rect()
textRectObj.center = (x, y)
screen.blit(textSurfaceObj, textRectObj)
写分
新增了个性化功能:最高分 ,我用最简单的方法实现,直接把最高分写进一个txt文件,开始游戏的时候读取即可
def draw_score (self) :
global heightScore
self.show_text( u'得分 : {}' .format(score), 18 , WIDTH + SIDE_WIDTH // 2 , 180 )
if heightScore is None :
self.getHeightScore()
if score>heightScore:
heightScore = score
self.show_text( u'level : {} 最高分 : {}' .format(level,heightScore), 15 , WIDTH + SIDE_WIDTH // 2 , 205 )
def showPause (self) :
GREEN = ( 0 , 255 , 0 )
BLUE = ( 0 , 0 , 128 )
self.show_text(u'暂停中...' , 50 , 250 , 200 , BLUE , GREEN)
pygame.display.update()
def drawAll (self) :
screen.fill(BLACK)
self.draw_grids()
self.draw_matrix()
self.draw_score()
self.drawNextBrick()
self.show_text( u'再来一次:R' ,13 , WIDTH+100 ,HEIGHT-140 , WHITE )
self.show_text( u'我要暂停:P' ,13 , WIDTH+100 ,HEIGHT-120 , WHITE )
self.show_text( u'我要变形:↑' ,13 , WIDTH+100 ,HEIGHT-100 , WHITE )
self.show_text( u'左右移动: ← or →' ,13 , WIDTH+100 ,HEIGHT-