python turtle直接显示结果_Python turtle |显示不会sh

让我们先谈谈一些基本事实:此代码的两个版本都不起作用

为了使它看起来更好而做的细微改变并不能使它看起来更好

而且并不轻微,存在显著差异

两个版本的代码都没有正确使用Python类

在更换海龟通常提供自己的实现

原稿的子集

程序就像一个故事,你首先解释事件的背景,然后按照合乎逻辑的顺序讲述故事。在

以下是我对您的代码进行的重新编写,试图讲述一个更好的故事,但它仍然不能完全起作用,因为故事的某些部分缺失了:from turtle import Turtle, Screen

from random import randint

WIDTH, HEIGHT = 300, 300

class Food():

def block():

cube.penup()

cube.goto(x1, y1)

cube.pendown()

cube.begin_fill()

for _ in range(4):

cube.forward(10)

cube.right(90)

cube.end_fill()

class Player():

def forward():

for _ in range(10):

t.forward(LENGTH)

# t.delay(25) # no such turtle method, perhaps repl.it addition

t.clear()

x, y = t.position()

if not -WIDTH / 2 < x < WIDTH / 2 or not -HEIGHT / 2 < y < HEIGHT/2:

cube.clear()

t.hideturtle()

title.clear()

title.penup()

title.goto(0, 0)

title.pendown()

title.write("GAMEOVER", align='center', font=('lobster', 18, 'bold'))

def left():

t.speed(0)

t.left(90)

t.speed(3)

def right():

t.speed(0)

t.right(90)

t.speed(3)

class Collision_check():

def check():

global LENGTH

crash = True

if t.xcor() >= cube.xcor() and t.xcor() <= cube.xcor() + 10 and t.ycor() >= cube.ycor() and t.ycor() <= cube.ycor() + 10:

cube.clear()

cube.goto(x1, y1)

LENGTH += 1

if cube.xcor() >= t.xcor() and cube.xcor() <= t.xcor()+10 and cube.ycor() >= t.ycor() and cube.ycor() <= t.ycor()+10:

cube.clear()

cube.goto(x1, y1)

LENGTH += 1

else:

crash = False

return crash

s = Screen()

s.setup(WIDTH, HEIGHT)

s.bgcolor('dark green')

t = Turtle()

t.shape('square')

t.width(1)

cube = Turtle()

cube.hideturtle()

cube.color('red')

cube.speed('fastest')

# Title

title = Turtle()

title.hideturtle()

title.color('red')

title.penup()

title.goto(0, 130)

title.write("SNAK3", align='center', font=('lobster', 20, 'bold'))

LENGTH = 5

# Random cords

x1 = randint(cube.xcor()/2 - WIDTH/2, WIDTH/2 - cube.xcor()/2)

y1 = randint(cube.ycor()/2 - HEIGHT/2, HEIGHT/2 - cube.ycor()/2)

food = Food.block()

player1 = Player()

s.onkey(Player.forward, 'Up')

s.onkey(Player.left, 'Left')

s.onkey(Player.right, 'Right')

s.listen()

# this makes no sense here, needs to happen on a regular basis after moves

collision = Collision_check.check()

s.mainloop()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值