[英]How can I make the code that judges the collision a turtle and a rectangle
In my task requirement, 1. Draw a random square between 100 and 200. 2. a square's width and height must be 10. 3. Get to the user inputting the launch speed and the launch angle.在我的任务要求中, 1. 在 100 到 200 之间随机绘制一个正方形。 2. 正方形的宽度和高度必须为 10。 3. 让用户输入发射速度和发射角度。 4. Fire a shell from the origin to the square.
4. 从原点向正方形发射 shell。 but a shell must draw a parabola.
但是 shell 必须画一条抛物线。 5. If a shell hit a square, end the program, else return to number 3.
5. 如果 shell 击中方块,则结束程序,否则返回数字 3。
but the problem is number 5. I try to embody number 5, but It didn't work.但问题是数字 5。我尝试体现数字 5,但它没有用。 please look at my code.
请看我的代码。
import turtle as t
import math
import random
import sys
def square(): //function that make the square
for i in range(4):
t.forward(10)
t.left(90)
t.forward(500)
t.goto(0,0)
d1 = random.randint(100,200) //choose the random distance between 100 to 200
t.up()
t.forward(d1)
t.down()
square() //satisfy condition 1
t.up()
t.goto(0,0)
t.down()
def fire(): //function that fire a shell to a square direction.
x = 0
y = 0
gameover = False
speed = int(input("speed:")) //satisfy condition 3
angle = int(input("angle:")) //satisfy condition 3
vx = speed * math.cos(angle * 3.14/180.0)
vy = speed * math.sin(angle * 3.14/180.0)
while t.ycor()>=0: // until y becomes negative. and satisfy condition 4
vx = vx
vy = vy - 10
x = x + vx
y = y + vy
t.goto(x,y)
x_pos = t.xcor() // save x-coordinate to x_pos
y_pos = t.ycor() // save y-coordinate to y_pos
if d1<=x_pos<=d1+10 and 0 <= y_pos <= 10: // if x_pos and y_pos are within range, save gameover to True and break
gameover = True
break
if gameover == True: // trying to satisfy number 5
exit()
else: // if a shell not hit a square repeat the fire.
t.up()
t.home()
t.down()
fire()
fire()
please tell me how could I embody number 5 or tell me what I did wrong.请告诉我如何体现数字 5 或告诉我我做错了什么。
Instead of fire()
calling it self recursively on subsequent attempts, you would be better off with an while not gameover:
loop in fire()
and adjust the rest of the code accordingly.与其
fire()
在后续尝试中递归地调用它,不如使用while not gameover:
循环fire()
并相应地调整代码的 rest。 In my rewrite of your code below, I use such a loop and I also use turtle's own graphic numinput()
function instead of the console:在我重写下面的代码时,我使用了这样的循环,并且我还使用了海龟自己的图形
numinput()
function 而不是控制台:
from turtle import Screen, Turtle
from math import radians, sin, cos
from random import randint
def square(t):
''' function that make the square '''
for _ in range(4):
t.forward(10)
t.left(90)
def fire(t):
''' function that fire a shell to a square direction '''
gameover = False
speed = None
angle = None
while not gameover:
t.up()
t.home()
t.down()
speed = screen.numinput("Angry Turtle", "Speed:", default=speed, minval=1, maxval=1000) # satisfy condition 3
if speed is None:
continue
angle = screen.numinput("Angry Turtle", "Angle (in degrees):", default=angle, minval=1, maxval=89) # satisfy condition 3
if angle is None:
continue
vx = speed * cos(radians(angle))
vy = speed * sin(radians(angle))
x = 0
y = 0
while t.ycor() >= 0: # until y becomes negative; satisfy condition 4
vy -= 10
x += vx
y += vy
t.goto(x, y)
x_pos, y_pos = t.position()
# if x_pos and y_pos are within range, set gameover to True and break loop
if d1 <= x_pos <= d1 + 10 and 0 <= y_pos <= 10:
gameover = True
break
screen = Screen()
screen.title("Angry Turtle")
turtle = Turtle()
turtle.shape("turtle")
turtle.forward(screen.window_width() // 2)
turtle.home()
d1 = randint(100, 200) # choose the random distance between 100 to 200
turtle.up()
turtle.forward(d1)
turtle.down()
square(turtle) # satisfy condition 1
fire(turtle)
However, the above design traps the user in a "win the game if you want to exit" scenario and you might want to give them a better way to quit the program.然而,上面的设计让用户陷入“如果你想退出就赢得游戏”的场景,你可能想给他们一个更好的退出程序的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.