How do I determine a Python Turtle crash?

Asked 2 years ago, Updated 2 years ago, 15 views

My challenge is to implement Angry Bird Python under these four conditions. So I tried to implement it with Python. But when I tried it, I couldn't implement Condition 4. I'm sure that when the turtle moves, it compares the x and y values within a given range, and if it's within that range, it ends, or it's re-launched, but it's not implemented properly.

import turtle as t
import math
import random

def square(): // Functions that draw squares
    for i in range(4):
        t.forward(10)
        t.left(90)


t.forward(500) //Drawing the Ground
t.goto(0,0)

d1 = random.randomint(100,200) //Implementing a random selection condition from 100 to 200

t.up()
t.forward(d1)
t.down()
Create a target by drawing squares() // squares

t.up()
t.home()
t.down()

def fire(): // Generate a launch function
    x=0
    y=0
    gameover = False

    speed = int("input:") // Implement condition 2
    angle = int(input("angle:") // Implement condition 2

    vx = speed * math.cos(angle * 3.14/180.0)
    vy = speed * math.sin(angle * 3.14/180.0)

    When gameover == False and t.ycor()> = 0: // Repeat only when gameover is False and the y value of t is not negative to implement condition 3
        vx = vx
        vy = vy - 10
        x = x + vx
        y = y + vy
        t.goto(x,y) // Condition 3 firing shells in the rectangular direction
        x_pos = t.xcor()
        y_pos = t.ycor()
        If d1<=x_pos<=d1+10 or 0<=y_pos<= 10: // If x and y values tried to implement // condition 4.
            gameover = True
            break

    if gameover == True: 
        exit()
    else:
        t.up()
        t.home()
        t.down()
        fire()
fire()

The code you created to implement Condition 4 in question is below.

while gameover == False and t.ycor()>=0: /
        vx = vx
        vy = vy - 10
        x = x + vx
        y = y + vy
        t.goto(x,y) //Problematic Condition 4 Implementation Code.
        x_pos = t.xcor() // After moving the turtle, return the x value and store it in x_pos
        y_pos = t.ycor() // Save the same y value in x_pos
        if d1<=x_pos<=d1+10 and 0<=y_pos<=10: //if the values of x and y fit within this range,
            Assign a True value to the gameover = True //gameover variable
            Break //After exiting the repeat statement

    If gameover == True: //If the gameover variable is True
        exit() // Exit the program
    else: // if not
        t.up()
        t.home() //Back to square one
        t.down()
        fire() // Fire again

I wrote it with this thought, but even if the turtle falls within that range, the program has not ended... I would appreciate it if you could tell me what the problem is with the code I wrote.

python

2022-09-21 09:59

1 Answers

The problem is the same as in the last question.


                                                        o x+vx, y+vy
                       _________________________
                       |                       |
                       |                       |
                       |                       |
        o              | ______________________|
    x, y 

So the line connecting (x,y) points and (x+vx, y+vy) is clearly past the square area, but your condition is that you check if these two points are in the square, so it doesn't conflict.

Find the equation of the straight line passing through the two points and see if the straight line passes through the square area. You can do your best at math in middle and high school. However, if you think about it carefully, you can only check when x enters the area where d1<=x<=d1+10, and if you think about the conditions, you can find a surprisingly simple condition.

What might be a little simpler is not to draw a parabola in a straight line, but to find an equation for the parabola (y = ax^2 + b), and to see if it enters the target square area in the d1<= x <=d1+10 area. The parabolic equation is fairly simple if t is erased from the pink equation in the attached image.


2022-09-21 09:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.