美文网首页
玩蛇系列之Pygame教程(十二)-- Tetromino俄罗斯

玩蛇系列之Pygame教程(十二)-- Tetromino俄罗斯

作者: 峰峰小 | 来源:发表于2017-04-10 02:52 被阅读506次

失踪人口回归了!

失踪人口回归了!

失踪人口回归了!

实在是抱歉,这么久没更新这个系列的文章了,(●'◡'●),都是因为接了个坑爹的外包,改去干老本行撸android代码了,感觉外包还是有点坑,钱没赚到多少,人都累死了。

搞外包期间,一时勾起了我自己撸app的兴趣,就没时间写文章了,顺便给自己撸的应用打个广告
http://www.coolapk.com/apk/com.ckdroid.seachimg


有兴趣可以下载下来玩玩,很好用的哦 (●'◡'●)

好了,废话不多说了,这次带来的是俄罗斯方块的实现。

先上个图:

游戏主要是对各种形状piece(S,Z,I,O,J,L,T)的处理,代码注释我写得也比较清楚了,模块都和上一篇 《玩蛇系列之Pygame教程(十一)-- Wormy贪吃蛇》 大同小异,甚至感觉比贪吃蛇还要好理解,主要还是python对数组的操作实在是太简洁方便了,可能会有点不好理解,不过认真多看一下,还是看的懂的。

友情提醒:
我已经把代码托管到github了,这样对游戏用到的资源(图片,字体,音效等)管理会方便些,你们也可以直接到github下载到所有的代码和资源。
但是,对于代码的学习,我还是觉得不要简单的复制粘贴,不然感觉还是领悟不到的

GitHub:https://github.com/ckdroid/PygameLearning

代码:

# -*- coding: UTF-8 -*-
'''
Created on 2017年1月7日

@author: 小峰峰
'''

import random, time, pygame, sys
from pygame.locals import *



FPS = 25 #设置屏幕刷新率
WINDOWWIDTH = 640 # 设置窗口宽度
WINDOWHEIGHT = 480 # 设置窗口高度
BOXSIZE = 20 # 方格大小

# 放置俄罗斯方块窗口的大小
BOARDWIDTH = 10 
BOARDHEIGHT = 20

BLANK = '.' # 代表空的形状

MOVESIDEWAYSFREQ = 0.15 # ?
MOVEDOWNFREQ = 0.1 # 向下的频率

# x方向的边距
XMARGIN = int((WINDOWWIDTH - BOARDWIDTH * BOXSIZE) / 2)
# 距离窗口顶部的边距
TOPMARGIN = WINDOWHEIGHT - (BOARDHEIGHT * BOXSIZE) - 5

# 定义几个颜色
#               R    G    B
WHITE       = (255, 255, 255)
GRAY        = (185, 185, 185)
BLACK       = (  0,   0,   0)
RED         = (155,   0,   0)
LIGHTRED    = (175,  20,  20)
GREEN       = (  0, 155,   0)
LIGHTGREEN  = ( 20, 175,  20)
BLUE        = (  0,   0, 155)
LIGHTBLUE   = ( 20,  20, 175)
YELLOW      = (155, 155,   0)
LIGHTYELLOW = (175, 175,  20)




BORDERCOLOR = BLUE
BGCOLOR = BLACK
TEXTCOLOR = WHITE
TEXTSHADOWCOLOR = GRAY


COLORS      = (     BLUE,      GREEN,      RED,      YELLOW)
LIGHTCOLORS = (LIGHTBLUE, LIGHTGREEN, LIGHTRED, LIGHTYELLOW)

# 断言 每一个颜色都应该对应有亮色
assert len(COLORS) == len(LIGHTCOLORS) # each color must have light color

# 模板的宽高
TEMPLATEWIDTH = 5
TEMPLATEHEIGHT = 5

# 形状_S(S旋转有2种)
S_SHAPE_TEMPLATE = [['.....',
                     '.....',
                     '..OO.',
                     '.OO..',
                     '.....'],
                    ['.....',
                     '..O..',
                     '..OO.',
                     '...O.',
                     '.....']]

# 形状_Z(Z旋转有2种)
Z_SHAPE_TEMPLATE = [['.....',
                     '.....',
                     '.OO..',
                     '..OO.',
                     '.....'],
                    ['.....',
                     '..O..',
                     '.OO..',
                     '.O...',
                     '.....']]

# 形状_I(I旋转有2种)
I_SHAPE_TEMPLATE = [['..O..',
                     '..O..',
                     '..O..',
                     '..O..',
                     '.....'],
                    ['.....',
                     '.....',
                     'OOOO.',
                     '.....',
                     '.....']]

