前言
最近正在练习使用 pygame, 感觉这东西真的是功能强大、用途广泛。前几天用 pygame,写了一个 Console 程序,看起来很极客(实际上很弱智),今天觉得可以写一个简易的贪吃蛇程序。
游戏界面
其中,浅绿色的部分是水果(吃了之后蛇的长度 +1),深绿色的部分是蛇身(蛇头和蛇尾一个样,懒得处理了),暗绿色的部分是墙。
snake.py
贪吃蛇游戏的主程序。
# Author : GGN_2015
# Content: 贪吃蛇游戏
from game_method import *
import random # 生成随机位置
import time
# 棋盘相关的常量, (棋盘是左右上下循环的)
BOARD_EMPTY = 0 # 空位置
BOARD_SNAKE = 1 # 棋盘上蛇占据的位置
BOARD_WALL = 2 # 棋盘上墙占据的位置
BOARD_FRUIT = 3 # 水果
chessboard = {} # 棋盘存储空间
WIDTH = 20
HEIGHT = 20 # 棋盘的宽度和高度,下标为 (高度-1, 宽度-1)
for i in range(0, HEIGHT):
for j in range(0, WIDTH):
chessboard[(i, j)] = BOARD_EMPTY # 初始化将棋盘清空
RESOLUTION = (1920, 1080) # 屏幕分辨率
REDIUS = int((RESOLUTION[1] - 50) / HEIGHT / 2) - 2 # 半径算法,各种圆的半径
SNAKE_BODY = [] # 描述蛇身每个位置的坐标
INIT_LEN = 4
for i in range(0, INIT_LEN):
SNAKE_BODY.append((int(HEIGHT/2), int(WIDTH/2) + i)) # 初始蛇身长度为 4, 左头 [0], 右尾 [len - 1]
DIRX = {} # 方向向下的坐标轴
DIRY = {} # 方向向右的坐标轴
DIRX['LEFT'] = 0
DIRX['RIGHT'] = 0
DIRX['UP'] = -1
DIRX['DOWN'] = 1 # 描述蛇移动时 X 坐标的改变情况
DIRY['LEFT'] = -1
DIRY['RIGHT'] = 1
DIRY['UP'] = 0
DIRY['DOWN'] = 0 # 描述蛇移动时 Y 坐标的改变情况
DIRECTION = 'LEFT' # 描述蛇移动的方向
def NextHead(): # 通过当前蛇头计算新的蛇头坐标(空间是循环的)
NowHead = SNAKE_BODY[0]
nextHead = ((NowHead[0] + DIRX[DIRECTION]) % HEIGHT, (NowHead[1] + DIRY[DIRECTION]) % WIDTH)
return nextHead
def Crash(): # 检测是否会发生蛇头碰撞
return chessboard[NextHead()] != BOARD_EMPTY and chessboard[NextHead()] != BOARD_FRUIT
def Grow():
return chessboard[NextHead()] == BOARD_FRUIT # 蛇身长度增长
Window = screen(Title = "Snake Game", FullScreen = True, Resolution = RESOLUTION)
Window.Fill((0, 0, 0)) # BLACK 纯色填充
def Modify(R, G, B, rate):
return (int(R*rate), int(G*rate), int(B*rate))
# LINECOLOR = (127, 127, 127)
LINECOLOR = Modify(34, 252, 43, 0.10)
"""
def GetColor(BOARD_TYPE): # 获得某种区块对应的颜色
if BOARD_TYPE == BOARD_EMPTY: return ( 0, 0, 0)
if BOARD_TYPE == BOARD_FRUIT: return (255, 50, 50)
if BOARD_TYPE == BOARD_SNAKE: return ( 50, 50, 255)
if BOARD_TYPE == BOARD_WALL : return (127, 127, 127)
return (0, 255, 0) # Error color
"""
def GetColor(BOARD_TYPE): # 获得某种区块对应的颜色极客配色
if BOARD_TYPE == BOARD_EMPTY: return Modify(34, 252, 43, 0.00)
if BOARD_TYPE == BOARD_FRUIT: return Modify(34, 252, 43, 1.00)
if BOARD_TYPE == BOARD_SNAKE: return Modify(34, 252, 43, 0.50)
if BOARD_TYPE == BOARD_WALL : return Modify(34, 252, 43, 0.10)
return (0, 255, 0) # Error color
def ShowChessBoard(): # 在屏幕上显示棋盘
D = (RESOLUTION[1] - 50) / HEIGHT # 棋盘方格的边长
for i in range(0, HEIGHT + 1): # 画横线
pygame.draw.line(Window.Screen, LINECOLOR, (0, D*i), (RESOLUTION[1] - 50, D*i), 3)
for i in range(0, WIDTH + 1): # 画竖线
pygame.draw.line(Window.Screen, LINECOLOR, (D*i, 0), (D*i, RESOLUTION[1] - 50), 3)
for i in range(0, HEIGHT):
for j in range(0, WIDTH): # 清空蛇的坐标
if chessboard[(i, j)] == BOARD_SNAKE:
chessboard[(i, j)] = BOARD_EMPTY
for pos in SNAKE_BODY:
chessboard[pos] = BOARD_SNAKE # 重新设置蛇身坐标
for i in range(0, HEIGHT):
for j in range(0, WIDTH): # 枚举坐标系中的每一个位置
pos = (D*j + int(D/2), D*i + int(D/2))
pygame.draw.circle(Window.Screen, GetColor(chessboard[(i, j)]), pos, int(D/2) - 4)
def dir(EventKey): # 通过按键得到方向字符串
if EventKey == K_LEFT: return 'LEFT'
elif EventKey == K_RIGHT: return 'RIGHT'
elif EventKey == K_UP: return 'UP'
else: return 'DOWN'
SPEED = 0.2 # 每 SPEED 秒前进一步
def RandFruit():
posList = []
for i in range(0, HEIGHT):
for j in range(0, WIDTH): # 枚举坐标系中的每一个位置
if chessboard[(i, j)] == BOARD_EMPTY:
posList.append((i, j))
if len(posList) > 0:
id = random.randint(0, len(posList) - 1) # 随机选择一个位置
chessboard[posList[id]] = BOARD_FRUIT
Message = "Paused ..." # 状态信息
state = text(Window, Message = Message) # 显示状态信息
length = text(Window)
if __name__ == "__main__": # 启动游戏
running = False # 表示