Python turtle实现贪吃蛇游戏

 更新时间:2021年06月18日 08:33:19   作者:allway2  
这篇文章主要为大家详细介绍了Python turtle实现贪吃蛇游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Python turtle实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下

# Simple Snake Game in Python 3 for Beginners
 
import turtle
import time
import random
 
delay = 0.1
 
# Score
score = 0
high_score = 0
 
# Set up the screen
wn = turtle.Screen()
wn.title("Snake Game by @TokyoEdTech")
wn.bgcolor("green")
wn.setup(width=600, height=600)
wn.tracer(0)  # Turns off the screen updates
 
# Snake head
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("black")
head.penup()
head.goto(0, 0)
head.direction = "stop"
 
# Snake food
food = turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("red")
food.penup()
food.goto(0, 100)
 
segments = []
 
# Pen
pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("Score: 0  High Score: 0", align="center",
          font=("Courier", 24, "normal"))
 
# Functions
 
 
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"
 
 
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)
 
 
# Keyboard bindings
wn.listen()
wn.onkeypress(go_up, "Up")
wn.onkeypress(go_down, "Down")
wn.onkeypress(go_left, "Left")
wn.onkeypress(go_right, "Right")
 
# Main game loop
while True:
    wn.update()
 
    # Check for a collision with the border
    if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
        time.sleep(1)
        head.goto(0, 0)
        head.direction = "stop"
 
        # Hide the segments
        for segment in segments:
            segment.goto(1000, 1000)
 
        # Clear the segments list
        segments.clear()
 
        # Reset the score
        score = 0
 
        # Reset the delay
        delay = 0.1
 
        pen.clear()
        pen.write("Score: {}  High Score: {}".format(score, high_score),
                  align="center", font=("Courier", 24, "normal"))
 
    # Check for a collision with the food
    if head.distance(food) < 20:
        # Move the food to a random spot
        x = random.randint(-290, 290)
        y = random.randint(-290, 290)
        food.goto(x, y)
 
        # Add a segment
        new_segment = turtle.Turtle()
        new_segment.speed(0)
        new_segment.shape("square")
        new_segment.color("grey")
        new_segment.penup()
        segments.append(new_segment)
 
        # Shorten the delay
        delay -= 0.001
 
        # Increase the score
        score += 10
 
        if score > high_score:
            high_score = score
 
        pen.clear()
        pen.write("Score: {}  High Score: {}".format(score, high_score),
                  align="center", font=("Courier", 24, "normal"))
 
    # Move the end segments first in reverse order
    for index in range(len(segments)-1, 0, -1):
        x = segments[index-1].xcor()
        y = segments[index-1].ycor()
        segments[index].goto(x, y)
 
    # Move segment 0 to where the head is
    if len(segments) > 0:
        x = head.xcor()
        y = head.ycor()
        segments[0].goto(x, y)
 
    move()
 
    # Check for head collision with the body segments
    for segment in segments:
        if segment.distance(head) < 20:
            time.sleep(1)
            head.goto(0, 0)
            head.direction = "stop"
 
            # Hide the segments
            for segment in segments:
                segment.goto(1000, 1000)
 
            # Clear the segments list
            segments.clear()
 
            # Reset the score
            score = 0
 
            # Reset the delay
            delay = 0.1
 
            # Update the score display
            pen.clear()
            pen.write("Score: {}  High Score: {}".format(
                score, high_score), align="center", font=("Courier", 24, "normal"))
 
    time.sleep(delay)
 
wn.mainloop()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 在python中实现调用可执行文件.exe的3种方法

    在python中实现调用可执行文件.exe的3种方法

    今天小编就为大家分享一篇在python中实现调用可执行文件.exe的3种方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-07-07
  • 排序算法之希尔排序法解析

    排序算法之希尔排序法解析

    这篇文章主要介绍了排序算法之希尔排序法解析,希尔排序法(Shell Sort),也称为缩小增量排序,是一种改进的插入排序算法,它通过将待排序的元素按照一定的间隔分组,对每个分组进行插入排序,逐渐减小间隔直至为1,最后对整个序列进行一次插入排序
    2023-07-07
  • Python Django2 model 查询介绍(条件、范围、模糊查询)

    Python Django2 model 查询介绍(条件、范围、模糊查询)

    这篇文章主要介绍了Python Django2 model 查询介绍(条件、范围、模糊查询),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03
  • OpenCV根据面积筛选连通域学习示例

    OpenCV根据面积筛选连通域学习示例

    这篇文章主要为大家介绍了OpenCV根据面积筛选连通域学习示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-06-06
  • 一篇教程教你学会Python进制转换(十进制转二进制、八进制、十六进制)

    一篇教程教你学会Python进制转换(十进制转二进制、八进制、十六进制)

    计算机中只有1和0,所以就导致很多时候需要进制转换,本文介绍了Python进制转换,十进制转二进制,十进制转八进制,十进制转十六进制,有兴趣的可以了解一下
    2021-05-05
  • Python备份Mysql脚本

    Python备份Mysql脚本

    特点是多平台,一个脚本内可以备份多个数据库,并分别打包上传到ftp进行备份。调用了mysqldump及tar来进行数据库dump及打包。 具体参数说明参见源文件
    2008-08-08
  • wxPython 入门教程

    wxPython 入门教程

    您可以在几分钟内编写一段 Python脚本和让桌面拥有令人难以置信的相当漂亮的 GUI应用程序。这篇文章向您展示如何使用一 Python-著称的 GUI 库wxPython,来做到这一点的。向您的朋友和邻居介绍!
    2008-10-10
  • Python读取mat文件,并转为csv文件的实例

    Python读取mat文件,并转为csv文件的实例

    今天小编就为大家分享一篇Python读取mat文件,并转为csv文件的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • python自定义函数def的应用详解

    python自定义函数def的应用详解

    这篇文章主要介绍了python自定义函数def的应用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • 使用Python实现桥接模式的代码详解

    使用Python实现桥接模式的代码详解

    桥接模式是一种结构型设计模式,它将抽象部分与其实现部分分离,使它们都可以独立地变化,本文将给大家介绍如何使用Python实现桥接模式,需要的朋友可以参考下
    2024-02-02

最新评论