[英]How can I make this 9 different balls with different colors and sizes in python
[英]How can I make my (python)balls stay in the box?
除了 sussy 标题(对不起,我没有更好的说法),我正在做一个项目,我必须让有弹性的球在一个正方形中弹跳。 这是我的代码(请使用相同的海龟名称)
import turtle
import random
turt = turtle.Turtle()
tart = turtle.Turtle()
screen = turtle.Screen()
turt.speed('fastest')
tart.speed('fastest')
turt.penup()
turt.goto(-250,250)
turt.pendown()
tart.penup()
tart.color("red")
tart.pensize(10)
tart.shape("circle")
for i in range(4):
turt.forward(450)
turt.right(90)
def up():
turt.setheading(90)
if turt.ycor() < 240:
tart.forward(1)
def down():
turt.setheading(270)
if -240 < tart.ycor():
tart.forward(1)
def left():
turt.setheading(180)
if -240 < turt.xcor():
tart.forward(1)
def right():
tart.setheading(0)
if turt.xcor() < 240:
tart.forward(1)
screen.onkey(up, 'w')
screen.onkey(down, 's')
screen.onkey(left, 'a')
screen.onkey(right, 'd')
screen.mainloop()
tart.right(
random.randint(1,359)
)
tart.pendown()
while True:
tart.forward(10)
我只是想让球在广场上暂时停止弹跳
您在运动功能中混合了turt
和tart
名称(更好的名称会有所帮助!)并且边界坐标是错误的。 下面我还添加了一个listen()
,因为键不起作用并增加了转发大小,因为它真的很慢。
import turtle
import random
turt = turtle.Turtle()
tart = turtle.Turtle()
screen = turtle.Screen()
turt.speed('fastest')
tart.speed('fastest')
turt.penup()
turt.goto(-250,250)
turt.pendown()
tart.penup()
tart.color("red")
tart.pensize(10)
tart.shape("circle")
for i in range(4):
turt.forward(450)
turt.right(90)
speed = 10
def up():
tart.setheading(90)
if tart.ycor() < 240:
tart.forward(speed)
def down():
tart.setheading(270)
if tart.ycor() > -190:
tart.forward(speed)
def left():
tart.setheading(180)
if tart.xcor() > -240:
tart.forward(speed)
def right():
tart.setheading(0)
if tart.xcor() < 190:
tart.forward(speed)
screen.onkey(up, 'w')
screen.onkey(down, 's')
screen.onkey(left, 'a')
screen.onkey(right, 'd')
screen.listen()
screen.mainloop()
tart.right(
random.randint(1,359)
)
tart.pendown()
while True:
tart.forward(10)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.