Python貪喫蛇雙人大戰

Python貪喫蛇雙人大戰

晚上家裏小朋友要玩貪喫蛇遊戲,還要跟我對戰,一時半會我沒想到去哪裏下這樣一個遊戲,忽然靈機一動,可以自己寫一個,順便還可以跟小朋友展示一下程序員的厲害,於是開工。

原始版本

這是一個很基礎的程序,自然不用從頭寫,在網上隨便一搜,找到有人共享的代碼,鏈接如下:https://www.cnblogs.com/qiu2013/p/6087627.html,據說是來源於《Raspberry Pi 用戶指南》的代碼,我也沒有去查。代碼如下:

#!/usr/bin/env python
import pygame,sys,time,random
from pygame.locals import *
# 定義顏色變量
redColour = pygame.Color(255,0,0)
blackColour = pygame.Color(0,0,0)
whiteColour = pygame.Color(255,255,255)
greyColour = pygame.Color(150,150,150)

# 定義gameOver函數
def gameOver(playSurface):
    gameOverFont = pygame.font.Font('arial.ttf',72)
    gameOverSurf = gameOverFont.render('Game Over', True, greyColour)
    gameOverRect = gameOverSurf.get_rect()
    gameOverRect.midtop = (320, 10)
    playSurface.blit(gameOverSurf, gameOverRect)
    pygame.display.flip()
    time.sleep(5)
    pygame.quit()
    sys.exit()

# 定義main函數
def main():
    # 初始化pygame
    pygame.init()
    fpsClock = pygame.time.Clock()
    # 創建pygame顯示層
    playSurface = pygame.display.set_mode((640,480))
    pygame.display.set_caption('Raspberry Snake')

    # 初始化變量
    snakePosition = [100,100]
    snakeSegments = [[100,100],[80,100],[60,100]]
    raspberryPosition = [300,300]
    raspberrySpawned = 1
    direction = 'right'
    changeDirection = direction
    while True:
        # 檢測例如按鍵等pygame事件
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == KEYDOWN:
                # 判斷鍵盤事件
                if event.key == K_RIGHT or event.key == ord('d'):
                    changeDirection = 'right'
                if event.key == K_LEFT or event.key == ord('a'):
                    changeDirection = 'left'
                if event.key == K_UP or event.key == ord('w'):
                    changeDirection = 'up'
                if event.key == K_DOWN or event.key == ord('s'):
                    changeDirection = 'down'
                if event.key == K_ESCAPE:
                    pygame.event.post(pygame.event.Event(QUIT))
        # 判斷是否輸入了反方向
        if changeDirection == 'right' and not direction == 'left':
            direction = changeDirection
        if changeDirection == 'left' and not direction == 'right':
            direction = changeDirection
        if changeDirection == 'up' and not direction == 'down':
            direction = changeDirection
        if changeDirection == 'down' and not direction == 'up':
            direction = changeDirection
        # 根據方向移動蛇頭的座標
        if direction == 'right':
            snakePosition[0] += 20
        if direction == 'left':
            snakePosition[0] -= 20
        if direction == 'up':
            snakePosition[1] -= 20
        if direction == 'down':
            snakePosition[1] += 20
        # 增加蛇的長度
        snakeSegments.insert(0,list(snakePosition))
        # 判斷是否喫掉了樹莓
        if snakePosition[0] == raspberryPosition[0] and snakePosition[1] == raspberryPosition[1]:
            raspberrySpawned = 0
        else:
            snakeSegments.pop()
        # 如果喫掉樹莓,則重新生成樹莓
        if raspberrySpawned == 0:
            x = random.randrange(1,32)
            y = random.randrange(1,24)
            raspberryPosition = [int(x*20),int(y*20)]
            raspberrySpawned = 1
        # 繪製pygame顯示層
        playSurface.fill(blackColour)
        for position in snakeSegments:
            pygame.draw.rect(playSurface,whiteColour,Rect(position[0],position[1],20,20))
            pygame.draw.rect(playSurface,redColour,Rect(raspberryPosition[0], raspberryPosition[1],20,20))

        # 刷新pygame顯示層
        pygame.display.flip()
        # 判斷是否死亡
        if snakePosition[0] > 620 or snakePosition[0] < 0:
            gameOver(playSurface)
        if snakePosition[1] > 460 or snakePosition[1] < 0:
            for snakeBody in snakeSegments[1:]:
                if snakePosition[0] == snakeBody[0] and snakePosition[1] == snakeBody[1]:
                    gameOver(playSurface)
        # 控制遊戲速度
        fpsClock.tick(5)

if __name__ == "__main__":
    main()