# 形状_O(O旋转只有一个)
O_SHAPE_TEMPLATE = [['.....',
                     '.....',
                     '.OO..',
                     '.OO..',
                     '.....']]

# 形状_J(J旋转有4种)
J_SHAPE_TEMPLATE = [['.....',
                     '.O...',
                     '.OOO.',
                     '.....',
                     '.....'],
                    ['.....',
                     '..OO.',
                     '..O..',
                     '..O..',
                     '.....'],
                    ['.....',
                     '.....',
                     '.OOO.',
                     '...O.',
                     '.....'],
                    ['.....',
                     '..O..',
                     '..O..',
                     '.OO..',
                     '.....']]

# 形状_L(L旋转有4种)
L_SHAPE_TEMPLATE = [['.....',
                     '...O.',
                     '.OOO.',
                     '.....',
                     '.....'],
                    ['.....',
                     '..O..',
                     '..O..',
                     '..OO.',
                     '.....'],
                    ['.....',
                     '.....',
                     '.OOO.',
                     '.O...',
                     '.....'],
                    ['.....',
                     '.OO..',
                     '..O..',
                     '..O..',
                     '.....']]

# 形状_T(T旋转有4种)
T_SHAPE_TEMPLATE = [['.....',
                     '..O..',
                     '.OOO.',
                     '.....',
                     '.....'],
                    ['.....',
                     '..O..',
                     '..OO.',
                     '..O..',
                     '.....'],
                    ['.....',
                     '.....',
                     '.OOO.',
                     '..O..',
                     '.....'],
                    ['.....',
                     '..O..',
                     '.OO..',
                     '..O..',
                     '.....']]

# 定义一个数据结构存储,对应的形状
PIECES = {'S': S_SHAPE_TEMPLATE,
          'Z': Z_SHAPE_TEMPLATE,
          'J': J_SHAPE_TEMPLATE,
          'L': L_SHAPE_TEMPLATE,
          'I': I_SHAPE_TEMPLATE,
          'O': O_SHAPE_TEMPLATE,
          'T': T_SHAPE_TEMPLATE}


def main():
    global FPSCLOCK, DISPLAYSURF, BASICFONT, BIGFONT # 定义全局变量
    
    pygame.init() # 初始化pygame
    
    FPSCLOCK = pygame.time.Clock() # 获得pygame时钟
    
    DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT)) # 设置窗口
    
    BASICFONT = pygame.font.Font('resource/PAPYRUS.ttf', 18) # 设置基础的字体
    
    BIGFONT = pygame.font.Font('resource/PAPYRUS.ttf', 100) # 设置大字体
    
    pygame.display.set_caption('Tetromino') # 窗口标题

    showTextScreen('Tetromino') # 显示开始画面
    
    
    while True: # 游戏主循环
        
        # 二选一随机播放背景音乐
        if random.randint(0, 1) == 0:
            pygame.mixer.music.load('resource/tetrisb.mid')
        else:
            pygame.mixer.music.load('resource/tetrisc.mid')
        pygame.mixer.music.play(-1, 0.0)
        
        # 运行游戏
        runGame()
        
        # 退出游戏后,结束播放音乐
        pygame.mixer.music.stop()
        
        # 显示结束画面
        showTextScreen('Game Over')


