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实现遍历包含大量文件的文件夹

    Python实现遍历包含大量文件的文件夹

    在处理大模型的训练数据时,经常需要遍历大型文件夹,其中,可能包括数千万或数亿个文件,所以本文为大家整理了Python遍历包含大量文件的文件夹的方法,希望对大家有所帮助
    2023-04-04
  • Django REST 异常处理详解

    Django REST 异常处理详解

    这篇文章主要介绍了Django REST 异常处理详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07
  • 简单几步教你学会Python接口自动化测试

    简单几步教你学会Python接口自动化测试

    这篇文章主要介绍了简单几步教你学会Python接口自动化测试,本文从一个简单的登录接口测试入手,一步步调整优化接口调用姿势,期望读者可以通过本文对接口自动化测试有一个大致的了解,需要的朋友可以参考下
    2023-08-08
  • Python 从subprocess运行的子进程中实时获取输出的例子

    Python 从subprocess运行的子进程中实时获取输出的例子

    今天小编就为大家分享一篇Python 从subprocess运行的子进程中实时获取输出的例子,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-08-08
  • Python中查看变量的类型内存地址所占字节的大小

    Python中查看变量的类型内存地址所占字节的大小

    这篇文章主要介绍了Python中查看变量的类型,内存地址,所占字节的大小,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-06-06
  • python中ASCII码和字符的转换方法

    python中ASCII码和字符的转换方法

    今天小编就为大家分享一篇python中ASCII码和字符的转换方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • python生成式的send()方法(详解)

    python生成式的send()方法(详解)

    下面小编就为 大家带来一篇python生成式的send()方法(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • Anaconda中Python虚拟环境的创建使用与删除方法详解

    Anaconda中Python虚拟环境的创建使用与删除方法详解

    这篇文章主要为大家介绍了在Anaconda环境下,创建、使用与删除Python虚拟环境的方法,具有一定的借鉴价值,需要的小伙伴可以跟随小编一起了解一下
    2023-08-08
  • 跟老齐学Python之Python安装

    跟老齐学Python之Python安装

    本文主要讲诉了在Linux,Windows,MacOS三大系统中如何安装Python环境,非常的实用,虽然前面絮絮叨叨的说了不少题外话,但都是作者的肺腑之言,还是仔细看看吧
    2014-09-09
  • 集调试共享及成本控制Prompt工具PromptLayer使用指南

    集调试共享及成本控制Prompt工具PromptLayer使用指南

    这篇文章主要介绍了集调试共享及成本控制Prompt工具PromptLayer使用指南,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03

最新评论