此代碼實現了基本功能,主循環中先判斷按鍵事件,然後調整蛇的位置,若蛇喫到了豆子(這個代碼裏叫樹莓,我嫌名字太長,改成了習慣的豆子),則增加蛇的長度,並重新生成豆子,接着刷新顯示,最後判斷是否死亡,若死亡則調用gameOver。

當然這個是滿足不了小朋友的需求的,小朋友嘗試了一下,馬上提取了如下需求:

  1. 要跟我一起玩,也就是要有兩條蛇,每人控制一個,看誰喫得多。
  2. 蛇死了之後不要結束,太麻煩,改爲重新開始。
  3. 蛇的顏色要能自己定。
  4. 要能看出來蛇頭,即蛇頭需要用不同的顏色。
  5. 豆子數量太少,每次才一個,要一下子出現很多豆子,可以隨便喫。
    看來天下用戶都一樣,總是各種需求。於是爲了便於以後的修改,我把蛇相關的操作提取了一個蛇的類如下。

蛇類

class Snake:    
    def __init__(self, color, headColor, ctrlKeys):
        self.color = color
        self.headColor = headColor
        self.ctrlKeys = ctrlKeys #按[上,下,左,右]的順序
        self.direction = random.choice([-2,2,-1,1]) # 方向[-2,2,-1,1]分別表示[上,下,左,右]
        x = random.randrange(5,SCREEN_WIDTH/20-5)
        y = random.randrange(5,SCREEN_HEIGHT/20-5)
        self.headPos = [int(x*20),int(y*20)]
        self.segments = [self.headPos]
        self.moveAndAdd()
        self.moveAndAdd()      
    def changeDirection(self, pressKey):
        directions = [-2,2,-1,1]
        for direct, key in zip(directions, self.ctrlKeys):
            if key == pressKey and direct + self.direction != 0:
                self.direction = direct      
    def moveAndAdd(self):
        # 根據方向移動蛇頭的座標
        if self.direction == 1:
            self.headPos[0] += 20
        if self.direction == -1:
            self.headPos[0] -= 20
        if self.direction == -2:
            self.headPos[1] -= 20
        if self.direction == 2:
            self.headPos[1] += 20
        self.segments.insert(0,list(self.headPos)) # 在蛇頭插入一格
    def pop(self):
        self.segments.pop()  # 在蛇尾減去一格
    def show(self, playSurface):
        # 畫蛇身
        for pos in self.segments[1:]:
            pygame.draw.rect(playSurface,self.color,Rect(pos[0],pos[1],20,20))
        # 畫蛇頭
        pygame.draw.rect(playSurface,self.headColor,Rect(self.headPos[0],self.headPos[1],20,20))
    def respawnIfDead(self):
        if self.headPos[0] > SCREEN_WIDTH-20 or self.headPos[0] < 0 or self.headPos[1] > SCREEN_HEIGHT-20 or self.headPos[1] < 0:       
            x = random.randrange(5,SCREEN_WIDTH/20-5)
            y = random.randrange(5,SCREEN_HEIGHT/20-5)
            self.direction = random.choice([-2,2,-1,1])
            self.headPos = [int(x*20),int(y*20)]
            self.segments = [self.headPos]
            self.moveAndAdd()
            self.moveAndAdd()       

其中初始化函數有三個參數,分別是蛇的顏色,蛇頭顏色,以及控制的按鍵。初始化的蛇爲3格,隨機出現在中央區域(太靠邊怕還來不及反應就死了)。調用初始化的代碼如下:

    # 初始化蛇
    ctrlKeys1 = [ord('w'),ord('s'),ord('a'),ord('d')]
    ctrlKeys2 = [K_UP,K_DOWN,K_LEFT,K_RIGHT]
    snake1 = Snake(GREEN,GREEN_HEAD,ctrlKeys1)
    snake2 = Snake(RED,RED_HEAD,ctrlKeys2)

changeDirection 函數顧名思義是改變方向的,有一個參數是按鍵。self.direction 記錄當前蛇移動的方向,用[-2,2,-1,1]分別表示[上,下,左,右],這主要是爲了簡化代碼。changeDirection 函數根據按鍵值判斷是否要改變方向。這裏要注意蛇是不能後退的,例如往上走的時候按下鍵是沒有效果的。

moveAndAdd 函數根據移動方向移動一格,並增加一格在蛇頭。pop 函數在蛇尾減去一格。這兩個函數結合起來即可實現蛇的移動,以及蛇增長一格並移動。

show 函數將蛇顯示出來,先畫蛇身,再畫蛇頭。以防蛇頭被蛇身擋住。

