我正在努力修复一些代码以使其看起来更好,当我完成代码复制并完成修复后,我运行它并且没有任何反应。加载两件事然后一切都清除了。我找不到问题。
原始代码有效
import turtle
from random import randint
import time
"""-------------"""
t=turtle.Turtle()
s=turtle.Screen()
cube=turtle.Turtle()
title=turtle.Turtle()
"""--------------"""
WIDTH, HEIGHT=300, 300
LENGTH=(5)
"""--------------"""
x1 = randint(cube.xcor()/2 - WIDTH/2, WIDTH/2 - cube.xcor()/2)
y1 = randint(cube.ycor()/2 - HEIGHT/2, HEIGHT/2 - cube.ycor()/2)
"""------------"""
s.setup(WIDTH,HEIGHT)
"""------------"""
t.width(1)
s.bgcolor("dark green")
"""-----------------"""
#Title
title.color("Red")
title.hideturtle()
title.penup()
title.goto(0,130)
title.pendown()
title.write("SNAK3",align="center", font=("Lobster", 20, "Bold"))
"""-------------"""
class Food():
def block():
cube.hideturtle()
cube.color("red")
cube.penup()
cube.goto(x1,y1)
cube.speed(0)
cube.begin_fill()
for i in range(4):
cube.pendown()
cube.forward(10)
cube.right(90)
cube.end_fill()
"""---------------"""
t.shape("square")
class Player():
def move_up():
player=False
while player==False:
for i in range(10):
t.forward(LENGTH)
t.delay(25)
t.clear()
x, y = t.position()
if not -WIDTH / 2 < x < WIDTH / 2 or not -HEIGHT / 2 < y < HEIGHT / 2:
player=True
t.hideturtle()
cube.clear()
title.clear()
title.penup()
title.goto(0,0)
title.pendown()
title.write("GAMEOVER", align="center", font=("lobster", 18, "bold"))
def move_left():
t.speed(0)
t.left(90)
t.speed(3)
def move_right():
t.speed(0)
t.right(90)
t.speed(3)
"""------------"""
food=Food.block()
player1=Player()
"""----------------"""
s.onkey(Player.move_up,"up")
s.onkey(Player.move_left,"left")
s.onkey(Player.move_right,"right")
s.listen()
"""--------------"""
collision=Collision_check.check()
"""--------------------"""
t1=t.xcor(),t.ycor()
cube1=cube.xcor(),cube.ycor()
class Collision_check():
def check():
crash=True
if t1.xcor()>=cube.xcor() and t1.xcor()<=cube.xcor()+10 and t1.ycor>=cube.ycor() and t1.ycor()<=cube.ycor+10:
cube.clear()
cube.goto(x1,y1)
LENGTH=+1
elif t1!=cube1:
crash=False
return crash
修正代码稍作修改
import turtle
from random import randint
t=turtle.Turtle()
s=turtle.Screen()
cube=turtle.Turtle()
title=turtle.Turtle()
t.width(1)
s.bgcolor("dark green")
#Title
title.color("red")
title.hideturtle()
title.penup()
title.goto(0,130)
title.write("SNAK3",align="center",font=("lobster",20,"Bold"))
WIDTH, HEIGHT=300, 300
LENGTH=[5]
s.setup(WIDTH,HEIGHT)
#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)
class Food():
def block():
cube.hideturtle()
cube.color("red")
cube.penup()
cube.goto(x1,y1)
cube.speed(0)
cube.begin_fill()
cube.pendown()
for i in range(4):
cube.forward(10)
cube.right(90)
cube.end_fill()
t.shape("square")
class Player():
def forward():
player=False
while player==False:
for i in range(10):
t.forward(LENGTH)
t.delay(25)
t.clear
x, y=t.position()
if not -WIDTH / 2 < x < WIDTH / 2 or not -HEIGHT / 2 < y < HEIGHT/2:
player=True
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)
food=Food.block()
player1=Player()
s.onkey(player1.forward,("up"))
s.onkey(player1.left,("left"))
s.onkey(player1.left,("right"))
class Collision_check():
def check():
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
collision=Collision_check.check()
上面代码中的所有内容与原始代码几乎完全相同。我已经多次查看代码并找不到问题。 我已经排除了collision_check不是问题,所有的类都按顺序排列。我能想到的另一件事就是我拼错了。
答案 0 :(得分:0)
让我们首先解决一些基本事实:
此代码的任何版本均无效
使它看起来更好的微小变化不会让它看起来更好 并且不是轻微的,存在显着差异
这两个版本的代码都没有正确使用Python类
repl.it通常提供自己的乌龟实现 原始
一个程序就像一个故事,你首先要解释事件的背景,然后你继续以符合逻辑的顺序讲述故事。
这是我对你的代码进行的修改,试图讲述一个更好的故事,但由于缺少部分故事,它仍然无法完全发挥作用:
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()