色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

Python Pygame實(shí)現(xiàn)俄羅斯方塊

瀏覽:4日期:2022-06-27 16:20:36

本文實(shí)例為大家分享了Python Pygame實(shí)現(xiàn)俄羅斯方塊的具體代碼,供大家參考,具體內(nèi)容如下

源碼:

# coding : utf-8#: pip install pygameimport randomimport sysimport pygame#: 顏色定義COLOR_WHITE = (255, 255, 255)COLOR_BLACK = (0, 0, 0)class Block: '''小塊''' width = 24 height = 24 @staticmethod def draw(s, left, top, color, bg_color): pygame.draw.rect(s, bg_color, pygame.Rect(left, top, Block.width, Block.height)) pygame.draw.rect(s, color, pygame.Rect(left, top, Block.width - 1, Block.height - 1))class Building: '''積木''' def __init__(self): ''' 方塊的7種基本形狀 每次初始化隨機(jī)選擇一個(gè)形狀 @:return True / False ''' self.form = random.choice( [ [ [0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0] ], [ [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1, 1, 1, 1, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0] ], [ [0, 0, 0, 0, 0], [0, 1, 1, 0, 0], [0, 0, 1, 1, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0] ], [ [0, 0, 0, 0, 0], [0, 0, 1, 1, 0], [0, 1, 1, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0] ], [ [0, 0, 0, 0, 0], [0, 1, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0] ], [ [0, 0, 0, 0, 0], [0, 0, 1, 1, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0] ], [ [0, 0, 0, 0, 0], [0, 1, 1, 0, 0], [0, 1, 1, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0] ] ]) def __getitem__(self, pos): return self.form[pos] def __setitem__(self, key, value): self.form[key] = valueclass Layout: '''棋盤''' def __init__(self): self.block_x_count = 16; self.block_y_count = 22; self.layout = [[0 if 1 < i < self.block_x_count - 2 and j < self.block_y_count - 2 else 1 for i in range(self.block_x_count)] for j in range(self.block_y_count)] @property def size(self): '''返回棋盤屏幕大小(width,height)''' return (self.block_x_count * Block.width, self.block_y_count * Block.height) def create_new_building(self): ''' 創(chuàng)建新的積木,初始化位置為第5,0格, 速度為4 :return: 返回是否無空間創(chuàng)建了 ''' self.building = Building() self.building_left, self.building_top = 5, 0 # self.drop_speed = 3 print(self.test_building_touch_wall()) return self.test_building_touch_wall() @property def speed(self): return self.drop_speed def test_building_touch_wall(self, x_offset=0, y_offset=0): ''' 積木是否已經(jīng)觸底/墻壁 具體操作: 判斷積木最后一排的1,是否在當(dāng)前棋牌對應(yīng)的位置是也是1 @:param x_offset: x的偏移量 移動(dòng)時(shí)可以傳入1/-1來判斷 @:param y_offset: y的偏移量 正常下落時(shí)可以傳入1來判斷 ''' for i in range(4, -1, -1): for j in range(5): if self.building[i][j]: if self.layout[i + self.building_top + y_offset][j + self.building_left + x_offset]: return True return False def move_left_right(self, x): ''' 左右移動(dòng) @:param x: 移動(dòng)量 x_offset ''' #: 移動(dòng)時(shí)不能撞墻 if not self.test_building_touch_wall(x_offset=x): self.building_left += x def down_build(self): ''' 盒子的自動(dòng)下移 ''' self.building_top += 1 def direct_down(self): ''' 手動(dòng)快速降落 ''' self.drop_speed = 50 def convert_building(self): ''' * 扭轉(zhuǎn)盒子的總方位 (右轉(zhuǎn)) 具體操作: 把第一豎排的倒序給第一橫排的 把第二豎排的倒序給第二橫排的 后面同理. ''' new_box = [[0 for i in range(5)] for j in range(5)] for i in range(5): for j in range(4, -1, -1): new_box[i][j] = self.building[4 - j][i] self.building = new_box def clear_full_lines(self): '''消除滿行的所有行''' new_layout = [[0 if 1 < i < self.block_x_count - 2 and j < self.block_y_count - 2 else 1 for i in range(self.block_x_count)] for j in range(self.block_y_count)] row_len = self.block_x_count - 4 new_row = self.block_y_count - 2 - 1 for cur_row in range(self.block_y_count - 2 - 1, 0, -1): if sum(self.layout[cur_row][2:self.block_x_count - 2]) < row_len: new_layout[new_row] = self.layout[cur_row] new_row -= 1 self.layout = new_layout def put_building_to_layout(self): '''將積木放到棋盤里''' for i in range(4, -1, -1): for j in range(5): if self.building[i][j]: self.layout[i + self.building_top][j + self.building_left] = 1 #: 這里會(huì)調(diào)用消除函數(shù) self.clear_full_lines() def draw_building(self, s): ''' 顯示積木 @:param s : pygame = screen ''' cur_left, cur_top = self.building_left * Block.width, self.building_top * Block.height for i in range(5): for j in range(5): # 只畫積木實(shí)體,不管盒子本身 if self.building[j][i]: Block.draw(s, cur_left + i * Block.width, cur_top + j * Block.height, COLOR_BLACK, COLOR_WHITE) def draw(self, s): ''' 顯示棋盤 @:param s : pygame = screen ''' for i in range(self.block_x_count): for j in range(self.block_y_count): if self.layout[j][i] == 0: Block.draw(s, i * Block.width, j * Block.height, COLOR_WHITE, COLOR_BLACK) else: Block.draw(s, i * Block.width, j * Block.height, COLOR_BLACK, COLOR_WHITE)# -------------------------------------------------------------------# Main# -------------------------------------------------------------------def main(): #: 初始化 while True: layout = Layout() layout.create_new_building() pygame.init() pygame.display.set_caption(’俄羅斯方塊’) screen = pygame.display.set_mode((layout.size), 0, 32) is_over = False #: 單局游戲循環(huán)開始 [結(jié)束后直接重新開始] while not is_over: #: 處理游戲消息 for e in pygame.event.get(): if e.type == pygame.QUIT: sys.exit() #: 處理按鍵 if e.type == pygame.KEYDOWN: if e.key == pygame.K_UP: layout.convert_building() if e.key == pygame.K_DOWN: layout.direct_down() if e.key == pygame.K_LEFT: layout.move_left_right(-1) if e.key == pygame.K_RIGHT: layout.move_left_right(1) #: 是否碰觸底部地面了,是 -> 融合背景 否 -> 繼續(xù)下落 if layout.test_building_touch_wall(y_offset=1): layout.put_building_to_layout() is_over = layout.create_new_building() else: layout.down_build() #: 繪制 layout.draw(screen) layout.draw_building(screen) pygame.display.update() #: 速度 pygame.time.Clock().tick(layout.speed)if __name__ == ’__main__’: main()