respawnIfDead 函數判斷蛇是否死亡,若死了就重生。目前死亡方式爲超出邊界。重生後的蛇隨機出現在中央區域,身體恢復爲3格。

爲了滿足很多豆子可以隨便喫的需求,考慮到以後的擴展,把豆子也做了一個類Bean,並給豆子們也做了一個類Beans,如下。

豆類

class Bean:
    def __init__(self, color, pos):
        self.color = color
        self.pos = pos
    def beEaten(self, snakePos):
        if snakePos[0] == self.pos[0] and snakePos[1] == self.pos[1]:
            return True
        else:
            return False

class Beans:
    def __init__(self, color, totalNum):
        self.color = color
        self.totalNum = totalNum
        self.curNum = 0
        self.beans = []
    def generate(self):
        while self.curNum < self.totalNum:
            x = random.randrange(0,SCREEN_WIDTH/20)
            y = random.randrange(0,SCREEN_HEIGHT/20)
            self.beans.append(Bean(self.color, [int(x*20),int(y*20)]))
            self.curNum = self.curNum + 1
    def beEaten(self, snakePos):
        for bean in self.beans:
            if bean.beEaten(snakePos):
                self.beans.remove(bean)
                self.curNum = self.curNum - 1
                return True
        return False
    def show(self, playSurface):
        for bean in self.beans:
            pygame.draw.rect(playSurface,self.color,Rect(bean.pos[0],bean.pos[1],20,20))

豆類比較簡單,初始化的時候要指定顏色和位置,有一個函數beEaten判斷是否被吃了。

豆子們的類稍微複雜點,其包含了 totalNum 個豆子。豆子們初始化的時候需要指定顏色和數量。 curNum 用來記錄當前有多少個豆子,因爲有的豆子可能被喫掉了。generate 函數負責生成豆子,初始化以及豆子被喫掉後都可以用它來生成豆子,生成的豆子隨機出現在屏幕範圍內。beEaten 函數判斷豆子們中是否有的被吃了,若被吃了就從列表 beans 中移除它,同時調整 curNum 用來記錄當前還剩多少豆子。show 函數將豆子們都顯示出來。
初始化豆子們的代碼如下:

    # 初始化豆子
    yellowBeans = Beans(YELLOW, BEAN_NUM)
    yellowBeans.generate()

在蛇和豆子們都初始化好了之後,主循環的代碼可以簡化如下:

    while True:
        # 檢測按鍵等pygame事件
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    pygame.event.post(pygame.event.Event(QUIT))
                else:
                    snake1.changeDirection(event.key)
                    snake2.changeDirection(event.key)

        # 根據方向移動蛇並增加蛇長度一格
        snake1.moveAndAdd()
        snake2.moveAndAdd()

        # 如果豆子被喫掉,則重新生成豆子,否則蛇長度減少一格
        if yellowBeans.beEaten(snake1.headPos):
            yellowBeans.generate()
        else:
            snake1.pop()
        if yellowBeans.beEaten(snake2.headPos):
            yellowBeans.generate()
        else:
            snake2.pop()

        # 繪製刷新
        playSurface.fill(BLACK) # 繪製pygame顯示層
        yellowBeans.show(playSurface)
        snake1.show(playSurface)
        snake2.show(playSurface)
        pygame.display.flip()   # 刷新pygame顯示層

        # 若死亡則重生
        snake1.respawnIfDead()
        snake2.respawnIfDead()

        # 控制遊戲速度
        fpsClock.tick(5)

當然,爲了能運行,pygame的初始化還是需要的:

    pygame.init()
    fpsClock = pygame.time.Clock()
    # 創建pygame顯示層
    playSurface = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
    pygame.display.set_caption('Snake Eat Beans')

最後,代碼中用到的一些固定值定義如下:

# 定義界面大小
SCREEN_WIDTH = 1080
SCREEN_HEIGHT = 720

# 定義豆子數量
BEAN_NUM = 10

# 定義顏色變量
RED = pygame.Color(255,0,0)
RED_HEAD = pygame.Color(255,150,0)
BLACK = pygame.Color(0,0,0)
WHITE = pygame.Color(255,255,255)
GREY = pygame.Color(150,150,150)
GREEN = pygame.Color(0,255,0)
GREEN_HEAD = pygame.Color(100,200,200)
YELLOW = pygame.Color(255,255,0)
BLUE = pygame.Color(0,0,255)

終於,可以和我家小朋友一起愉快的玩耍了 😃
貪喫蛇

PS:小朋友提了新的需求,於是又寫了個升級版,可以參考 Python貪喫蛇雙人大戰-升級版

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章