首页 > 编程语言 >python贪吃蛇小游戏

python贪吃蛇小游戏

时间:2024-09-27 17:24:28浏览次数:3  
标签:turtle head python segments direction 小游戏 贪吃蛇 go segment

在这里插入图片描述

1.简介

使用了turtle库来创建图形界面,你可以使用键盘的W、A、S、D键来控制蛇的移动方向。蛇吃到食物后,身体会增长,如果蛇撞到自己或者游戏边界,游戏就会结束。

2. 代码

import turtle
import time
import random

delay = 0.1

# 生成食物的位置
food = turtle.Turtle()
food.shape("circle")
food.color("red")
food.penup()
food.goto(random.randint(-15, 15), random.randint(-15, 15))
food.speed(0)

# 蛇头
head = turtle.Turtle()
head.shape("square")
head.color("blue")
head.penup()
head.goto(0, 0)
head.direction = "stop"

# 蛇的食物
segments = []

# 用于控制蛇的移动
def move():
    if head.direction == "up":
        y = head.ycor()
        head.sety(y + 20)

    if head.direction == "down":
        y = head.ycor()
        head.sety(y - 20)

    if head.direction == "left":
        x = head.xcor()
        head.setx(x - 20)

    if head.direction == "right":
        x = head.xcor()
        head.setx(x + 20)

#游戏结束
def game_over():
    # 提示游戏结束
    print("Game Over")
    turtle.write("Game Over", align="center", font=("Courier", 24, "normal"))

    head.goto(0, 0)
    head.direction = "stop"

    for segment in segments:
        segment.goto(1000, 1000)
    segments.clear()

    turtle.done()

# 键盘绑定
def go_up():
    if head.direction != "down":
        head.direction = "up"
def go_down():
    if head.direction != "up":
        head.direction = "down"
def go_left():
    if head.direction != "right":
        head.direction = "left"
def go_right():
    if head.direction != "left":
        head.direction = "right"

# 键盘监听
turtle.listen()
turtle.onkey(go_up, "w")
turtle.onkey(go_down, "s")
turtle.onkey(go_left, "a")
turtle.onkey(go_right, "d")

# 主游戏循环
while True:
    turtle.update()

    # 检查碰撞边界
    if head.xcor()> 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
        game_over()

    # 检查蛇头是否接触到食物
    if head.distance(food) < 20:
        # 移动食物到随机位置
        x = random.randint(-15, 15)
        y = random.randint(-15, 15)
        food.goto(x, y)

        # 添加蛇的身体段
        new_segment = turtle.Turtle()
        new_segment.shape("square")
        new_segment.color("grey")
        new_segment.penup()
        segments.append(new_segment)

    # 移动蛇身体的最后部分到前一部分的位置
    for index in range(len(segments)-1, 0, -1):
        x = segments[index-1].xcor()
        y = segments[index-1].ycor()
        segments[index].goto(x, y)

    if len(segments) > 0:
        x = head.xcor()
        y = head.ycor()
        segments[0].goto(x, y)

    move()

    # 检查蛇头碰到身体
    for segment in segments:
        if 0 < segment.distance(head) < 20:
            game_over()

    time.sleep(delay)

turtle.done()

标签:turtle,head,python,segments,direction,小游戏,贪吃蛇,go,segment
From: https://blog.csdn.net/qq_45906972/article/details/142598694

相关文章

  • 不用写一行Python代码,“Excel” 能直接爬虫了!
    家人们,要爬虫——现在用一个电子表格就行了。一行代码也别写,第三方软件也甭安。只需在表格里点几下就ok。不信,你瞧:就这么两下,网页上的商品信息都有了。网友看完都惊呆了,码个不停。一看到这是来自谷歌的产品(GoogleSheet,谷歌的“Excel”),大家就立马cue起了微软,问它慌......
  • python简易倒计时
    小伙伴们,国庆要到了,是不是很激动,几行python代码,实现倒计时。代码很简单,有几个点注意一下:①"\r":称为“回车”字符。在文本输出中,\r通常用于将光标移回到当前行的起始位置。光标会被移到这一行的最前面,从而覆盖原有的文本。简单说:就是在需要刷新的位置覆盖之前的的文本内容,广泛......
  • 10个高效的Python爬虫框架
    ​前言小型爬虫需求,requests库+bs4库就能解决;大型爬虫数据,尤其涉及异步抓取、内容管理及后续扩展等功能时,就需要用到爬虫框架了。下面介绍了10个爬虫框架,大家可以学习使用!Scrapyscrapy官网:https://scrapy.org/scrapy中文文档:https://www.osgeo.cn/scrapy/intro/oScrapy......
  • 将Python文件编译为exe可执行程序
      Python程序py格式文件的优点是可以跨平台,但运行必须有Python环境,没有Python环境无法运行py格式文件。有没有方法,用户不同安装Python就可直接运行开发的项目工程?答案是肯定的。这就涉及到需要将Python的.py格式文件编写的脚本编译成一个系统可执行文件,这可用PyInstaller来实......
  • Python自动整理文件夹
    编写一个Python脚本来遍历指定目录下的所有文件,根据文件的扩展名来创建相应的子文件夹(如果尚不存在的话),然后将文件移动到对应的子文件夹中。同时,我们需要处理重名文件的问题,通过在文件名后添加_和数字来区分。下面是一个实现这个功能的Python脚本示例:importosimportshu......
  • 数据结构编程实践20讲(Python版)—02链表
    本文目录02链表linked-listS1说明S2示例单向链表双向链表循环链表S3问题:反转单向链表求解思路Python3程序S4问题:双向链表实现历史浏览网页求解思路Python3程序S5问题:基于循环链表的玩家出牌顺序求解思路Python3程序往期链接01数组02链表linked-lis......
  • 自动化办公-python中的open()函数
    Python中的open()函数用于打开一个文件,并返回一个文件对象,您可以通过该对象对文件进行读写操作。基本语法:open(file,mode='r',buffering=-1,encoding=None,errors=None,newline=None,closefd=True,opener=None)参数说明:file:要打开的文件路径(字符串)。可以是......
  • 自动化办公-Python-os模块的使用
    os.path模块的使用在指定文件路径时,由于操作系统的差异,直接使用硬编码的路径可能会导致程序在不同平台上无法正常运行。为了解决这个问题,Python提供了os.path模块,它包含了一系列用于路径操作的函数,可以帮助您以跨平台的方式处理文件路径。为什么要使用os.path模块......
  • Python 迭代器双指针
    我们知道在cpp这种指针语言里面,双指针是这么写的:for(autoi=v.begin(),j=v.begin();j<v,end();j++){//dosomething...//updatepointeriwhile(cond){i++;}}对于py这样不带指针的,一般就只能这么写:i=0forjinrange(len(lst)):#do_something......
  • python爬虫案例——抓取三级跳转网页,实现逐页抓取,数据存入mysql数据库(10)
    文章目录1、目标任务2、网页分析3、完整代码1、目标任务目标站点:情话网(http://www.ainicr.cn/tab/)任务:抓取该网站下所有标签下的所有情话语句,并将其存入mysql数据库2、网页分析用浏览器打开网页,按F12或右键检查,进入开发者模式,在Network-Doc下找到网页的数......