最近写了一个python库叫fastgame,下载方式:

pip install fastgame -i https://pypi.org/project/

用了下Github Page,文档在https://stripepython.github.io/fastgame-document/

创建游戏:

from fastgame import FastGame
game = FastGame()

写贪吃蛇:

import random

import fastgame
from fastgame.utils.color import *  # 导入常用颜色

SNAKE_WIDTH = 20  # 定义贪吃蛇的宽
WIDTH = 850
HEIGHT = 700

game = fastgame.FastGame('Snake', size=(WIDTH, HEIGHT), fps=10)  # 创建游戏
canvas = fastgame.Canvas(size=(WIDTH, HEIGHT), bgcolor=WHITE)  # 创建贪吃蛇画布
pen = canvas.init_pen()  # 创建画笔
score_text = fastgame.Label('Score: 0', size=36, color=BLUE)

snake = [
    [WIDTH // 2, HEIGHT // 2 - SNAKE_WIDTH],
    [WIDTH // 2, HEIGHT // 2],
    [WIDTH // 2, HEIGHT // 2 + SNAKE_WIDTH],
    [WIDTH // 2, HEIGHT // 2 + SNAKE_WIDTH * 2]
]  # 定义蛇的身体
head = snake[0].copy()  # 蛇头
angel = 'left'  # 移动方向
score = 0  # 分数

def get_food():  # 定义获取食物的方法
    x = random.randint(SNAKE_WIDTH, WIDTH - SNAKE_WIDTH)
    y = random.randint(SNAKE_WIDTH, HEIGHT - SNAKE_WIDTH)
    x -= x % SNAKE_WIDTH  # 防止蛇吃不到食物
    y -= y % SNAKE_WIDTH
    return [x, y]

def eaten_food():  # 判断是否吃到食物
    x0, y0 = food
    x1, y1 = head
    return abs(x0 - x1) <= SNAKE_WIDTH and abs(y0 - y1) <= SNAKE_WIDTH

def move(move_angel: str = 'left'):  # 定义移动方法
    global angel, food, score
    angel = move_angel
    
    if eaten_food():  # 吃到食物
        score += 1
        food = get_food()
    else:
        snake.pop()  # 删除蛇的尾巴
    
    if move_angel == 'left':
        head[0] -= SNAKE_WIDTH
    elif move_angel == 'right':
        head[0] += SNAKE_WIDTH
    elif move_angel == 'up':
        head[1] -= SNAKE_WIDTH
    elif move_angel == 'down':
        head[1] += SNAKE_WIDTH
    snake.insert(0, head.copy())  # 插入蛇头
    
def dead():  # 判断是否死亡
    body = snake.copy()
    del body[0]
    return (
        (head in body)  # 撞到自己
        or head[0] <= 0  # 撞墙
        or head[0] >= WIDTH
        or head[1] <= 0
        or head[1] >= HEIGHT
    )


food = get_food()

@game.update
def update(over=False):  # 定义刷新函数
    pen.fill_screen(PINK)
    score_text.set_text(f'Score: {score}')
    score_text.update()
    
    pen.set_color(WHITE)  # 蛇身颜色
    for body in snake:  # 遍历绘制蛇身
        pen.rectangle(*body, (SNAKE_WIDTH, SNAKE_WIDTH))
        
    pen.set_color(BLACK)  # 蛇头颜色
    pen.rectangle(*head, (SNAKE_WIDTH, SNAKE_WIDTH))
    
    pen.set_color(GREEN)  # 食物颜色
    pen.rectangle(*food, (SNAKE_WIDTH, SNAKE_WIDTH))
    
    move(angel)
    
    if dead():  # Game Over
        if not over:
            update(True)   # 将Game Over那一帧渲染出来
        game_over()
        
def game_over():
    over_sprite = fastgame.screenshot()
    game_over_text = fastgame.Label('Game Over', size=96, color=WHITE)
    game_over_text.position = (0, HEIGHT // 2)
    
    @game.update  # 重定义渲染回调
    def reset_update():
        over_sprite.update()
        game_over_text.update()
    
@game.on_key_down
def on_key_down():
    global angel
    key = game.event.get('key')
    if key == fastgame.K_LEFT and angel != 'right':
        angel = 'left'
    elif key == fastgame.K_RIGHT and angel != 'left':
        angel = 'right'
    elif key == fastgame.K_UP and angel != 'down':
        angel = 'up'
    elif key == fastgame.K_DOWN and angel != 'up':
        angel = 'down'
        

if __name__ == '__main__':
    game.mainloop()

效果:

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