# 运行游戏
def runGame():
    
    # 在游戏开始前初始化变量
    
    board = getBlankBoard() # 获得一个空的board
    
    lastMoveDownTime = time.time() # 最后向下移动的时刻
    
    lastMoveSidewaysTime = time.time() # 最后侧向移动的时刻
    
    lastFallTime = time.time() # 最后的下降时间
    
    # 是否可以  向下,向左,向右
    # 注意:这里没有向上可用
    movingDown = False 
    movingLeft = False
    movingRight = False
    
    # 分数
    score = 0
    
    # 根据分数计算等级和下降的频率
    level, fallFreq = calculateLevelAndFallFreq(score)

    # 获得新的形状(当前的形状)
    fallingPiece = getNewPiece()
    
    # 获得下一个形状
    nextPiece = getNewPiece()

    while True: # 游戏循环体
        
        if fallingPiece == None: # 当前没有下降的形状
            # 重新获得新的形状和下一个形状
            fallingPiece = nextPiece
            nextPiece = getNewPiece()
            
            # 重置最后下降的时间
            lastFallTime = time.time()

            
            # 判断界面上是否还有空位(方块是否到顶),没有则结束游戏
            if not isValidPosition(board, fallingPiece):
                return # can't fit a new piece on the board, so game over

        # 检查是否有退出事件
        checkForQuit()
        
        for event in pygame.event.get(): # 事件处理loop
            
            if event.type == KEYUP: # KEYUP事件处理
                if (event.key == K_p): # 用户按P键暂停
                    # Pausing the game
                    DISPLAYSURF.fill(BGCOLOR)
                    pygame.mixer.music.stop() #停止音乐
                    
                    showTextScreen('Paused') # 显示暂停界面,until a key press
                    
                    pygame.mixer.music.play(-1, 0.0) # 继续循环音乐
                    
                    # 重置各种时间
                    lastFallTime = time.time()
                    lastMoveDownTime = time.time()
                    lastMoveSidewaysTime = time.time()
                    
                elif (event.key == K_LEFT or event.key == K_a):
                    movingLeft = False
                elif (event.key == K_RIGHT or event.key == K_d):
                    movingRight = False
                elif (event.key == K_DOWN or event.key == K_s):
                    movingDown = False

            elif event.type == KEYDOWN: # KEYDOWN事件处理
                
                # 左右移动piece
                if (event.key == K_LEFT or event.key == K_a) and isValidPosition(board, fallingPiece, adjX=-1):
                    fallingPiece['x'] -= 1
                    movingLeft = True
                    movingRight = False
                    lastMoveSidewaysTime = time.time()

                elif (event.key == K_RIGHT or event.key == K_d) and isValidPosition(board, fallingPiece, adjX=1):
                    fallingPiece['x'] += 1
                    movingRight = True
                    movingLeft = False
                    lastMoveSidewaysTime = time.time()

                # UP或W键 旋转piece (在有空间旋转的前提下)
                elif (event.key == K_UP or event.key == K_w): # 正向旋转
                    fallingPiece['rotation'] = (fallingPiece['rotation'] + 1) % len(PIECES[fallingPiece['shape']])
                    if not isValidPosition(board, fallingPiece):
                        fallingPiece['rotation'] = (fallingPiece['rotation'] - 1) % len(PIECES[fallingPiece['shape']])
                elif (event.key == K_q): # Q键,反向旋转
                    fallingPiece['rotation'] = (fallingPiece['rotation'] - 1) % len(PIECES[fallingPiece['shape']])
                    if not isValidPosition(board, fallingPiece):
                        fallingPiece['rotation'] = (fallingPiece['rotation'] + 1) % len(PIECES[fallingPiece['shape']])

                # DOWN或S键 使piece下降得更快
                elif (event.key == K_DOWN or event.key == K_s):
                    movingDown = True
                    if isValidPosition(board, fallingPiece, adjY=1):
                        fallingPiece['y'] += 1
                    lastMoveDownTime = time.time()

                # 空格键,直接下降到最下面且可用的地方
                elif event.key == K_SPACE:
                    movingDown = False
                    movingLeft = False
                    movingRight = False
                    for i in range(1, BOARDHEIGHT):
                        if not isValidPosition(board, fallingPiece, adjY=i):
                            break
                    fallingPiece['y'] += i - 1

        # 根据记录的用户输入方向的变量来移动piece
        if (movingLeft or movingRight) and time.time() - lastMoveSidewaysTime > MOVESIDEWAYSFREQ:
            if movingLeft and isValidPosition(board, fallingPiece, adjX=-1):
                fallingPiece['x'] -= 1
            elif movingRight and isValidPosition(board, fallingPiece, adjX=1):
                fallingPiece['x'] += 1
            lastMoveSidewaysTime = time.time()

        if movingDown and time.time() - lastMoveDownTime > MOVEDOWNFREQ and isValidPosition(board, fallingPiece, adjY=1):
            fallingPiece['y'] += 1
            lastMoveDownTime = time.time()

        # 自动下降piece
        if time.time() - lastFallTime > fallFreq:
            # see if the piece has landed
            if not isValidPosition(board, fallingPiece, adjY=1):
                # falling piece has landed, set it on the board
                addToBoard(board, fallingPiece)
                score += removeCompleteLines(board)
                level, fallFreq = calculateLevelAndFallFreq(score)
                fallingPiece = None
            else:
                # piece did not land, just move the piece down
                fallingPiece['y'] += 1
                lastFallTime = time.time()

        # 绘制屏幕上的所有东西
        DISPLAYSURF.fill(BGCOLOR)
        drawBoard(board)
        drawStatus(score, level)
        drawNextPiece(nextPiece)
        if fallingPiece != None:
            drawPiece(fallingPiece)

        pygame.display.update()
        FPSCLOCK.tick(FPS)


# 创建文本绘制对象
def makeTextObjs(text, font, color):
    surf = font.render(text, True, color)
    return surf, surf.get_rect()


