最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python實(shí)現(xiàn)俄羅斯方塊小游戲

 更新時(shí)間:2020年04月24日 15:14:58   作者:Code進(jìn)階狼人  
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)俄羅斯方塊小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

回顧我們的python制作小游戲之路,幾篇非常精彩的文章

我們用python實(shí)現(xiàn)了坦克大戰(zhàn)

python制作坦克大戰(zhàn)

我們用python實(shí)現(xiàn)了飛船大戰(zhàn)

python制作飛船大戰(zhàn)

我們用python實(shí)現(xiàn)了兩種不同的貪吃蛇游戲

200行python代碼實(shí)現(xiàn)貪吃蛇游戲

150行代碼實(shí)現(xiàn)貪吃蛇游戲

我們用python實(shí)現(xiàn)了掃雷游戲

python實(shí)現(xiàn)掃雷游戲

我們用python實(shí)現(xiàn)了五子棋游戲

python實(shí)現(xiàn)五子棋游戲

今天我們用python來(lái)實(shí)現(xiàn)小時(shí)候玩過(guò)的俄羅斯方塊游戲吧
具體代碼與文件可以訪問(wèn)我的GitHub地址獲取

第一步——構(gòu)建各種方塊

import random
from collections import namedtuple

Point = namedtuple('Point', 'X Y')
Shape = namedtuple('Shape', 'X Y Width Height')
Block = namedtuple('Block', 'template start_pos end_pos name next')

# 方塊形狀的設(shè)計(jì),我最初我是做成 4 × 4,因?yàn)殚L(zhǎng)寬最長(zhǎng)都是4,這樣旋轉(zhuǎn)的時(shí)候就不考慮怎么轉(zhuǎn)了,就是從一個(gè)圖形替換成另一個(gè)
# 其實(shí)要實(shí)現(xiàn)這個(gè)功能,只需要固定左上角的坐標(biāo)就可以了

# S形方塊
S_BLOCK = [Block(['.OO',
  'OO.',
  '...'], Point(0, 0), Point(2, 1), 'S', 1),
 Block(['O..',
  'OO.',
  '.O.'], Point(0, 0), Point(1, 2), 'S', 0)]
# Z形方塊
Z_BLOCK = [Block(['OO.',
  '.OO',
  '...'], Point(0, 0), Point(2, 1), 'Z', 1),
 Block(['.O.',
  'OO.',
  'O..'], Point(0, 0), Point(1, 2), 'Z', 0)]
# I型方塊
I_BLOCK = [Block(['.O..',
  '.O..',
  '.O..',
  '.O..'], Point(1, 0), Point(1, 3), 'I', 1),
 Block(['....',
  '....',
  'OOOO',
  '....'], Point(0, 2), Point(3, 2), 'I', 0)]
# O型方塊
O_BLOCK = [Block(['OO',
  'OO'], Point(0, 0), Point(1, 1), 'O', 0)]
# J型方塊
J_BLOCK = [Block(['O..',
  'OOO',
  '...'], Point(0, 0), Point(2, 1), 'J', 1),
 Block(['.OO',
  '.O.',
  '.O.'], Point(1, 0), Point(2, 2), 'J', 2),
 Block(['...',
  'OOO',
  '..O'], Point(0, 1), Point(2, 2), 'J', 3),
 Block(['.O.',
  '.O.',
  'OO.'], Point(0, 0), Point(1, 2), 'J', 0)]
# L型方塊
L_BLOCK = [Block(['..O',
  'OOO',
  '...'], Point(0, 0), Point(2, 1), 'L', 1),
 Block(['.O.',
  '.O.',
  '.OO'], Point(1, 0), Point(2, 2), 'L', 2),
 Block(['...',
  'OOO',
  'O..'], Point(0, 1), Point(2, 2), 'L', 3),
 Block(['OO.',
  '.O.',
  '.O.'], Point(0, 0), Point(1, 2), 'L', 0)]
# T型方塊
T_BLOCK = [Block(['.O.',
  'OOO',
  '...'], Point(0, 0), Point(2, 1), 'T', 1),
 Block(['.O.',
  '.OO',
  '.O.'], Point(1, 0), Point(2, 2), 'T', 2),
 Block(['...',
  'OOO',
  '.O.'], Point(0, 1), Point(2, 2), 'T', 3),
 Block(['.O.',
  'OO.',
  '.O.'], Point(0, 0), Point(1, 2), 'T', 0)]

