tried to fix simulation, not done yet
This commit is contained in:
@@ -36,7 +36,7 @@ class System:
|
||||
"""Return the force of attraction of the astre2 on the astre1."""
|
||||
norm = G * astre1.mass * astre2.mass / cls.distance(astre1, astre2)**2
|
||||
angle = (astre2.position - astre1.position).angle
|
||||
return Force.createFromPolarCoordonnates(norm, angle) / astre1.mass
|
||||
return Force.createFromPolar(norm, angle) / astre1.mass
|
||||
|
||||
def random(n=10):
|
||||
"""Create a random system."""
|
||||
@@ -159,10 +159,10 @@ class Planet(Astre):
|
||||
"""Find the motion of a planet with its distance and its mass."""
|
||||
angle = random.uniform(0, 2 * math.pi)
|
||||
norm = self.distance
|
||||
position = Vector.createFromPolarCoordonnates(norm, angle)
|
||||
position = Vector.createFromPolar(norm, angle)
|
||||
angle = (angle + math.pi / 2) % (2 * math.pi)
|
||||
norm = self.speed # in m/s-1
|
||||
velocity = Vector.createFromPolarCoordonnates(norm, angle)
|
||||
velocity = Vector.createFromPolar(norm, angle)
|
||||
acceleration = Vector.null()
|
||||
return Motion(position, velocity, acceleration)
|
||||
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
from pygame_geometry.events import Control, Event, Manager
|
||||
import pygame
|
||||
|
||||
def zoom_in(manager:Manager):
|
||||
manager.context.plane.zoom([1.1, 1.1])
|
||||
|
||||
def zoom_out(manager:Manager):
|
||||
manager.context.plane.zoom([0.9, 0.9])
|
||||
|
||||
controls = [
|
||||
Control(Event(pygame.KEYDOWN, key=pygame.K_RSHIFT), zoom_in),
|
||||
Control(Event(pygame.KEYDOWN, key=pygame.K_LSHIFT), zoom_out),
|
||||
]
|
||||
Executable
+72
@@ -0,0 +1,72 @@
|
||||
#!/usr/bin/env python
|
||||
from pygame_geometry.context import Context
|
||||
from pygame_geometry.events import on, Manager
|
||||
from pygame_geometry.triangle import Triangle
|
||||
from pygame_geometry.body import Body
|
||||
from pygame_geometry.anatomies import Anatomy
|
||||
from pygame_geometry.motion import Motion
|
||||
from pygame_geometry.abstract import Vector
|
||||
from pygame_geometry import colors
|
||||
from controls import controls
|
||||
import pygame
|
||||
|
||||
class TriangleAnatomy(Anatomy):
|
||||
def __init__(self):
|
||||
self.triangle:Triangle = Triangle.random(area_color=colors.RED)
|
||||
|
||||
def show(self, context):
|
||||
self.triangle.show(context)
|
||||
|
||||
def rotate(self, angle):
|
||||
self.triangle.rotate(angle)
|
||||
|
||||
|
||||
class PlaneController(Manager):
|
||||
@on.press(pygame.KEYDOWN, pygame.K_RSHIFT)
|
||||
def zoom(self):
|
||||
print('zoom')
|
||||
self.context.zoom()
|
||||
|
||||
|
||||
class SpaceInvader(Manager):
|
||||
def __init__(self, context:Context, controls:list):
|
||||
super().__init__(context)
|
||||
self.player = Body(
|
||||
TriangleAnatomy(),
|
||||
[Motion(Vector(0, 0), Vector(0, 0))]
|
||||
)
|
||||
self.controls.extend(controls)
|
||||
|
||||
@on.press(pygame.KEYDOWN, pygame.K_ESCAPE)
|
||||
def quit(self):
|
||||
"""Quit the game."""
|
||||
self.on = False
|
||||
|
||||
@on.press(pygame.KEYDOWN, pygame.K_w)
|
||||
def up(self):
|
||||
"""Make the player go up."""
|
||||
self.player.position.y += 1
|
||||
|
||||
def show(self):
|
||||
"""Show the game."""
|
||||
super().show()
|
||||
self.player.show(self.context)
|
||||
self.context.console.show()
|
||||
|
||||
|
||||
class SpaceInvaderDebugger(Manager):
|
||||
@on.press(pygame.KEYDOWN, pygame.K_t)
|
||||
def triangle(self):
|
||||
self.context.console(self.player.anatomy.triangle)
|
||||
|
||||
@on.press(pygame.KEYDOWN, pygame.K_p)
|
||||
def position(self):
|
||||
"""Show the position of the player."""
|
||||
self.context.console(self.player.position)
|
||||
|
||||
|
||||
if __name__=="__main__":
|
||||
context = Context(name="Space Invader")
|
||||
# controls = SpaceInvaderDebugger(context).controls + PlaneController(context).controls
|
||||
space_invader = SpaceInvader(context, controls)
|
||||
space_invader()
|
||||
Reference in New Issue
Block a user