效果:

Python Pygame實(shí)現(xiàn)俄羅斯方塊

更多俄羅斯方塊精彩文章請點(diǎn)擊專題:俄羅斯方塊游戲集合 進(jìn)行學(xué)習(xí)。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 色悠久| 免费国产一级特黄久久 | 中文字幕亚洲视频 | 久久免费视频2 | 亚洲欧美日韩中文字幕在线 | 日本乱人伦片中文三区 | 中文字幕有码在线视频 | 极品美女写真菠萝蜜视频 | 日韩欧美精品一区二区三区 | 国产韩国精品一区二区三区 | 成人亚洲欧美 | 日本免费人做人一区在线观看 | 91pao强力打造免费高清 | 久草热在线观看 | 一级特黄a免费大片 | 五月久久亚洲七七综合中文网 | 波多野结衣一区二区三区高清在线 | 国产成人精品三级在线 | 日韩在线一区二区三区视频 | 欧美特黄一级aa毛片 | 操美女大逼视频 | 免费观看成人毛片 | 美女在线网站免费的 | 成年男女拍拍拍免费视频 | 99这里只有精品66视频 | 午夜免费69性视频爽爽爽 | 99视频国产热精品视频 | 亚洲欧美成人 | 给我一个可以看片的www日本 | 免费一级大毛片a一观看不卡 | 玖玖视频精品 | 99r精品在线 | 美毛片| 成年人视频在线免费 | 全免费a级毛片免费看视频免 | 91久久国产露脸精品 | 欧美极品video粗暴 | 男女一级 | 欧美操操操 | 天天精品在线 | 欧美a级毛片免费播敢 |