【Python黑科技】tkinter库实战“俄罗斯方块”小游戏(保姆级图文+实现代码)
目錄
- 實現效果
- 實現思路
- 實現代碼
- 總結
歡迎關注 『Python黑科技』 系列,持續更新中
歡迎關注 『Python黑科技』 系列,持續更新中
實現效果
實現思路
-
空間――由 15*25 個空格組成
-
盒子――組成方塊的其中小方塊,是組成方塊的基本單元。
-
方塊――從邊框頂掉下的東西,游戲者可以翻轉和改變位置。每個方塊由 4 個盒子組成。
-
預設的方塊類型――不同類型的方塊。這里形狀的名字被叫做 T, S, Z ,J, L, I , O。
- 通過左右的箭頭進行控制左右位移,上下箭頭控制順時針逆時針的翻轉。
實現代碼
需要準備一個方圓體.ttc文件
鏈接: https://pan.baidu.com/s/1ld0yqhJ_wETv2UtIghEXSQ?pwd=7djn 提取碼: 7djn # @Time : 2022/2/9 20:16 # @Author : 南黎 # @FileName: 俄羅斯方塊.pyimport pygame import random import ospygame.init()GRID_WIDTH = 20 GRID_NUM_WIDTH = 15 GRID_NUM_HEIGHT = 25 WIDTH, HEIGHT = GRID_WIDTH * GRID_NUM_WIDTH, GRID_WIDTH * GRID_NUM_HEIGHT SIDE_WIDTH = 200 SCREEN_WIDTH = WIDTH + SIDE_WIDTH WHITE = (0xff, 0xff, 0xff) BLACK = (0, 0, 0) LINE_COLOR = (0x33, 0x33, 0x33)CUBE_COLORS = [(0xcc, 0x99, 0x99), (0xff, 0xff, 0x99), (0x66, 0x66, 0x99),(0x99, 0x00, 0x66), (0xff, 0xcc, 0x00), (0xcc, 0x00, 0x33),(0xff, 0x00, 0x33), (0x00, 0x66, 0x99), (0xff, 0xff, 0x33),(0x99, 0x00, 0x33), (0xcc, 0xff, 0x66), (0xff, 0x99, 0x00) ]screen = pygame.display.set_mode((SCREEN_WIDTH, HEIGHT)) pygame.display.set_caption("俄羅斯方塊") clock = pygame.time.Clock() FPS = 30score = 0 level = 1screen_color_matrix = [[None] * GRID_NUM_WIDTH for i in range(GRID_NUM_HEIGHT)]# 設置游戲的根目錄為當前文件夾 base_folder = os.path.dirname(__file__)def show_text(surf, text, size, x, y, color=WHITE):font_name = os.path.join(base_folder, '方圓體.ttc')font = pygame.font.Font(font_name, size)text_surface = font.render(text, True, color)text_rect = text_surface.get_rect()text_rect.midtop = (x, y)surf.blit(text_surface, text_rect)class CubeShape(object):SHAPES = ['I', 'J', 'L', 'O', 'S', 'T', 'Z']I = [[(0, -1), (0, 0), (0, 1), (0, 2)],[(-1, 0), (0, 0), (1, 0), (2, 0)]]J = [[(-2, 0), (-1, 0), (0, 0), (0, -1)],[(-1, 0), (0, 0), (0, 1), (0, 2)],[(0, 1), (0, 0), (1, 0), (2, 0)],[(0, -2), (0, -1), (0, 0), (1, 0)]]L = [[(-2, 0), (-1, 0), (0, 0), (0, 1)],[(1, 0), (0, 0), (0, 1), (0, 2)],[(0, -1), (0, 0), (1, 0), (2, 0)],[(0, -2), (0, -1), (0, 0), (-1, 0)]]O = [[(0, 0), (0, 1), (1, 0), (1, 1)]]S = [[(-1, 0), (0, 0), (0, 1), (1, 1)],[(1, -1), (1, 0), (0, 0), (0, 1)]]T = [[(0, -1), (0, 0), (0, 1), (-1, 0)],[(-1, 0), (0, 0), (1, 0), (0, 1)],[(0, -1), (0, 0), (0, 1), (1, 0)],[(-1, 0), (0, 0), (1, 0), (0, -1)]]Z = [[(0, -1), (0, 0), (1, 0), (1, 1)],[(-1, 0), (0, 0), (0, -1), (1, -1)]]SHAPES_WITH_DIR = {'I': I, 'J': J, 'L': L, 'O': O, 'S': S, 'T': T, 'Z': Z}def __init__(self):self.shape = self.SHAPES[random.randint(0, len(self.SHAPES) - 1)]# 骨牌所在的行列self.center = (2, GRID_NUM_WIDTH // 2)self.dir = random.randint(0, len(self.SHAPES_WITH_DIR[self.shape]) - 1)self.color = CUBE_COLORS[random.randint(0, len(CUBE_COLORS) - 1)]def get_all_gridpos(self, center=None):curr_shape = self.SHAPES_WITH_DIR[self.shape][self.dir]if center is None:center = [self.center[0], self.center[1]]return [(cube[0] + center[0], cube[1] + center[1])for cube in curr_shape]def conflict(self, center):for cube in self.get_all_gridpos(center):# 超出屏幕之外,說明不合法if cube[0] < 0 or cube[1] < 0 or cube[0] >= GRID_NUM_HEIGHT or \cube[1] >= GRID_NUM_WIDTH:return True# 不為None,說明之前已經有小方塊存在了,也不合法if screen_color_matrix[cube[0]][cube[1]] is not None:return Truereturn Falsedef rotate(self):new_dir = self.dir + 1new_dir %= len(self.SHAPES_WITH_DIR[self.shape])old_dir = self.dirself.dir = new_dirif self.conflict(self.center):self.dir = old_dirreturn Falsedef down(self):# import pdb; pdb.set_trace()center = (self.center[0] + 1, self.center[1])if self.conflict(center):return Falseself.center = centerreturn Truedef left(self):center = (self.center[0], self.center[1] - 1)if self.conflict(center):return Falseself.center = centerreturn Truedef right(self):center = (self.center[0], self.center[1] + 1)if self.conflict(center):return Falseself.center = centerreturn Truedef draw(self):for cube in self.get_all_gridpos():pygame.draw.rect(screen, self.color,(cube[1] * GRID_WIDTH, cube[0] * GRID_WIDTH,GRID_WIDTH, GRID_WIDTH))pygame.draw.rect(screen, WHITE,(cube[1] * GRID_WIDTH, cube[0] * GRID_WIDTH,GRID_WIDTH, GRID_WIDTH),1)def draw_grids():for i in range(GRID_NUM_WIDTH):pygame.draw.line(screen, LINE_COLOR,(i * GRID_WIDTH, 0), (i * GRID_WIDTH, HEIGHT))for i in range(GRID_NUM_HEIGHT):pygame.draw.line(screen, LINE_COLOR,(0, i * GRID_WIDTH), (WIDTH, i * GRID_WIDTH))pygame.draw.line(screen, WHITE,(GRID_WIDTH * GRID_NUM_WIDTH, 0),(GRID_WIDTH * GRID_NUM_WIDTH, GRID_WIDTH * GRID_NUM_HEIGHT))def draw_matrix():for i, row in zip(range(GRID_NUM_HEIGHT), screen_color_matrix):for j, color in zip(range(GRID_NUM_WIDTH), row):if color is not None:pygame.draw.rect(screen, color,(j * GRID_WIDTH, i * GRID_WIDTH,GRID_WIDTH, GRID_WIDTH))pygame.draw.rect(screen, WHITE,(j * GRID_WIDTH, i * GRID_WIDTH,GRID_WIDTH, GRID_WIDTH), 2)def draw_score():show_text(screen, u'得分:{}'.format(score), 20, WIDTH + SIDE_WIDTH // 2, 100)def remove_full_line():global screen_color_matrixglobal scoreglobal levelnew_matrix = [[None] * GRID_NUM_WIDTH for i in range(GRID_NUM_HEIGHT)]index = GRID_NUM_HEIGHT - 1n_full_line = 0for i in range(GRID_NUM_HEIGHT - 1, -1, -1):is_full = Truefor j in range(GRID_NUM_WIDTH):if screen_color_matrix[i][j] is None:is_full = Falsecontinueif not is_full:new_matrix[index] = screen_color_matrix[i]index -= 1else:n_full_line += 1score += n_full_linelevel = score // 20 + 1screen_color_matrix = new_matrixdef show_welcome(screen):show_text(screen, u'俄羅斯方塊', 30, WIDTH / 2, HEIGHT / 2)show_text(screen, u'按任意鍵開始游戲', 20, WIDTH / 2, HEIGHT / 2 + 50)running = True gameover = True counter = 0 live_cube = None while running:clock.tick(FPS)for event in pygame.event.get():if event.type == pygame.QUIT:running = Falseelif event.type == pygame.KEYDOWN:if gameover:gameover = Falselive_cube = CubeShape()breakif event.key == pygame.K_LEFT:live_cube.left()elif event.key == pygame.K_RIGHT:live_cube.right()elif event.key == pygame.K_DOWN:live_cube.down()elif event.key == pygame.K_UP:live_cube.rotate()elif event.key == pygame.K_SPACE:while live_cube.down() == True:passremove_full_line()# level 是為了方便游戲的難度,level 越高 FPS // level 的值越小# 這樣屏幕刷新的就越快,難度就越大if gameover is False and counter % (FPS // level) == 0:# down 表示下移骨牌,返回False表示下移不成功,可能超過了屏幕或者和之前固定的# 小方塊沖突了if live_cube.down() == False:for cube in live_cube.get_all_gridpos():screen_color_matrix[cube[0]][cube[1]] = live_cube.colorlive_cube = CubeShape()if live_cube.conflict(live_cube.center):gameover = Truescore = 0live_cube = Nonescreen_color_matrix = [[None] * GRID_NUM_WIDTH for i in range(GRID_NUM_HEIGHT)]# 消除滿行remove_full_line()counter += 1# 更新屏幕screen.fill(BLACK)draw_grids()draw_matrix()draw_score()if live_cube is not None:live_cube.draw()if gameover:show_welcome(screen)pygame.display.update()總結
大家喜歡的話,給個👍,點個關注!給大家分享更多有趣好玩的Python黑科技!
歡迎關注 『Python黑科技』 系列,持續更新中
歡迎關注 『Python黑科技』 系列,持續更新中
【Python黑科技】tkinter庫實戰7個小項目合集(保姆級圖文+實現代碼)
【Python黑科技】tkinter庫實戰制作一個計算器(保姆級圖文+實現代碼)
【Python黑科技】tkinter庫實戰制作一個記事本(保姆級圖文+實現代碼)
【Python黑科技】tkinter庫實戰用戶的注冊和登錄(保姆級圖文+實現代碼)
【Python黑科技】tkinter庫實戰“2048”小游戲(保姆級圖文+實現代碼)
【Python黑科技】tkinter庫實戰“俄羅斯方塊”小游戲(保姆級圖文+實現代碼)
【Python黑科技】tkinter庫實戰“貪吃蛇”小游戲(保姆級圖文+實現代碼)
【Python黑科技】tkinter庫實戰“連連看”小游戲(保姆級圖文+實現代碼)
【Python安裝第三方庫一行命令永久提高速度】
【使用PyInstaller打包exe】
【免登陸爬蟲一鍵下載知乎文章圖片(保姆級圖文+實現代碼)】
【孤獨的程序員和AI機器人朋友聊天解悶(免費接口+保姆級圖文+實現代碼注釋)】
【幾行代碼繪制gif動圖(保姆級圖文+實現代碼)】
【幾行代碼實現網課定時循環截屏,保存重要知識點(保姆級圖文+實現代碼)】
【常用的user_agent 瀏覽器頭爬蟲模擬用戶(保姆級圖文+實現代碼)】
【更多內容敬請期待】
總結
以上是生活随笔為你收集整理的【Python黑科技】tkinter库实战“俄罗斯方块”小游戏(保姆级图文+实现代码)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: echarts 地图去除点击事件_ECh
- 下一篇: JS一个元素怎么绑定多个事件