BLOCKS = {'O': O_BLOCK,
 'I': I_BLOCK,
 'Z': Z_BLOCK,
 'T': T_BLOCK,
 'L': L_BLOCK,
 'S': S_BLOCK,
 'J': J_BLOCK}


def get_block():
 block_name = random.choice('OIZTLSJ')
 b = BLOCKS[block_name]
 idx = random.randint(0, len(b) - 1)
 return b[idx]


def get_next_block(block):
 b = BLOCKS[block.name]
 return b[block.next]

第二部——構(gòu)建主函數(shù)

import sys
import time
import pygame
from pygame.locals import *
import blocks

SIZE = 30 # 每個(gè)小方格大小
BLOCK_HEIGHT = 25 # 游戲區(qū)高度
BLOCK_WIDTH = 10 # 游戲區(qū)寬度
BORDER_WIDTH = 4 # 游戲區(qū)邊框?qū)挾?
BORDER_COLOR = (40, 40, 200) # 游戲區(qū)邊框顏色
SCREEN_WIDTH = SIZE * (BLOCK_WIDTH + 5) # 游戲屏幕的寬
SCREEN_HEIGHT = SIZE * BLOCK_HEIGHT # 游戲屏幕的高
BG_COLOR = (40, 40, 60) # 背景色
BLOCK_COLOR = (20, 128, 200) #
BLACK = (0, 0, 0)
RED = (200, 30, 30) # GAME OVER 的字體顏色


def print_text(screen, font, x, y, text, fcolor=(255, 255, 255)):
 imgText = font.render(text, True, fcolor)
 screen.blit(imgText, (x, y))


