在上一節(jié)中巴元,我們創(chuàng)建了一個(gè)隨機(jī)組合的圖形組, 如何繪制呢?
(1)我們有圖形的行列位置, 有形狀, 有顏色, 首先论矾, 我們需要一個(gè) 函數(shù), 能夠根據(jù)這些信息來(lái)繪制一個(gè)圖形:
def drawIcon(displaysurf, shape, color, boxx, boxy):
# 根據(jù)位置,形狀沛简,顏色齐鲤,繪制一個(gè)圖形
quarter = int(BOXSIZE * 0.25)
half = int(BOXSIZE * 0.5)
left, top = leftTopCoordsOfBox(boxx, boxy)
if shape == DONUT:
pygame.draw.circle(displaysurf, color, (left+half, top+half), half-5)
pygame.draw.circle(displaysurf, BGCOLOR, (left+half, top+half), quarter-5)
elif shape == DIAMOND:
pygame.draw.polygon(displaysurf, color, ((left+half, top),
(left+BOXSIZE-1, top+half),
(left+half, top+BOXSIZE-1),
(left, top+half)))
elif shape == SQUARE:
pygame.draw.rect(displaysurf, color, (left+quarter, top+quarter,
BOXSIZE-half, BOXSIZE-half))
elif shape == LINES:
for i in range(0, BOXSIZE, 4):
pygame.draw.line(displaysurf, color, (left, top+i), (left + i, top))
pygame.draw.line(displaysurf, color, (left+i, top+BOXSIZE-1),
(left+BOXSIZE-1, top+i))
elif shape == OVAL:
pygame.draw.ellipse(displaysurf, color, (left, top+quarter, BOXSIZE, half))
(2)然后,再創(chuàng)建一個(gè)函數(shù)椒楣,來(lái)繪制所有的隨機(jī)圖形的組合
def drawBoard(displaysurf, board):
# 繪制所有的圖形組
for boxx in range(BOARD_WIDTH):
for boxy in range(BOARD_HEIGHT):
shape, color = getShapeAndColor(board, boxx, boxy)
drawIcon(displaysurf, shape, color, boxx, boxy)
(3)在main函數(shù)中測(cè)試一下功能:
def main():
pygame.init()
fpsclock = pygame.time.Clock()
displaysurf = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("Memory Game")
displaysurf.fill(BGCOLOR)
# 創(chuàng)建圖形組合
board = getRandomizedBoard()
while True:
displaysurf.fill(BGCOLOR)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
# 繪制圖形組合
drawBoard(displaysurf, board
pygame.display.update()
fpsclock.tick(FPS)
if __name__ == '__main__':
main()
結(jié)果顯示如下:
ScreenClip.png
下面是到目前為止的完整代碼:
# Memory Puzzle
import random, pygame, sys
from pygame.locals import *
# 配置基礎(chǔ) ------------------------------------------------------------
FPS = 30 # 設(shè)置幀數(shù)為30
WINDOW_WIDTH = 640 # 窗口寬度640像素
WINDOW_HEIGHT = 480 # 窗口告訴480像素
BOARD_WIDTH = 3 # 每行有3個(gè)圖形
BOARD_HEIGHT = 2 # 每行有2個(gè)圖形
BOXSIZE = 40 # 圖形范圍
GAPSIZE = 10 # 每個(gè)圖形之間的間隔
# 左邊邊緣位置
XMARGIN = int((WINDOW_WIDTH - BOARD_WIDTH*(BOXSIZE+GAPSIZE)) / 2)
# 頂部邊緣位置
YMARGIN = int((WINDOW_HEIGHT - BOARD_HEIGHT*(BOXSIZE+GAPSIZE)) /2 )
# 游戲中需要用到的顏色設(shè)置 -----------------------------------------------
# R G B 顏色
GRAY = (100,100,100)
NAVYBLUE = (60,60,100)
WHITE = (255,255,255)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
YELLOW = (255,255,0)
ORANGE = (255,128,0)
PUPPLE = (255,0,255)
CYAN = (0,255,255)
BGCOLOR = NAVYBLUE # 背景色設(shè)置為navyblue
ALLCOLORS = (RED, GREEN, BLUE, YELLOW, ORANGE, PUPPLE, CYAN)
# 游戲中用到的5種形狀 ---------------------------------------------------
DONUT = 'donut'
SQUARE = 'square'
DIAMOND = 'diamond'
LINES = 'lines'
OVAL = 'oval'
ALLSHAPES = (DONUT, SQUARE, DIAMOND, LINES, OVAL)
# 工具函數(shù) -------------------------------------------------------------
def leftTopCoordsOfBox(boxx, boxy):
# 將圖形行列位置轉(zhuǎn)換為屏幕上的像素坐標(biāo)
left = boxx * (BOXSIZE + GAPSIZE) + XMARGIN
top = boxy * (BOXSIZE + GAPSIZE) + YMARGIN
return left, top
def getRandomizedBoard():
# 生成隨機(jī)的圖形顏色組合
icons = [] # 用列表保存
for color in ALLCOLORS:
for shape in ALLSHAPES:
icons.append((shape, color))
random.shuffle(icons) # 打亂序列
numIconsUsed = int(BOARD_WIDTH * BOARD_HEIGHT / 2) # 計(jì)算要使用的圖形數(shù)
icons = icons[:numIconsUsed] * 2 # 根據(jù)要使用的圖形數(shù)截取出來(lái)圖形, 并翻倍配對(duì)
random.shuffle(icons) # 再次打亂圖形
# 將創(chuàng)建好的圖形放入圖形組列表
board = []
for x in range(BOARD_WIDTH):
column = []
for y in range(BOARD_HEIGHT):
column.append(icons[0])
del icons[0]
board.append(column)
return board
def getShapeAndColor(board, boxx, boxy):
# 根據(jù)行列信息返回形狀和顏色
return board[boxx][boxy][0], board[boxx][boxy][1]
def drawIcon(displaysurf, shape, color, boxx, boxy):
# 根據(jù)位置佳遂,形狀,顏色撒顿,繪制一個(gè)圖形
quarter = int(BOXSIZE * 0.25)
half = int(BOXSIZE * 0.5)
left, top = leftTopCoordsOfBox(boxx, boxy)
if shape == DONUT:
pygame.draw.circle(displaysurf, color, (left+half, top+half), half-5)
pygame.draw.circle(displaysurf, BGCOLOR, (left+half, top+half), quarter-5)
elif shape == DIAMOND:
pygame.draw.polygon(displaysurf, color, ((left+half, top),
(left+BOXSIZE-1, top+half),
(left+half, top+BOXSIZE-1),
(left, top+half)))
elif shape == SQUARE:
pygame.draw.rect(displaysurf, color, (left+quarter, top+quarter,
BOXSIZE-half, BOXSIZE-half))
elif shape == LINES:
for i in range(0, BOXSIZE, 4):
pygame.draw.line(displaysurf, color, (left, top+i), (left + i, top))
pygame.draw.line(displaysurf, color, (left+i, top+BOXSIZE-1),
(left+BOXSIZE-1, top+i))
elif shape == OVAL:
pygame.draw.ellipse(displaysurf, color, (left, top+quarter, BOXSIZE, half))
def drawBoard(displaysurf, board):
# 繪制所有的圖形組
for boxx in range(BOARD_WIDTH):
for boxy in range(BOARD_HEIGHT):
shape, color = getShapeAndColor(board, boxx, boxy)
drawIcon(displaysurf, shape, color, boxx, boxy)
# 定義main函數(shù) ---------------------------------------------------------
def main():
pygame.init() # pygame初始化
fpsclock = pygame.time.Clock()
displaysurf = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("Memory Game") # 設(shè)置窗口標(biāo)題
displaysurf.fill(BGCOLOR)
# 創(chuàng)建圖形組合
board = getRandomizedBoard()
while True: # 游戲主題循環(huán)
displaysurf.fill(BGCOLOR) # 用背景色填充窗口
for event in pygame.event.get(): # 獲取游戲事件
if event.type == QUIT:
pygame.quit()
sys.exit()
# 繪制圖形組合
drawBoard(displaysurf, board)
pygame.display.update()
fpsclock.tick(FPS)
if __name__ == '__main__':
main()