python贪吃蛇代码「建议收藏」

python贪吃蛇代码「建议收藏」语言:python外置组建:pygamepython是一款十分简单的语言,功能也十分巨大它也能实现贪吃蛇。游戏过程:废话不多说我们来看代码:#0.imortxxxximportpygame#pygame游戏库,sys操控python运行的环境importpygame,sys,random#这个模块包含所有pygame所使用的常亮frompygame.localsimport*#1,定义颜色变量#0-2550黑色255白色red

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

语言:python

外置组建:pygame


python是一款十分简单的语言,功能也十分巨大它也能实现贪吃蛇。

游戏过程:

python贪吃蛇代码「建议收藏」

废话不多说我们来看代码 :

#0.imort xxxx
import pygame


# pygame游戏库,sys操控python运行的环境
import pygame, sys, random
# 这个模块包含所有pygame所使用的常亮
from pygame.locals import *

# 1,定义颜色变量
# 0-255 0黑色 255白色
redColor = pygame.Color(255, 0, 0)
# 背景为黑色
blackColor = pygame.Color(0, 0, 0)
# 贪吃蛇为白色
whiteColor = pygame.Color(255, 255, 255)


# 定义游戏结束的函数
def gameover():
    pygame.quit()
    sys.exit()


# 定义main函数--》定义我们的入口函数
def main():
    # 初始化pygame
    pygame.init()
    # 定义一个变量来控制速度
    fpsClock = pygame.time.Clock()
    # 创建pygame显示层,创建一个界面
    playsurface = pygame.display.set_mode((640, 480))
    pygame.display.set_caption('贪吃蛇')
    # 初始化变量
    # 贪吃蛇初始坐标位置 (先以100,100为基准)
    snakePosition = [100, 100]
    # 初始化贪吃蛇的长度列表中有个元素就代表有几段身体
    snakeBody = [[100, 100], [80, 100], [60, 100]]
    # 初始化目标方向额位置
    targetPosition = [300, 300]
    # 目标方块的标记 目的:判断是否吃掉了这个目标方块1 就是没有吃 0就是吃掉
    targetflag = 1
    # 初始化方向 --》往右
    direction = 'right'
    # 定义一个方向变量(人为控制 按键)
    changeDirection = direction
    while True:

        for event in pygame.event.get():  # 从队列中获取事件
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == KEYDOWN:
                if event.key == K_d:
                    changeDirection = 'right'
                if event.key == K_a:
                    changeDirection = 'left'
                if event.key == K_w:
                    changeDirection = 'up'
                if event.key == K_s:
                    changeDirection = 'down'
                    # 对应键盘上的esc文件
                if event.key == K_ESCAPE:
                    pygame.event.post(pygame.event.Event(QUIT))

        # 确定方向
        if changeDirection == 'left' and not direction == 'right':
            direction = changeDirection
        if changeDirection == 'right' and not direction == 'left':
            direction = changeDirection
        if changeDirection == 'up' and not direction == 'down':
            direction = changeDirection
        if changeDirection == 'down' and not direction == 'up':
            direction = changeDirection

        # 根据方向移动蛇头
        if direction == 'right':
            snakePosition[0] += 20
        if direction == 'left':
            snakePosition[0] -= 20
        if direction == 'up':
            snakePosition[1] -= 20
        if direction == 'down':
            snakePosition[1] += 20
        # 增加蛇的长度
        snakeBody.insert(0, list(snakePosition))
        # 如果贪吃蛇和目标方块的位置重合
        if snakePosition[0] == targetPosition[0] and snakePosition[1] == targetPosition[1]:
            targetflag = 0
        else:
            snakeBody.pop()
        if targetflag == 0:
            x = random.randrange(1, 32)
            y = random.randrange(1, 24)
            targetPosition = [int(x * 20), int(y * 20)]
            targetflag = 1
        # 填充背景颜色
        playsurface.fill(blackColor)
        for position in snakeBody:
            # 第一个参数serface指定一个serface编辑区,在这个区域内绘制
            # 第二个参数color:颜色
            # 第三个参数:rect:返回一个矩形(xy),(width,height)
            # 第四个参数:width:表示线条的粗细 width0填充 实心
            # 化蛇
            pygame.draw.rect(playsurface, redColor, Rect(position[0], position[1], 20, 20))
            pygame.draw.rect(playsurface, whiteColor, Rect(targetPosition[0], targetPosition[1], 20, 20))

        # 更新显示到屏幕表面
        pygame.display.flip()
        # 判断是否游戏结束
        if snakePosition[0] > 620 or snakePosition[0] < 0:
            gameover()
        elif snakePosition[1] > 460 or snakePosition[1] < 0:
            gameover()
        # 控制游戏速度
        fpsClock.tick(2)


# 启动入口函数
if __name__ == '__main__':
    main()

这个比我之前发的“java版贪吃蛇”要简单的多!   

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/169537.html原文链接:https://javaforall.net

(0)
上一篇 2022年8月11日 下午5:00
下一篇 2022年8月11日 下午5:16


相关推荐

  • 使用socks4 socks5 http代理的客户端

    使用socks4 socks5 http代理的客户端///定义的结构structsock4req1{   charVN;   charCD;   unsignedshortPort;   unsignedlongIPAddr;   charother[1];};structsock4ans1{   charVN;   charCD;};structsock5req1{   charVer;   char

    2022年6月26日
    38
  • 数学建模五个步骤_思考问题的五步方法

    数学建模五个步骤_思考问题的五步方法五步方法五步方法顾名思义,通过五个步骤完成用数学模型解决实际问题。它包含以下五个步骤:提出问题 选择建模方法 推导模型的数学表达式 求解模型 回答问题第一步是提出问题,即对遇到的实际问题使用恰当的数学语言进行表达。一般而言,首要任务是对术语进行定义。无论是实际问题涉及到的变量,还是这些变量的单位、相关假设,都应当用等式或者不等式进行表达。在这一基础上,我们就可以用数学语言对实际问…

    2026年2月21日
    5
  • int是什么_uint16范围是多少

    int是什么_uint16范围是多少收到反馈:9位条码更改为12位后,条形码无法自动+1原因:条码的数值超过当前定义的变量的范围调整:将int类型的变量定义为Int64,调整后测试值可自动+1附:Int16值类型表示-32768~+32767之间的整数。Int32值类型表示-2,147,483,648~+2,147,483,647之间的整数。Int64值类型表示-9,223,…

    2022年8月15日
    4
  • 深度讲解智能体:ReACT Agent

    深度讲解智能体:ReACT Agent

    2026年3月15日
    2
  • 遍历qvector,嵌套QVector指针内存处理

    遍历qvector,嵌套QVector指针内存处理I veinheriteda wheretheaccu Yes memoryleakag

    2026年3月17日
    2
  • idea配置tomcat error:duplicate context path

    idea配置tomcat error:duplicate context pathidea 配置 tomcaterror duplicatecon nbsp 解决方法 nbsp nbsp nbsp nbsp 在 editconfig 中部署 tomcat nbsp nbsp nbsp nbsp nbsp server nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp tomcathome nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp D software apache tomcat 7 0 65 nbsp nbsp nbsp

    2026年3月18日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号