From 359e7accb23bcce3759e7270cc2ed57030119ab3 Mon Sep 17 00:00:00 2001 From: Valentin Gehrke Date: Tue, 16 Feb 2016 19:36:35 +0100 Subject: [PATCH] Random staff lengths + mouse interaction --- pendel.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/pendel.py b/pendel.py index 55f57de..afaf1c3 100644 --- a/pendel.py +++ b/pendel.py @@ -43,9 +43,11 @@ class PendelSim(Game): def __init__(self): super(PendelSim,self).__init__(1000,600, 60) self.init_data() + self.mousedown = False + self.mousepos = (0,0) def init_data(self): - self.staff_lengths = staff_lengths = [ randint(100,200) for i in range(3) ] + self.staff_lengths = staff_lengths = [ randint(150,200), randint(100,200), randint(100,150) ] fixed_joint = Vector( self._width/2, self._height/5 ) joint_a = fixed_joint - Vector(staff_lengths[0],0) joint_b = joint_a - Vector(0,-staff_lengths[1]) @@ -56,7 +58,10 @@ class PendelSim(Game): def on_update(self, dtime): new_joints = self.joints[:] for i in range(1,4): - new_joints[i] += (self.joint_speed[i] + Vector(0,40)) * dtime + new_joints[i] += (0.99 * self.joint_speed[i] + Vector(0,40)) * dtime + + if self.mousedown: + new_joints[1] = Vector(self.mousepos[0], self.mousepos[1]) for i in range(0,4-1): relvec = new_joints[i+1] - new_joints[i] @@ -90,7 +95,13 @@ class PendelSim(Game): def on_event(self, event): if event.type == MOUSEBUTTONDOWN: - self.init_data() + self.mousedown = True + self.mousepos = event.pos[:] + elif event.type == MOUSEBUTTONUP: + self.mousedown = False + self.mousepos = event.pos[:] + elif event.type == MOUSEMOTION: + self.mousepos = event.pos[:] else: super(PendelSim, self).on_event(event)