pygame實(shí)現(xiàn)俄羅斯方塊游戲(基礎(chǔ)篇2)
接上章《pygame實(shí)現(xiàn)俄羅斯方塊游戲(基礎(chǔ)篇1)》繼續(xù)寫(xiě)俄羅斯方塊游戲
五、計(jì)算方塊之間的碰撞
在Panel類(lèi)里增加函數(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)在的效果是方塊可以堆疊了

六、鍵盤(pán)控制左右移動(dòng)
導(dǎo)入變量
from pygame.locals import KEYDOWN,K_LEFT,K_RIGHT,K_UP,K_DOWN
Panel類(lèi)里增加一個(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)處做下鍵盤(pán)的響應(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類(lèi)的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è)粗線(xiàn)段來(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() # 主面盤(pán)繪制
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è)方塊子類(lèi)的初始化函數(shù)稍作修改,將獲取形狀做一個(gè)獨(dú)立的get_shape函數(shù),并且給每個(gè)子類(lèi)增加一個(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父類(lèi)里可以加一個(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類(lèi)里的再增加一個(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類(lèi)的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
在鍵盤(pán)響應(yīng)處增加鍵盤(pán)處理
if event.key == K_SPACE: while main_panel.move_block()==1: pass
十、增加游戲結(jié)束判斷
游戲結(jié)束同樣可以在Panel類(lèi)的move_block中處理,如果一個(gè)方塊到底,并且消除進(jìn)行后,發(fā)現(xiàn)有方塊的y值小于0,那么一定是失敗了
修改Panel類(lèi)的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)鍵盤(pán)響應(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è)粗線(xiàn)段來(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() # 主面盤(pán)繪制
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()
今天先寫(xiě)到這,下章繼續(xù)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- pygame實(shí)現(xiàn)俄羅斯方塊游戲(對(duì)戰(zhàn)篇1)
- pygame實(shí)現(xiàn)俄羅斯方塊游戲(AI篇2)
- pygame實(shí)現(xiàn)俄羅斯方塊游戲(AI篇1)
- pygame實(shí)現(xiàn)俄羅斯方塊游戲(基礎(chǔ)篇3)
- pygame實(shí)現(xiàn)俄羅斯方塊游戲(基礎(chǔ)篇1)
- pygame實(shí)現(xiàn)俄羅斯方塊游戲
- python和pygame實(shí)現(xiàn)簡(jiǎn)單俄羅斯方塊游戲
- Python使用pygame模塊編寫(xiě)俄羅斯方塊游戲的代碼實(shí)例
- pygame庫(kù)實(shí)現(xiàn)俄羅斯方塊小游戲
相關(guān)文章
Django 實(shí)現(xiàn)Admin自動(dòng)填充當(dāng)前用戶(hù)的示例代碼
今天小編就為大家分享一篇Django 實(shí)現(xiàn)Admin自動(dòng)填充當(dāng)前用戶(hù)的示例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
Laravel+Dingo/Api 自定義響應(yīng)的實(shí)現(xiàn)
這篇文章主要介紹了Laravel+Dingo/Api 自定義響應(yīng)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02
python嵌套字典比較值與取值的實(shí)現(xiàn)示例
這篇文章主要給大家介紹了關(guān)于python嵌套字典比較值與取值的實(shí)現(xiàn)方法,詳細(xì)介紹了python字典嵌套字典的情況下獲取某個(gè)key的value的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),需要的朋友們下面來(lái)一起看看吧。2017-11-11
Python?Fire中兩種命令行參數(shù)靈活設(shè)置方式詳解
Python的Fire庫(kù),一個(gè)用來(lái)生成命令行工具的的庫(kù),這篇文章主要針對(duì)命令行參數(shù),補(bǔ)充兩種更加靈活的設(shè)置方式,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01
解決Python httpx 運(yùn)行過(guò)程中無(wú)限阻塞的問(wèn)題
這篇文章主要介紹了解決Python httpx 運(yùn)行過(guò)程中無(wú)限阻塞的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
pandas的drop_duplicates無(wú)法去重問(wèn)題解決
在我們利用Pandas進(jìn)行數(shù)據(jù)清洗的時(shí)候,往往會(huì)用到drop_duplicates()進(jìn)行去重,本文主要介紹了pandas的drop_duplicates無(wú)法去重問(wèn)題解決,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03
python實(shí)現(xiàn)智能語(yǔ)音天氣預(yù)報(bào)
今天小編就為大家分享一篇python實(shí)現(xiàn)智能語(yǔ)音天氣預(yù)報(bào),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12

