贪吃蛇----(自制)


import pygame as pg
import coduck 
from random import randint
pg.init()
side = 30
w = 17
h = 15
disp = pg.display.set_mode((w * side, h * side))

game_running = 0

dirs = [pg.K_RIGHT, pg.K_DOWN, pg.K_LEFT, pg.K_UP]
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
snake = [(7, 3), (7, 4)]
direction = 0
def place_apple():
    a = (randint(0, w - 1), randint(0, h - 1))
    while a in snake:
        a = (randint(0, w - 1), randint(0, h - 1))
    return a
apple = place_apple()
def check_collisions():
    head = snake[-1]
    for i in range(len(snake) - 1):
        if head == snake[i]:
            return 2
    if head[0] >= w or head[0] < 0:
        return 3
    if head[1] >= h or head[1] < 0:
        return 3
    if head == apple:
        return 1
    return 0
clock = pg.time.Clock()
f = pg.font.SysFont('console', 24, True)

while True:
    clock.tick(8)
    for e in pg.event.get():
        if e.type == pg.QUIT:
            pg.quit()
            break
        if e.type == pg.KEYDOWN:
            if game_running == 0:
                game_runnig = 1
            if e.key == pg.K_SPACE:
                game_running = 0
                snake = [(7, 3), (7, 4)]
                direction = 0
                apple = place_apple()
            for i in range(4):
                if e.key == dirs[i] and direction != (i + 2) % 4:
                    direction = i
    if game_running == 2:
        disp.fill(pg.Color("black"))
        disp.blit(f.render(coduck.font("\游戏结束!"), False, pg.Color("blue")), (3 * side, 3 * side))
        pg.display.update()
        game_running = 3
    if game_running == 3:
        continue
    snake.append((snake[-1][0] + dx[direction], snake[-1][1] + dy[direction]))
    coll = check_collisions()
    if coll >= 2:
        game_running = 2
        pass
    elif coll == 1:
        apple = place_apple()
        pass
    elif coll == 0:
        snake.pop(0)
    disp.fill(pg.Color("black"))
    for i in range(len(snake)):
        pg.draw.rect(disp, pg.Color("green"), pg.Rect(snake[i][0] * side, snake[i][1] * side, side, side))
    pg.draw.rect(disp, pg.Color("red"), pg.Rect(apple[0] * side, apple[1] * side, side, side))
    pg.display.update()

如有漏洞,欢迎评论!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值