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í)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 教你一步步利用python實(shí)現(xiàn)貪吃蛇游戲
- python實(shí)現(xiàn)貪吃蛇游戲源碼
- 200行python代碼實(shí)現(xiàn)貪吃蛇游戲
- python實(shí)戰(zhàn)之利用pygame實(shí)現(xiàn)貪吃蛇游戲(一)
- python實(shí)戰(zhàn)之利用pygame實(shí)現(xiàn)貪吃蛇游戲(二)
- Python使用海龜繪圖實(shí)現(xiàn)貪吃蛇游戲
- Python實(shí)現(xiàn)貪吃蛇小游戲(單人模式)
- Python實(shí)現(xiàn)貪吃蛇小游戲(雙人模式)
- Python利用手勢識別實(shí)現(xiàn)貪吃蛇游戲
- python實(shí)現(xiàn)一個簡單的貪吃蛇游戲附代碼
相關(guān)文章
Python實(shí)現(xiàn)遍歷包含大量文件的文件夾
在處理大模型的訓(xùn)練數(shù)據(jù)時(shí),經(jīng)常需要遍歷大型文件夾,其中,可能包括數(shù)千萬或數(shù)億個文件,所以本文為大家整理了Python遍歷包含大量文件的文件夾的方法,希望對大家有所幫助2023-04-04Python 從subprocess運(yùn)行的子進(jìn)程中實(shí)時(shí)獲取輸出的例子
今天小編就為大家分享一篇Python 從subprocess運(yùn)行的子進(jìn)程中實(shí)時(shí)獲取輸出的例子,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08Python中查看變量的類型內(nèi)存地址所占字節(jié)的大小
這篇文章主要介紹了Python中查看變量的類型,內(nèi)存地址,所占字節(jié)的大小,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06Anaconda中Python虛擬環(huán)境的創(chuàng)建使用與刪除方法詳解
這篇文章主要為大家介紹了在Anaconda環(huán)境下,創(chuàng)建、使用與刪除Python虛擬環(huán)境的方法,具有一定的借鑒價(jià)值,需要的小伙伴可以跟隨小編一起了解一下2023-08-08集調(diào)試共享及成本控制Prompt工具PromptLayer使用指南
這篇文章主要介紹了集調(diào)試共享及成本控制Prompt工具PromptLayer使用指南,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03