Planet sim and Etch added, working properly AFAIK
This commit is contained in:
commit
88f86dc04f
19 changed files with 638 additions and 0 deletions
101
Turtle/Etch/etch_a_sketch_turtle.py
Normal file
101
Turtle/Etch/etch_a_sketch_turtle.py
Normal file
|
@ -0,0 +1,101 @@
|
|||
from turtle import Turtle, mainloop
|
||||
|
||||
|
||||
class Etch:
|
||||
def __init__(self):
|
||||
self.__t = Turtle()
|
||||
self.__screen = self.__t.screen
|
||||
self.__color_list = ['#35b070', 'black', 'white', 'red', 'orange', 'yellow', 'green', 'blue', 'purple', 'pink']
|
||||
self.__color_index = 0
|
||||
self.__t.color(self.__color_list[self.__color_index])
|
||||
self.__t.pensize(1)
|
||||
self.__t.speed(0)
|
||||
self.__distance = 10
|
||||
self.__turn = 10
|
||||
|
||||
# Add callbacks
|
||||
self.__screen.onkey(self.__fwd, 'w')
|
||||
self.__screen.onkey(self.__back, 's')
|
||||
self.__screen.onkey(self.__left, 'a')
|
||||
self.__screen.onkey(self.__right, 'd')
|
||||
self.__screen.onkey(self.__increase_distance, 'e')
|
||||
self.__screen.onkey(self.__decrease_distance, 'q')
|
||||
self.__screen.onkey(self.__increase_turn, 'r')
|
||||
self.__screen.onkey(self.__decrease_turn, 'f')
|
||||
self.__screen.onkey(self.__toggle_pen, 'z')
|
||||
self.__screen.onkey(self.__color, 'c')
|
||||
self.__screen.onkey(self.__clear, 'x')
|
||||
self.__screen.onkey(self.__quit, 'Escape')
|
||||
self.__screen.listen()
|
||||
|
||||
|
||||
def main(self):
|
||||
mainloop()
|
||||
|
||||
# Callback methods
|
||||
def __increase_distance(self):
|
||||
if self.__distance == 100:
|
||||
pass
|
||||
elif self.__distance < 5:
|
||||
self.__distance += 1
|
||||
else:
|
||||
self.__distance += 5
|
||||
|
||||
def __decrease_distance(self):
|
||||
if self.__distance == 1:
|
||||
pass
|
||||
elif self.__distance <= 5:
|
||||
self.__distance -= 1
|
||||
else:
|
||||
self.__distance -= 5
|
||||
|
||||
def __increase_turn(self):
|
||||
if self.__turn == 180:
|
||||
pass
|
||||
elif self.__turn < 5:
|
||||
self.__turn += 1
|
||||
else:
|
||||
self.__turn += 5
|
||||
|
||||
def __decrease_turn(self):
|
||||
if self.__turn == 1:
|
||||
pass
|
||||
elif self.__turn <= 5:
|
||||
self.__turn -= 1
|
||||
else:
|
||||
self.__turn -= 5
|
||||
|
||||
def __fwd(self):
|
||||
self.__t.forward(self.__distance)
|
||||
|
||||
def __back(self):
|
||||
self.__t.backward(self.__distance)
|
||||
|
||||
def __left(self):
|
||||
self.__t.left(self.__turn)
|
||||
|
||||
def __right(self):
|
||||
self.__t.right(self.__turn)
|
||||
|
||||
def __toggle_pen(self):
|
||||
if self.__t.isdown():
|
||||
self.__t.penup()
|
||||
else:
|
||||
self.__t.pendown()
|
||||
|
||||
def __clear(self):
|
||||
self.__t.clear()
|
||||
|
||||
def __color(self):
|
||||
if self.__color_index == 9:
|
||||
self.__color_index = -1
|
||||
self.__color_index += 1
|
||||
self.__t.color(self.__color_list[self.__color_index])
|
||||
|
||||
def __quit(self):
|
||||
exit(0)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
draw = Etch()
|
||||
draw.main()
|
3
Turtle/PlanetSim/README.md
Normal file
3
Turtle/PlanetSim/README.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
### Planet Simulator
|
||||
|
||||
A very basic planet simulator. Really just a demo of using classes with turtle.
|
56
Turtle/PlanetSim/planet.py
Normal file
56
Turtle/PlanetSim/planet.py
Normal file
|
@ -0,0 +1,56 @@
|
|||
import math
|
||||
import turtle
|
||||
|
||||
|
||||
class Planet:
|
||||
def __init__(self, name, radius, mass, distanceFromSun, velocity, color):
|
||||
"""
|
||||
initializes the Planet
|
||||
:param name: name of planet
|
||||
:param radius: radius of planet
|
||||
:param mass: mass of planet
|
||||
:param distanceFromSun: distance of planet from the sun
|
||||
:param velocity: tuple containing the velocity of the planet in the format (x_velocity, y_velocity)
|
||||
:param color: color of planet
|
||||
"""
|
||||
self.__name = name
|
||||
self.__radius = radius
|
||||
self.__mass = mass
|
||||
self.__distance = distanceFromSun
|
||||
self.__velocity = velocity
|
||||
self.__color = color
|
||||
self.__coords = [distanceFromSun, 0]
|
||||
|
||||
self.__pT = turtle.Turtle()
|
||||
self.__pT.pensize(1)
|
||||
self.__pT.color(self.__color)
|
||||
if name == 'earth' or name == 'Earth':
|
||||
self.__pT.shape('turtle')
|
||||
else:
|
||||
self.__pT.shape('circle')
|
||||
self.__pT.up()
|
||||
self.__pT.goto(self.__coords[0], self.__coords[1])
|
||||
self.__pT.down()
|
||||
|
||||
def moveTo(self, new_coords):
|
||||
self.__coords = new_coords
|
||||
self.__pT.goto(self.__coords[0], self.__coords[1])
|
||||
|
||||
@property
|
||||
def velocity(self):
|
||||
return self.__velocity
|
||||
|
||||
@velocity.setter
|
||||
def velocity(self, vel):
|
||||
self.__velocity = vel
|
||||
|
||||
@property
|
||||
def coords(self):
|
||||
return self.__coords
|
||||
|
||||
@coords.setter
|
||||
def coords(self, new_coords):
|
||||
self.__coords = new_coords
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.__name} at distance {self.__distance}'
|
29
Turtle/PlanetSim/planet_sim.py
Normal file
29
Turtle/PlanetSim/planet_sim.py
Normal file
|
@ -0,0 +1,29 @@
|
|||
from sun import Sun
|
||||
from solar_system import SolarSystem
|
||||
from planet import Planet
|
||||
|
||||
|
||||
# Create a SolarSystem
|
||||
solar_sys = SolarSystem(2, 2)
|
||||
|
||||
# Create sun
|
||||
sun = Sun('Sun', 5000, 40, 1000)
|
||||
solar_sys.add_sun(sun)
|
||||
|
||||
# Create planets
|
||||
p = Planet('Mercury', 1, 500, 0.15, (0, 2), 'black')
|
||||
solar_sys.add_planet(p)
|
||||
p = Planet('Venus', 1, 500, 0.25, (0, 2), 'purple')
|
||||
solar_sys.add_planet(p)
|
||||
p = Planet('Earth', 1, 700, 0.5, (0, 2), '#2152a6')
|
||||
solar_sys.add_planet(p)
|
||||
p = Planet('Mars', 1, 700, 0.6, (0, 2), '#b32b10')
|
||||
solar_sys.add_planet(p)
|
||||
|
||||
solar_sys.show_planets()
|
||||
|
||||
num_time_periods = 1000
|
||||
for _ in range(num_time_periods):
|
||||
solar_sys.move_planets()
|
||||
|
||||
solar_sys.freeze()
|
50
Turtle/PlanetSim/solar_system.py
Normal file
50
Turtle/PlanetSim/solar_system.py
Normal file
|
@ -0,0 +1,50 @@
|
|||
import turtle
|
||||
import math
|
||||
|
||||
|
||||
class SolarSystem:
|
||||
def __init__(self, width, height):
|
||||
"""
|
||||
initializes SolarSystem
|
||||
:param width: width of world
|
||||
:param height: height of world
|
||||
"""
|
||||
self.__theSun = None
|
||||
self.__planets = []
|
||||
self.__screen = turtle.Screen()
|
||||
self.__screen.setup(width=950, height=950)
|
||||
self.__screen.setworldcoordinates(-width / 2, -height / 2, width / 2, height / 2)
|
||||
|
||||
def move_planets(self):
|
||||
G = 0.025
|
||||
dt = 0.001
|
||||
|
||||
for p in self.__planets:
|
||||
p.moveTo((p.coords[0] + dt * p.velocity[0], p.coords[1] + dt * p.velocity[1]))
|
||||
|
||||
radius_x = self.__theSun.x - p.coords[0]
|
||||
radius_y = self.__theSun.y - p.coords[1]
|
||||
radius = math.sqrt((radius_x ** 2) + (radius_y ** 2)) # Uses pythagorean theorem to find radius
|
||||
|
||||
acc_x = G * self.__theSun.mass * radius_x / radius ** 3 # I don't know,
|
||||
acc_y = G * self.__theSun.mass * radius_y / radius ** 3 # I haven't taken physics
|
||||
|
||||
p.velocity = (p.velocity[0] + dt * acc_x, p.velocity[1] + dt * acc_y)
|
||||
|
||||
def add_sun(self, sun):
|
||||
self.__theSun = sun
|
||||
|
||||
def add_planet(self, planet):
|
||||
self.__planets.append(planet)
|
||||
|
||||
def remove_planet(self, planet_name):
|
||||
for p in self.__planets:
|
||||
if p.name == planet_name:
|
||||
self.__planets.remove(p)
|
||||
|
||||
def show_planets(self):
|
||||
for p in self.__planets:
|
||||
print(p)
|
||||
|
||||
def freeze(self):
|
||||
self.__screen.exitonclick()
|
67
Turtle/PlanetSim/sun.py
Normal file
67
Turtle/PlanetSim/sun.py
Normal file
|
@ -0,0 +1,67 @@
|
|||
import math
|
||||
import turtle
|
||||
|
||||
|
||||
class Sun:
|
||||
def __init__(self, name, radius, mass, temp):
|
||||
"""
|
||||
initializes Sun
|
||||
:param name: name of Sun
|
||||
:param radius: radius of Sun
|
||||
:param mass: mass of Sun
|
||||
:param temp: temperature of Sun
|
||||
"""
|
||||
self.__name = name
|
||||
self.__radius = radius
|
||||
self.__mass = mass
|
||||
self.__temp = temp
|
||||
# List of coordinates in order of [x, y]
|
||||
self.__coords = (0, 0)
|
||||
|
||||
self.__sunT = turtle.Turtle()
|
||||
self.__sunT.shape('circle')
|
||||
self.__sunT.color('orange')
|
||||
self.__sunT.shapesize(2)
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return self.__name
|
||||
|
||||
@property
|
||||
def x(self):
|
||||
return self.__coords[0]
|
||||
|
||||
@property
|
||||
def y(self):
|
||||
return self.__coords[1]
|
||||
|
||||
@property
|
||||
def mass(self):
|
||||
return self.__mass
|
||||
|
||||
@property
|
||||
def radius(self):
|
||||
return self.__radius
|
||||
|
||||
@property
|
||||
def temperature(self):
|
||||
return self.__temp
|
||||
|
||||
@property
|
||||
def volume(self):
|
||||
return (4 / 3) * math.pi * (self.__radius ** 3)
|
||||
|
||||
@property
|
||||
def surface_area(self):
|
||||
return 4 * math.pi * (self.__radius ** 2)
|
||||
|
||||
@property
|
||||
def density(self):
|
||||
return self.__mass / self.volume
|
||||
|
||||
@name.setter
|
||||
def name(self, name):
|
||||
self.__name = name
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.__name} at ({self.__coords[0]}, {self.__coords[1]})'
|
11
Turtle/turtle.md
Normal file
11
Turtle/turtle.md
Normal file
|
@ -0,0 +1,11 @@
|
|||
### Turtle
|
||||
##### [Turtle documentation](https://docs.python.org/3/library/turtle.html)
|
||||
|
||||
- What is turtle?
|
||||
- Imagine you control a robotic turtle (default is actually a black triangle) which moves around on screen.
|
||||
- You draw lines, like a digital etch-a-sketch (but you can lift the "pen" off the screen)
|
||||
- For example:
|
||||
- Move forward: `turtle.forward(num_pixels)`
|
||||
- Turn right: `turtle.right(degrees)`
|
||||
|
||||
- For usage of turtle, look in Etch class. It's pretty self-explanatory.
|
Loading…
Add table
Add a link
Reference in a new issue