日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

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

python 基于pygame實(shí)現(xiàn)俄羅斯方塊

瀏覽:133日期:2022-06-26 15:48:59
一、簡(jiǎn)單說明

80、90后的小伙伴都玩過“俄羅斯方塊”,那種“叱咤風(fēng)云”場(chǎng)景 偶爾閃現(xiàn)在腦海 真的是太爽了;如果沒有來得及玩過的同學(xué),這次可以真正的自己做一個(gè)了

本實(shí)例用的是Python3(當(dāng)然了Python3.5 3.6 3.7....都行 )+ pygame實(shí)現(xiàn)的

運(yùn)行之前需要安裝pygame模塊,安裝命令如下

pip install pygame -i https://mirrors.aliyun.com/pypi/simple/二、運(yùn)行效果

python 基于pygame實(shí)現(xiàn)俄羅斯方塊

python 基于pygame實(shí)現(xiàn)俄羅斯方塊

三、完整代碼

文件main.py代碼如下:

'''作者:it項(xiàng)目實(shí)例網(wǎng)更多項(xiàng)目實(shí)例,請(qǐng)?jiān)L問:www.itprojects.cn'''import randomimport sysimport timeimport pygamefrom blocks import block_s, block_i, block_j, block_l, block_o, block_t, block_zSCREEN_WIDTH, SCREEN_HEIGHT = 450, 750BG_COLOR = (40, 40, 60) # 背景色BLOCK_COL_NUM = 10 # 每行的方格數(shù)SIZE = 30 # 每個(gè)小方格大小BLOCK_ROW_NUM = 25 # 每列的方個(gè)數(shù)BORDER_WIDTH = 4 # 游戲區(qū)邊框?qū)挾萊ED = (200, 30, 30) # 紅色,GAME OVER 的字體顏色def judge_game_over(stop_all_block_list): ''' 判斷游戲是否結(jié)束 ''' if 'O' in stop_all_block_list[0]: return Truedef change_speed(score): speed_level = [('1', 0.5, 0, 20), ('2', 0.4, 21, 50), ('3', 0.3, 51, 100), ('4', 0.2, 101, 200), ('5', 0.1, 201, None)] for speed_info, speed, score_start, score_stop in speed_level: if score_stop and score_start <= score <= score_stop: return speed_info, speed elif score_stop is None and score >= score_start: return speed_info, speeddef judge_lines(stop_all_block_list): ''' 判斷是否有同一行的方格,如果有則消除 ''' # 記錄剛剛消除的行數(shù) move_row_list = list() # 消除滿格的行 for row, line in enumerate(stop_all_block_list): if '.' not in line: # 如果這一行沒有. 那么就意味著全部是O,則消除這一行 stop_all_block_list[row] = [’.’ for _ in range(len(line))] move_row_list.append(row) # 如果沒有滿格的行,則結(jié)束此函數(shù) if not move_row_list: return 0 # 移動(dòng)剩余的行到下一行 for row in move_row_list: stop_all_block_list.pop(row) stop_all_block_list.insert(0, [’.’ for _ in range(len(line))]) return len(move_row_list) * 10def add_to_stop_all_block_list(stop_all_block_list, current_block, current_block_start_row, current_block_start_col): ''' 將當(dāng)前已經(jīng)停止移動(dòng)的block添加到列表中 ''' for row, line in enumerate(current_block): for col, block in enumerate(line): if block != ’.’: stop_all_block_list[current_block_start_row + row][current_block_start_col + col] = 'O'def change_current_block_style(current_block): ''' 改變圖形的樣式 ''' # 計(jì)算出,當(dāng)前圖形樣式屬于哪個(gè)圖形 current_block_style_list = None for block_style_list in [block_s, block_i, block_j, block_l, block_o, block_t, block_z]: if current_block in block_style_list: current_block_style_list = block_style_list # 得到當(dāng)前正在用的圖形的索引(下標(biāo)) index = current_block_style_list.index(current_block) # 它的下一個(gè)圖形的索引 index += 1 # 防止越界 index = index % len(current_block_style_list) # 返回下一個(gè)圖形 return current_block_style_list[index]def judge_move_right(current_block, current_block_start_col): ''' 判斷是否可以向右移動(dòng) ''' # 先判斷列的方式是從右到左 for col in range(len(current_block[0]) - 1, -1, -1): # 得到1列的所有元素 col_list = [line[col] for line in current_block] # 判斷是否碰到右邊界 if ’O’ in col_list and current_block_start_col + col >= BLOCK_COL_NUM: return False return Truedef judge_move_left(current_block, current_block_start_col): ''' 判斷是否可以向左移動(dòng) ''' # 先判斷列的方式是從左到右 for col in range(len(current_block[0])): # 得到1列的所有元素 col_list = [line[col] for line in current_block] # 判斷是否碰到右邊界 if ’O’ in col_list and current_block_start_col + col < 0: return False return Truedef judge_move_down(current_block, current_block_start_row, current_block_start_col, stop_all_block_list): ''' 判斷是否碰撞到其它圖形或者底邊界 ''' # 得到其它圖形所有的坐標(biāo) stop_all_block_position = list() for row, line in enumerate(stop_all_block_list): for col, block in enumerate(line): if block != '.': stop_all_block_position.append((row, col)) # print(stop_all_block_position) # 判斷碰撞 for row, line in enumerate(current_block): if ’O’ in line and current_block_start_row + row >= BLOCK_ROW_NUM: # 如果當(dāng)前行有0,且從起始行開始算+當(dāng)前顯示的行,超過了總行數(shù),那么就認(rèn)為碰到了底部 return False for col, block in enumerate(line): if block != '.' and (current_block_start_row + row, current_block_start_col + col) in stop_all_block_position: return False return Truedef get_block(): ''' 創(chuàng)建一個(gè)圖形 ''' block_style_list = random.choice([block_s, block_i, block_j, block_l, block_o, block_t, block_z]) return random.choice(block_style_list)def main(): pygame.init() screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption(’俄羅斯方塊’) current_block = get_block() # 當(dāng)前圖形 current_block_start_row = -2 # 當(dāng)前圖片從哪一行開始顯示圖形 current_block_start_col = 4 # 當(dāng)前圖形從哪一列開始顯示 next_block = get_block() # 下一個(gè)圖形 last_time = time.time() speed = 0.5 # 降落的速度 speed_info = ’1’ # 顯示的速度等級(jí) # 定義一個(gè)列表,用來存儲(chǔ)所有的已經(jīng)停止移動(dòng)的形狀 stop_all_block_list = [[’.’ for i in range(BLOCK_COL_NUM)] for j in range(BLOCK_ROW_NUM)] # 字體 font = pygame.font.Font(’yh.ttf’, 24) # 黑體24 game_over_font = pygame.font.Font('yh.ttf', 72) game_over_font_width, game_over_font_height = game_over_font.size(’GAME OVER’) game_again_font_width, game_again_font_height = font.size(’鼠標(biāo)點(diǎn)擊任意位置,再來一局’) # 得分 score = 0 # 標(biāo)記游戲是否結(jié)束 game_over = False # 創(chuàng)建計(jì)時(shí)器(防止while循環(huán)過快,占用太多CPU的問題) clock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: if judge_move_left(current_block, current_block_start_col - 1): current_block_start_col -= 1 elif event.key == pygame.K_RIGHT: if judge_move_right(current_block, current_block_start_col + 1): current_block_start_col += 1 elif event.key == pygame.K_UP: current_block_next_style = change_current_block_style(current_block) if judge_move_left(current_block_next_style, current_block_start_col) andjudge_move_right(current_block_next_style, current_block_start_col) andjudge_move_down(current_block, current_block_start_row, current_block_start_col, stop_all_block_list): # 判斷新的樣式?jīng)]有越界 current_block = current_block_next_style elif event.key == pygame.K_DOWN: # 判斷是否可以向下移動(dòng),如果碰到底部或者其它的圖形就不能移動(dòng)了 if judge_move_down(current_block, current_block_start_row + 1, current_block_start_col, stop_all_block_list): current_block_start_row += 1 elif event.type == pygame.MOUSEBUTTONDOWN and event.button: if game_over: # 重置游戲用到的變量 current_block = get_block() # 當(dāng)前圖形 current_block_start_row = -2 # 當(dāng)前圖片從哪一行開始顯示圖形 current_block_start_col = 4 # 當(dāng)前圖形從哪一列開始顯示 next_block = get_block() # 下一個(gè)圖形 stop_all_block_list = [[’.’ for i in range(BLOCK_COL_NUM)] for j in range(BLOCK_ROW_NUM)] score = 0 game_over = False # 判斷是否修改當(dāng)前圖形顯示的起始行 if not game_over and time.time() - last_time > speed: last_time = time.time() # 判斷是否可以向下移動(dòng),如果碰到底部或者其它的圖形就不能移動(dòng)了 if judge_move_down(current_block, current_block_start_row + 1, current_block_start_col, stop_all_block_list): current_block_start_row += 1 else: # 將這個(gè)圖形存儲(chǔ)到統(tǒng)一的列表中,這樣便于判斷是否成為一行 add_to_stop_all_block_list(stop_all_block_list, current_block, current_block_start_row, current_block_start_col) # 判斷是否有同一行的,如果有就消除,且加上分?jǐn)?shù) score += judge_lines(stop_all_block_list) # 判斷游戲是否結(jié)束(如果第一行中間有O那么就表示游戲結(jié)束) game_over = judge_game_over(stop_all_block_list) # 調(diào)整速度 speed_info, speed = change_speed(score) # 創(chuàng)建新的圖形 current_block = next_block next_block = get_block() # 重置數(shù)據(jù) current_block_start_col = 4 current_block_start_row = -2 # 畫背景(填充背景色) screen.fill(BG_COLOR) # 畫游戲區(qū)域分隔線 pygame.draw.line(screen, (100, 40, 200), (SIZE * BLOCK_COL_NUM, 0), (SIZE * BLOCK_COL_NUM, SCREEN_HEIGHT), BORDER_WIDTH) # 顯示當(dāng)前圖形 for row, line in enumerate(current_block): for col, block in enumerate(line): if block != ’.’: pygame.draw.rect(screen, (20, 128, 200), ((current_block_start_col + col) * SIZE, (current_block_start_row + row) * SIZE, SIZE, SIZE), 0) # 顯示所有停止移動(dòng)的圖形 for row, line in enumerate(stop_all_block_list): for col, block in enumerate(line): if block != ’.’: pygame.draw.rect(screen, (20, 128, 200), (col * SIZE, row * SIZE, SIZE, SIZE), 0) # 畫網(wǎng)格線 豎線 for x in range(BLOCK_COL_NUM): pygame.draw.line(screen, (0, 0, 0), (x * SIZE, 0), (x * SIZE, SCREEN_HEIGHT), 1) # 畫網(wǎng)格線 橫線 for y in range(BLOCK_ROW_NUM): pygame.draw.line(screen, (0, 0, 0), (0, y * SIZE), (BLOCK_COL_NUM * SIZE, y * SIZE), 1) # 顯示右側(cè)(得分、速度、下一行圖形) # 得分 score_show_msg = font.render(’得分: ’, True, (255, 255, 255)) screen.blit(score_show_msg, (BLOCK_COL_NUM * SIZE + 10, 10)) score_show_msg = font.render(str(score), True, (255, 255, 255)) screen.blit(score_show_msg, (BLOCK_COL_NUM * SIZE + 10, 50)) # 速度 speed_show_msg = font.render(’速度: ’, True, (255, 255, 255)) screen.blit(speed_show_msg, (BLOCK_COL_NUM * SIZE + 10, 100)) speed_show_msg = font.render(speed_info, True, (255, 255, 255)) screen.blit(speed_show_msg, (BLOCK_COL_NUM * SIZE + 10, 150)) # 下一個(gè)圖形(文字提示) next_style_msg = font.render(’下一個(gè): ’, True, (255, 255, 255)) screen.blit(next_style_msg, (BLOCK_COL_NUM * SIZE + 10, 200)) # 下一個(gè)圖形(圖形) for row, line in enumerate(next_block): for col, block in enumerate(line): if block != ’.’: pygame.draw.rect(screen, (20, 128, 200), (320 + SIZE * col, (BLOCK_COL_NUM + row) * SIZE, SIZE, SIZE), 0) # 顯示這個(gè)方格的4個(gè)邊的顏色 # 左 pygame.draw.line(screen, (0, 0, 0), (320 + SIZE * col, (BLOCK_COL_NUM + row) * SIZE), (320 + SIZE * col, (BLOCK_COL_NUM + row + 1) * SIZE), 1) # 上 pygame.draw.line(screen, (0, 0, 0), (320 + SIZE * col, (BLOCK_COL_NUM + row) * SIZE), (320 + SIZE * (col + 1), (BLOCK_COL_NUM + row) * SIZE), 1) # 下 pygame.draw.line(screen, (0, 0, 0), (320 + SIZE * col, (BLOCK_COL_NUM + row + 1) * SIZE), (320 + SIZE * (col + 1), (BLOCK_COL_NUM + row + 1) * SIZE), 1) # 右 pygame.draw.line(screen, (0, 0, 0), (320 + SIZE * (col + 1), (BLOCK_COL_NUM + row) * SIZE), (320 + SIZE * (col + 1), (BLOCK_COL_NUM + row + 1) * SIZE), 1) # 顯示游戲結(jié)束畫面 if game_over: game_over_tips = game_over_font.render(’GAME OVER’, True, RED) screen.blit(game_over_tips, ((SCREEN_WIDTH - game_over_font_width) // 2, (SCREEN_HEIGHT - game_over_font_height) // 2)) # 顯示'鼠標(biāo)點(diǎn)擊任意位置,再來一局' game_again = font.render(’鼠標(biāo)點(diǎn)擊任意位置,再來一局’, True, RED) screen.blit(game_again, ((SCREEN_WIDTH - game_again_font_width) // 2, (SCREEN_HEIGHT - game_again_font_height) // 2 + 80)) # 刷新顯示(此時(shí)窗口才會(huì)真正的顯示) pygame.display.update() # FPS(每秒鐘顯示畫面的次數(shù)) clock.tick(60) # 通過一定的延時(shí),實(shí)現(xiàn)1秒鐘能夠循環(huán)60次if __name__ == ’__main__’: main()

