美文网首页
玩蛇系列之Pygame教程(十三)-- 娱乐版Wormy贪吃蛇(

玩蛇系列之Pygame教程(十三)-- 娱乐版Wormy贪吃蛇(

作者: 峰峰小 | 来源:发表于2017-04-10 03:14 被阅读154次

请参考前面的教程篇 《玩蛇系列之Pygame教程(十一)-- Wormy贪吃蛇》

以前在网上看到过一个动图,很好玩,就是贪吃蛇自己吃苹果,然后把全屏都占满了,感觉确实很酷,我就想能不能把我们的这个版本也改成贪吃蛇自动寻找食物。

  • 由于时间关系还是有bug,贪吃蛇容易把自己把自己圈起来,就出不去了。

实现也是比较简单,在游戏主循环里每次都生成一个最佳的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."

# 横向和纵向的方格数
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


# 运动方向数组
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: 
        
        # 这里一直循环于开始游戏和显示游戏结束画面之间,
        # 运行游戏里有一个循环,显示游戏结束画面也有一个循环
        # 两个循环都有相应的return,这样就可以达到切换这两个模块的效果
        
        runGame() # 运行游戏
        
        showGameOverScreen() # 显示游戏结束画面
        
        
def runGame():
    # 随机初始化设置一个点作为贪吃蛇的起点
    startx = random.randint(5, CELLWIDTH - 6)
    starty = random.randint(5, CELLHEIGHT - 6)
    
    # 以这个点为起点,建立一个长度为3格的贪吃蛇(数组)
    wormCoords = [{'x': startx, 'y': starty},
                  {'x': startx - 1, 'y': starty},
                  {'x': startx - 2, 'y': starty}]


    direction = RIGHT # 初始化一个运动的方向

    # 随机一个apple的位置
    apple = getRandomLocation(wormCoords)
    
    
    while True: # 游戏主循环
        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] # 移除蛇的最后一个尾巴格

        # 根据方向,添加一个新的蛇头,以这种方式来移动贪吃蛇
        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']}
            
            
        # 插入新的蛇头在数组的最前面
        wormCoords.insert(0, newHead)
        
        # 绘制背景
        DISPLAYSURF.fill(BGCOLOR)
        
        # 绘制所有的方格
        drawGrid()
        
        # 绘制贪吃蛇
        drawWorm(wormCoords)
        
        # 绘制apple
        drawApple(apple)
        
        # 绘制分数(分数为贪吃蛇数组当前的长度-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
        
# 绘制分数        
def drawScore(score):
    scoreSurf = BASICFONT.render('Score: %s' % (score), True, WHITE)
    scoreRect = scoreSurf.get_rect()
    scoreRect.topleft = (WINDOWWIDTH - 120, 10)
    DISPLAYSURF.blit(scoreSurf, scoreRect)


# 根据 wormCoords 数组绘制贪吃蛇
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)


# 根据 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()


相关文章

网友评论

      本文标题:玩蛇系列之Pygame教程(十三)-- 娱乐版Wormy贪吃蛇(

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