python游戲?qū)崙?zhàn)項(xiàng)目之俄羅斯方塊的魅力
導(dǎo)語
為什么有這么一個(gè)簡單的游戲?這個(gè)游戲如此受歡迎?

僅僅是因?yàn)樗谟螒蛐袠I(yè)異常匱乏的年代出現(xiàn),從而成為了一代人的記憶嗎?恐怕并不是。
玩過俄羅斯方塊的人都明白,它給人的感覺就像是嗑瓜子一樣,一旦開始就會(huì)像上癮一樣難以停下來,絞盡腦汁只想填滿空缺的地方。
哈哈哈!小編每周的話基本上都會(huì)整理一些游戲代碼的哈!
這一期文章就帶大家來開發(fā)一款俄羅斯方塊小游戲!
正文
游戲規(guī)則:由小方塊組成的不同形狀的板塊陸續(xù)從屏幕上方落下來,玩家通過調(diào)整板塊的位置和方向,使它們?cè)谄聊坏撞科闯鐾暾囊粭l或幾條。
這些完整的橫條會(huì)隨即消失,給新落下來的板塊騰出空間,與此同時(shí),玩家得到分?jǐn)?shù)獎(jiǎng)勵(lì)。沒有被消除掉的方塊不斷堆積起來,一旦堆到屏幕頂端,玩家便告輸,游戲結(jié)束。
(1)游戲定義,俄羅斯方塊兒的不同的類型:
class tetrisShape():
def __init__(self, shape=0):
# 空塊
self.shape_empty = 0
# 一字型塊
self.shape_I = 1
# L型塊
self.shape_L = 2
# 向左的L型塊
self.shape_J = 3
# T型塊
self.shape_T = 4
# 田字型塊
self.shape_O = 5
# 反向Z型塊
self.shape_S = 6
# Z型塊
self.shape_Z = 7
(2)獲得該形狀當(dāng)前旋轉(zhuǎn)狀態(tài)的四個(gè)小方塊的相對(duì)坐標(biāo)分布:
def getRotatedRelativeCoords(self, direction):
# 初始分布
if direction == 0 or self.shape == self.shape_O:
return self.relative_coords
# 逆時(shí)針旋轉(zhuǎn)90度
if direction == 1:
return [[-y, x] for x, y in self.relative_coords]
# 逆時(shí)針旋轉(zhuǎn)180度
if direction == 2:
if self.shape in [self.shape_I, self.shape_Z, self.shape_S]:
return self.relative_coords
else:
return [[-x, -y] for x, y in self.relative_coords]
# 逆時(shí)針旋轉(zhuǎn)270度
if direction == 3:
if self.shape in [self.shape_I, self.shape_Z, self.shape_S]:
return [[-y, x] for x, y in self.relative_coords]
else:
return [[y, -x] for x, y in self.relative_coords]
(3)游戲的方塊兒可以向不同方向移動(dòng):
'''向右移動(dòng)'''
def moveRight(self):
if self.ableMove([self.current_coord[0] + 1, self.current_coord[1]]):
self.current_coord[0] += 1
'''向左移動(dòng)'''
def moveLeft(self):
if self.ableMove([self.current_coord[0] - 1, self.current_coord[1]]):
self.current_coord[0] -= 1
'''順時(shí)針轉(zhuǎn)'''
def rotateClockwise(self):
if self.ableMove(self.current_coord, (self.current_direction - 1) % 4):
self.current_direction = (self.current_direction-1) % 4
'''逆時(shí)針轉(zhuǎn)'''
def rotateAnticlockwise(self):
if self.ableMove(self.current_coord, (self.current_direction + 1) % 4):
self.current_direction = (self.current_direction+1) % 4
'''向下移動(dòng)'''
def moveDown(self):
removed_lines = 0
if self.ableMove([self.current_coord[0], self.current_coord[1] + 1]):
self.current_coord[1] += 1
else:
x_min, x_max, y_min, y_max = self.current_tetris.getRelativeBoundary(self.current_direction)
# 簡單起見, 有超出屏幕就判定游戲結(jié)束
if self.current_coord[1] + y_min < 0:
self.is_gameover = True
return removed_lines
self.mergeTetris()
removed_lines = self.removeFullLines()
self.createNewTetris()
return removed_lines
'''墜落'''
def dropDown(self):
removed_lines = 0
while self.ableMove([self.current_coord[0], self.current_coord[1] + 1]):
self.current_coord[1] += 1
x_min, x_max, y_min, y_max = self.current_tetris.getRelativeBoundary(self.current_direction)
# 簡單起見, 有超出屏幕就判定游戲結(jié)束
if self.current_coord[1] + y_min < 0:
self.is_gameover = True
return removed_lines
self.mergeTetris()
removed_lines = self.removeFullLines()
self.createNewTetris()
return removed_lines
(4)合并俄羅斯方塊(最下面定型不能再動(dòng)的那些):
def mergeTetris(self):
for x, y in self.current_tetris.getAbsoluteCoords(self.current_direction, self.current_coord[0], self.current_coord[1]):
self.board_data[x + y * self.width] = self.current_tetris.shape
self.current_coord = [-1, -1]
self.current_direction = 0
self.current_tetris = tetrisShape()
(5)當(dāng)每行鋪滿之后會(huì)得分,相應(yīng)的消失一行:

'''移出整行都有小方塊的'''
def removeFullLines(self):
new_board_data = [0] * self.width * self.height
new_y = self.height - 1
removed_lines = 0
for y in range(self.height - 1, -1, -1):
cell_count = sum([1 if self.board_data[x + y * self.width] > 0 else 0 for x in range(self.width)])
if cell_count < self.width:
for x in range(self.width):
new_board_data[x + new_y * self.width] = self.board_data[x + y * self.width]
new_y -= 1
else:
removed_lines += 1
self.board_data = new_board_data
return removed_lines
效果圖:

總結(jié)
哈哈哈!好啦!按住方向鍵也可以變形的哈!趕快試試~
到此這篇關(guān)于python游戲?qū)崙?zhàn)項(xiàng)目之俄羅斯方塊的魅力的文章就介紹到這了,更多相關(guān)python 俄羅斯方塊內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
PyTorch梯度裁剪避免訓(xùn)練loss nan的操作
這篇文章主要介紹了PyTorch梯度裁剪避免訓(xùn)練loss nan的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
Python 循環(huán)讀取數(shù)據(jù)內(nèi)存不足的解決方案
這篇文章主要介紹了Python 循環(huán)讀取數(shù)據(jù)內(nèi)存不足的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
python django model聯(lián)合主鍵的例子
今天小編就為大家分享一篇python django model聯(lián)合主鍵的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08
社區(qū)版pycharm創(chuàng)建django項(xiàng)目的方法(pycharm的newproject左側(cè)沒有項(xiàng)目選項(xiàng))
pycharm的newproject左側(cè)沒有出現(xiàn)項(xiàng)目選項(xiàng)的情況下,創(chuàng)建Django項(xiàng)目的解決方法./社區(qū)版pycharm創(chuàng)建django項(xiàng)目的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-09-09
pytorch實(shí)現(xiàn)ResNet結(jié)構(gòu)的實(shí)例代碼
ResNet網(wǎng)絡(luò)可以達(dá)到很深的層數(shù)的原因就是不斷的堆疊殘差結(jié)構(gòu)而來的,接下來通過本文給大家介紹pytorch實(shí)現(xiàn)ResNet結(jié)構(gòu)的示例代碼,喜歡的朋友跟隨小編一起看看吧2021-05-05

