欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python turtle實(shí)現(xiàn)貪吃蛇游戲

 更新時(shí)間:2021年06月18日 08:33:19   作者:allway2  
這篇文章主要為大家詳細(xì)介紹了Python turtle實(shí)現(xiàn)貪吃蛇游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Python turtle實(shí)現(xiàn)貪吃蛇游戲的具體代碼,供大家參考,具體內(nèi)容如下

# 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()

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python實(shí)現(xiàn)遍歷包含大量文件的文件夾

    Python實(shí)現(xiàn)遍歷包含大量文件的文件夾

    在處理大模型的訓(xùn)練數(shù)據(jù)時(shí),經(jīng)常需要遍歷大型文件夾,其中,可能包括數(shù)千萬或數(shù)億個文件,所以本文為大家整理了Python遍歷包含大量文件的文件夾的方法,希望對大家有所幫助
    2023-04-04
  • Django REST 異常處理詳解

    Django REST 異常處理詳解

    這篇文章主要介紹了Django REST 異常處理詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • 簡單幾步教你學(xué)會Python接口自動化測試

    簡單幾步教你學(xué)會Python接口自動化測試

    這篇文章主要介紹了簡單幾步教你學(xué)會Python接口自動化測試,本文從一個簡單的登錄接口測試入手,一步步調(diào)整優(yōu)化接口調(diào)用姿勢,期望讀者可以通過本文對接口自動化測試有一個大致的了解,需要的朋友可以參考下
    2023-08-08
  • Python 從subprocess運(yùn)行的子進(jìn)程中實(shí)時(shí)獲取輸出的例子

    Python 從subprocess運(yùn)行的子進(jìn)程中實(shí)時(shí)獲取輸出的例子

    今天小編就為大家分享一篇Python 從subprocess運(yùn)行的子進(jìn)程中實(shí)時(shí)獲取輸出的例子,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • Python中查看變量的類型內(nèi)存地址所占字節(jié)的大小

    Python中查看變量的類型內(nèi)存地址所占字節(jié)的大小

    這篇文章主要介紹了Python中查看變量的類型,內(nèi)存地址,所占字節(jié)的大小,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-06-06
  • python中ASCII碼和字符的轉(zhuǎn)換方法

    python中ASCII碼和字符的轉(zhuǎn)換方法

    今天小編就為大家分享一篇python中ASCII碼和字符的轉(zhuǎn)換方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • python生成式的send()方法(詳解)

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

    下面小編就為 大家?guī)硪黄猵ython生成式的send()方法(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • Anaconda中Python虛擬環(huán)境的創(chuàng)建使用與刪除方法詳解

    Anaconda中Python虛擬環(huán)境的創(chuàng)建使用與刪除方法詳解

    這篇文章主要為大家介紹了在Anaconda環(huán)境下,創(chuàng)建、使用與刪除Python虛擬環(huán)境的方法,具有一定的借鑒價(jià)值,需要的小伙伴可以跟隨小編一起了解一下
    2023-08-08
  • 跟老齊學(xué)Python之Python安裝

    跟老齊學(xué)Python之Python安裝

    本文主要講訴了在Linux,Windows,MacOS三大系統(tǒng)中如何安裝Python環(huán)境,非常的實(shí)用,雖然前面絮絮叨叨的說了不少題外話,但都是作者的肺腑之言,還是仔細(xì)看看吧
    2014-09-09
  • 集調(diào)試共享及成本控制Prompt工具PromptLayer使用指南

    集調(diào)試共享及成本控制Prompt工具PromptLayer使用指南

    這篇文章主要介紹了集調(diào)試共享及成本控制Prompt工具PromptLayer使用指南,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03

最新評論