Initial commit
This commit is contained in:
37
simulation/ball.py
Normal file
37
simulation/ball.py
Normal file
@@ -0,0 +1,37 @@
|
||||
from geometry.point import Point
|
||||
from geometry.vector import Vector
|
||||
from geometry.validation import expect_type
|
||||
from simulation.drawing import centered_circle
|
||||
from turtle import begin_fill, end_fill, fillcolor
|
||||
|
||||
class Ball:
|
||||
def __init__(self, position, velocity=None, mass=1, radius=20, color="black"):
|
||||
expect_type(position, Point)
|
||||
self.position = position
|
||||
self.velocity = velocity or Vector()
|
||||
expect_type(self.velocity, Vector)
|
||||
self.acceleration = Vector()
|
||||
self.radius = radius
|
||||
self.mass = mass
|
||||
self.color = color
|
||||
|
||||
def update(self):
|
||||
"""Update the ball's position and velocity at this moment.
|
||||
|
||||
"""
|
||||
self.velocity += self.acceleration
|
||||
self.position += self.velocity
|
||||
self.acceleration = Vector()
|
||||
|
||||
def collided_with(self, other_ball):
|
||||
"""Checks whether this ball collided with another ball.
|
||||
"""
|
||||
return (self.position - other_ball.position).mag() <= self.radius + other_ball.radius
|
||||
|
||||
def draw(self):
|
||||
"Draws a colored ball."
|
||||
fillcolor(self.color)
|
||||
begin_fill()
|
||||
centered_circle(self.radius)
|
||||
end_fill()
|
||||
|
||||
Reference in New Issue
Block a user