From e9d094b95fff9e62acf61ae022a593a7003bef3e Mon Sep 17 00:00:00 2001 From: Valentin Gehrke Date: Thu, 17 Mar 2016 11:54:41 +0100 Subject: [PATCH] Added random rotation to pendel --- pendel.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/pendel.py b/pendel.py index 4e101a6..b2c3480 100644 --- a/pendel.py +++ b/pendel.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from math import sin, cos, pi from game import * @@ -30,6 +31,9 @@ class Vector(object): def __sub__(self, other): return self.__add__(other.__neg__()) + def rotate(self,alpha): + return Vector( cos(alpha) * self.x - sin(alpha) * self.y, sin(alpha) * self.x + cos(alpha) * self.y) + def len(self): return math.sqrt( self.x * self.x + self.y * self.y ) @@ -41,7 +45,8 @@ class Vector(object): class PendelSim(Game): def __init__(self): - super(PendelSim,self).__init__(1000,600, 60) + super(PendelSim,self).__init__(1300,700, 60) + self.time = 0 self.init_data() self.mousedown = False self.mousepos = (0,0) @@ -54,16 +59,22 @@ class PendelSim(Game): def init_data(self): - self.staff_lengths = staff_lengths = [ randint(150,200), randint(100,200), randint(100,150) ] + self.staff_lengths = staff_lengths = [ randint(200,300), randint(100,220), randint(30,120) ] 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]) - joint_c = joint_b - Vector(-staff_lengths[2],0) + joint_a = fixed_joint - Vector(staff_lengths[0],0).rotate( (randint(0,180) / 180 * pi) ) + joint_b = joint_a - Vector(staff_lengths[1],0).rotate( (randint(0,180) / 180 * pi) ) + joint_c = joint_b - Vector(staff_lengths[2],0).rotate( (randint(0,180) / 180 * pi) ) self.joints = [fixed_joint, joint_a, joint_b, joint_c ] self.joint_speed = [ Vector(0,0), Vector(0,0), Vector(0,0), Vector(0,0) ] self.old_joints = self.joints def on_update(self, dtime): + self.time += dtime + if self.time > 10: + self.resetpath() + self.init_data() + self.time = 0 + new_joints = self.joints[:] for i in range(1,4): new_joints[i] += (0.99 * self.joint_speed[i] + Vector(0,40)) * dtime