Strike an Angry Bird-like target with a Python turtleneck. Obtain collision conditions on the target.

Asked 2 years ago, Updated 2 years ago, 134 views

I'm going to make something similar to the Angry Bird game with Python. So I made the code

import turtle as t
import math
import random

t.shape("turtle")

def square():
    for i in range(4):
        t.forward(10)
        t.left(90)

d1 = random.randomint (100,200) #d1 is set to random
t.up()
t.forward(d1)
t.down()

square() # This square is the target.

t.up()
t.goto(0,0)
t.down()

def fire(): #FireFunction.
    x = 0
    y = 0
    speed = int (input ("speed:")
    angle = int (input ("angle:")
    vx = speed * math.cos(angle * 3.14/180.0)
    vy = speed * math.sin(angle * 3.14/180.0)

    while.ycor() > = 0: Repeat until #y coordinates are negative
        vx = vx
        vy = vy - 10
        x = x + vx
        y = y + vy
        if x>=d1 and x<=d1+10 and y>=0 and y<=10: #If the x value is between d1 and d1+10 and the y value is between 0 and 10, exit the program
            break
            exit()
        else: #If not, continue drawing
            t.goto(x,y)
    t.up() 
    t.goto(0,0)
    t.down()
    fire()


fire()

Especially

    while t.ycor() >= 0: 
        vx = vx
        vy = vy - 10
        x = x + vx
        y = y + vy
        if x>= d1 and x<=d1+10 and y>=0 and y<=10:
            break
            exit()
        else:
            t.goto(x,y)
    t.up()
    t.goto(0,0)
    t.down()
    fire()

This is the problem. Originally, I was going to end the game when the turtle touched a square target, but it doesn't work even if it meets the conditions.

When I use a while statement to draw a parabola and move, I tried to end the program if the x-coordinate and y-coordinate satisfy the if conditional statement, but if the program is turned, the program will not end even if the if condition is satisfied. What's wrong with you?

python while-statement turtle

2022-09-21 10:01

1 Answers

If (x(i), y(i)) in the intersection, which approximates the parabola, is larger than v, you can skip inside the target rectangle.

So,


2022-09-21 10:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.