python實(shí)現(xiàn)俄羅斯方塊
網(wǎng)上搜到一個(gè)Pygame寫(xiě)的俄羅斯方塊(tetris),大部分看懂的前提下增加了注釋?zhuān)現(xiàn)edora19下運(yùn)行OK的
主程序:
#coding:utf8
#! /usr/bin/env python
# 注釋說(shuō)明:shape表示一個(gè)俄羅斯方塊形狀 cell表示一個(gè)小方塊
import sys
from random import choice
import pygame
from pygame.locals import *
from block import O, I, S, Z, L, J, T
COLS = 16
ROWS = 20
CELLS = COLS * ROWS
CELLPX = 32 # 每個(gè)cell的像素寬度
POS_FIRST_APPEAR = COLS / 2
SCREEN_SIZE = (COLS * CELLPX, ROWS * CELLPX)
COLOR_BG = (0, 0, 0)
def draw(grid, pos=None):
# grid是一個(gè)list,要么值為None,要么值為'Block'
# 非空值在eval()的作用下,用于配置顏色
if pos: # 6x5
s = pos - 3 - 2 * COLS # upper left position
for p in range(0, COLS):
q = s + p * COLS
for i in range(q, q + 6):
if 0 <= i < CELLS:
# 0 <=i < CELLS:表示i這個(gè)cell在board內(nèi)部。
c = eval(grid[i] + ".color") if grid[i] else COLOR_BG
# 執(zhí)行著色。shape的cell涂對(duì)應(yīng)的class設(shè)定好的顏色,否則涂黑(背景色)
a = i % COLS * CELLPX
b = i / COLS * CELLPX
screen.fill(c, (a, b, CELLPX, CELLPX))
else: # all
screen.fill(COLOR_BG)
for i, occupied in enumerate(grid):
if occupied:
c = eval(grid[i] + ".color") # 獲取方塊對(duì)應(yīng)的顏色
a = i % COLS * CELLPX # 橫向長(zhǎng)度
b = i / COLS * CELLPX # 縱向長(zhǎng)度
screen.fill(c, (a, b, CELLPX, CELLPX))
# fill:為cell上色, 第二個(gè)參數(shù)表示rect
pygame.display.flip()
# 刷新屏幕
def phi(grid1, grid2, pos): # 4x4
# 兩個(gè)grid之4*4區(qū)域內(nèi)是否會(huì)相撞(沖突)
s = pos - 2 - 1 * COLS # upper left position
for p in range(0, 4):
q = s + p * COLS
for i in range(q, q + 4):
try:
if grid1[i] and grid2[i]:
return False
except:
pass
return True
def merge(grid1, grid2):
# 合并兩個(gè)grid
grid = grid1[:]
for i, c in enumerate(grid2):
if c:
grid[i] = c
return grid
def complete(grid):
# 減去滿(mǎn)行
n = 0
for i in range(0, CELLS, COLS):
# 步長(zhǎng)為一行。
if not None in grid[i:i + COLS]:
#這一句很容易理解錯(cuò)誤。
#實(shí)際含義是:如果grid[i:i + COLS]都不是None,那么執(zhí)行下面的語(yǔ)句
grid = [None] * COLS + grid[:i] + grid[i + COLS:]
n += 1
return grid, n
#n表示減去的行數(shù),用作統(tǒng)計(jì)分?jǐn)?shù)
pygame.init()
pygame.event.set_blocked(None)
pygame.event.set_allowed((KEYDOWN, QUIT))
pygame.key.set_repeat(75, 0)
pygame.display.set_caption('Tetris')
screen = pygame.display.set_mode(SCREEN_SIZE)
pygame.display.update()
grid = [None] * CELLS
speed = 500
screen.fill(COLOR_BG)
while True: # spawn a block
block = choice([O, I, S, Z, L, J, T])()
pos = POS_FIRST_APPEAR
if not phi(grid, block.grid(pos), pos): break # you lose
pygame.time.set_timer(KEYDOWN, speed)
# repeatedly create an event on the event queue
# speed是時(shí)間間隔。。。speed越小,方塊下落的速度越快。。。speed應(yīng)該換為其他名字
while True: # move the block
draw(merge(grid, block.grid(pos)), pos)
event = pygame.event.wait()
if event.type == QUIT: sys.exit()
try:
aim = {
K_UNKNOWN: pos+COLS,
K_UP: pos,
K_DOWN: pos+COLS,
K_LEFT: pos-1,
K_RIGHT: pos+1,
}[event.key]
except KeyError:
continue
if event.key == K_UP:
# 變形
block.rotate()
elif event.key in (K_LEFT, K_RIGHT) and pos / COLS != aim / COLS:
# pos/COLS表示當(dāng)前位置所在行
# aim/COLS表示目標(biāo)位置所在行
# 此判斷表示,當(dāng)shape在左邊界時(shí),不允許再向左移動(dòng)(越界。。),在最右邊時(shí)向右也禁止
continue
grid_aim = block.grid(aim)
if grid_aim and phi(grid, grid_aim, aim):
pos = aim
else:
if event.key == K_UP:
block.rotate(times=3)
elif not event.key in (K_LEFT, K_RIGHT):
break
grid = merge(grid, block.grid(pos))
grid, n = complete(grid)
if n:
draw(grid)
speed -= 5 * n
if speed < 75: speed = 75
調(diào)用的模塊:
#coding:utf-8
#! /usr/bin/env python
COLS = 16
ROWS = 20
class Block():
color = (255,255,255)
def __init__(self):
self._state = 0
def __str__(self):
return self.__class__.__name__
def _orientations(self):
raise NotImplementedError()
def rotate(self, times=1):
for i in range(times):
if len(self._orientations())-1 == self._state:
self._state = 0
#只要_state比_orientations長(zhǎng)度-1還要小,就讓_state加1
else:
self._state += 1
def blades(self):
# 返回對(duì)應(yīng)形狀的一種旋轉(zhuǎn)形狀。(返回一個(gè)list,list中每個(gè)元素是一個(gè)(x,y))
return self._orientations()[self._state]
def grid(self, pos, cols=COLS, rows=ROWS):
# grid()函數(shù):對(duì)于一個(gè)形狀,從它的cell中的pos位置,按照orientations的位置提示,把所有cell涂色
# pos表示的是shape中的一個(gè)cell,也就是(0,0)
if cols*rows <= pos:
return None
# 這種情況應(yīng)該不可能出現(xiàn)吧。如果出現(xiàn)<=的情況
# 那么,pos都跑到界外了。。
grid = [None] * cols * rows
grid[pos] = str(self)
for b in self.blades():
x, y = b
# pos/cols表示pos處于board的第幾行
if pos/cols != (pos+x)/cols:
return None
i = pos + x + y * cols
if i < 0:
continue
elif cols*rows <= i:
return None
grid[i] = str(self)
# 給相應(yīng)的其他位置都“涂色”,比如對(duì)于方塊,是O型的,那么pos肯定是有值的,pos位于有上角。。
return grid
# 以下每個(gè)形狀class,_orientations()都返回形狀的列表。(0,0)一定被包含在其中,為了省略空間所以都沒(méi)有寫(xiě)出.
class O(Block):
color = (207,247,0)
def _orientations(self):
return (
[(-1,0), (-1,1), (0,1)],
)
class I(Block):
color = (135,240,60)
def _orientations(self):
return (
[(-2,0), (-1,0), (1,0)],
[(0,-1), (0,1), (0,2)],
)
class S(Block):
color = (171,252,113)
def _orientations(self):
return (
[(1,0), (-1,1), (0,1)],
[(0,-1), (1,0), (1,1)],
)
class Z(Block):
color = (243,61,110)
def _orientations(self):
return (
[(-1,0), (0,1), (1,1)],
[(1,-1), (1,0), (0,1)],
)
class L(Block):
color = (253,205,217)
def _orientations(self):
return (
[(-1,1), (-1,0), (1,0)],
[(0,-1), (0,1), (1,1)],
[(-1,0), (1,0), (1,-1)],
[(-1,-1), (0,-1), (0,1)],
)
class J(Block):
color = (140,180,225)
def _orientations(self):
return (
[(-1,0), (1,0), (1,1)],
[(0,1), (0,-1), (1,-1)],
[(-1,-1), (-1,0), (1,0)],
[(-1,1), (0,1), (0,-1)],
)
class T(Block):
color = (229,251,113)
def _orientations(self):
return (
[(-1,0), (0,1), (1,0)],
[(0,-1), (0,1), (1,0)],
[(-1,0), (0,-1), (1,0)],
[(-1,0), (0,-1), (0,1)],
)
更多俄羅斯方塊精彩文章請(qǐng)點(diǎn)擊專(zhuān)題:俄羅斯方塊游戲集合 進(jìn)行學(xué)習(xí)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 用Python編寫(xiě)一個(gè)簡(jiǎn)單的俄羅斯方塊游戲的教程
- python實(shí)現(xiàn)俄羅斯方塊游戲
- python實(shí)現(xiàn)簡(jiǎn)單的俄羅斯方塊
- python實(shí)現(xiàn)簡(jiǎn)單俄羅斯方塊
- python和pygame實(shí)現(xiàn)簡(jiǎn)單俄羅斯方塊游戲
- python實(shí)現(xiàn)俄羅斯方塊小游戲
- python實(shí)現(xiàn)俄羅斯方塊游戲(改進(jìn)版)
- python編寫(xiě)俄羅斯方塊
- Python實(shí)現(xiàn)簡(jiǎn)單的俄羅斯方塊游戲
- 利用python制作俄羅斯方塊詳細(xì)圖文教程
相關(guān)文章
解讀Scrapy回調(diào)函數(shù)callback傳遞參數(shù)的方式
這篇文章主要介紹了解讀Scrapy回調(diào)函數(shù)callback傳遞參數(shù)的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
Python實(shí)現(xiàn)將多個(gè)文件的名稱(chēng)或后綴名由大寫(xiě)改為小寫(xiě)
這篇文章主要介紹了如何基于Python語(yǔ)言實(shí)現(xiàn)將多個(gè)文件的名稱(chēng)或后綴名由大寫(xiě)字母修改為小寫(xiě),文中的示例代碼講解詳細(xì),感興趣的可以了解下2023-09-09
django admin.py 外鍵,反向查詢(xún)的實(shí)例
今天小編就為大家分享一篇django admin.py 外鍵,反向查詢(xún)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07
關(guān)于Python的文本文件轉(zhuǎn)換編碼問(wèn)題
這篇文章主要介紹了關(guān)于Python的文本文件轉(zhuǎn)換編碼問(wèn)題,編程過(guò)程中,經(jīng)成會(huì)遇到字符編碼的問(wèn)題,需要的朋友可以參考下2023-04-04
Django高級(jí)編程之自定義Field實(shí)現(xiàn)多語(yǔ)言
這篇文章主要介紹了Django高級(jí)編程之自定義Field實(shí)現(xiàn)多語(yǔ)言,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
淺談python標(biāo)準(zhǔn)庫(kù)--functools.partial
這篇文章主要介紹了python標(biāo)準(zhǔn)庫(kù)--functools.partial,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
python3 實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)連接池的示例代碼
這篇文章主要介紹了python3 實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)連接池的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04

