欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python游戲?qū)崙?zhàn)項目之俄羅斯方塊的魅力

 更新時間:2021年09月18日 11:14:21   作者:顧木子吖  
遲早一定會掛掉的俄羅斯方塊,為什么至今仍是世界游戲之王?它是怎么編寫的?本文將給大家詳細的介紹,對大家的學習或工作具有一定的參考借鑒價值

導語

圖片

為什么有這么一個簡單的游戲?這個游戲如此受歡迎?

僅僅是因為它在游戲行業(yè)異常匱乏的年代出現(xiàn),從而成為了一代人的記憶嗎?恐怕并不是。

玩過俄羅斯方塊的人都明白,它給人的感覺就像是嗑瓜子一樣,一旦開始就會像上癮一樣難以停下來,絞盡腦汁只想填滿空缺的地方。

圖片

哈哈哈!小編每周的話基本上都會整理一些游戲代碼的哈!

這一期文章就帶大家來開發(fā)一款俄羅斯方塊小游戲!

正文

游戲規(guī)則:由小方塊組成的不同形狀的板塊陸續(xù)從屏幕上方落下來,玩家通過調(diào)整板塊的位置和方向,使它們在屏幕底部拼出完整的一條或幾條。

這些完整的橫條會隨即消失,給新落下來的板塊騰出空間,與此同時,玩家得到分數(shù)獎勵。沒有被消除掉的方塊不斷堆積起來,一旦堆到屏幕頂端,玩家便告輸,游戲結(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)​獲得該形狀當前旋轉(zhuǎn)狀態(tài)的四個小方塊的相對坐標分布:

def getRotatedRelativeCoords(self, direction):
        # 初始分布
        if direction == 0 or self.shape == self.shape_O:
            return self.relative_coords
        # 逆時針旋轉(zhuǎn)90度
        if direction == 1:
            return [[-y, x] for x, y in self.relative_coords]
        # 逆時針旋轉(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]
        # 逆時針旋轉(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)游戲的方塊兒可以向不同方向移動:​

'''向右移動'''
    def moveRight(self):
        if self.ableMove([self.current_coord[0] + 1, self.current_coord[1]]):
            self.current_coord[0] += 1
    '''向左移動'''
    def moveLeft(self):
        if self.ableMove([self.current_coord[0] - 1, self.current_coord[1]]):
            self.current_coord[0] -= 1
    '''順時針轉(zhuǎn)'''
    def rotateClockwise(self):
        if self.ableMove(self.current_coord, (self.current_direction - 1) % 4):
            self.current_direction = (self.current_direction-1) % 4
    '''逆時針轉(zhuǎn)'''
    def rotateAnticlockwise(self):
        if self.ableMove(self.current_coord, (self.current_direction + 1) % 4):
            self.current_direction = (self.current_direction+1) % 4
    '''向下移動'''
    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)合并俄羅斯方塊(最下面定型不能再動的那些):

    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)當每行鋪滿之后會得分,相應的消失一行:

圖片

'''移出整行都有小方塊的'''
    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é)

哈哈哈!好啦!按住方向鍵也可以變形的哈!趕快試試~

到此這篇關于python游戲?qū)崙?zhàn)項目之俄羅斯方塊的魅力的文章就介紹到這了,更多相關python 俄羅斯方塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • python數(shù)字圖像處理實現(xiàn)圖像的形變與縮放

    python數(shù)字圖像處理實現(xiàn)圖像的形變與縮放

    這篇文章主要為大家介紹了python數(shù)字圖像處理實現(xiàn)圖像的形變與縮放示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06
  • 在ubuntu16.04中將python3設置為默認的命令寫法

    在ubuntu16.04中將python3設置為默認的命令寫法

    這篇文章主要介紹了在ubuntu16.04中將python3設置為默認python的方法,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧
    2018-10-10
  • python實現(xiàn)字符串連接的三種方法及其效率、適用場景詳解

    python實現(xiàn)字符串連接的三種方法及其效率、適用場景詳解

    本篇文章主要介紹了python實現(xiàn)字符串連接的三種方法及其效率、適用場景詳解,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-01-01
  • 在PyCharm中使用FMEObjects的操作步驟

    在PyCharm中使用FMEObjects的操作步驟

    這篇文章主要介紹了在PyCharm中使用FMEObjects,本文將ArcGISPro2.8的Python3.7解釋器與FME2022.0配合使用,通過圖文并茂的形式給大家介紹的非常詳細,需要的朋友可以參考下
    2022-08-08
  • python 類中函數(shù)名前后加下劃線的具體使用

    python 類中函數(shù)名前后加下劃線的具體使用

    在Python編程語言中,函數(shù)名前后有下劃線是一種常見的命名約定,,被廣泛應用于類中的函數(shù),本文將介紹下劃線命名風格的由來、使用場景以及如何正確應用它,感興趣的可以了解一下
    2024-01-01
  • python獲取本機mac地址和ip地址的方法

    python獲取本機mac地址和ip地址的方法

    這篇文章主要介紹了python獲取本機mac地址和ip地址的方法,涉及Python獲取系統(tǒng)相關信息的技巧,需要的朋友可以參考下
    2015-04-04
  • Python實現(xiàn)一個簡單三層神經(jīng)網(wǎng)絡的搭建及測試 代碼解析

    Python實現(xiàn)一個簡單三層神經(jīng)網(wǎng)絡的搭建及測試 代碼解析

    一個完整的神經(jīng)網(wǎng)絡一般由三層構(gòu)成:輸入層,隱藏層(可以有多層)和輸出層。本文所構(gòu)建的神經(jīng)網(wǎng)絡隱藏層只有一層。一個神經(jīng)網(wǎng)絡主要由三部分構(gòu)成(代碼結(jié)構(gòu)上):初始化,訓練,和預測。,需要的朋友可以參考下面文章內(nèi)容的具體內(nèi)容
    2021-09-09
  • Python實現(xiàn)利用163郵箱遠程關電腦腳本

    Python實現(xiàn)利用163郵箱遠程關電腦腳本

    這篇文章主要為大家詳細介紹了Python實現(xiàn)利用163郵箱遠程關電腦腳本,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • Python字符串詳細介紹

    Python字符串詳細介紹

    這篇文章主要介紹了Python字符串詳解,本文講解了字符串相關知識、字符串的一些特性、原始字符串、unicode字符串、字符串的常用操作方法、內(nèi)建函數(shù)列表等內(nèi)容,需要的朋友可以參考下
    2015-05-05
  • Python實現(xiàn)完全數(shù)的示例詳解

    Python實現(xiàn)完全數(shù)的示例詳解

    完全數(shù),又稱完美數(shù),定義為:這個數(shù)的所有因數(shù)(不包括這個數(shù)本身)加起來剛好等于這個數(shù)。本文就來用Python實現(xiàn)計算完全數(shù),需要的可以參考一下
    2023-01-01

最新評論