用户:
泠尘晓查看:0 回复:1 评论:0 创建时间:2021-06-05T23:09:46
import os
import turtle
import random
import time
board = turtle.Pen()
board.pencolor("red")
game = turtle.Screen()
game.title('贪吃龟·by hermit_')
game.setup(2000,2000,0,0)
game.bgcolor('black')
tck = turtle.Turtle()
tck.shape('turtle')
tck.color('red')
tck.speed(1)
tck.penup()
tck.shapesize(1.5)
leaf = turtle.Turtle()
leaf_shape = ((0,0),(10,0),(10,10),(0,10),(0,0))
game.addshape('leaf',leaf_shape)
print(game.getshapes())
leaf.shape('leaf')
leaf.color('yellow')
leaf.speed(0)
leaf.penup()
def new_leaf():
leaf.hideturtle()
leaf.setx(random.randint(-180,180))
leaf.sety(random.randint(-180,180))
leaf.showturtle()
def out_window():
(x,y) = tck.pos()
outside = x < -750 or x > 750 or y < -450 or y > 450
return outside
def game_over():
tck.color('gray')
leaf.color('gray')
board.pencolor("gray")
board.clear()
board.write(f'得分:{score}',align='center',font=fonts)
def you ():
tck.setheading(0)
def zuo ():
tck.setheading(180)
def qian ():
tck.setheading(90)
def hou ():
tck.setheading(270)
game.onkeypress(you,"d")
game.onkeypress(zuo,"a")
game.onkeypress(qian,"w")
game.onkeypress(hou,"s")
game.listen()
sd = 1
new_leaf()
tck_len = 1
score = 0
board.goto(0,200)
fonts = ('隶书',25,'bold')
board.hideturtle()
board.clear()
board.write(f'得分:{score}',align='center',font=fonts)
while True:
tck.shapesize(1.5)
tck.forward(sd)
if tck.distance(leaf) < 20:
tck.shapesize(1.5)
new_leaf()
score = score+1
sd = sd + 0.2
board.clear()
board.write(f'得分:{score}',align='center',font=fonts)
tck.shapesize(1,tck_len,1)
if out_window():
game_over()
break