# 退出
def terminate():
    pygame.quit()
    sys.exit()


# 检查是否有按键被按下
def checkForKeyPress():
    # 通过事件队列寻找KEYUP事件
    # 从事件队列删除KEYDOWN事件
    
    checkForQuit()

    for event in pygame.event.get([KEYDOWN, KEYUP]):
        if event.type == KEYDOWN:
            continue
        return event.key
    return None


# 显示开始、暂停、结束画面
def showTextScreen(text):
    # This function displays large text in the
    # center of the screen until a key is pressed.
    # Draw the text drop shadow
    titleSurf, titleRect = makeTextObjs(text, BIGFONT, TEXTSHADOWCOLOR)
    titleRect.center = (int(WINDOWWIDTH / 2), int(WINDOWHEIGHT / 2))
    DISPLAYSURF.blit(titleSurf, titleRect)

    # Draw the text
    titleSurf, titleRect = makeTextObjs(text, BIGFONT, TEXTCOLOR)
    titleRect.center = (int(WINDOWWIDTH / 2) - 3, int(WINDOWHEIGHT / 2) - 3)
    DISPLAYSURF.blit(titleSurf, titleRect)

    # Draw the additional "Press a key to play." text.
    pressKeySurf, pressKeyRect = makeTextObjs('Press a key to play.', BASICFONT, TEXTCOLOR)
    pressKeyRect.center = (int(WINDOWWIDTH / 2), int(WINDOWHEIGHT / 2) + 100)
    DISPLAYSURF.blit(pressKeySurf, pressKeyRect)

    while checkForKeyPress() == None:
        pygame.display.update()
        FPSCLOCK.tick()


# 检查是否有退出事件
def checkForQuit():
    for event in pygame.event.get(QUIT): # get all the QUIT events
        terminate() # terminate if any QUIT events are present
    for event in pygame.event.get(KEYUP): # get all the KEYUP events
        if event.key == K_ESCAPE:
            terminate() # terminate if the KEYUP event was for the Esc key
        pygame.event.post(event) # put the other KEYUP event objects back


# 根据分数来计算等级和下落的频率
def calculateLevelAndFallFreq(score):
    
    level = int(score / 10) + 1
    fallFreq = 0.27 - (level * 0.02)
    return level, fallFreq

# 随机获得一个新的形状(形状,方向,颜色)
def getNewPiece():
    # return a random new piece in a random rotation and color
    shape = random.choice(list(PIECES.keys()))
    newPiece = {'shape': shape,
                'rotation': random.randint(0, len(PIECES[shape]) - 1),
                'x': int(BOARDWIDTH / 2) - int(TEMPLATEWIDTH / 2), # x居中
                'y': -2, # y在屏幕的上方,小于0
                'color': random.randint(0, len(COLORS)-1)}
    return newPiece


# 将一个piece添加到board中
def addToBoard(board, piece):
    # fill in the board based on piece's location, shape, and rotation
    for x in range(TEMPLATEWIDTH):
        for y in range(TEMPLATEHEIGHT):
            if PIECES[piece['shape']][piece['rotation']][y][x] != BLANK:
                board[x + piece['x']][y + piece['y']] = piece['color']

# 清空board
def getBlankBoard():
    # create and return a new blank board data structure
    board = []
    for i in range(BOARDWIDTH):
        board.append([BLANK] * BOARDHEIGHT)
    return board

# board边界
def isOnBoard(x, y):
    return x >= 0 and x < BOARDWIDTH and y < BOARDHEIGHT

# piece在当前的board里是否是一个合法可用的位置
def isValidPosition(board, piece, adjX=0, adjY=0):
    # Return True if the piece is within the board and not colliding
    for x in range(TEMPLATEWIDTH):
        for y in range(TEMPLATEHEIGHT):
            isAboveBoard = y + piece['y'] + adjY < 0
            if isAboveBoard or PIECES[piece['shape']][piece['rotation']][y][x] == BLANK:
                continue
            if not isOnBoard(x + piece['x'] + adjX, y + piece['y'] + adjY):
                return False
            if board[x + piece['x'] + adjX][y + piece['y'] + adjY] != BLANK:
                return False
    return True

# 判断当前的这行是否被全部填满
def isCompleteLine(board, y):
    # Return True if the line filled with boxes with no gaps.
    for x in range(BOARDWIDTH):
        if board[x][y] == BLANK:
            return False
    return True


