這篇文章給大家介紹pygame實(shí)現(xiàn)俄羅斯方塊游戲,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
創(chuàng)新互聯(lián)建站專注于企業(yè)全網(wǎng)整合營(yíng)銷推廣、網(wǎng)站重做改版、桓仁網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5頁(yè)面制作、商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)公司、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為桓仁等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。在Panel類里增加函數(shù)
def check_overlap(self, diffx, diffy): for x,y in self.moving_block.get_rect_arr(): for rx,ry in self.rect_arr: if x+diffx==rx and y+diffy==ry: return True return False
修改move_block函數(shù)的判斷,增加check_overlap函數(shù)檢測(cè)
def move_block(self): if self.moving_block is None: create_move_block() if self.moving_block.can_move(0,1) and not self.check_overlap(0,1): self.moving_block.move(0,1) else: self.add_block(self.moving_block) self.create_move_block()
現(xiàn)在的效果是方塊可以堆疊了
六、鍵盤控制左右移動(dòng)
導(dǎo)入變量
from pygame.locals import KEYDOWN,K_LEFT,K_RIGHT,K_UP,K_DOWN
Panel類里增加一個(gè)控制移動(dòng)方塊的函數(shù)
def control_block(self, diffx, diffy): if self.moving_block.can_move(diffx,diffy) and not self.check_overlap(diffx, diffy): self.moving_block.move(diffx,diffy)
鼠標(biāo)事件監(jiān)聽(tīng)處做下鍵盤的響應(yīng)
if event.type == KEYDOWN: if event.key == K_LEFT: main_panel.control_block(-1,0) if event.key == K_RIGHT: main_panel.control_block(1,0) if event.key == K_UP: pass # 變形過(guò)會(huì)實(shí)現(xiàn) if event.key == K_DOWN: main_panel.control_block(0,1)
由于Block類的can_move函數(shù)沒(méi)有實(shí)現(xiàn)左右移動(dòng)的判斷,所以需要再對(duì)can_move
增加左右邊界的處理
def can_move(self,xdiff,ydiff): for x,y in self.rect_arr: if y+ydiff>=20: return False if x+xdiff<0 or x+xdiff>=10: return False return True
現(xiàn)在,左右的移動(dòng)也正常了,效果圖如下
貼下目前的代碼
# -*- coding=utf-8 -*- import random import pygame from pygame.locals import KEYDOWN,K_LEFT,K_RIGHT,K_UP,K_DOWN class Panel(object): # 用于繪制整個(gè)游戲窗口的版面 rect_arr=[] # 已經(jīng)落底下的方塊 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] def add_block(self,block): for rect in block.get_rect_arr(): self.rect_arr.append(rect) def create_move_block(self): block = create_block() block.move(5-2,-2) # 方塊挪到中間 self.moving_block=block def check_overlap(self, diffx, diffy, check_arr=None): if check_arr is None: check_arr = self.moving_block.get_rect_arr() for x,y in check_arr: for rx,ry in self.rect_arr: if x+diffx==rx and y+diffy==ry: return True return False def control_block(self, diffx, diffy): if self.moving_block.can_move(diffx,diffy) and not self.check_overlap(diffx, diffy): self.moving_block.move(diffx,diffy) def move_block(self): if self.moving_block is None: create_move_block() if self.moving_block.can_move(0,1) and not self.check_overlap(0,1): self.moving_block.move(0,1) else: self.add_block(self.moving_block) self.create_move_block() def paint(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) # 用一個(gè)粗線段來(lái)填充背景 # 繪制已經(jīng)落底下的方塊 bz=self._block_size for rect in self.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+1,bz+1],1) # 繪制正在落下的方塊 if self.move_block: for rect in self.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+1,bz+1],1) class Block(object): def __init__(self): self.rect_arr=[] def get_rect_arr(self): # 用于獲取方塊種的四個(gè)矩形列表 return self.rect_arr def move(self,xdiff,ydiff): # 用于移動(dòng)方塊的方法 self.new_rect_arr=[] for x,y in self.rect_arr: self.new_rect_arr.append((x+xdiff,y+ydiff)) self.rect_arr=self.new_rect_arr def can_move(self,xdiff,ydiff): for x,y in self.rect_arr: if y+ydiff>=20: return False if x+xdiff<0 or x+xdiff>=10: return False return True class LongBlock(Block): def __init__(self, n=None): # 兩種形態(tài) super(LongBlock, self).__init__() if n is None: n=random.randint(0,1) self.rect_arr=[(1,0),(1,1),(1,2),(1,3)] if n==0 else [(0,2),(1,2),(2,2),(3,2)] class SquareBlock(Block): # 一種形態(tài) def __init__(self, n=None): super(SquareBlock, self).__init__() self.rect_arr=[(1,1),(1,2),(2,1),(2,2)] class ZBlock(Block): # 兩種形態(tài) def __init__(self, n=None): super(ZBlock, self).__init__() if n is None: n=random.randint(0,1) self.rect_arr=[(2,0),(2,1),(1,1),(1,2)] if n==0 else [(0,1),(1,1),(1,2),(2,2)] class SBlock(Block): # 兩種形態(tài) def __init__(self, n=None): super(SBlock, self).__init__() if n is None: n=random.randint(0,1) self.rect_arr=[(1,0),(1,1),(2,1),(2,2)] if n==0 else [(0,2),(1,2),(1,1),(2,1)] class LBlock(Block): # 四種形態(tài) def __init__(self, n=None): super(LBlock, self).__init__() if n is None: n=random.randint(0,3) if n==0: self.rect_arr=[(1,0),(1,1),(1,2),(2,2)] elif n==1: self.rect_arr=[(0,1),(1,1),(2,1),(0,2)] elif n==2: self.rect_arr=[(0,0),(1,0),(1,1),(1,2)] else: self.rect_arr=[(0,1),(1,1),(2,1),(2,0)] class JBlock(Block): # 四種形態(tài) def __init__(self, n=None): super(JBlock, self).__init__() if n is None: n=random.randint(0,3) if n==0: self.rect_arr=[(1,0),(1,1),(1,2),(0,2)] elif n==1: self.rect_arr=[(0,1),(1,1),(2,1),(0,0)] elif n==2: self.rect_arr=[(2,0),(1,0),(1,1),(1,2)] else: self.rect_arr=[(0,1),(1,1),(2,1),(2,2)] class TBlock(Block): # 四種形態(tài) def __init__(self, n=None): super(TBlock, self).__init__() if n is None: n=random.randint(0,3) if n==0: self.rect_arr=[(0,1),(1,1),(2,1),(1,2)] elif n==1: self.rect_arr=[(1,0),(1,1),(1,2),(0,1)] elif n==2: self.rect_arr=[(0,1),(1,1),(2,1),(1,0)] else: self.rect_arr=[(1,0),(1,1),(1,2),(2,1)] def create_block(): n = random.randint(0,19) if n==0: return SquareBlock(n=0) elif n==1 or n==2: return LongBlock(n=n-1) elif n==3 or n==4: return ZBlock(n=n-3) elif n==5 or n==6: return SBlock(n=n-5) elif n>=7 and n<=10: return LBlock(n=n-7) elif n>=11 and n<=14: return JBlock(n=n-11) else: return TBlock(n=n-15) def run(): 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)) #設(shè)置窗口長(zhǎng)寬 main_panel=Panel(screen,main_block_size,[space,space,main_panel_width,main_panel_height]) pygame.key.set_repeat(200, 30) main_panel.create_move_block() diff_ticks = 300 # 移動(dòng)一次蛇頭的事件,單位毫秒 ticks = pygame.time.get_ticks() + diff_ticks while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() if event.type == KEYDOWN: if event.key == K_LEFT: main_panel.control_block(-1,0) if event.key == K_RIGHT: main_panel.control_block(1,0) if event.key == K_UP: pass # 變形過(guò)會(huì)實(shí)現(xiàn) if event.key == K_DOWN: main_panel.control_block(0,1) screen.fill((100,100,100)) # 將界面設(shè)置為灰色 main_panel.paint() # 主面盤繪制 pygame.display.update() # 必須調(diào)用update才能看到繪圖顯示 if pygame.time.get_ticks() >= ticks: ticks+=diff_ticks main_panel.move_block() run()
七、控制變形
變形的實(shí)現(xiàn),我們對(duì)每個(gè)方塊子類的初始化函數(shù)稍作修改,將獲取形狀做一個(gè)獨(dú)立的get_shape函數(shù),并且給每個(gè)子類增加一個(gè)變量用于記錄當(dāng)前形態(tài)id,用一個(gè)變量用于標(biāo)識(shí)每種方塊的形態(tài)數(shù)量,以T型為例,修改后代碼如下
class TBlock(Block): # 四種形態(tài) shape_id=0 shape_num=4 def __init__(self, n=None): super(TBlock, self).__init__() if n is None: n=random.randint(0,3) self.shape_id=n self.rect_arr=self.get_shape() def get_shape(self): if self.shape_id==0: return [(0,1),(1,1),(2,1),(1,2)] elif self.shape_id==1: return [(1,0),(1,1),(1,2),(0,1)] elif self.shape_id==2: return [(0,1),(1,1),(2,1),(1,0)] else: return [(1,0),(1,1),(1,2),(2,1)]
這樣我們?cè)贐lock父類里可以加一個(gè)change函數(shù),用于變換至下一形態(tài),由于變化時(shí)要保持原來(lái)的移動(dòng)位置,我們?cè)黾觭x,sy兩個(gè)變量將方塊移動(dòng)過(guò)的位置存著,便于在變化時(shí)使用
class Block(object): sx=0 sy=0 def __init__(self): self.rect_arr=[] def get_rect_arr(self): # 用于獲取方塊種的四個(gè)矩形列表 return self.rect_arr def move(self,xdiff,ydiff): # 用于移動(dòng)方塊的方法 self.sx+=xdiff self.sy+=ydiff self.new_rect_arr=[] for x,y in self.rect_arr: self.new_rect_arr.append((x+xdiff,y+ydiff)) self.rect_arr=self.new_rect_arr def can_move(self,xdiff,ydiff): for x,y in self.rect_arr: if y+ydiff>=20: return False if x+xdiff<0 or x+xdiff>=10: return False return True def change(self): self.shape_id+=1 # 下一形態(tài) if self.shape_id >= self.shape_num: self.shape_id=0 arr = self.get_shape() new_arr = [] for x,y in arr: if x+self.sx<0 or x+self.sx>=10: # 變形不能超出左右邊界 self.shape_id -= 1 if self.shape_id < 0: self.shape_id = self.shape_num - 1 return None new_arr.append([x+self.sx,y+self.sy]) return new_arr
在Panel類里的再增加一個(gè)change函數(shù),直接調(diào)用moving_block的change
def change_block(self): if self.moving_block: new_arr = self.moving_block.change() if new_arr and not self.check_overlap(0, 0, check_arr=new_arr): # 變形不能造成方塊重疊 self.moving_block.rect_arr=new_arr
最后將key_up事件的響應(yīng)加入change_block的調(diào)用就好了
if event.key == K_UP: main_panel.change_block()
現(xiàn)在已經(jīng)實(shí)現(xiàn)了,變形和移動(dòng)了,方塊基本可以正常下落了
八、方塊的消除
這個(gè)計(jì)算主要是處理Panel類的rect_arr,如果數(shù)組中出現(xiàn)某一行有10個(gè)就符合消除條件,為簡(jiǎn)化計(jì)算,我們將這些矩形按y值存到一個(gè)數(shù)組中,便于計(jì)算
def check_clear(self): tmp_arr = [[] for i in range(20)] # 先將方塊按行存入數(shù)組 for x,y in self.rect_arr: if y<0: return tmp_arr[y].append([x,y]) clear_num=0 clear_lines=set([]) y_clear_diff_arr=[[] for i in range(20)] # 從下往上計(jì)算可以消除的行,并記錄消除行后其他行的向下偏移數(shù)量 for y in range(19,-1,-1): if len(tmp_arr[y])==10: clear_lines.add(y) clear_num += 1 y_clear_diff_arr[y] = clear_num if clear_num>0: new_arr=[] # 跳過(guò)移除行,并將其他行做偏移 for y in range(19,-1,-1): if y in clear_lines: continue tmp_row = tmp_arr[y] y_clear_diff=y_clear_diff_arr[y] for x,y in tmp_row: new_arr.append([x,y+y_clear_diff]) self.rect_arr = new_arr
在Panel的move_block處增加check_clear的調(diào)用
def move_block(self): if self.moving_block is None: create_move_block() if self.moving_block.can_move(0,1) and not self.check_overlap(0,1): self.moving_block.move(0,1) else: self.add_block(self.moving_block) self.check_clear() self.create_move_block()
現(xiàn)在游戲可以消除方塊了
九、增加空格鍵使快速落下
快速落下可以快速調(diào)用Panel的move_block函數(shù),我們?cè)趍ove_block函數(shù)增加一個(gè)返回值,用于標(biāo)記使正常下移還是移到底部后新的方塊
def move_block(self): if self.moving_block is None: create_move_block() if self.moving_block.can_move(0,1) and not self.check_overlap(0,1): self.moving_block.move(0,1) return 1 else: self.add_block(self.moving_block) self.check_clear() self.create_move_block() return 2
在鍵盤響應(yīng)處增加鍵盤處理
if event.key == K_SPACE: while main_panel.move_block()==1: pass
十、增加游戲結(jié)束判斷
游戲結(jié)束同樣可以在Panel類的move_block中處理,如果一個(gè)方塊到底,并且消除進(jìn)行后,發(fā)現(xiàn)有方塊的y值小于0,那么一定是失敗了
修改Panel類的move_block函數(shù)
def move_block(self): if self.moving_block is None: create_move_block() if self.moving_block.can_move(0,1) and not self.check_overlap(0,1): self.moving_block.move(0,1) return 1 else: self.add_block(self.moving_block) self.check_clear() for x,y in self.rect_arr: if y<0: return 9 # 游戲失敗 self.create_move_block() return 2
增加一個(gè)變量記錄游戲狀態(tài)
game_state = 1 # 游戲狀態(tài)1.表示正常 2.表示失敗
計(jì)時(shí)器處修改程序
if game_state == 1 and pygame.time.get_ticks() >= ticks: ticks+=diff_ticks if main_panel.move_block()==9: game_state = 2
鼠標(biāo)鍵盤響應(yīng)空格鍵中也增加一下判斷
if event.key == K_SPACE: flag = main_panel.move_block() while flag==1: flag = main_panel.move_block() if flag == 9: game_state = 2
最后增加游戲結(jié)束文字的繪制
if game_state == 2: myfont = pygame.font.Font(None,30) white = 255,255,255 textImage = myfont.render("Game over", True, white) screen.blit(textImage, (160,190))
好了,現(xiàn)在會(huì)提示游戲結(jié)束了
最后附下目前的完整代碼
# -*- coding=utf-8 -*- import random import pygame from pygame.locals import KEYDOWN,K_LEFT,K_RIGHT,K_UP,K_DOWN,K_SPACE class Panel(object): # 用于繪制整個(gè)游戲窗口的版面 rect_arr=[] # 已經(jīng)落底下的方塊 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] def add_block(self,block): for rect in block.get_rect_arr(): self.rect_arr.append(rect) def create_move_block(self): block = create_block() block.move(5-2,-2) # 方塊挪到中間 self.moving_block=block def check_overlap(self, diffx, diffy, check_arr=None): if check_arr is None: check_arr = self.moving_block.get_rect_arr() for x,y in check_arr: for rx,ry in self.rect_arr: if x+diffx==rx and y+diffy==ry: return True return False def control_block(self, diffx, diffy): if self.moving_block.can_move(diffx,diffy) and not self.check_overlap(diffx, diffy): self.moving_block.move(diffx,diffy) def change_block(self): if self.moving_block: new_arr = self.moving_block.change() if new_arr and not self.check_overlap(0, 0, check_arr=new_arr): # 變形不能造成方塊重疊 self.moving_block.rect_arr=new_arr def move_block(self): if self.moving_block is None: create_move_block() if self.moving_block.can_move(0,1) and not self.check_overlap(0,1): self.moving_block.move(0,1) return 1 else: self.add_block(self.moving_block) self.check_clear() for x,y in self.rect_arr: if y<0: return 9 # 游戲失敗 self.create_move_block() return 2 def check_clear(self): tmp_arr = [[] for i in range(20)] # 先將方塊按行存入數(shù)組 for x,y in self.rect_arr: if y<0: return tmp_arr[y].append([x,y]) clear_num=0 clear_lines=set([]) y_clear_diff_arr=[[] for i in range(20)] # 從下往上計(jì)算可以消除的行,并記錄消除行后其他行的向下偏移數(shù)量 for y in range(19,-1,-1): if len(tmp_arr[y])==10: clear_lines.add(y) clear_num += 1 y_clear_diff_arr[y] = clear_num if clear_num>0: new_arr=[] # 跳過(guò)移除行,并將其他行做偏移 for y in range(19,-1,-1): if y in clear_lines: continue tmp_row = tmp_arr[y] y_clear_diff=y_clear_diff_arr[y] for x,y in tmp_row: new_arr.append([x,y+y_clear_diff]) self.rect_arr = new_arr def paint(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) # 用一個(gè)粗線段來(lái)填充背景 # 繪制已經(jīng)落底下的方塊 bz=self._block_size for rect in self.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+1,bz+1],1) # 繪制正在落下的方塊 if self.move_block: for rect in self.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+1,bz+1],1) class Block(object): sx=0 sy=0 def __init__(self): self.rect_arr=[] def get_rect_arr(self): # 用于獲取方塊種的四個(gè)矩形列表 return self.rect_arr def move(self,xdiff,ydiff): # 用于移動(dòng)方塊的方法 self.sx+=xdiff self.sy+=ydiff self.new_rect_arr=[] for x,y in self.rect_arr: self.new_rect_arr.append((x+xdiff,y+ydiff)) self.rect_arr=self.new_rect_arr def can_move(self,xdiff,ydiff): for x,y in self.rect_arr: if y+ydiff>=20: return False if x+xdiff<0 or x+xdiff>=10: return False return True def change(self): self.shape_id+=1 # 下一形態(tài) if self.shape_id >= self.shape_num: self.shape_id=0 arr = self.get_shape() new_arr = [] for x,y in arr: if x+self.sx<0 or x+self.sx>=10: # 變形不能超出左右邊界 self.shape_id -= 1 if self.shape_id < 0: self.shape_id = self.shape_num - 1 return None new_arr.append([x+self.sx,y+self.sy]) return new_arr class LongBlock(Block): shape_id=0 shape_num=2 def __init__(self, n=None): # 兩種形態(tài) super(LongBlock, self).__init__() if n is None: n=random.randint(0,1) self.shape_id=n self.rect_arr=self.get_shape() def get_shape(self): return [(1,0),(1,1),(1,2),(1,3)] if self.shape_id==0 else [(0,2),(1,2),(2,2),(3,2)] class SquareBlock(Block): # 一種形態(tài) shape_id=0 shape_num=1 def __init__(self, n=None): super(SquareBlock, self).__init__() self.rect_arr=self.get_shape() def get_shape(self): return [(1,1),(1,2),(2,1),(2,2)] class ZBlock(Block): # 兩種形態(tài) shape_id=0 shape_num=2 def __init__(self, n=None): super(ZBlock, self).__init__() if n is None: n=random.randint(0,1) self.shape_id=n self.rect_arr=self.get_shape() def get_shape(self): return [(2,0),(2,1),(1,1),(1,2)] if self.shape_id==0 else [(0,1),(1,1),(1,2),(2,2)] class SBlock(Block): # 兩種形態(tài) shape_id=0 shape_num=2 def __init__(self, n=None): super(SBlock, self).__init__() if n is None: n=random.randint(0,1) self.shape_id=n self.rect_arr=self.get_shape() def get_shape(self): return [(1,0),(1,1),(2,1),(2,2)] if self.shape_id==0 else [(0,2),(1,2),(1,1),(2,1)] class LBlock(Block): # 四種形態(tài) shape_id=0 shape_num=4 def __init__(self, n=None): super(LBlock, self).__init__() if n is None: n=random.randint(0,3) self.shape_id=n self.rect_arr=self.get_shape() def get_shape(self): if self.shape_id==0: return [(1,0),(1,1),(1,2),(2,2)] elif self.shape_id==1: return [(0,1),(1,1),(2,1),(0,2)] elif self.shape_id==2: return [(0,0),(1,0),(1,1),(1,2)] else: return [(0,1),(1,1),(2,1),(2,0)] class JBlock(Block): # 四種形態(tài) shape_id=0 shape_num=4 def __init__(self, n=None): super(JBlock, self).__init__() if n is None: n=random.randint(0,3) self.shape_id=n self.rect_arr=self.get_shape() def get_shape(self): if self.shape_id==0: return [(1,0),(1,1),(1,2),(0,2)] elif self.shape_id==1: return [(0,1),(1,1),(2,1),(0,0)] elif self.shape_id==2: return [(2,0),(1,0),(1,1),(1,2)] else: return [(0,1),(1,1),(2,1),(2,2)] class TBlock(Block): # 四種形態(tài) shape_id=0 shape_num=4 def __init__(self, n=None): super(TBlock, self).__init__() if n is None: n=random.randint(0,3) self.shape_id=n self.rect_arr=self.get_shape() def get_shape(self): if self.shape_id==0: return [(0,1),(1,1),(2,1),(1,2)] elif self.shape_id==1: return [(1,0),(1,1),(1,2),(0,1)] elif self.shape_id==2: return [(0,1),(1,1),(2,1),(1,0)] else: return [(1,0),(1,1),(1,2),(2,1)] def create_block(): n = random.randint(0,19) if n==0: return SquareBlock(n=0) elif n==1 or n==2: return LongBlock(n=n-1) elif n==3 or n==4: return ZBlock(n=n-3) elif n==5 or n==6: return SBlock(n=n-5) elif n>=7 and n<=10: return LBlock(n=n-7) elif n>=11 and n<=14: return JBlock(n=n-11) else: return TBlock(n=n-15) def run(): 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)) #設(shè)置窗口長(zhǎng)寬 main_panel=Panel(screen,main_block_size,[space,space,main_panel_width,main_panel_height]) pygame.key.set_repeat(200, 30) main_panel.create_move_block() diff_ticks = 300 # 移動(dòng)一次蛇頭的事件,單位毫秒 ticks = pygame.time.get_ticks() + diff_ticks game_state = 1 # 游戲狀態(tài)1.表示正常 2.表示失敗 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() if event.type == KEYDOWN: if event.key == K_LEFT: main_panel.control_block(-1,0) if event.key == K_RIGHT: main_panel.control_block(1,0) if event.key == K_UP: main_panel.change_block() if event.key == K_DOWN: main_panel.control_block(0,1) if event.key == K_SPACE: flag = main_panel.move_block() while flag==1: flag = main_panel.move_block() if flag == 9: game_state = 2 screen.fill((100,100,100)) # 將界面設(shè)置為灰色 main_panel.paint() # 主面盤繪制 if game_state == 2: myfont = pygame.font.Font(None,30) white = 255,255,255 textImage = myfont.render("Game over", True, white) screen.blit(textImage, (160,190)) pygame.display.update() # 必須調(diào)用update才能看到繪圖顯示 if game_state == 1 and pygame.time.get_ticks() >= ticks: ticks+=diff_ticks if main_panel.move_block()==9: game_state = 2 # 游戲結(jié)束 run()
關(guān)于pygame實(shí)現(xiàn)俄羅斯方塊游戲就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
網(wǎng)站標(biāo)題:pygame實(shí)現(xiàn)俄羅斯方塊游戲-創(chuàng)新互聯(lián)
文章URL:http://jinyejixie.com/article16/ccssdg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、網(wǎng)站營(yíng)銷、建站公司、品牌網(wǎng)站建設(shè)、小程序開(kāi)發(fā)、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容