class GLWidget(QGLWidget): def __init__(self): super(GLWidget, self).__init__() self.xres = 1024 self.yres = 768 self.setMinimumSize(self.xres, self.yres) self.info = PInfo() self.globalParams = PGlobal() self.leaderParams = PLeader() self.followerParams = PFollower() self.leader = Leader(self.leaderParams, self.globalParams) self.grid = Grid() self.followers = [] self.setFollowers() # set info timer self.infoTimer = QtCore.QTime() self.infoTimer.start() # set update timer self.updateTimer = QtCore.QTimer() self.updateTimer.setInterval(16) self.updateTimer.timeout.connect(self.update) self.updateTimer.start() # set collision check timer self.collisionTimer = QtCore.QTimer() self.collisionTimer.setInterval(32) self.collisionTimer.timeout.connect(self.collision) self.collisionTimer.start() # set render timer self.renderTimer = QtCore.QTimer() self.renderTimer.setInterval(16) self.renderTimer.timeout.connect(self.updateGL) self.renderTimer.start() def setFollowers(self): num = self.followerParams.num - len(self.followers) if num > 0: for i in range(0, num): self.followers.append(Follower(self.leader, self.followerParams,\ self.globalParams, self.grid)) elif num < 0: num *= -1 for i in range(0, num): popd = self.followers.pop() self.grid.remove(popd, popd.loc.x, popd.loc.y) def update(self): self.setFollowers() self.leader.move() for i in self.followers: i.move() def collision(self): collisions = 0 for i in self.followers: collisions += i.calcRepulsion(self.followers) self.info.collisionChecks = collisions def paintGL(self): self.info.computeTime.pop(0) compTime = self.infoTimer.elapsed() - self.info.lastFrame self.info.computeTime.append(compTime) glClear(GL_COLOR_BUFFER_BIT) glLoadIdentity() self.leader.render() for i in self.followers: i.render() curTime = self.infoTimer.elapsed() self.info.frameTime.pop(0) self.info.frameTime.append(curTime - self.info.lastFrame) self.info.lastFrame = curTime self.updateInfo() def updateInfo(self): print( "FPS: " + str(1 / (sum(self.info.frameTime) / 1000 / len(self.info.frameTime)))) print("Collision checks: " + str(self.info.collisionChecks)) print("RendToRend: " + str(sum(self.info.computeTime) / len(self.info.computeTime)) + 'ms\n') def resizeGL(self, w, h): glMatrixMode(GL_PROJECTION) glOrtho(-w / h, w / h, -1, 1, -1.0, 1.0) glViewport(0, 0, w, h) glMatrixMode(GL_MODELVIEW) glLoadIdentity() def initializeGL(self): glClearColor(0.0, 0.0, 0.0, 1.0) def keyPressEvent(self, event): # Handle keypresses for movement if event.key() == QtCore.Qt.Key_Space and not event.isAutoRepeat(): self.leader.toggleControl() elif self.leader.isControlled(): if event.key() == QtCore.Qt.Key_Left: self.leader.turn('L') elif event.key() == QtCore.Qt.Key_Right: self.leader.turn('R') def setLeaderTarget(self, target): # Calculate simulation coordinates from click and set leader target vecTarget = Vector2f(target.x(), target.y()) vecTarget.x -= 16 vecTarget.y = 768 - (vecTarget.y - 23) if vecTarget.x > 0 and vecTarget.x < 1024 and vecTarget.y > 0 and vecTarget.y < 768: self.leader.goTo(vecTarget)
def test_turn_position_value_error(self): leader = Leader(test_mode=True) self.assertEqual(None, leader.turn("position"))
def test_turn_position_type_errro(self): leader = Leader(test_mode=True) self.assertEqual(None, leader.turn(None))
def test_turn_position_zero(self): leader = Leader(test_mode=True) self.assertEqual(90, leader.turn(0.0))
def test_turn_position_above_upper_bound(self): leader = Leader(test_mode=True) self.assertEqual(135, leader.turn(1.1))
def test_turn_position_below_lower_bound(self): leader = Leader(test_mode=True) self.assertEqual(45, leader.turn(-1.1))