# 检查每一行,移除完成填满的一行,将这一行上面的所有的都下降一行,返回完成填满的总行数
def removeCompleteLines(board):
    numLinesRemoved = 0
    # 从-1开始从下往上检查每一行
    y = BOARDHEIGHT - 1 
    while y >= 0:
        if isCompleteLine(board, y):
            # Remove the line and pull boxes down by one line.
            for pullDownY in range(y, 0, -1):
                for x in range(BOARDWIDTH):
                    board[x][pullDownY] = board[x][pullDownY-1]
            # Set very top line to blank.
            for x in range(BOARDWIDTH):
                board[x][0] = BLANK
            numLinesRemoved += 1
            # Note on the next iteration of the loop, y is the same.
            # This is so that if the line that was pulled down is also
            # complete, it will be removed.
        else:
            y -= 1 # move on to check next row up
    return numLinesRemoved

# 根据box的坐标转化成像素坐标
def convertToPixelCoords(boxx, boxy):
    # Convert the given xy coordinates of the board to xy
    # coordinates of the location on the screen.
    return (XMARGIN + (boxx * BOXSIZE)), (TOPMARGIN + (boxy * BOXSIZE))


# 绘制box
def drawBox(boxx, boxy, color, pixelx=None, pixely=None):
    # draw a single box (each tetromino piece has four boxes)
    # at xy coordinates on the board. Or, if pixelx & pixely
    # are specified, draw to the pixel coordinates stored in
    # pixelx & pixely (this is used for the "Next" piece).
    if color == BLANK:
        return
    if pixelx == None and pixely == None:
        pixelx, pixely = convertToPixelCoords(boxx, boxy)
    pygame.draw.rect(DISPLAYSURF, COLORS[color], (pixelx + 1, pixely + 1, BOXSIZE - 1, BOXSIZE - 1))
    pygame.draw.rect(DISPLAYSURF, LIGHTCOLORS[color], (pixelx + 1, pixely + 1, BOXSIZE - 4, BOXSIZE - 4))


# 绘制board
def drawBoard(board):
    # draw the border around the board
    pygame.draw.rect(DISPLAYSURF, BORDERCOLOR, (XMARGIN - 3, TOPMARGIN - 7, (BOARDWIDTH * BOXSIZE) + 8, (BOARDHEIGHT * BOXSIZE) + 8), 5)

    # fill the background of the board
    pygame.draw.rect(DISPLAYSURF, BGCOLOR, (XMARGIN, TOPMARGIN, BOXSIZE * BOARDWIDTH, BOXSIZE * BOARDHEIGHT))
    # draw the individual boxes on the board
    for x in range(BOARDWIDTH):
        for y in range(BOARDHEIGHT):
            drawBox(x, y, board[x][y])


# 绘制游戏分数、等级信息
def drawStatus(score, level):
    # draw the score text
    scoreSurf = BASICFONT.render('Score: %s' % score, True, TEXTCOLOR)
    scoreRect = scoreSurf.get_rect()
    scoreRect.topleft = (WINDOWWIDTH - 150, 20)
    DISPLAYSURF.blit(scoreSurf, scoreRect)

    # draw the level text
    levelSurf = BASICFONT.render('Level: %s' % level, True, TEXTCOLOR)
    levelRect = levelSurf.get_rect()
    levelRect.topleft = (WINDOWWIDTH - 150, 50)
    DISPLAYSURF.blit(levelSurf, levelRect)


# 绘制各种形状piece(S,Z,I,O,J,L,T)
def drawPiece(piece, pixelx=None, pixely=None):
    shapeToDraw = PIECES[piece['shape']][piece['rotation']]
    if pixelx == None and pixely == None:
        # if pixelx & pixely hasn't been specified, use the location stored in the piece data structure
        pixelx, pixely = convertToPixelCoords(piece['x'], piece['y'])

    # draw each of the boxes that make up the piece
    for x in range(TEMPLATEWIDTH):
        for y in range(TEMPLATEHEIGHT):
            if shapeToDraw[y][x] != BLANK:
                drawBox(None, None, piece['color'], pixelx + (x * BOXSIZE), pixely + (y * BOXSIZE))


# 绘制提示信息,下一个现状
def drawNextPiece(piece):
    # draw the "next" text
    nextSurf = BASICFONT.render('Next:', True, TEXTCOLOR)
    nextRect = nextSurf.get_rect()
    nextRect.topleft = (WINDOWWIDTH - 120, 80)
    DISPLAYSURF.blit(nextSurf, nextRect)
    # draw the "next" piece
    drawPiece(piece, pixelx=WINDOWWIDTH-120, pixely=100)


if __name__ == '__main__':
    main()

相关文章

网友评论

      本文标题:玩蛇系列之Pygame教程(十二)-- Tetromino俄罗斯

      本文链接:https://www.haomeiwen.com/subject/cnafbttx.html