文件blocks.py代碼如下:

# S形方塊block_s = [[’.OO’, ’OO.’, ’...’], [’O..’, ’OO.’, ’.O.’]]# Z形方塊block_z = [[’OO.’, ’.OO’, ’...’], [’.O.’, ’OO.’, ’O..’]]# I型方塊block_i = [[’.O..’, ’.O..’, ’.O..’, ’.O..’], [’....’, ’....’, ’OOOO’, ’....’]]# O型方塊block_o = [[’OO’, ’OO’]]# J型方塊block_j = [[’O..’, ’OOO’, ’...’], [’.OO’, ’.O.’, ’.O.’], [’...’, ’OOO’, ’..O’], [’.O.’, ’.O.’, ’OO.’]]# L型方塊block_l = [[’..O’, ’OOO’, ’...’], [’.O.’, ’.O.’, ’.OO’], [’...’, ’OOO’, ’O..’], [’OO.’, ’.O.’, ’.O.’]]# T型方塊block_t = [[’.O.’, ’OOO’, ’...’], [’.O.’, ’.OO’, ’.O.’], [’...’, ’OOO’, ’.O.’], [’.O.’, ’OO.’, ’.O.’]]

以上就是python 基于pygame實(shí)現(xiàn)俄羅斯方塊的詳細(xì)內(nèi)容,更多關(guān)于python 俄羅斯方塊的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Python 編程
相關(guān)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
日韩精品诱惑一区?区三区| 国产亚洲在线| 国产精品1区在线| 成人在线黄色| 欧美精品黄色| 日韩高清成人在线| 国产精品久久久久蜜臀| 亚洲高清不卡| 亚洲精品激情| 国产精品美女午夜爽爽| 高清不卡亚洲| 免费看欧美美女黄的网站| 国产午夜精品一区在线观看| 国产黄大片在线观看| 夜久久久久久| 国产精品videosex极品| 欧美.日韩.国产.一区.二区| 日韩超碰人人爽人人做人人添| 国产va免费精品观看精品视频| 亚洲综合不卡| 麻豆视频一区| 免费视频久久| 亚洲精品永久免费视频| 亚洲1区在线观看| 国产超碰精品| 亚洲精品乱码日韩| 欧美日韩在线观看首页| 伊人精品久久| 日韩激情一区| 亚洲三级国产| 四虎4545www国产精品| 日韩福利视频一区| 日韩高清不卡| 国产精品一区二区三区美女| 香蕉精品视频在线观看| 久久不见久久见国语| 伊人久久亚洲热| 国产一区二区三区网| 免费看日韩精品| 92国产精品| 国产精品tv| 亚洲日本欧美| 不卡中文字幕| 中文一区一区三区高中清不卡免费| 亚洲欧美一级| 日韩视频一区二区三区在线播放免费观看| 捆绑调教美女网站视频一区| 免费观看在线综合| 亚洲调教视频在线观看| 精品一区二区三区四区五区| 一区二区精品| 一本色道久久精品| 欧美成a人免费观看久久| 国产精品伊人| 亚洲午夜国产成人| 亚洲国产专区校园欧美| 国产不卡精品在线| 欧美一区二区三区久久精品| 亚洲欧美不卡| 久久国产影院| 成人在线丰满少妇av| 日韩福利视频一区| 在线看片日韩| 国产亚洲综合精品| 日韩不卡在线| 国产夫妻在线| 国产一区二区三区四区| 国产精品一区二区三区av| 亚洲欧美日韩视频二区| 蜜臀av免费一区二区三区| 天堂а√在线最新版中文在线| 老司机精品视频网| 国产精品va| 免费一级欧美片在线观看网站| 日韩精品a在线观看91| 综合激情一区| 久久av一区二区三区| 欧美~级网站不卡| 国产91精品对白在线播放| 日韩在线短视频| 91亚洲国产| 中文在线а√在线8| 成午夜精品一区二区三区软件| 久久一区精品| 国产精品成人a在线观看| 精品国产乱码久久久久久樱花| 美女尤物国产一区| 精品久久国产一区| 国产精品99久久精品| 色一区二区三区四区| 丰满少妇一区| 国产亚洲一区二区手机在线观看| www在线观看黄色| 正在播放日韩精品| 亚洲精品一区三区三区在线观看| 日本韩国欧美超级黄在线观看| 麻豆视频在线看| 日韩精品一卡| 午夜精品一区二区三区国产| 亚洲激情二区| 中文字幕日韩亚洲| 日韩精品乱码av一区二区| 青青草国产成人99久久| 国产高清精品二区| 狠狠久久伊人中文字幕| 美女福利一区二区三区| 欧美日中文字幕| 日韩中文字幕麻豆| 日韩成人精品一区二区三区| 欧美激情日韩| 一区二区精品伦理...| 久久中文视频| 亚洲精品字幕| 国产探花一区二区| 精品一区二区三区中文字幕在线| 中文字幕在线官网| 99riav1国产精品视频| 日本不卡高清视频| 久久亚洲黄色| 久久一级电影| 中文字幕一区二区三区在线视频| 久久激五月天综合精品| 九九99久久精品在免费线bt| 日韩欧美一区二区三区免费观看| 国产一级久久| 久久国内精品| 国产高潮在线| 另类av一区二区| 久久激情五月激情| 日韩免费小视频| 一区二区电影| 97视频热人人精品免费| 不卡一区2区| 国产日韩亚洲欧美精品| 麻豆视频在线看| 蜜臀久久久久久久| 精品一区91| 日韩午夜在线| 久久精品国产福利| 亚洲激情av| 美女高潮久久久| 悠悠资源网久久精品| 欧美日韩一区二区三区四区在线观看| 岛国av在线网站| 亚洲精品护士| 日韩成人高清| 日本成人手机在线| 99久久激情| 国产日产高清欧美一区二区三区| 婷婷激情一区| 国产欧美午夜| 在线亚洲观看| 高潮久久久久久久久久久久久久| 免费在线观看一区二区三区| 精品国产aⅴ| 一区二区精品| 特黄特色欧美大片| 久久国产精品免费一区二区三区| 欧美影院三区| 精品一区二区三区视频在线播放| 三级一区在线视频先锋| 国产精品精品| 久久精品xxxxx| 先锋影音久久久| 日韩在线观看不卡| 国产精品高潮呻吟久久久久| 黄色亚洲在线| 福利欧美精品在线| 日韩中文字幕在线一区| 999国产精品视频| 麻豆精品久久| 婷婷成人av| 五月天久久久| 岛国av在线播放| 日韩动漫一区| 免费在线欧美视频| 久久中文亚洲字幕| 国产一区二区三区探花| 日韩激情综合| 羞羞答答国产精品www一本| 日韩欧美午夜| 久久99高清| 国产精品国码视频| 69堂免费精品视频在线播放| 久久亚洲图片| 99视频一区| 伊人精品一区| 狠狠躁少妇一区二区三区| 久久不见久久见中文字幕免费| 日本一不卡视频| 热久久国产精品| 在线视频日韩| 不卡av一区二区| 91精品蜜臀一区二区三区在线| 日产精品一区二区| 国产精品1luya在线播放| 日韩激情啪啪| 亚洲欧美专区| 亚洲色图国产| 亚洲我射av|