Python制作当年第一款手机游戏-贪吃蛇游戏(练习)

作者:松鼠爱吃饼干? 时间:2022-06-20 02:37:35 

前言:

文章利用Python pygame做一个贪吃蛇的小游戏而且讲清楚每一段代码是用来干嘛的。

据说是贪吃蛇游戏是1976年,Gremlin公司推出的经典街机游戏,那我们今天用Python制作的这个贪吃蛇小游戏是一个像素版的,虽然简陋,但还是可以玩起来的

Python制作当年第一款手机游戏-贪吃蛇游戏(练习)

我们主要要做的内容:

  • 创建游戏窗口

  • 绘制贪吃蛇与食物

  • 蛇吃食物

贪吃蛇的棋盘模型:

Python制作当年第一款手机游戏-贪吃蛇游戏(练习)

Python制作当年第一款手机游戏-贪吃蛇游戏(练习)

现在就开始我们的代码,首先,还是导入模块:

import pygame
import random
import copy

1. 创建游戏窗口

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]]

首先设置蛇的一个运行方向 接下来判断键盘事件在决定蛇的运行方向
蛇可以运行起来了,那么接下来就是,吃食物增加自己的长度和不吃食物在不同的位置显示

初始小蛇方向:

move_up = False
move_down = False
move_left = False
move_right = True

1.3 初始化食物的位置

x = random.randint(10, 490)
y = random.randint(10, 490)
food_point = [x, y]

1.4 开启游戏循环

running = True
while running:
    # 游戏时钟 刷新频率
    clock.tick(20)

1.5 填充背景为白色

screen.fill([255, 255, 255])

1.6 绘制背景

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)
    food_rect = pygame.draw.circle(screen, [255, 0, 0], food_point, 15, 0)

Python制作当年第一款手机游戏-贪吃蛇游戏(练习)

1.7绘制蛇

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

Python制作当年第一款手机游戏-贪吃蛇游戏(练习)

2. 绘制贪吃蛇与食物

2.1 获取蛇的长度,移动蛇的身子

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

2.4 获取蛇的长度,移动蛇的身子

pos = len(snake_list) - 1
while pos > 0:
    snake_list[pos] = copy.deepcopy(snake_list[pos - 1])
    pos -= 1

Python制作当年第一款手机游戏-贪吃蛇游戏(练习)

3. 蛇吃食物

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

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()

Python制作当年第一款手机游戏-贪吃蛇游戏(练习)

来源:https://pythonjx.blog.csdn.net/article/details/121912060

标签:Python,制作,贪吃蛇,游戏
0
投稿

猜你喜欢

  • link 和 style 元素在 HTML 文档中的位置

    2008-06-02 13:56:00
  • 界面内容优化的层次

    2007-11-06 13:07:00
  • 详解MySQL存储过程的创建和调用

    2024-01-24 18:06:21
  • Go Grpc Gateway兼容HTTP协议文档自动生成网关

    2024-05-21 10:27:16
  • 微信小程序实现吸顶盒效果

    2024-04-28 09:33:38
  • 用js更好地截取定长字符串

    2008-01-16 12:48:00
  • 使用Pytorch搭建模型的步骤

    2022-03-05 21:28:38
  • python函数装饰器构造和参数传递

    2023-05-24 16:49:17
  • PHP fprintf()函数用法讲解

    2023-06-01 20:09:20
  • python下setuptools的安装详解及No module named setuptools的解决方法

    2022-12-21 00:56:46
  • 为什么视觉设计师需要懂HTML

    2009-06-25 14:16:00
  • python3 与python2 异常处理的区别与联系

    2022-11-06 22:39:30
  • django修改models重建数据库的操作

    2024-01-12 21:58:07
  • Vue.js实现无限加载与分页功能开发

    2024-05-02 16:41:40
  • python分布式编程实现过程解析

    2023-11-10 21:13:48
  • Ubuntu 18.04.4安装mysql的过程详解 亲测可用

    2024-01-15 18:46:21
  • 数字人组件反写[asp组件开发实例5]

    2009-06-09 13:23:00
  • Tensorflow Summary用法学习笔记

    2023-08-12 15:08:53
  • SQLSERVER 本地查询更新操作远程数据库的代码

    2023-07-23 21:58:55
  • Python条件语句与循环语句

    2023-01-03 05:07:21
  • asp之家 网络编程 m.aspxhome.com