小時候經常玩的一個遊戲,還有個俄羅斯方塊,話說經常撞牆有沒有。除了遊戲的基本框架,還需要三大結構,遊戲開始,遊戲運行,遊戲結束。走起。
import pygame, sys, random
from pygame.locals import *
FPS = 15
WINDOWWIDTH = 640
WINDOWHEIGHT = 480
CELLSIZE = 20
assert WINDOWWIDTH % CELLSIZE == 0, "window width must be a multiple of cell size"
assert WINDOWHEIGHT % CELLSIZE == 0, "window height must be multiple if cell size"
CELLWIDTH = WINDOWWIDTH / CELLSIZE
CELLHEIGHT = WINDOWHEIGHT / CELLSIZE
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
DARKGREEN = (0, 155, 0)
DARKGRAY = (40, 40, 40)
BGCOLOR = BLACK
UP = "up"
DOWN = "down"
LEFT = "left"
RIGHT = "right"
HEAD = 0
def main():
global FPSCLOCK, DISPLAYSURF, BASICFONT
pygame.init()
FPSCLOCK = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
BASICFONT = pygame.font.Font('freesansbold.ttf', 18)
pygame.display.set_caption('Wormy')
showStartScreen()
while True:
runGame()
showGameOverScreen()
def runGame():
startx = random.randint(5, CELLSIZE - 6)
starty = random.randint(5, CELLSIZE - 6)
wormCoods = [{'x': startx, 'y': starty},
{'x': startx - 1, 'y': starty},
{'x': startx - 2, 'y': starty}]
direction = RIGHT
apple = getRandomLocation()
while True:
for event in pygame.event.get():
if event.type == QUIT:
terminate()
elif event.type == KEYDOWN:
if (event.key == K_LEFT or event.key == K_a) and direction != RIGHT:
direction = LEFT
elif (event.key == K_RIGHT or event.key == K_d) and direction != LEFT:
direction = RIGHT
elif (event.key == K_UP or event.key == K_w) and direction != DOWN:
direction = UP
elif (event.key == K_DOWN or event.key == K_s) and direction != UP:
direction = DOWN
elif event.key == K_ESCAPE:
terminate()
if wormCoods[HEAD]['x'] == -1 or wormCoods[HEAD]['x'] == CELLWIDTH or wormCoods[HEAD]['y'] == -1 or wormCoods[HEAD]['y'] == CELLHEIGHT:
return
for wormBody in wormCoods[1:]:
if wormBody['x'] == wormCoods[HEAD]['x'] and wormBody['y'] == wormCoods[HEAD]['y']:
return
if wormCoods[HEAD]['x'] == apple['x'] and wormCoods[HEAD]['y'] == apple['y']:
apple = getRandomLocation()
else:
del wormCoods[-1]
if direction == UP:
newHead = {'x': wormCoods[HEAD]['x'], 'y': wormCoods[HEAD]['y'] - 1}
elif direction == DOWN:
newHead = {'x': wormCoods[HEAD]['x'], 'y': wormCoods[HEAD]['y'] + 1}
elif direction == LEFT:
newHead = {'x': wormCoods[HEAD]['x'] - 1, 'y': wormCoods[HEAD]['y']}
elif direction == RIGHT:
newHead = {'x': wormCoods[HEAD]['x'] + 1, 'y': wormCoods[HEAD]['y']}
wormCoods.insert(0, newHead)
DISPLAYSURF.fill(BGCOLOR)
drawGrid()
drawWorm(wormCoods)
drawApple(apple)
drawScore(len(wormCoods) - 3)
pygame.display.update()
FPSCLOCK.tick(FPS)
def drawPressKeyMsg():
pressKeySurf = BASICFONT.render('Press a key to play.', True, DARKGRAY)
pressKeyRect = pressKeySurf.get_rect()
pressKeyRect.topleft = (WINDOWWIDTH - 200, WINDOWHEIGHT - 30)
DISPLAYSURF.blit(pressKeySurf, pressKeyRect)
def checkForKeyPress():
if len(pygame.event.get(QUIT)) > 0:
terminate()
keyUpEvents = pygame.event.get(KEYUP)
if len(keyUpEvents) == 0:
return None
if keyUpEvents[0].key == K_ESCAPE:
terminate()
return keyUpEvents[0].key
def showStartScreen():
titleFont = pygame.font.Font('freesansbold.ttf', 100)
titleSurf1 = titleFont.render('Wormy!', True, WHITE, DARKGREEN)
titleSurf2 = titleFont.render('Wormy!', True, GREEN)
degrees1 = 0
degrees2 = 0
while True:
DISPLAYSURF.fill(BGCOLOR)
rotatedSurf1 = pygame.transform.rotate(titleSurf1, degrees1)
rotatedRect1 = rotatedSurf1.get_rect()
rotatedRect1.center = (WINDOWWIDTH / 2, WINDOWHEIGHT / 2)
DISPLAYSURF.blit(rotatedSurf1, rotatedRect1)
rotatedSurf2 = pygame.transform.rotate(titleSurf2, degrees2)
rotatedRect2 = rotatedSurf2.get_rect()
rotatedRect2.center = (WINDOWWIDTH / 2, WINDOWHEIGHT / 2)
DISPLAYSURF.blit(rotatedSurf2, rotatedRect2)
drawPressKeyMsg()
if checkForKeyPress():
pygame.event.get()
return
pygame.display.update()
FPSCLOCK.tick(FPS)
degrees1 += 3
degrees2 += 7
def terminate():
pygame.quit()
sys.exit()
def getRandomLocation():
return {'x': random.randint(0, CELLWIDTH - 1), 'y': random.randint(0, CELLHEIGHT - 1)}
def showGameOverScreen():
gameOverFont = pygame.font.Font('freesansbold.ttf', 150)
gameSurf = gameOverFont.render('GAME', True, WHITE)
overSurf = gameOverFont.render('OVER', True, WHITE)
gameRect = gameSurf.get_rect()
overRect = overSurf.get_rect()
gameRect.midtop = (WINDOWWIDTH / 2, 10)
overRect.midtop = (WINDOWWIDTH / 2, gameRect.height + 10 + 25)
DISPLAYSURF.blit(gameSurf, gameRect)
DISPLAYSURF.blit(overSurf, overRect)
drawPressKeyMsg()
pygame.display.update()
pygame.time.wait(500)
checkForKeyPress()
while True:
if checkForKeyPress():
pygame.event.get()
return
def drawScore(score):
scoreSurf = BASICFONT.render('Score: %s' %(score), True, WHITE)
scoreRect = scoreSurf.get_rect()
scoreRect.topleft = (WINDOWWIDTH - 120, 10)
DISPLAYSURF.blit(scoreSurf, scoreRect)
def drawWorm(wormCoods):
for coord in wormCoods:
x = coord['x'] * CELLSIZE
y = coord['y'] * CELLSIZE
wormSegmentRect = pygame.Rect(x, y, CELLSIZE, CELLSIZE)
pygame.draw.rect(DISPLAYSURF, DARKGREEN, wormSegmentRect)
wormInnerSegmentRect = pygame.Rect(x+4, y+4, CELLSIZE-8, CELLSIZE-8)
pygame.draw.rect(DISPLAYSURF, GREEN, wormInnerSegmentRect)
def drawApple(coord):
x = coord['x'] * CELLSIZE
y = coord['y'] * CELLSIZE
appleRect = pygame.Rect(x, y, CELLSIZE, CELLSIZE)
pygame.draw.rect(DISPLAYSURF, RED, appleRect)
def drawGrid():
for x in range(0, WINDOWWIDTH, CELLSIZE):
pygame.draw.line(DISPLAYSURF, DARKGRAY, (x, 0), (x, WINDOWHEIGHT))
for y in range(0, WINDOWHEIGHT, CELLSIZE):
pygame.draw.line(DISPLAYSURF, DARKGRAY, (0, y), (WINDOWWIDTH, y))
if __name__ == '__main__':
main()
剛開始都是一些窗口,顏色,按鍵之類的定義,代碼從main主函數開始,
一個main()函數裏面就是這個程序的大概流程,首先相關pygame的初始化,顯示遊戲啓動畫面,接着進入死循環,runGame()就是遊戲主體部分了,是程序核心。而如果遊戲失敗,就會只執行顯示結束畫面showGameOverScreen(),然後再次進行遊戲。我們把蛇身看成是一段一段組成的,CELLSIZE這個常量就是蛇身每段的大小。我們要確保蛇身與整個顯示屏幕的大小是成整數倍的關係,不然顯示就有問題,所以在開始加了異常處理。assert 語句檢測我們給定的屏幕大小是否與蛇身段大小成整數倍。
初始化pygame模塊(首先必須的),然後把定義的一些變量生成窗口,字體類型和窗口標題,showStartScreen()是遊戲開始界面的方法,最後就是無限循環,運行遊戲和遊戲結束。先看showStartScreen()方法,開始畫面的顯示效果是兩個字符串Wormy! 不停的旋轉,還創建兩個字體對象和兩個旋轉角度的變量,右下角還有提示顯示按下按鍵按開始遊戲,所以還有個顯示按鍵消息的函數drawPressKeyMsg(),隨便按下個按鍵就會進入到runGame(),除了ESC外。checkForPress方法就是檢查你的輸入信息,最後在函數末尾顯示更新每次的改變,旋轉圖像用到的方法是pygame.transform.rotate()。
drawPressKeyMsg方法就是顯示按任意鍵進入遊戲的信息,pressKeySurf對象存了字符串信息。然後調用get_rect()方法,獲取字符串應在的位置,最後blit方法進行繪製。
現在看最主要的runGame()方法,剛開始就是定義蛇隨機出現的位置,而且爲了防止蛇一出來就撞牆,所以蛇出現的位置都和牆有一點距離,使用random模塊中的randint方法隨機產生(5,CELLSIZE - 6)的數,並且蛇出來默認的方向是右,有蛇當然有食物,getRandomLocation()方法就是產生食物的位置,然後就是一個主循環了,循環中一直在獲取你所輸入的信息,如果輸入的類型是QUIT(也就是關閉窗口),那麼就調用方法terminate()結束遊戲。如果類型是KEYDOWN(鍵盤輸入的信息),下面是主要的控制按鍵,如果你按下LEFT鍵,並且當前蛇不是往右的,那麼蛇就會改變向左走得方向(畢竟不能讓蛇直接回頭裝自己),下面的一樣。ESC按鍵也會結束遊戲。
if wormCoods[HEAD]['x'] == -1 or wormCoods[HEAD]['x'] == CELLWIDTH or wormCoods[HEAD]['y'] == -1 or wormCoods[HEAD]['y'] == CELLHEIGHT:
return
for wormBody in wormCoods[1:]:
if wormBody['x'] == wormCoods[HEAD]['x'] and wormBody['y'] == wormCoods[HEAD]['y']:
return
上面這段代碼的意思是蛇撞到牆和自己的處理,首先第一個if是判斷是否撞到牆,拿X方向來說,最左邊是-1,左右邊則是CELLSIZE,因爲座標從0開始,到CELLWIDTH-1結束,Y軸上一樣。下面這個if是檢測是否撞到自身,如果蛇頭的座標和蛇身中某個座標相等,那就是撞到自身了。
if wormCoods[HEAD]['x'] == apple['x'] and wormCoods[HEAD]['y'] == apple['y']:
apple = getRandomLocation()
else:
del wormCoods[-1]
這個是蛇喫到食物的情況下,只要判斷蛇頭的座標和食物的座標相等。需要注意的是,當喫到食物的話,蛇的尾巴還是保留的,然後得到一個新的放置蘋果的隨機位置,如果沒有此貨到的話,我們就要刪除尾端,蛇前進一格。
if direction == UP:
newHead = {'x': wormCoods[HEAD]['x'], 'y': wormCoods[HEAD]['y']-1}
elif direction == DOWN:
newHead = {'x': wormCoods[HEAD]['x'], 'y': wormCoods[HEAD]['y']+1}
elif direction == LEFT:
newHead = {'x': wormCoods[HEAD]['x']-1, 'y': wormCoods[HEAD]['y']}
elif direction == RIGHT:
newHead = {'x': wormCoods[HEAD]['x']+1, 'y': wormCoods[HEAD]['y']}
wormCoods.insert(0,newHead)
刪除蛇身尾端的話,接着 就要把丟失的尾端補回來,也就是要添加蛇頭端。添加蛇頭端的位置就要根據蛇當前的移動方向來決定了,找到新的蛇頭,然後把蛇頭插入到新的列表中。
最後就是對上面的蛇和食物的狀態做出顯示操作了。
還有個遊戲結束的界面方法showGameOverScreen().