pygame实现俄罗斯方块游戏(基础篇1)
本文实例为大家分享了pygame实现俄罗斯方块游戏的具体代码,基础的第一篇,供大家参考,具体内容如下
一、初始界面
之前的游戏都比较简单,所以代码都是面向过程的写法,这次游戏后面可能会写比较复杂(比如人机对战、联机对战、使用道具对战等),这次面向对象一点来写这个项目。
游戏的窗口设计一个专门的Panel类便于负责单个游戏窗口的管理控制。
游戏主窗口按每个方块30像素,那么宽3010=300,高是3020=600
#-*-coding=utf-8-*- importrandom importpygame classPanel(object):#用于绘制整个游戏窗口的版面 def__init__(self,bg,position): self._bg=bg; self._x,self._y,self._width,self._height=position self._bgcolor=[0,0,0] defpaint(self): mid_x=self._x+self._width/2 pygame.draw.line(self._bg,self._bgcolor,[mid_x,self._y],[mid_x,self._y+self._height],self._width) defrun(): pygame.init() space=40 main_panel_width=300 main_panel_height=main_panel_width*2 screencaption=pygame.display.set_caption('Tetris') screen=pygame.display.set_mode((main_panel_width+160+space*3,main_panel_height+space*2))#设置窗口长宽 main_panel=Panel(screen,[space,space,main_panel_width,main_panel_height]) whileTrue: foreventinpygame.event.get(): ifevent.type==pygame.QUIT: pygame.quit() exit() screen.fill((100,100,100))#将界面设置为灰色 main_panel.paint()#主面盘绘制 pygame.display.update()#必须调用update才能看到绘图显示 run()
效果图
二、方块管理
这里首先想到方块不同种类的可以使用工厂模式,所以先定义一个基类的Block,然后不同种类的方块分别继承自这个Block类,分别有这样七种方块
classBlock(object): def__init__(self): self.rect_arr=[] defget_rect_arr(self):#用于获取方块种的四个矩形列表 returnself.rect_arr defmove(self,xdiff,ydiff):#用于移动方块的方法 self.new_rect_arr=[] forx,yinself.rect_arr: self.new_rect_arr.append((x+xdiff,y+ydiff)) self.rect_arr=self.new_rect_arr classLongBlock(Block): def__init__(self,n=None):#两种形态 super(LongBlock,self).__init__() ifnisNone:n=random.randint(0,1) self.rect_arr=[(1,0),(1,1),(1,2),(1,3)]ifn==0else[(0,2),(1,2),(2,2),(3,2)] classSquareBlock(Block):#一种形态 def__init__(self,n=None): super(SquareBlock,self).__init__() self.rect_arr=[(1,1),(1,2),(2,1),(2,2)] classZBlock(Block):#两种形态 def__init__(self,n=None): super(ZBlock,self).__init__() ifnisNone:n=random.randint(0,1) self.rect_arr=[(2,0),(2,1),(1,1),(1,2)]ifn==0else[(0,1),(1,1),(1,2),(2,2)] classSBlock(Block):#两种形态 def__init__(self,n=None): super(SBlock,self).__init__() ifnisNone:n=random.randint(0,1) self.rect_arr=[(1,0),(1,1),(2,1),(2,2)]ifn==0else[(0,2),(1,2),(1,1),(2,1)] classLBlock(Block):#四种形态 def__init__(self,n=None): super(LBlock,self).__init__() ifnisNone:n=random.randint(0,3) ifn==0:self.rect_arr=[(1,0),(1,1),(1,2),(2,2)] elifn==1:self.rect_arr=[(0,1),(1,1),(2,1),(0,2)] elifn==2:self.rect_arr=[(0,0),(1,0),(1,1),(1,2)] else:self.rect_arr=[(0,1),(1,1),(2,1),(2,0)] classJBlock(Block):#四种形态 def__init__(self,n=None): super(JBlock,self).__init__() ifnisNone:n=random.randint(0,3) ifn==0:self.rect_arr=[(1,0),(1,1),(1,2),(0,2)] elifn==1:self.rect_arr=[(0,1),(1,1),(2,1),(0,0)] elifn==2:self.rect_arr=[(2,0),(1,0),(1,1),(1,2)] else:self.rect_arr=[(0,1),(1,1),(2,1),(2,2)] classTBlock(Block):#四种形态 def__init__(self,n=None): super(TBlock,self).__init__() ifnisNone:n=random.randint(0,3) ifn==0:self.rect_arr=[(0,1),(1,1),(2,1),(1,2)] elifn==1:self.rect_arr=[(1,0),(1,1),(1,2),(0,1)] elifn==2:self.rect_arr=[(0,1),(1,1),(2,1),(1,0)] else:self.rect_arr=[(1,0),(1,1),(1,2),(2,1)]
三、创建方块和方块落下
定义一个创建方块的函数
defcreate_block(): n=random.randint(0,19) ifn==0:returnSquareBlock(n=0) elifn==1orn==2:returnLongBlock(n=n-1) elifn==3orn==4:returnZBlock(n=n-3) elifn==5orn==6:returnSBlock(n=n-5) elifn>=7andn<=10:returnLBlock(n=n-7) elifn>=11andn<=14:returnJBlock(n=n-11) else:returnTBlock(n=n-15)
给Panel类加一下当前移动方块的属性,并且修改它的paint方法,将移动方块绘制
classPanel(object):#用于绘制整个游戏窗口的版面 moving_block=None#正在落下的方块 def__init__(self,bg,block_size,position): self._bg=bg; self._x,self._y,self._width,self._height=position self._block_size=block_size self._bgcolor=[0,0,0] defcreate_move_block(self): block=create_block() block.move(5-2,-2)#方块挪到中间 self.moving_block=block defmove_block(self): self.moving_block.move(0,1) defpaint(self): mid_x=self._x+self._width/2 pygame.draw.line(self._bg,self._bgcolor,[mid_x,self._y],[mid_x,self._y+self._height],self._width)#用一个粗线段来填充背景 #绘制正在落下的方块 ifself.move_block: forrectinself.moving_block.get_rect_arr(): x,y=rect pygame.draw.line(self._bg,[0,0,255],[self._x+x*bz+bz/2,self._y+y*bz],[self._x+x*bz+bz/2,self._y+(y+1)*bz],bz) pygame.draw.rect(self._bg,[255,255,255],[self._x+x*bz,self._y+y*bz,bz,bz],1)
主循环中创建方块并将方块调整到下落的起始位置
main_panel.create_move_block()
设定位置刷新时间
diff_ticks=300#移动一次蛇头的事件,单位毫秒 ticks=pygame.time.get_ticks()+diff_ticks
在主循环中刷新当前移动方块的位置
ifpygame.time.get_ticks()>=ticks: ticks+=diff_ticks main_panel.move_block()
当前可以看到方块下落的效果了
四、方块落地的判断
在Block类里增加一个移动判断函数,下面这个这个can_move函数可以判断方块是不是落到底部了
defcan_move(self,xdiff,ydiff): forx,yinself.rect_arr: ify+ydiff>=20:returnFalse returnTrue
修改Panel的move函数,改为
defmove_block(self): ifself.moving_blockisNone:create_move_block() ifself.moving_block.can_move(0,1): self.moving_block.move(0,1) else: self.add_block(self.moving_block) self.create_move_block()
这里增加了一个add_block函数,用于将已经落地的方块存起来,所以Panel另外做了三处改动
1.增加一个存已落下方块的数组变量
rect_arr=[]#已经落底下的方块
2.定义add_block函数
defadd_block(self,block): forrectinblock.get_rect_arr(): self.rect_arr.append(rect)
3.在paint里进行self.rect_arr的绘制
#绘制已经落底下的方块 bz=self._block_size forrectinself.rect_arr: x,y=rect pygame.draw.line(self._bg,[0,0,255],[self._x+x*bz+bz/2,self._y+y*bz],[self._x+x*bz+bz/2,self._y+(y+1)*bz],bz) pygame.draw.rect(self._bg,[255,255,255],[self._x+x*bz,self._y+y*bz,bz,bz],1)
现在可以看到方块会落到底部,然后新的方块落下了
贴下目前的完整程序
#-*-coding=utf-8-*- importrandom importpygame classPanel(object):#用于绘制整个游戏窗口的版面 rect_arr=[]#已经落底下的方块 moving_block=None#正在落下的方块 def__init__(self,bg,block_size,position): self._bg=bg; self._x,self._y,self._width,self._height=position self._block_size=block_size self._bgcolor=[0,0,0] defadd_block(self,block): forrectinblock.get_rect_arr(): self.rect_arr.append(rect) defcreate_move_block(self): block=create_block() block.move(5-2,-2)#方块挪到中间 self.moving_block=block defmove_block(self): ifself.moving_blockisNone:create_move_block() ifself.moving_block.can_move(0,1): self.moving_block.move(0,1) else: self.add_block(self.moving_block) self.create_move_block() defpaint(self): mid_x=self._x+self._width/2 pygame.draw.line(self._bg,self._bgcolor,[mid_x,self._y],[mid_x,self._y+self._height],self._width)#用一个粗线段来填充背景 #绘制已经落底下的方块 bz=self._block_size forrectinself.rect_arr: x,y=rect pygame.draw.line(self._bg,[0,0,255],[self._x+x*bz+bz/2,self._y+y*bz],[self._x+x*bz+bz/2,self._y+(y+1)*bz],bz) pygame.draw.rect(self._bg,[255,255,255],[self._x+x*bz,self._y+y*bz,bz,bz],1) #绘制正在落下的方块 ifself.move_block: forrectinself.moving_block.get_rect_arr(): x,y=rect pygame.draw.line(self._bg,[0,0,255],[self._x+x*bz+bz/2,self._y+y*bz],[self._x+x*bz+bz/2,self._y+(y+1)*bz],bz) pygame.draw.rect(self._bg,[255,255,255],[self._x+x*bz,self._y+y*bz,bz,bz],1) classBlock(object): def__init__(self): self.rect_arr=[] defget_rect_arr(self):#用于获取方块种的四个矩形列表 returnself.rect_arr defmove(self,xdiff,ydiff):#用于移动方块的方法 self.new_rect_arr=[] forx,yinself.rect_arr: self.new_rect_arr.append((x+xdiff,y+ydiff)) self.rect_arr=self.new_rect_arr defcan_move(self,xdiff,ydiff): forx,yinself.rect_arr: ify+ydiff>=20:returnFalse returnTrue classLongBlock(Block): def__init__(self,n=None):#两种形态 super(LongBlock,self).__init__() ifnisNone:n=random.randint(0,1) self.rect_arr=[(1,0),(1,1),(1,2),(1,3)]ifn==0else[(0,2),(1,2),(2,2),(3,2)] classSquareBlock(Block):#一种形态 def__init__(self,n=None): super(SquareBlock,self).__init__() self.rect_arr=[(1,1),(1,2),(2,1),(2,2)] classZBlock(Block):#两种形态 def__init__(self,n=None): super(ZBlock,self).__init__() ifnisNone:n=random.randint(0,1) self.rect_arr=[(2,0),(2,1),(1,1),(1,2)]ifn==0else[(0,1),(1,1),(1,2),(2,2)] classSBlock(Block):#两种形态 def__init__(self,n=None): super(SBlock,self).__init__() ifnisNone:n=random.randint(0,1) self.rect_arr=[(1,0),(1,1),(2,1),(2,2)]ifn==0else[(0,2),(1,2),(1,1),(2,1)] classLBlock(Block):#四种形态 def__init__(self,n=None): super(LBlock,self).__init__() ifnisNone:n=random.randint(0,3) ifn==0:self.rect_arr=[(1,0),(1,1),(1,2),(2,2)] elifn==1:self.rect_arr=[(0,1),(1,1),(2,1),(0,2)] elifn==2:self.rect_arr=[(0,0),(1,0),(1,1),(1,2)] else:self.rect_arr=[(0,1),(1,1),(2,1),(2,0)] classJBlock(Block):#四种形态 def__init__(self,n=None): super(JBlock,self).__init__() ifnisNone:n=random.randint(0,3) ifn==0:self.rect_arr=[(1,0),(1,1),(1,2),(0,2)] elifn==1:self.rect_arr=[(0,1),(1,1),(2,1),(0,0)] elifn==2:self.rect_arr=[(2,0),(1,0),(1,1),(1,2)] else:self.rect_arr=[(0,1),(1,1),(2,1),(2,2)] classTBlock(Block):#四种形态 def__init__(self,n=None): super(TBlock,self).__init__() ifnisNone:n=random.randint(0,3) ifn==0:self.rect_arr=[(0,1),(1,1),(2,1),(1,2)] elifn==1:self.rect_arr=[(1,0),(1,1),(1,2),(0,1)] elifn==2:self.rect_arr=[(0,1),(1,1),(2,1),(1,0)] else:self.rect_arr=[(1,0),(1,1),(1,2),(2,1)] defcreate_block(): n=random.randint(0,19) ifn==0:returnSquareBlock(n=0) elifn==1orn==2:returnLongBlock(n=n-1) elifn==3orn==4:returnZBlock(n=n-3) elifn==5orn==6:returnSBlock(n=n-5) elifn>=7andn<=10:returnLBlock(n=n-7) elifn>=11andn<=14:returnJBlock(n=n-11) else:returnTBlock(n=n-15) defrun(): pygame.init() space=30 main_block_size=30 main_panel_width=main_block_size*10 main_panel_height=main_block_size*20 screencaption=pygame.display.set_caption('Tetris') screen=pygame.display.set_mode((main_panel_width+160+space*3,main_panel_height+space*2))#设置窗口长宽 main_panel=Panel(screen,main_block_size,[space,space,main_panel_width,main_panel_height]) main_panel.create_move_block() diff_ticks=300#移动一次蛇头的事件,单位毫秒 ticks=pygame.time.get_ticks()+diff_ticks whileTrue: foreventinpygame.event.get(): ifevent.type==pygame.QUIT: pygame.quit() exit() screen.fill((100,100,100))#将界面设置为灰色 main_panel.paint()#主面盘绘制 pygame.display.update()#必须调用update才能看到绘图显示 ifpygame.time.get_ticks()>=ticks: ticks+=diff_ticks main_panel.move_block() run()
这章先写到这,下章继续
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。