贪吃蛇代码

贪吃蛇代码

# -*- coding: utf-8 -*-
import pygame
import random
import copy

'''
    首先设置蛇的一个运行方向  接下来判断键盘事件在决定蛇的运行方向
    蛇可以运行起来了  那么接下来就是 吃食物增加自己的长度 和 不吃食物在不同的位置显示
'''
# 初始小蛇方向
move_up = False
move_down = False
move_left = False
move_right = True

# 1.1 游戏初始化
pygame.init()
clock = pygame.time.Clock()  # 设置游戏时钟
pygame.display.set_caption("贪吃蛇")  # 初始化标题
screen = pygame.display.set_mode((500, 500))  # 初始化窗口 窗体的大小为 500  500

# 1.2 初始化蛇的位置  蛇的长度  10 10 也就是蛇的 X Y 坐标
snake_list = [[10, 10]]

# 1.3 初始化食物的位置
x = random.randint(10, 490)
y = random.randint(10, 490)
food_point = [x, y]

# 1.4 开启游戏循环
running = True
while running:
    # 1.5 游戏时钟 刷新频率
    clock.tick(20)

    # 1.6 填充背景为白色
    screen.fill([255, 255, 255])

    # 1.9 绘制背景
    for x in range(0, 501, 10):
        pygame.draw.line(screen, (195, 197, 199), (x, 0), (x, 500), 1)
        pygame.draw.line(screen, (195, 197, 199), (0, x), (500, x), 1)

    # 1.7 绘制食物
    # pygame.draw.circle()
    # 用来画圆形,具体包括五个参数:
    # (1)画圆的表面,在本例中用screen创建了一个窗口,所以是画在screen表面上。
    # (2)用什么颜色来画,如用红色[255,0,0]。
    # (3)在什么位置画,[top,left]。
    # (4)直径。
    # (5)线宽,其中0表示完成填充。
    food_rect = pygame.draw.circle(screen, [255, 0, 0], food_point, 15, 0)

    # 1.8 绘制蛇
    snake_rect = []
    for pos in snake_list:
        # 1.8.1 绘制蛇的身子
        snake_rect.append(pygame.draw.circle(screen, [255, 0, 0], pos, 5, 0))

        # 3.1 碰撞检测 如果蛇吃掉食物
        if food_rect.collidepoint(pos):
            # 贪吃蛇吃掉食物
            snake_list.append(food_point)
            # 重置食物位置
            food_point = [random.randint(10, 490), random.randint(10, 490)]
            food_rect = pygame.draw.circle(screen, [255, 0, 0], food_point, 15, 0)
            break

    # # 2.1 移动位置 获取蛇头位置
    # pos = snake_list[0]

    # 2.4 获取蛇的长度,移动蛇的身子
    pos = len(snake_list) - 1
    while pos > 0:
        snake_list[pos] = copy.deepcopy(snake_list[pos - 1])
        pos -= 1

    # 2.2 更改蛇头位置
    if move_up:
        snake_list[pos][1] -= 10
        if snake_list[pos][1] < 0:
            snake_list[pos][1] = 500

    if move_down:
        snake_list[pos][1] += 10
        if snake_list[pos][1] > 500:
            snake_list[pos][1] = 0

    if move_left:
        snake_list[pos][0] -= 10
        if snake_list[pos][0] < 0:
            snake_list[pos][0] = 500

    if move_right:
        snake_list[pos][0] += 10
        if snake_list[pos][0] > 500:
            snake_list[pos][0] = 0

    # 2.3 键盘控制移动职位
    for event in pygame.event.get():
        # print(event)
        # 判断按下的按键
        if event.type == pygame.KEYDOWN:
            # 上键
            if event.key == pygame.K_UP:
                move_up = True
                move_down = False
                move_left = False
                move_right = False
            # 下键
            if event.key == pygame.K_DOWN:
                move_up = False
                move_down = True
                move_left = False
                move_right = False
            # 左键
            if event.key == pygame.K_LEFT:
                move_up = False
                move_down = False
                move_left = True
                move_right = False
            # 右键
            if event.key == pygame.K_RIGHT:
                move_up = False
                move_down = False
                move_left = False
                move_right = True

    # 3.2 如果蛇吃掉了自己
    head_rect = snake_rect[0]
    count = len(snake_rect)
    while count > 1:
        if head_rect.colliderect(snake_rect[count - 1]):
            running = False
        count -= 1

    pygame.display.update()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值