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实现企业微信机器人每天定时发消息实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-02-02
  • pygame实现贪吃蛇游戏(下)

    pygame实现贪吃蛇游戏(下)

    这篇文章主要为大家介绍了pygame实现贪吃蛇游戏的下篇,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-10-10
  • Python全栈之学习CSS(2)

    Python全栈之学习CSS(2)

    这篇文章主要为大家介绍了Python全栈之CSS,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-01-01
  • Python中input和raw_input的一点区别

    Python中input和raw_input的一点区别

    这篇文章主要介绍了Python中input和raw_input的一点区别,它们都是用来读取控制台输入的函数,需要的朋友可以参考下
    2014-10-10
  • python使用Bokeh库实现实时数据的可视化

    python使用Bokeh库实现实时数据的可视化

    Python语言以其丰富的数据科学生态系统而闻名,其中Bokeh库作为一种功能强大的可视化工具,为实时数据的可视化提供了优秀的支持,本文将介绍如何使用Bokeh库实现实时数据的可视化,并提供相关代码实例,需要的朋友可以参考下
    2024-05-05
  • 使用Python读取Excel数据在PPT中创建图表

    使用Python读取Excel数据在PPT中创建图表

    使用Python从Excel读取数据并在PowerPoint幻灯片中创建图表不仅能够极大地简化图表创建过程,通过Python这一桥梁,我们可以轻松实现数据自动化处理和图表生成,本文将演示如何使用Python读取Excel数据在PPT中创建图表,需要的朋友可以参考下
    2024-08-08
  • 使用 Python 实现微信群友统计器的思路详解

    使用 Python 实现微信群友统计器的思路详解

    这篇文章主要介绍了使用 Python 实现微信群友统计器的思路详解,需要的朋友可以参考下
    2018-09-09
  • Python实现处理Excel数据并生成只读模式

    Python实现处理Excel数据并生成只读模式

    这篇文章主要为大家详细介绍了如何使用 Python 处理 Excel 数据,并生成只读模式的 Excel 文档,文中的示例代码简洁易懂,有需要的小伙伴可以参考下
    2023-11-11
  • 详解Python中math和decimal模块的解析与实践

    详解Python中math和decimal模块的解析与实践

    在Python中,math 和 decimal 模块是处理数学运算的重要工具,本文将深入探讨这两个模块的基础知识,并通过实际的代码示例演示它们的用法,希望对大家有所帮助
    2024-02-02
  • pytorch张量和numpy数组相互转换

    pytorch张量和numpy数组相互转换

    在使用pytorch作为深度学习的框架时,经常会遇到张量tensor和矩阵numpy的类型的相互转化的问题,本文主要介绍了pytorch张量和numpy数组相互转换,感兴趣的可以了解一下
    2024-02-02

最新评论