pygame實現俄羅斯方塊遊戲(基礎篇2)
接上章《pygame實現俄羅斯方塊遊戲(基礎篇1)》繼續寫俄羅斯方塊遊戲
五、計算方塊之間的碰撞
在Panel類裡增加函式
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函式的判斷,增加check_overlap函式檢測
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()
現在的效果是方塊可以堆疊了
六、鍵盤控制左右移動
匯入變數
from pygame.locals import KEYDOWN,K_LEFT,K_RIGHT,K_UP,K_DOWN
Panel類裡增加一個控制移動方塊的函式
def control_block(self,diffy): if self.moving_block.can_move(diffx,diffy) and not self.check_overlap(diffx,diffy): self.moving_block.move(diffx,diffy)
滑鼠事件監聽處做下鍵盤的響應
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 # 變形過會實現 if event.key == K_DOWN: main_panel.control_block(0,1)
由於Block類的can_move函式沒有實現左右移動的判斷,所以需要再對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
現在,左右的移動也正常了,效果圖如下
貼下目前的程式碼
# -*- coding=utf-8 -*- import random import pygame from pygame.locals import KEYDOWN,K_DOWN class Panel(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] 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,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,diffy) def move_block(self): if self.moving_block is None: create_move_block() if self.moving_block.can_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],self._y+self._height],self._width) # 用一個粗線段來填充背景 # 繪製已經落底下的方塊 bz=self._block_size for rect in self.rect_arr: x,y=rect pygame.draw.line(self._bg,[0,255],[self._x+x*bz+bz/2,self._y+y*bz],self._y+(y+1)*bz],bz) pygame.draw.rect(self._bg,[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,bz) pygame.draw.rect(self._bg,1) class Block(object): def __init__(self): self.rect_arr=[] def get_rect_arr(self): # 用於獲取方塊種的四個矩形列表 return self.rect_arr def move(self,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,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): # 兩種形態 super(LongBlock,self).__init__() if n is None: n=random.randint(0,1) self.rect_arr=[(1,0),(1,1),2),3)] if n==0 else [(0,(2,(3,2)] class SquareBlock(Block): # 一種形態 def __init__(self,n=None): super(SquareBlock,self).__init__() self.rect_arr=[(1,2)] class ZBlock(Block): # 兩種形態 def __init__(self,n=None): super(ZBlock,1) self.rect_arr=[(2,2)] if n==0 else [(0,2)] class SBlock(Block): # 兩種形態 def __init__(self,n=None): super(SBlock,1)] class LBlock(Block): # 四種形態 def __init__(self,n=None): super(LBlock,3) if n==0: self.rect_arr=[(1,2)] elif n==1: self.rect_arr=[(0,(0,2)] elif n==2: self.rect_arr=[(0,2)] else: self.rect_arr=[(0,0)] class JBlock(Block): # 四種形態 def __init__(self,n=None): super(JBlock,0)] elif n==2: self.rect_arr=[(2,2)] class TBlock(Block): # 四種形態 def __init__(self,n=None): super(TBlock,3) if n==0: self.rect_arr=[(0,2)] elif n==1: self.rect_arr=[(1,1)] elif n==2: self.rect_arr=[(0,0)] else: self.rect_arr=[(1,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)) #設定視窗長寬 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 # 移動一次蛇頭的事件,單位毫秒 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,1) screen.fill((100,100,100)) # 將介面設定為灰色 main_panel.paint() # 主面盤繪製 pygame.display.update() # 必須呼叫update才能看到繪圖顯示 if pygame.time.get_ticks() >= ticks: ticks+=diff_ticks main_panel.move_block() run()
七、控制變形
變形的實現,我們對每個方塊子類的初始化函式稍作修改,將獲取形狀做一個獨立的get_shape函式,並且給每個子類增加一個變數用於記錄當前形態id,用一個變數用於標識每種方塊的形態數量,以T型為例,修改後程式碼如下
class TBlock(Block): # 四種形態 shape_id=0 shape_num=4 def __init__(self,3) self.shape_id=n self.rect_arr=self.get_shape() def get_shape(self): if self.shape_id==0: return [(0,2)] elif self.shape_id==1: return [(1,1)] elif self.shape_id==2: return [(0,0)] else: return [(1,1)]
這樣我們在Block父類裡可以加一個change函式,用於變換至下一形態,由於變化時要保持原來的移動位置,我們增加sx,sy兩個變數將方塊移動過的位置存著,便於在變化時使用
class Block(object): sx=0 sy=0 def __init__(self): self.rect_arr=[] def get_rect_arr(self): # 用於獲取方塊種的四個矩形列表 return self.rect_arr def move(self,ydiff): # 用於移動方塊的方法 self.sx+=xdiff self.sy+=ydiff self.new_rect_arr=[] 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 # 下一形態 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類裡的再增加一個change函式,直接呼叫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,check_arr=new_arr): # 變形不能造成方塊重疊 self.moving_block.rect_arr=new_arr
最後將key_up事件的響應加入change_block的呼叫就好了
if event.key == K_UP: main_panel.change_block()
現在已經實現了,變形和移動了,方塊基本可以正常下落了
八、方塊的消除
這個計算主要是處理Panel類的rect_arr,如果陣列中出現某一行有10個就符合消除條件,為簡化計算,我們將這些矩形按y值存到一個數組中,便於計算
def check_clear(self): tmp_arr = [[] for i in range(20)] # 先將方塊按行存入陣列 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)] # 從下往上計算可以消除的行,並記錄消除行後其他行的向下偏移數量 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=[] # 跳過移除行,並將其他行做偏移 for y in range(19,-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的呼叫
def move_block(self): if self.moving_block is None: create_move_block() if self.moving_block.can_move(0,1) else: self.add_block(self.moving_block) self.check_clear() self.create_move_block()
現在遊戲可以消除方塊了
九、增加空格鍵使快速落下
快速落下可以快速呼叫Panel的move_block函式,我們在move_block函式增加一個返回值,用於標記使正常下移還是移到底部後新的方塊
def move_block(self): if self.moving_block is None: create_move_block() if self.moving_block.can_move(0,1) return 1 else: self.add_block(self.moving_block) self.check_clear() self.create_move_block() return 2
在鍵盤響應處增加鍵盤處理
if event.key == K_SPACE: while main_panel.move_block()==1: pass
十、增加遊戲結束判斷
遊戲結束同樣可以在Panel類的move_block中處理,如果一個方塊到底,並且消除進行後,發現有方塊的y值小於0,那麼一定是失敗了
修改Panel類的move_block函式
def move_block(self): if self.moving_block is None: create_move_block() if self.moving_block.can_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
增加一個變數記錄遊戲狀態
game_state = 1 # 遊戲狀態1.表示正常 2.表示失敗
計時器處修改程式
if game_state == 1 and pygame.time.get_ticks() >= ticks: ticks+=diff_ticks if main_panel.move_block()==9: game_state = 2
滑鼠鍵盤響應空格鍵中也增加一下判斷
if event.key == K_SPACE: flag = main_panel.move_block() while flag==1: flag = main_panel.move_block() if flag == 9: game_state = 2
最後增加遊戲結束文字的繪製
if game_state == 2: myfont = pygame.font.Font(None,30) white = 255,255 textImage = myfont.render("Game over",True,white) screen.blit(textImage,(160,190))
好了,現在會提示遊戲結束了
最後附下目前的完整程式碼
# -*- coding=utf-8 -*- import random import pygame from pygame.locals import KEYDOWN,K_DOWN,K_SPACE class Panel(object): # 用於繪製整個遊戲視窗的版面 rect_arr=[] # 已經落底下的方塊 moving_block=None # 正在落下的方塊 def __init__(self,diffy) def change_block(self): if self.moving_block: new_arr = self.moving_block.change() if new_arr and not self.check_overlap(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,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)] # 先將方塊按行存入陣列 for 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,1) class Block(object): sx=0 sy=0 def __init__(self): self.rect_arr=[] def get_rect_arr(self): # 用於獲取方塊種的四個矩形列表 return self.rect_arr def move(self,y+self.sy]) return new_arr class LongBlock(Block): shape_id=0 shape_num=2 def __init__(self,1) self.shape_id=n self.rect_arr=self.get_shape() def get_shape(self): return [(1,3)] if self.shape_id==0 else [(0,2)] class SquareBlock(Block): # 一種形態 shape_id=0 shape_num=1 def __init__(self,self).__init__() self.rect_arr=self.get_shape() def get_shape(self): return [(1,2)] class ZBlock(Block): # 兩種形態 shape_id=0 shape_num=2 def __init__(self,1) self.shape_id=n self.rect_arr=self.get_shape() def get_shape(self): return [(2,2)] if self.shape_id==0 else [(0,2)] class SBlock(Block): # 兩種形態 shape_id=0 shape_num=2 def __init__(self,1)] class LBlock(Block): # 四種形態 shape_id=0 shape_num=4 def __init__(self,3) self.shape_id=n self.rect_arr=self.get_shape() def get_shape(self): if self.shape_id==0: return [(1,2)] elif self.shape_id==1: return [(0,2)] elif self.shape_id==2: return [(0,2)] else: return [(0,0)] class JBlock(Block): # 四種形態 shape_id=0 shape_num=4 def __init__(self,0)] elif self.shape_id==2: return [(2,2)] class TBlock(Block): # 四種形態 shape_id=0 shape_num=4 def __init__(self,1)] def create_block(): n = random.randint(0,30) main_panel.create_move_block() diff_ticks = 300 # 移動一次蛇頭的事件,單位毫秒 ticks = pygame.time.get_ticks() + diff_ticks game_state = 1 # 遊戲狀態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_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)) # 將介面設定為灰色 main_panel.paint() # 主面盤繪製 if game_state == 2: myfont = pygame.font.Font(None,30) white = 255,255 textImage = myfont.render("Game over",white) screen.blit(textImage,190)) pygame.display.update() # 必須呼叫update才能看到繪圖顯示 if game_state == 1 and pygame.time.get_ticks() >= ticks: ticks+=diff_ticks if main_panel.move_block()==9: game_state = 2 # 遊戲結束 run()
今天先寫到這,下章繼續
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。