Reputation: 447
I am making a simple Object-oriented projectile simulation program in python. I am just using the Turtle and Math modules. The problem is when I try to simply move my projectile (as a test before integrating some equations) it does not move.
import turtle
from turtle import Turtle
import math
window = turtle.Screen()
window.title("Projectile")
window.bgcolor("black")
window.setup(width=800, height=800)
window.tracer(0)
def drawLine():
line = turtle.Turtle()
line.penup()
line.pencolor("white")
line.pensize(10)
line.goto(-400, -300)
line.pendown()
line.forward(800)
line.penup()
line.ht()
class Projectile:
def __init__(self, x, y, color, shape, a, b):
self.turtle = turtle.Turtle()
self.turtle.goto(x, y)
self.turtle.color(color)
self.turtle.shape(shape)
self.turtle.shapesize(a, b)
def launch(self, x, y):
self.turtle.penup()
self.turtle.setx(self.turtle.xcor() + x)
self.turtle.sety(self.turtle.ycor() + y)
running = True
while running:
window.update()
drawLine()
projectileOne = Projectile(-290, -290, "red", "circle", 1, 1)
projectileOne.launch(25, 25)
This is supposed to move my turtle, isn't it?
self.turtle.setx(self.turtle.xcor() + x)
self.turtle.sety(self.turtle.ycor() + y)
I don't understand what's happening. Why isn't the projectile moving? It just moves (25, 25) and stops.
The errors I get after the code runs:
Traceback (most recent call last):
File "C:\Users\HP\Desktop\Python projects\test\test.py", line 39, in <module>
drawLine()
File "C:\Users\HP\Desktop\Python projects\test\test.py", line 12, in drawLine
line = turtle.Turtle()
File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\turtle.py", line 3813, in __init__
RawTurtle.__init__(self, Turtle._screen,
File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\turtle.py", line 2557, in __init__
self._update()
File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\turtle.py", line 2660, in _update
self._update_data()
File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\turtle.py", line 2646, in _update_data
self.screen._incrementudc()
File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\turtle.py", line 1292, in _incrementudc
raise Terminator
turtle.Terminator
[Finished in 7.4s]
And if I completely remove drawLine()
from the code I get:
Traceback (most recent call last):
File "C:\Users\HP\Desktop\Python projects\test\test.py", line 40, in <module>
projectileOne = Projectile(-290, -290, "red", "circle", 1, 1)
File "C:\Users\HP\Desktop\Python projects\test\test.py", line 24, in __init__
self.turtle = turtle.Turtle()
File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\turtle.py", line 3813, in __init__
RawTurtle.__init__(self, Turtle._screen,
File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\turtle.py", line 2557, in __init__
self._update()
File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\turtle.py", line 2660, in _update
self._update_data()
File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\turtle.py", line 2646, in _update_data
self.screen._incrementudc()
File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\turtle.py", line 1292, in _incrementudc
raise Terminator
turtle.Terminator
[Finished in 5.2s]
Upvotes: 5
Views: 2210
Reputation: 41872
The errors you get after you close the program are not directly related to your drawing issue. They're caused by this logic:
running = True
while running:
An infinite loop like this has no place in an event-driven environment like turtle, it potentially blocks events. (Like the "window is closing" event.) Instead, use a timer event to keep things running.
Below is a rewrite of your code that uses timer events to launch a couple of (gravity ignorant) projectiles that will keep flying until they reach the top of the window. Hopefully this will give you a framework upon which to build your simulation:
from turtle import Screen, Turtle
class Projectile(Turtle):
def __init__(self, position, color, shape, width, length):
super().__init__(shape=shape)
self.color('white', color)
self.shapesize(width, length)
self.penup()
self.setposition(position)
self.pendown()
self.pensize(4)
self.flying = False
screen.update()
def launch(self, angle):
self.setheading(angle)
self.flying = True
self.fly()
def fly(self):
if self.flying:
self.forward(3)
if self.ycor() > screen.window_height()/2:
self.flying = False
screen.ontimer(self.fly, 50)
screen.update()
screen = Screen()
screen.setup(width=800, height=800)
screen.title("Projectile")
screen.bgcolor('black')
screen.tracer(0)
projectileOne = Projectile((-290, -290), 'red', 'triangle', 1, 2)
projectileTwo = Projectile((290, -290), 'green', 'circle', 1, 1)
projectileOne.launch(75)
projectileTwo.launch(130)
screen.mainloop()
Upvotes: 2