def main():
 pygame.init()
 screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
 pygame.display.set_caption('俄羅斯方塊')

 font1 = pygame.font.SysFont('SimHei', 24) # 黑體24
 font2 = pygame.font.Font(None, 72) # GAME OVER 的字體
 font_pos_x = BLOCK_WIDTH * SIZE + BORDER_WIDTH + 10 # 右側(cè)信息顯示區(qū)域字體位置的X坐標(biāo)
 gameover_size = font2.size('GAME OVER')
 font1_height = int(font1.size('得分')[1])

 cur_block = None # 當(dāng)前下落方塊
 next_block = None # 下一個(gè)方塊
 cur_pos_x, cur_pos_y = 0, 0

 game_area = None # 整個(gè)游戲區(qū)域
 game_over = True
 start = False # 是否開(kāi)始,當(dāng)start = True,game_over = True 時(shí),才顯示 GAME OVER
 score = 0 # 得分
 orispeed = 0.5 # 原始速度
 speed = orispeed # 當(dāng)前速度
 pause = False # 暫停
 last_drop_time = None # 上次下落時(shí)間
 last_press_time = None # 上次按鍵時(shí)間

 def _dock():
 nonlocal cur_block, next_block, game_area, cur_pos_x, cur_pos_y, game_over, score, speed
 for _i in range(cur_block.start_pos.Y, cur_block.end_pos.Y + 1):
 for _j in range(cur_block.start_pos.X, cur_block.end_pos.X + 1):
 if cur_block.template[_i][_j] != '.':
  game_area[cur_pos_y + _i][cur_pos_x + _j] = '0'
 if cur_pos_y + cur_block.start_pos.Y <= 0:
 game_over = True
 else:
 # 計(jì)算消除
 remove_idxs = []
 for _i in range(cur_block.start_pos.Y, cur_block.end_pos.Y + 1):
 if all(_x == '0' for _x in game_area[cur_pos_y + _i]):
  remove_idxs.append(cur_pos_y + _i)
 if remove_idxs:
 # 計(jì)算得分
 remove_count = len(remove_idxs)
 if remove_count == 1:
  score += 100
 elif remove_count == 2:
  score += 300
 elif remove_count == 3:
  score += 700
 elif remove_count == 4:
  score += 1500
 speed = orispeed - 0.03 * (score // 10000)
 # 消除
 _i = _j = remove_idxs[-1]
 while _i >= 0:
  while _j in remove_idxs:
  _j -= 1
  if _j < 0:
  game_area[_i] = ['.'] * BLOCK_WIDTH
  else:
  game_area[_i] = game_area[_j]
  _i -= 1
  _j -= 1
 cur_block = next_block
 next_block = blocks.get_block()
 cur_pos_x, cur_pos_y = (BLOCK_WIDTH - cur_block.end_pos.X - 1) // 2, -1 - cur_block.end_pos.Y

 def _judge(pos_x, pos_y, block):
 nonlocal game_area
 for _i in range(block.start_pos.Y, block.end_pos.Y + 1):
 if pos_y + block.end_pos.Y >= BLOCK_HEIGHT:
 return False
 for _j in range(block.start_pos.X, block.end_pos.X + 1):
 if pos_y + _i >= 0 and block.template[_i][_j] != '.' and game_area[pos_y + _i][pos_x + _j] != '.':
  return False
 return True

 while True:
 for event in pygame.event.get():
 if event.type == QUIT:
 sys.exit()
 elif event.type == KEYDOWN:
 if event.key == K_RETURN:
  if game_over:
  start = True
  game_over = False
  score = 0
  last_drop_time = time.time()
  last_press_time = time.time()
  game_area = [['.'] * BLOCK_WIDTH for _ in range(BLOCK_HEIGHT)]
  cur_block = blocks.get_block()
  next_block = blocks.get_block()
  cur_pos_x, cur_pos_y = (BLOCK_WIDTH - cur_block.end_pos.X - 1) // 2, -1 - cur_block.end_pos.Y
 elif event.key == K_SPACE:
  if not game_over:
  pause = not pause
 elif event.key in (K_w, K_UP):
  # 旋轉(zhuǎn)
  # 其實(shí)記得不是很清楚了,比如
  # .0.
  # .00
  # ..0
  # 這個(gè)在最右邊靠邊的情況下是否可以旋轉(zhuǎn),我試完了網(wǎng)上的俄羅斯方塊,是不能旋轉(zhuǎn)的,這里我們就按不能旋轉(zhuǎn)來(lái)做
  # 我們?cè)谛螤钤O(shè)計(jì)的時(shí)候做了很多的空白,這樣只需要規(guī)定整個(gè)形狀包括空白部分全部在游戲區(qū)域內(nèi)時(shí)才可以旋轉(zhuǎn)
  if 0 <= cur_pos_x <= BLOCK_WIDTH - len(cur_block.template[0]):
  _next_block = blocks.get_next_block(cur_block)
  if _judge(cur_pos_x, cur_pos_y, _next_block):
  cur_block = _next_block

 if event.type == pygame.KEYDOWN:
 if event.key == pygame.K_LEFT:
 if not game_over and not pause:
  if time.time() - last_press_time > 0.1:
  last_press_time = time.time()
  if cur_pos_x > - cur_block.start_pos.X:
  if _judge(cur_pos_x - 1, cur_pos_y, cur_block):
  cur_pos_x -= 1
 if event.key == pygame.K_RIGHT:
 if not game_over and not pause:
  if time.time() - last_press_time > 0.1:
  last_press_time = time.time()
  # 不能移除右邊框
  if cur_pos_x + cur_block.end_pos.X + 1 < BLOCK_WIDTH:
  if _judge(cur_pos_x + 1, cur_pos_y, cur_block):
  cur_pos_x += 1
 if event.key == pygame.K_DOWN:
 if not game_over and not pause:
  if time.time() - last_press_time > 0.1:
  last_press_time = time.time()
  if not _judge(cur_pos_x, cur_pos_y + 1, cur_block):
  _dock()
  else:
  last_drop_time = time.time()
  cur_pos_y += 1

 _draw_background(screen)

 _draw_game_area(screen, game_area)

 _draw_gridlines(screen)

 _draw_info(screen, font1, font_pos_x, font1_height, score)
 # 畫(huà)顯示信息中的下一個(gè)方塊
 _draw_block(screen, next_block, font_pos_x, 30 + (font1_height + 6) * 5, 0, 0)

 if not game_over:
 cur_drop_time = time.time()
 if cur_drop_time - last_drop_time > speed:
 if not pause:
  # 不應(yīng)該在下落的時(shí)候來(lái)判斷到底沒(méi),我們玩俄羅斯方塊的時(shí)候,方塊落到底的瞬間是可以進(jìn)行左右移動(dòng)
  if not _judge(cur_pos_x, cur_pos_y + 1, cur_block):
  _dock()
  else:
  last_drop_time = cur_drop_time
  cur_pos_y += 1
 else:
 if start:
 print_text(screen, font2,
  (SCREEN_WIDTH - gameover_size[0]) // 2, (SCREEN_HEIGHT - gameover_size[1]) // 2,
  'GAME OVER', RED)

 # 畫(huà)當(dāng)前下落方塊
 _draw_block(screen, cur_block, 0, 0, cur_pos_x, cur_pos_y)

 pygame.display.flip()


# 畫(huà)背景
def _draw_background(screen):
 # 填充背景色
 screen.fill(BG_COLOR)
 # 畫(huà)游戲區(qū)域分隔線
 pygame.draw.line(screen, BORDER_COLOR,
  (SIZE * BLOCK_WIDTH + BORDER_WIDTH // 2, 0),
  (SIZE * BLOCK_WIDTH + BORDER_WIDTH // 2, SCREEN_HEIGHT), BORDER_WIDTH)


# 畫(huà)網(wǎng)格線
def _draw_gridlines(screen):
 # 畫(huà)網(wǎng)格線 豎線
 for x in range(BLOCK_WIDTH):
 pygame.draw.line(screen, BLACK, (x * SIZE, 0), (x * SIZE, SCREEN_HEIGHT), 1)
 # 畫(huà)網(wǎng)格線 橫線
 for y in range(BLOCK_HEIGHT):
 pygame.draw.line(screen, BLACK, (0, y * SIZE), (BLOCK_WIDTH * SIZE, y * SIZE), 1)


# 畫(huà)已經(jīng)落下的方塊
def _draw_game_area(screen, game_area):
 if game_area:
 for i, row in enumerate(game_area):
 for j, cell in enumerate(row):
 if cell != '.':
  pygame.draw.rect(screen, BLOCK_COLOR, (j * SIZE, i * SIZE, SIZE, SIZE), 0)


# 畫(huà)單個(gè)方塊
def _draw_block(screen, block, offset_x, offset_y, pos_x, pos_y):
 if block:
 for i in range(block.start_pos.Y, block.end_pos.Y + 1):
 for j in range(block.start_pos.X, block.end_pos.X + 1):
 if block.template[i][j] != '.':
  pygame.draw.rect(screen, BLOCK_COLOR,
   (offset_x + (pos_x + j) * SIZE, offset_y + (pos_y + i) * SIZE, SIZE, SIZE), 0)


# 畫(huà)得分等信息
def _draw_info(screen, font, pos_x, font_height, score):
 print_text(screen, font, pos_x, 10, f'得分: ')
 print_text(screen, font, pos_x, 10 + font_height + 6, f'{score}')
 print_text(screen, font, pos_x, 20 + (font_height + 6) * 2, f'速度: ')
 print_text(screen, font, pos_x, 20 + (font_height + 6) * 3, f'{score // 10000}')
 print_text(screen, font, pos_x, 30 + (font_height + 6) * 4, f'下一個(gè):')


if __name__ == '__main__':
 main()

游戲截圖

運(yùn)行效果

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

更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專(zhuān)題,也分享給大家:

C++經(jīng)典小游戲匯總

python經(jīng)典小游戲匯總

python俄羅斯方塊游戲集合

JavaScript經(jīng)典游戲 玩不停

java經(jīng)典小游戲匯總

javascript經(jīng)典小游戲匯總

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 用Python從0開(kāi)始實(shí)現(xiàn)一個(gè)中文拼音輸入法的思路詳解

    用Python從0開(kāi)始實(shí)現(xiàn)一個(gè)中文拼音輸入法的思路詳解

    中文輸入法是一個(gè)歷史悠久的問(wèn)題,但也實(shí)在是個(gè)繁瑣的活,不知道這是不是網(wǎng)上很少有人分享中文拼音輸入法的原因,接下來(lái)通過(guò)本文給大家分享使用Python從0開(kāi)始實(shí)現(xiàn)一個(gè)中文拼音輸入法,需要的朋友可以參考下
    2019-07-07
  • python 實(shí)現(xiàn)端口掃描工具

    python 實(shí)現(xiàn)端口掃描工具

    這篇文章主要介紹了python 實(shí)現(xiàn)端口掃描工具的示例代碼,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-12-12
  • Python將列表數(shù)據(jù)寫(xiě)入文件(txt, csv,excel)

    Python將列表數(shù)據(jù)寫(xiě)入文件(txt, csv,excel)

    這篇文章主要介紹了Python將列表數(shù)據(jù)寫(xiě)入文件(txt, csv,excel),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • PyTorch?TensorFlow機(jī)器學(xué)習(xí)框架選擇實(shí)戰(zhàn)

    PyTorch?TensorFlow機(jī)器學(xué)習(xí)框架選擇實(shí)戰(zhàn)

    這篇文章主要為大家介紹了PyTorch?TensorFlow機(jī)器學(xué)習(xí)框架選擇實(shí)戰(zhàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • Python腳本實(shí)現(xiàn)datax全量同步mysql到hive

    Python腳本實(shí)現(xiàn)datax全量同步mysql到hive

    這篇文章主要和大家分享一下mysql全量同步到hive自動(dòng)生成json文件的python腳本,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參加一下
    2024-10-10
  • 在Python中測(cè)試訪問(wèn)同一數(shù)據(jù)的競(jìng)爭(zhēng)條件的方法

    在Python中測(cè)試訪問(wèn)同一數(shù)據(jù)的競(jìng)爭(zhēng)條件的方法

    這篇文章主要介紹了在Python中測(cè)試訪問(wèn)同一數(shù)據(jù)的競(jìng)爭(zhēng)條件的方法,探究多線程或多進(jìn)程情況下優(yōu)先訪問(wèn)權(quán)的問(wèn)題,需要的朋友可以參考下
    2015-04-04
  • Python HTTP庫(kù) requests 的簡(jiǎn)單使用詳情

    Python HTTP庫(kù) requests 的簡(jiǎn)單使用詳情

    requests是Python的一個(gè)HTTP客戶(hù)端庫(kù),基于urllib標(biāo)準(zhǔn)庫(kù),在urllib標(biāo)準(zhǔn)庫(kù)的基礎(chǔ)上做了高度封裝,因此更加簡(jiǎn)潔好用,下面就由小編來(lái)給大家詳細(xì)介紹吧,需要的朋友可以參考下
    2021-09-09
  • python實(shí)現(xiàn)跳表SkipList的示例代碼

    python實(shí)現(xiàn)跳表SkipList的示例代碼

    這篇文章主要介紹了python實(shí)現(xiàn)跳表SkipList的示例代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-07-07
  • python定時(shí)器(Timer)用法簡(jiǎn)單實(shí)例

    python定時(shí)器(Timer)用法簡(jiǎn)單實(shí)例

    這篇文章主要介紹了python定時(shí)器(Timer)用法,以一個(gè)簡(jiǎn)單實(shí)例形式分析了定時(shí)器(Timer)實(shí)現(xiàn)延遲調(diào)用的技巧,需要的朋友可以參考下
    2015-06-06
  • 解決TensorFlow程序無(wú)限制占用GPU的方法

    解決TensorFlow程序無(wú)限制占用GPU的方法

    這篇文章主要介紹了解決TensorFlow程序無(wú)限制占用GPU的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06

最新評(píng)論

苍山县| 永嘉县| 新建县| 香格里拉县| 舞阳县| 垫江县| 寿阳县| 闵行区| 缙云县| 嘉禾县| 九江市| 南部县| 三都| 阿克| 洞口县| 富顺县| 铅山县| 台前县| 都昌县| 旌德县| 沙河市| 岳池县| 南阳市| 朝阳区| 永丰县| 昆山市| 隆化县| 广河县| 衡阳市| 长治市| 仁寿县| 泊头市| 红河县| 醴陵市| 绥滨县| 长治县| 海阳市| 永平县| 灵宝市| 丰城市| 渝北区|