玩蛇系列之Pygame教程(十三)-- 娛樂版Wormy貪吃蛇(半成品有bug)

請參考前面的教程篇 《玩蛇系列之Pygame教程(十一)-- Wormy貪吃蛇》

以前在網(wǎng)上看到過一個動圖,很好玩,就是貪吃蛇自己吃蘋果,然后把全屏都占滿了,感覺確實很酷,我就想能不能把我們的這個版本也改成貪吃蛇自動尋找食物。

  • 由于時間關系還是有bug,貪吃蛇容易把自己把自己圈起來,就出不去了。

實現(xiàn)也是比較簡單,在游戲主循環(huán)里每次都生成一個最佳的best_move,來替代原來程序中的用戶的輸入,
但是關鍵就是這個best_move的生成,還是很有得研究的,我采用的就比較簡單粗暴的辦法:
搜索所有的方格,計算該方格到蘋果方格的距離(權值)

res=abs(i-a_x)+abs(j-a_y)

然后獲得蛇頭上下左右四個方格的權值,選出其中最小的作為best_move。

有bug哦,各位大神可以盡情修改哦,改好了記得告訴我一聲,膜拜一下!

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

下面是代碼:

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

@author: 小峰峰
'''

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

FPS = 10 # 屏幕刷新率(在這里相當于貪吃蛇的速度)
WINDOWWIDTH = 400 # 屏幕寬度
WINDOWHEIGHT = 300 # 屏幕高度
CELLSIZE = 20 # 小方格的大小

# 斷言,屏幕的寬和高必須能被方塊大小整除
assert WINDOWWIDTH % CELLSIZE == 0, "Window width must be a multiple of cell size."
assert WINDOWHEIGHT % CELLSIZE == 0, "Window height must be a multiple of cell size."

# 橫向和縱向的方格數(shù)
CELLWIDTH = int(WINDOWWIDTH / CELLSIZE)
CELLHEIGHT = int(WINDOWHEIGHT / CELLSIZE)


FIELD_SIZE = CELLHEIGHT * CELLWIDTH

# 定義幾個常用的顏色
# R G B
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'

# 錯誤碼
ERR = -1111

# 貪吃蛇的頭()
HEAD = 0 # syntactic sugar: index of the worm's head


# 運動方向數(shù)組
mov = [LEFT, RIGHT, UP, DOWN]

board = [[0 for x in range(CELLWIDTH)] for y in range(CELLHEIGHT+1)]

def find_best_move(apple,wormCoords,board,direction):
    
    best_move = ERR

    # 蛇頭
    w_x=wormCoords[HEAD]['x']
    w_y=wormCoords[HEAD]['y']
        
    # 蘋果
    a_x=apple['x']
    a_y=apple['y']
    
    
    for i in xrange (CELLWIDTH):
        for j in xrange (CELLHEIGHT):
            
            cell = {'x':i,'y':j}
            cell_free = is_cell_free(cell, wormCoords)
            if(cell_free):
                # 計算 
                res=abs(i-a_x)+abs(j-a_y)
                board[j][i]=res
            else:
                board[j][i]='x'
            
            
    
    for i in xrange (CELLHEIGHT):
        print board[i]
        
    
    # 邊界處理
    if(w_y-1<0):
        u=100
    else:
        u=board[w_y-1][w_x]
        
    if(w_y+1>CELLHEIGHT-1):
        d=100
    else:
        d=board[w_y+1][w_x]
        
    if(w_x-1<0):
        l=100
    else:
        l=board[w_y][w_x-1]
    
    if(w_x+1>CELLWIDTH-1):
        r=100
    else:
        r=board[w_y][w_x+1]
        
    
    # 選擇最小的作為best_move
    m=min(u,d,l,r)
    
    if(m==u):
        best_move=UP
 
    if(m==d):
        best_move=DOWN
        
    if(m==l):
        best_move=LEFT
 
    if(m==r):
        best_move=RIGHT
        
    print u,d,l,r
    print best_move

    return best_move
    


def main():
    
    # 定義全局變量
    global FPSCLOCK, DISPLAYSURF, BASICFONT

    pygame.init() # 初始化pygame
    FPSCLOCK = pygame.time.Clock() # 獲得pygame時鐘
    DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT)) # 設置屏幕寬高
    BASICFONT = pygame.font.Font('resource/PAPYRUS.ttf', 18) # BASICFONT
    pygame.display.set_caption('Wormy') # 設置窗口的標題
    
    showStartScreen() # 顯示開始畫面
    
    while True: 
        
        # 這里一直循環(huán)于開始游戲和顯示游戲結束畫面之間,
        # 運行游戲里有一個循環(huán),顯示游戲結束畫面也有一個循環(huán)
        # 兩個循環(huán)都有相應的return,這樣就可以達到切換這兩個模塊的效果
        
        runGame() # 運行游戲
        
        showGameOverScreen() # 顯示游戲結束畫面
        
        
def runGame():
    # 隨機初始化設置一個點作為貪吃蛇的起點
    startx = random.randint(5, CELLWIDTH - 6)
    starty = random.randint(5, CELLHEIGHT - 6)
    
    # 以這個點為起點,建立一個長度為3格的貪吃蛇(數(shù)組)
    wormCoords = [{'x': startx, 'y': starty},
                  {'x': startx - 1, 'y': starty},
                  {'x': startx - 2, 'y': starty}]


    direction = RIGHT # 初始化一個運動的方向

    # 隨機一個apple的位置
    apple = getRandomLocation(wormCoords)
    
    
    while True: # 游戲主循環(huán)
        for event in pygame.event.get(): # 事件處理
            if event.type == QUIT: # 退出事件
                terminate()
            elif event.type == KEYDOWN: # 按鍵事件
                #如果按下的是左鍵或a鍵,且當前的方向不是向右,就改變方向,以此類推
                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()

        
#         best_move=random.choice(mov)
        best_move=find_best_move(apple,wormCoords,board,direction)
        
        
        
        print best_move
        
        direction=best_move
        
        

        # 檢查貪吃蛇是否撞到撞到邊界
        if wormCoords[HEAD]['x'] == -1 or wormCoords[HEAD]['x'] == CELLWIDTH or wormCoords[HEAD]['y'] == -1 or wormCoords[HEAD]['y'] == CELLHEIGHT:
            return # game over
        
        # 檢查貪吃蛇是否撞到自己
        for wormBody in wormCoords[1:]:
            if wormBody['x'] == wormCoords[HEAD]['x'] and wormBody['y'] == wormCoords[HEAD]['y']:
                return # game over
            
        # 檢查貪吃蛇是否吃到apple
        if wormCoords[HEAD]['x'] == apple['x'] and wormCoords[HEAD]['y'] == apple['y']:
            # 不移除蛇的最后一個尾巴格
            apple = getRandomLocation(wormCoords) # 重新隨機生成一個apple
        else:
            del wormCoords[-1] # 移除蛇的最后一個尾巴格

        # 根據(jù)方向,添加一個新的蛇頭,以這種方式來移動貪吃蛇
        if direction == UP:
            newHead = {'x': wormCoords[HEAD]['x'], 'y': wormCoords[HEAD]['y'] - 1}
        elif direction == DOWN:
            newHead = {'x': wormCoords[HEAD]['x'], 'y': wormCoords[HEAD]['y'] + 1}
        elif direction == LEFT:
            newHead = {'x': wormCoords[HEAD]['x'] - 1, 'y': wormCoords[HEAD]['y']}
        elif direction == RIGHT:
            newHead = {'x': wormCoords[HEAD]['x'] + 1, 'y': wormCoords[HEAD]['y']}
            
            
        # 插入新的蛇頭在數(shù)組的最前面
        wormCoords.insert(0, newHead)
        
        # 繪制背景
        DISPLAYSURF.fill(BGCOLOR)
        
        # 繪制所有的方格
        drawGrid()
        
        # 繪制貪吃蛇
        drawWorm(wormCoords)
        
        # 繪制apple
        drawApple(apple)
        
        # 繪制分數(shù)(分數(shù)為貪吃蛇數(shù)組當前的長度-3)
        drawScore(len(wormCoords) - 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():
    
    DISPLAYSURF.fill(BGCOLOR)
    
    titleFont = pygame.font.Font('resource/PAPYRUS.ttf', 100)
    
    titleSurf = titleFont.render('Wormy!', True, GREEN)
    
    titleRect = titleSurf.get_rect()
    titleRect.center = (WINDOWWIDTH / 2, WINDOWHEIGHT / 2)
    DISPLAYSURF.blit(titleSurf, titleRect)
        
    drawPressKeyMsg()
    
    pygame.display.update()
    
    while True:
        
        if checkForKeyPress():
            pygame.event.get() # clear event queue
            return
        

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

# 檢查一個cell有沒有被蛇身覆蓋,沒有覆蓋則為free,返回true
def is_cell_free(idx, psnake):
    return not (idx in psnake) 

# 隨機生成一個坐標位置    
def getRandomLocation(wormCoords):
    cell_free = False
    while not cell_free:
        w = random.randint(0, CELLWIDTH - 1)
        h = random.randint(0, CELLHEIGHT - 1)
        food = {'x':w,'y':h}
        cell_free = is_cell_free(food, wormCoords)
#     return {'x': random.randint(0, CELLWIDTH - 1), 'y': random.randint(0, CELLHEIGHT - 1)}
    return food

# 顯示游戲結束畫面
def showGameOverScreen():
    gameOverFont = pygame.font.Font('resource/PAPYRUS.ttf', 50)
    gameSurf = gameOverFont.render('Game', True, WHITE)
    overSurf = gameOverFont.render('Over', True, WHITE)
    gameRect = gameSurf.get_rect()
    overRect = overSurf.get_rect()
    gameRect.midtop = (WINDOWWIDTH / 2, WINDOWHEIGHT / 2-gameRect.height-10)
    overRect.midtop = (WINDOWWIDTH / 2, WINDOWHEIGHT / 2)

    DISPLAYSURF.blit(gameSurf, gameRect)
    DISPLAYSURF.blit(overSurf, overRect)
    drawPressKeyMsg()
    pygame.display.update()
    pygame.time.wait(500)
    checkForKeyPress() # clear out any key presses in the event queue

    while True:
        if checkForKeyPress():
            pygame.event.get() # clear event queue
            return
        
# 繪制分數(shù)        
def drawScore(score):
    scoreSurf = BASICFONT.render('Score: %s' % (score), True, WHITE)
    scoreRect = scoreSurf.get_rect()
    scoreRect.topleft = (WINDOWWIDTH - 120, 10)
    DISPLAYSURF.blit(scoreSurf, scoreRect)


# 根據(jù) wormCoords 數(shù)組繪制貪吃蛇
def drawWorm(wormCoords):
    for coord in wormCoords:
        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)


# 根據(jù) coord 繪制 apple 
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): # draw vertical lines
        pygame.draw.line(DISPLAYSURF, DARKGRAY, (x, 0), (x, WINDOWHEIGHT))
    for y in range(0, WINDOWHEIGHT, CELLSIZE): # draw horizontal lines
        pygame.draw.line(DISPLAYSURF, DARKGRAY, (0, y), (WINDOWWIDTH, y))


if __name__ == '__main__':
    main()


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容