简体   繁体   中英

How can I draw in Turtle given speed and angle value?

First look at my code . If you run it, the program will look like this .

So, my question is: I have to input the speed and angle value and press the button to launch it, but I don't know how to do that. What I have in mind right now is

(int(speed1.get())*math.cos(int(angle1.get())))*(int(speed1.get())*math.sin(int(angle1.get()))/4.9)

I think I need to use this. if d > 25: is the determination, so I don't know if I have to put the formula in d .

How can I do that?

I see several issue with your code. First, is you're using standalone turtle with tkinter. When you embed turtle in a tkinter program, you need to use embedded turtle, ie RawTurtle instead of Turtle , and TurtleScreen instead of Screen , etc. Otherwise, you have two roots and strange problems will crop up.

Second, you need some sort of ballistic logic, simply calculating an angle isn't sufficient. Also, you have to be clear about which angles are degrees (eg possibly user input; what turtle wants by default) vs. radians (what the math.py library wants.)

Finally, one way to communicate values between your tkinter interface and your turtle output is by using IntVar values associated with the Entry widgets. Here's my (potentially buggy) rework of your code that addresses the above issues:

from tkinter import *
from turtle import TurtleScreen, RawTurtle
from random import randint
from math import sin, cos, radians

x0, y0 = -200, 10  # initial location

g = 11.0  # acceleration due to gravity in units per second squared

def fire():
    a = angle.get()
    turtle.setheading(a)

    v = velocity.get()

    vx, vy = cos(radians(a)) * v, sin(radians(a)) * v  # initial velocity in units per second

    for t in range(1, 10_000):

        x = x0 + vx * t
        y = y0 + vy * t - g / 2 * t**2

        turtle.goto(x, y)

        if y < y0:
            break

    distance = turtle.distance(target, y0)

    if distance < 25:
        turtle.color('blue')
        turtle.write("HIT", align='center', font=('', 10))
    else:
        turtle.color('red')
        turtle.write("MISS", align='center', font=('', 10))

    turtle.color('black')
    turtle.goto(x0, y0)
    turtle.setheading(0)

window = Tk()
window.geometry("+250+150")
window.title("Ballistics")

canvas = Canvas(window, width=600, height=300)
canvas.pack() # fill="both", expand=True)

screen = TurtleScreen(canvas)
turtle = RawTurtle(screen)

turtle.penup()
turtle.setx(-300)
turtle.pendown()
turtle.setx(300)

target = randint(50, 150)

turtle.pensize(3)
turtle.color('green')
turtle.penup()
turtle.goto(target - 25, 2)
turtle.pendown()
turtle.goto(target + 25, 2)

turtle.color('black')
turtle.pensize(2)
turtle.penup()
turtle.goto(x0, y0)

menu = Toplevel(window)
menu.geometry("200x150")
menu.title("Menu")

velocity = IntVar()
Label(menu, text="Velocity").pack()
Entry(menu, textvariable=velocity).pack()

angle = IntVar()
Label(menu, text="Angle").pack()
Entry(menu, textvariable=angle).pack()

Button(menu, text="Fire", command=fire).pack()

window.mainloop()

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM