最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

本文實例為大家分享了Python turtle實現(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實現(xiàn)企業(yè)微信機(jī)器人每天定時發(fā)消息實例

    Python實現(xiàn)企業(yè)微信機(jī)器人每天定時發(fā)消息實例

    這篇文章主要介紹了Python實現(xiàn)企業(yè)微信機(jī)器人每天定時發(fā)消息實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • pygame實現(xiàn)貪吃蛇游戲(下)

    pygame實現(xiàn)貪吃蛇游戲(下)

    這篇文章主要為大家介紹了pygame實現(xiàn)貪吃蛇游戲的下篇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • Python全棧之學(xué)習(xí)CSS(2)

    Python全棧之學(xué)習(xí)CSS(2)

    這篇文章主要為大家介紹了Python全棧之CSS,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01
  • Python中input和raw_input的一點區(qū)別

    Python中input和raw_input的一點區(qū)別

    這篇文章主要介紹了Python中input和raw_input的一點區(qū)別,它們都是用來讀取控制臺輸入的函數(shù),需要的朋友可以參考下
    2014-10-10
  • python使用Bokeh庫實現(xiàn)實時數(shù)據(jù)的可視化

    python使用Bokeh庫實現(xiàn)實時數(shù)據(jù)的可視化

    Python語言以其豐富的數(shù)據(jù)科學(xué)生態(tài)系統(tǒng)而聞名,其中Bokeh庫作為一種功能強(qiáng)大的可視化工具,為實時數(shù)據(jù)的可視化提供了優(yōu)秀的支持,本文將介紹如何使用Bokeh庫實現(xiàn)實時數(shù)據(jù)的可視化,并提供相關(guān)代碼實例,需要的朋友可以參考下
    2024-05-05
  • 使用Python讀取Excel數(shù)據(jù)在PPT中創(chuàng)建圖表

    使用Python讀取Excel數(shù)據(jù)在PPT中創(chuàng)建圖表

    使用Python從Excel讀取數(shù)據(jù)并在PowerPoint幻燈片中創(chuàng)建圖表不僅能夠極大地簡化圖表創(chuàng)建過程,通過Python這一橋梁,我們可以輕松實現(xiàn)數(shù)據(jù)自動化處理和圖表生成,本文將演示如何使用Python讀取Excel數(shù)據(jù)在PPT中創(chuàng)建圖表,需要的朋友可以參考下
    2024-08-08
  • 使用 Python 實現(xiàn)微信群友統(tǒng)計器的思路詳解

    使用 Python 實現(xiàn)微信群友統(tǒng)計器的思路詳解

    這篇文章主要介紹了使用 Python 實現(xiàn)微信群友統(tǒng)計器的思路詳解,需要的朋友可以參考下
    2018-09-09
  • Python實現(xiàn)處理Excel數(shù)據(jù)并生成只讀模式

    Python實現(xiàn)處理Excel數(shù)據(jù)并生成只讀模式

    這篇文章主要為大家詳細(xì)介紹了如何使用 Python 處理 Excel 數(shù)據(jù),并生成只讀模式的 Excel 文檔,文中的示例代碼簡潔易懂,有需要的小伙伴可以參考下
    2023-11-11
  • 詳解Python中math和decimal模塊的解析與實踐

    詳解Python中math和decimal模塊的解析與實踐

    在Python中,math?和?decimal?模塊是處理數(shù)學(xué)運算的重要工具,本文將深入探討這兩個模塊的基礎(chǔ)知識,并通過實際的代碼示例演示它們的用法,希望對大家有所幫助
    2024-02-02
  • pytorch張量和numpy數(shù)組相互轉(zhuǎn)換

    pytorch張量和numpy數(shù)組相互轉(zhuǎn)換

    在使用pytorch作為深度學(xué)習(xí)的框架時,經(jīng)常會遇到張量tensor和矩陣numpy的類型的相互轉(zhuǎn)化的問題,本文主要介紹了pytorch張量和numpy數(shù)組相互轉(zhuǎn)換,感興趣的可以了解一下
    2024-02-02

最新評論

黄浦区| 绥阳县| 合川市| 玛曲县| 遵义市| 宜兰县| 南木林县| 珠海市| 日喀则市| 驻马店市| 白朗县| 磐石市| 商水县| 红原县| 淳化县| 木兰县| 文昌市| 砚山县| 容城县| 诸暨市| 嘉兴市| 高青县| 辽宁省| 桃源县| 房山区| 绵竹市| 原阳县| 乡宁县| 洛川县| 吴旗县| 东方市| 扬中市| 青川县| 曲沃县| 丹寨县| 女性| 德保县| 舞阳县| 武隆县| 安义县| 汝阳县|