def __init__(self): super().__init__() self.wallTexture = QPixmap('textures/wall.png') self.floorTexture = QPixmap('textures/floor.png') self.createBoard() #init Robots Robot1 = Robots.Robot(1, QVector2D(200, 250), 290, 2, 2, 15, 90, colors["pink"]) Robot2 = Robots.Robot(2, QVector2D(100, 900), 10, 2, 2, 15, 90, colors["darkblue"]) Robot3 = Robots.Robot(3, QVector2D(250, 650), 270, 2, 2, 15, 90, colors["lightblue"]) Robot4 = Robots.Robot(4, QVector2D(950, 100), 180, 2, 2, 15, 90, colors["orange"]) Robot1.setProgram(Robots.Hunter(Robot1)) Robot2.setProgram(Robots.CircleMap1(Robot2)) Robot3.setProgram(Robots.CircleMap2(Robot3)) Robot4.setProgram(Robots.CircleMap3(Robot4)) self.robots = [Robot1, Robot2, Robot3, Robot4] Robot1.executeProgram() Robot2.executeProgram() Robot3.executeProgram() Robot4.executeProgram() self.timer = QBasicTimer() self.timer.start(FPS, self) self.tickCount = 0 self.initUI()
def barrierCollision(self, robo): #Collision with Obstacles PosX = int(round(robo.position.x()/ 10)) PosY = int(round(robo.position.y()/ 10)) Rad = int(round((robo.radius *2)/10)) for i in range(0, Rad, 1): #oben if (SpielFeld.PlayFieldAR[PosX + i][PosY-1] == 1) & (robo.v_vector.y()<0): robo.position.__isub__(robo.v_vector) robo.v_vector = QVector2D(0,0) robo.a = 0 #unten if (SpielFeld.PlayFieldAR[PosX + i][PosY + Rad] == 1) & (robo.v_vector.y()>0): robo.position.__isub__(robo.v_vector) robo.v_vector = QVector2D(0,0) robo.a = 0 #links if (SpielFeld.PlayFieldAR[PosX - 1][PosY + i] == 1) & (robo.v_vector.x()<0): robo.position.__isub__(robo.v_vector) robo.v_vector = QVector2D(0,0) robo.a = 0 #rechts if (SpielFeld.PlayFieldAR[PosX + Rad][PosY + i] == 1) & (robo.v_vector.x()>0): robo.position.__isub__(robo.v_vector) robo.v_vector = QVector2D(0,0) robo.a = 0
def orthProj(self, p1, p2): q1 = p1.toQPointF() q2 = p2.toQPointF() vdir = QVector2D(q2 - q1).normalized() vq1q = QVector2D(self.toQPointF() - q1) vq1h = QVector2D.dotProduct(vq1q, vdir) * vdir return RadarCoords.fromQPointF(q1 + QPointF(vq1h))
def _setUniformValueDirect(self, uniform, value): if type(value) is Vector: self._shader_program.setUniformValue( uniform, QVector3D(value.x, value.y, value.z)) elif type(value) is Matrix: self._shader_program.setUniformValue( uniform, self._matrixToQMatrix4x4(value)) elif type(value) is Color: self._shader_program.setUniformValue( uniform, QColor(value.r * 255, value.g * 255, value.b * 255, value.a * 255)) elif type(value) is list and type(value[0]) is list and len( value[0]) == 4: self._shader_program.setUniformValue( uniform, self._matrixToQMatrix4x4(Matrix(value))) elif type(value) is list and len(value) == 2: self._shader_program.setUniformValue(uniform, QVector2D(value[0], value[1])) elif type(value) is list and len(value) == 3: self._shader_program.setUniformValue( uniform, QVector3D(value[0], value[1], value[2])) elif type(value) is list and len(value) == 4: self._shader_program.setUniformValue( uniform, QVector4D(value[0], value[1], value[2], value[3])) elif type(value) is list and type(value[0]) is list and len( value[0]) == 2: self._shader_program.setUniformValueArray( uniform, [QVector2D(i[0], i[1]) for i in value]) else: self._shader_program.setUniformValue(uniform, value)
def teleport(self, target, robot): MID = 500 if robot != target: distance = self.distance(robot, target) if distance <= target.radius + robot.radius: if int(round(target.position.x())) > MID and int( round(target.position.y())) < MID: robot.position = QVector2D(100, 850) elif int(round(target.position.x())) > MID and int( round(target.position.y())) > MID: robot.position = QVector2D(100, 100) elif int(round(target.position.x())) < MID and int( round(target.position.y())) < MID: robot.position = QVector2D(850, 850) elif int(round(target.position.x())) < MID and int( round(target.position.y())) > MID: robot.position = QVector2D(850, 100) robot.v_vector = QVector2D(0, 0) robot.a = 0 robot.a_alpha = 0 robot.v_alpha = 0
def createBullet(self, bulletType, life, delayT, alpha, addSpeed): #Position bulletpos = QVector2D(self.position.x(), self.position.y()) #velocity speed = Bullet.Bullet_Speed + addSpeed #velocity based on angle GesX = math.cos(math.radians(alpha)) * speed GesY = -math.sin(math.radians(alpha)) * speed #set Bullet to middle of Robot OffsetVector = QVector2D((self.radius + Bullet.Bullet_Size) / 2, (self.radius + Bullet.Bullet_Size) / 2) bulletpos.__iadd__(OffsetVector) #set bullet to edge in firing direction OffsetX = math.cos(math.radians(alpha)) * (self.radius + 6) OffsetY = -math.sin(math.radians(alpha)) * (self.radius + 6) OffsetVector = QVector2D(OffsetX, OffsetY) bulletpos.__iadd__(OffsetVector) #set Bullet Speed Vel = QVector2D(GesX, GesY) Vel.__iadd__(self.v_vector) #create Bullet Bullet1 = Bullet.Bullet(bulletpos, Vel, speed, alpha, life, delayT, bulletType) return Bullet1
def collision(self, robo, target): for robot in self.robots: if robot != robo and robot != target and robo != target : distance = self.distanceTwoPoints(int(round(robot.position.x())) + robot.radius, int(round(robot.position.y()))+ robot.radius, int(round(robo.position.x())) + robo.radius, int(round(robo.position.y())) + robo.radius) if self.is_overlapping(int(round(robot.position.x())) + robot.radius, int(round(robot.position.y())) + robot.radius, robot.radius, int(round(robo.position.x())) + robo.radius, int(round(robo.position.y()))+ robo.radius, robo.radius) and distance < robot.radius + robo.radius : # with elastic collision, does not apply to the reality because of spin, friction etc. # our only concern is the mass of the robots # new velocity of robo1 newVelX1 = (int(round(robo.v_vector.x())) * (robo.mass - robot.mass) + (2 * robot.mass * int(round(robot.v_vector.x())))) / ( robo.mass + robot.mass) newVelY1 = (int(round(robo.v_vector.y()))* (robo.mass - robot.mass) + (2 * robot.mass * int(round(robot.v_vector.y())))) / ( robo.mass + robot.mass) # new velocity of robo2 newVelX2 = (int(round(robot.v_vector.x())) * (robot.mass - robo.mass) + (2 * robo.mass * int(round(robo.v_vector.x())))) / ( robo.mass + robot.mass) newVelY2 = (int(round(robot.v_vector.y())) * (robot.mass - robo.mass) + (2 * robo.mass * int(round(robo.v_vector.y())))) / ( robo.mass + robot.mass) newV_1 = QVector2D(newVelX1, newVelY1) newV_2 = QVector2D(newVelX2, newVelY2) robo.position.__iadd__(newV_1) robot.position.__iadd__(newV_2) else: self.teleport(target, robo)
def __init__(self): super().__init__() # change texture preferences self.wallTexture = self.changeWall(wtexture) self.floorTexture = self.changeFloor(ftexture) self.createBoard() #init Robots Robot1 = Robots.Robot(1, QVector2D(200,250), 290, 2, 2, 15, 90, colors["pink"]) Robot2 = Robots.Robot(2, QVector2D(200,600), 10, 2, 2, 15, 90, colors["darkblue"]) Robot3 = Robots.Robot(3, QVector2D(250,650), 270, 2, 2, 15, 90, colors["lightblue"]) Robot4 = Robots.Robot(4, QVector2D(950,100), 180, 2, 2, 15, 90, colors["orange"]) Robot1.setProgram(Controller.RunAwayKeyBoard(Robot1)) Robot2.setProgram(Controller.CircleMap1(Robot2)) Robot3.setProgram(Controller.TargetHunt(Robot3)) Robot4.setProgram(Controller.TargetHunt(Robot4)) self.robots = [Robot1, Robot2, Robot3, Robot4] Robot1.executeProgram() Robot2.executeProgram() Robot3.executeProgram() Robot4.executeProgram() self.timer = QBasicTimer() self.tickCount = 0 self.initUI()
def __init__(self, robotid, position, alpha, a_max, a_alpha_max, radius, FOV, color): threading.Thread.__init__(self) self.robotid = robotid self.position = position self.alpha = alpha self.a_max = a_max self.a_alpha_max = a_alpha_max self.radius = radius self.FOV = FOV self.color = color self.mass = radius * 3 #Values set to "0" so Robot stands still at the start self.v_vector = QVector2D(0, 0) self.v_alpha = 0 self.a = 0 self.a_alpha = 0 # Position, Distanz zueinander, Blickwinkel, seen self.RobotList = { 1: [QVector2D(0, 0), 0, 0, False], 2: [QVector2D(0, 0), 0, 0, False], 3: [QVector2D(0, 0), 0, 0, False], 4: [QVector2D(0, 0), 0, 0, False] }
def contains(self, point): center = QVector2D(self.vertex.x() + 12, self.vertex.y()) vector = QVector2D(point) diff = center - vector if diff.length() < self.radius + 1: return True else: return False
def initUI(self): self.wallTexture = QPixmap('wall.png') self.floorTexture = QPixmap('floor.png') self.setGeometry(0, 0, SCREENWIDTH, SCREENHEIGHT) self.setWindowTitle('Game.exe') self.center() self.timer = QBasicTimer() self.timer.start(FPS, self) self.tickCount = 0 #Array construction #set Walls, set array value to 1 to place Wall #set Wall around the edges for x in range(0, 100, 1): SpielFeld.PlayFieldAR[x][0] = 1 SpielFeld.PlayFieldAR[x][99] = 1 for y in range(1, 99, 1): SpielFeld.PlayFieldAR[0][y] = 1 SpielFeld.PlayFieldAR[99][y] = 1 #set some Obstacle for i in range(0, 25, 1): SpielFeld.PlayFieldAR[70][i + 45] = 1 SpielFeld.PlayFieldAR[71][i + 45] = 1 for i in range(0, 40, 1): SpielFeld.PlayFieldAR[i + 10][40] = 1 SpielFeld.PlayFieldAR[i + 10][41] = 1 for i in range(0, 50, 1): SpielFeld.PlayFieldAR[i + 30][70] = 1 SpielFeld.PlayFieldAR[i + 30][71] = 1 for i in range(0, 30, 1): SpielFeld.PlayFieldAR[i + 25][20] = 1 SpielFeld.PlayFieldAR[i + 25][21] = 1 for i in range(0, 10, 1): SpielFeld.PlayFieldAR[10][i + 50] = 1 SpielFeld.PlayFieldAR[11][i + 50] = 1 #init Robots Robot1 = RoboTypeRun(1, QVector2D(500, 500), 0, 2, 2, 20, 10, PINK) Robot2 = RoboTypeChase1(2, QVector2D(200, 200), 90, 2, 2, 20, 50, DARKBLUE) Robot3 = RoboTypeChase2(3, QVector2D(550, 550), 180, 2, 2, 30, 60, LIGHTBLUE) Robot4 = RoboTypeChase3(4, QVector2D(600, 150), 270, 2, 2, 25, 85, ORANGE) self.robots = [Robot1, Robot2, Robot3, Robot4] for robot in self.robots: robot.start() self.show()
def reflect(self): a = self._points[0] - self._position b = self._points[2] - self._position c = (a - b).normalized() a = self._points[1] - self._position b = self._points[3] - self._position a = 2 * QVector2D.dotProduct(a, c) * c - a b = 2 * QVector2D.dotProduct(b, c) * c - b self._points[1] = a + self._position self._points[3] = b + self._position
def __init__(self, id, spawn_x, spawn_y, aov, v_max, maxHealth, r=30, alpha=0, texturePath="textures/robot_base.png"): super().__init__() self.id = id self.pos = QVector2D(spawn_x, spawn_y) self.spawn = QVector2D(spawn_x, spawn_y) self.aov = aov self.r = r self.alpha = alpha # unit: degrees self.texture = QPixmap(texturePath) self.a = 0 # unit: pixels/second^2 self.a_max = A_MAX # unit: pixels/second^2 self.v = 0 # unit: pixels/second self.v_max = v_max # unit pixels/second self.a_alpha = 0 # unit: degrees/second^2 self.a_alpha_max = A_ALPHA_MAX # unit: degrees/second^2 self.v_alpha = 0 # unit: degrees/second self.v_alpha_max = V_ALPHA_MAX # unit: degrees/second self.guns = [] self.selected_gun = None self.currentGunIndex = 0 self.maxHealth = maxHealth self.health = maxHealth self.healthBar = HealthBar(maxHealth) self.active = True self.protected = False self.timeToRespawn = 0 self.protectionTime = 0 self.deathSound = QSoundEffect(self) self.deathSound.setSource(QUrl.fromLocalFile("sounds/death.wav")) self.deathSound.setVolume(0.1) self.emptyGunSound = QSoundEffect(self) self.emptyGunSound.setSource( QUrl.fromLocalFile("sounds/empty_gun.wav")) self.emptyGunSound.setVolume(0.1) self.respawnSound = QSoundEffect(self) self.respawnSound.setSource(QUrl.fromLocalFile("sounds/respawn.wav")) self.respawnSound.setVolume(0.1)
def __init__(self, robotid, position, alpha, a_max, a_alpha_max, radius, FOV, color): self.robotid = robotid self.position = position self.alpha = alpha % 360 self.radius = radius self.color = color self.RobotList = {1 : QVector2D(0,0), 2 : QVector2D(0,0), 3 : QVector2D(0,0), 4 : QVector2D(0,0)} self.BulList= [] # for FOV # Position, Distanz zueinander, Blickwinkel, seen self.ViewList = {1 : [ QVector2D(0,0), 0, 0, False], 2 : [ QVector2D(0,0), 0, 0, False], 3 : [ QVector2D(0,0), 0, 0, False], 4 : [ QVector2D(0,0), 0, 0, False]} self.FOV = FOV self.reload = 0 self.coolDown = 0 self.deathTime = 0 self.immuneTime = 0 self.a = 0 self.a_max = a_max self.a_alpha= 0 self.a_alpha_max = a_alpha_max self.v_vector = QVector2D(0,0) self.v_alpha = 0 self.v = 0
def __init__(self, robotid, position, alpha, a_max, a_alpha_max, radius, FOV, color): self.robotid = robotid self.position = position self.alpha = alpha % 360 self.radius = radius self.color = color self.RobotList = {1 : QVector2D(0,0), 2 : QVector2D(0,0), 3 : QVector2D(0,0), 4 : QVector2D(0,0)} self.BulList= [] # FOV data # Position, distance, perspective, seen self.ViewList = {1 : [ QVector2D(0,0), 0, 0, False], 2 : [ QVector2D(0,0), 0, 0, False], 3 : [ QVector2D(0,0), 0, 0, False], 4 : [ QVector2D(0,0), 0, 0, False]} self.FOV = FOV self.reload = 0 self.deathTime = 0 self.immuneTime = 0 self.a = 0 self.a_max = a_max self.a_alpha= 0 self.a_alpha_max = a_alpha_max self.v_vector = QVector2D(0,0) self.v_alpha = 0 self.v = 0
def __init__(self, color, *args): super().__init__() self._points = [] self._color = color for i in args: vec = QVector2D(i[0], i[1]) self._points.append(vec) sum = QVector2D(0, 0) for i in self._points: sum = sum + i self._position = sum * 1.0 / len(self._points) self._selected = False
def run(self): self.robot.a = 1 target = QVector2D(900, 900) while True: if self.robot.inVicinity(QVector2D(900, 900)): target = QVector2D(100, 100) if self.robot.inVicinity(QVector2D(100, 100)): target = QVector2D(900, 900) self.robot.inVicinity(target) self.robot.aimTarget(target) self.robot.aimTarget(target) self.msleep(100)
def teleport_bullet(self, robo): spot = random.randint(1, 5) if spot == 1: robo.position = QVector2D(100, 100) elif spot == 2: robo.position = QVector2D(100, 850) elif spot == 3: robo.position = QVector2D(850, 100) elif spot == 4: robo.position = QVector2D(850, 850) elif spot == 5: robo.position = QVector2D(500, 500)
def GetUnprojection(winXY: QPoint, winZ: float, winWH: QPoint, vpMat: QMatrix4x4) -> QVector3D: #- from gluUnproject definition, I don't consider glViewport ndcspaceCursorPos = QVector4D( (QVector2D(winXY) / QVector2D(winWH)) * 2 - QVector2D(1, 1), winZ * 2 - 1, 1) invertedVpMat, success = vpMat.inverted() if not success: raise NotImplementedError wspaceCursorPos = invertedVpMat * ndcspaceCursorPos return QVector3D(wspaceCursorPos) / wspaceCursorPos.w()
def initUI(self): self.setGeometry(0, 0, SCREENWIDTH, SCREENHEIGHT) self.setWindowTitle('Game.exe') self.center() self.timer = QBasicTimer() self.timer.start(FPS, self) self.tickCount = 0 #set Walls, set array value to 1 to place Wall #set Wall around the edges for x in range(0,100,1): SpielFeld.PlayFieldAR[x][0]= 1 SpielFeld.PlayFieldAR[x][99]= 1 for y in range(1,99,1): SpielFeld.PlayFieldAR[0][y]= 1 SpielFeld.PlayFieldAR[99][y]= 1 #set some Obstacle for i in range(0, 25, 1): SpielFeld.PlayFieldAR[70][i+45] = 1 for i in range(0, 40, 1): SpielFeld.PlayFieldAR[i+10][40] = 1 for i in range(0, 50, 1): SpielFeld.PlayFieldAR[i+30][70] = 1 for i in range(0, 30, 1): SpielFeld.PlayFieldAR[i+25][20] = 1 for i in range(0, 10, 1): SpielFeld.PlayFieldAR[10][i+50] = 1 for i in range(0, 100, 1): for j in range(0, 100, 1): if SpielFeld.PlayFieldAR[i][j]==1: SpielFeld.BarrierList.append(QVector2D(i,j)) #init Robots self.Robot1 = RoboTypeRun(1, QVector2D(50,110), 0, 2, 2, 20, 40 ,PINK) self.Robot2 = RoboTypeChase1(2, QVector2D(92,110), 90, 2, 2, 10, 50,DARKBLUE) self.Robot3 = RoboTypeChase2(3, QVector2D(400,280), 180, 2, 2, 30, 60,LIGHTBLUE) self.Robot4 = RoboTypeChase3(4, QVector2D(300,260), 270, 2, 2, 25, 85,ORANGE) self.robots = [self.Robot1, self.Robot2, self.Robot3, self.Robot4] for robot in self.robots: robot.start() self.show()
def initUI(self): self.setGeometry(0, 0, SCREENWIDTH, SCREENHEIGHT) self.setWindowTitle('Game.exe') self.center() self.timer = QBasicTimer() self.timer.start(FPS, self) self.tickCount = 0 #init Robots self.Robot1 = RobotData(1, QVector2D(50, 110), 300, 2, 2, 15, PINK) self.Robot2 = RobotData(2, QVector2D(500, 500), 150, 2, 2, 15, DARKBLUE) self.Robot3 = RobotData(3, QVector2D(400, 460), 240, 2, 2, 15, LIGHTBLUE) self.Robot4 = RobotData(4, QVector2D(360, 260), 30, 2, 2, 15, ORANGE) #temporary Robot Actions self.Robot1.v_vector = QVector2D(2, 2) self.Robot1.v_alpha = 3 self.Robot2.v_vector = QVector2D(2, 2) self.Robot3.v_vector = QVector2D(2, 2) self.Robot4.v_vector = QVector2D(2, 2) self.robots = [self.Robot1, self.Robot2, self.Robot3, self.Robot4] #Start RobotThreads #for robot in self.robots: # robot.start() self.show()
def repulsiveForces(self, kValue): for (i, vertex) in enumerate(self.vertices): changeDisp = QVector2D(0, 0) for (j, anotherVertex) in enumerate(self.vertices): if i != j: if self.colored: realK = kValue * vertex.distanceInColor(anotherVertex) else: realK = kValue differenceVector = QVector2D(vertex - anotherVertex) if differenceVector.length() < 0.1: differenceVector.setX(random.random() - 0.5) differenceVector.setY(random.random() - 0.5) changeDisp += differenceVector * pow(realK, 2) / pow(differenceVector.length(), 2) vertex.disp += changeDisp
def spellcard3(self): if self.coolDown == 0 and self.deathTime == 0: #Values BulletAmmount = 30 Repetitions = int(round(BulletAmmount / 2)) LifeTime = 400 + 4 * Repetitions #Calculate Angles alpha1 = self.alpha alphaStep = 360 / BulletAmmount #Calculate Target DistTo2 = QVector2D(self.position.x() - self.RobotList[2].x(), self.position.y() - self.RobotList[2].y()) DistTo3 = QVector2D(self.position.x() - self.RobotList[3].x(), self.position.y() - self.RobotList[3].y()) DistTo4 = QVector2D(self.position.x() - self.RobotList[4].x(), self.position.y() - self.RobotList[4].y()) Distance2 = DistTo2.x() * DistTo2.x() + DistTo2.y() * DistTo2.y() Distance3 = DistTo3.x() * DistTo3.x() + DistTo3.y() * DistTo3.y() Distance4 = DistTo4.x() * DistTo4.x() + DistTo4.y() * DistTo4.y() closest = min(Distance2, Distance3, Distance4) if closest == Distance2: #print("darkBlue") target = 2 elif closest == Distance3: #print("lightBlue") target = 3 elif closest == Distance4: #print("orange") target = 4 #Create Bullets for i in range(0, Repetitions, 1): self.BulList.append( self.createBullet(14, LifeTime - 4 * i, 4 * i, (alpha1 + i * alphaStep) % 360, 0, 50, self.RobotList[target])) self.BulList.append( self.createBullet(14, LifeTime - 4 * i, 4 * i, (alpha1 + 180 + i * alphaStep) % 360, 0, 50, self.RobotList[target])) pygame.mixer.Sound.play(self.SoundSpecial3) self.coolDown = 250
def mouseMoveEvent(self, event): vertices = self.parent().graph.vertices verticesPos = [QVector3D(i) for i in self.verticesPosWhenClicked] for (pos, afterPos) in zip(self.verticesPosWhenClicked, verticesPos): afterPos.setX(pos.x() - self.center().x()) afterPos.setY(pos.y() - self.center().y()) afterPos.setZ(pos.z() - self.center().z()) disp = event.pos() - self.mousePosWhenClicked xAngle = QVector2D(disp).length() * 2 * math.pi / 360 if QVector2D(disp).length() != 0: if (-numpy.sign(disp.x())) != 0: zAngle = math.acos( (-disp.y()) / QVector2D(disp).length()) * (-numpy.sign(disp.x())) else: zAngle = math.acos((-disp.y()) / QVector2D(disp).length()) else: zAngle = 0 xTurnList = [ 1, 0, 0, 0, math.cos(xAngle), -math.sin(xAngle), 0, math.sin(xAngle), math.cos(xAngle) ] zTurnList = [ math.cos(zAngle), -math.sin(zAngle), 0, math.sin(zAngle), math.cos(zAngle), 0, 0, 0, 1 ] minusZTurnList = [ math.cos(-zAngle), -math.sin(-zAngle), 0, math.sin(-zAngle), math.cos(-zAngle), 0, 0, 0, 1 ] zTurnMatrix = MyM3R(zTurnList) xTurnMatrix = MyM3R(xTurnList) minusZTurnMatrix = MyM3R(minusZTurnList) for (pos, vertex) in zip(verticesPos, vertices): zTurnMatrix.transform(pos) xTurnMatrix.transform(pos) minusZTurnMatrix.transform(pos) vertex.setX(pos.x() + self.center().x()) vertex.setY(pos.y() + self.center().y()) vertex.setZ(pos.z() + self.center().z()) for vertex in vertices: vertex.circle.move() for edge in self.parent().graph.edges: edge.move()
def u_moved(self, dx, dy): """React to change of the vector end position. Args: dx (float): dy (float): displacements of the vector end, in view coordinates. """ du_view = QPointF(dx, dy) self.u_view += QVector2D(du_view) self.update_line() view = self.h_p0.scene().active_view if view: u_scene_x, u_scene_y = view.map_vector_to_scene( self.u_view.x(), self.u_view.y()) else: # no active view (probably inside a test) => no transformation u_scene_x = self.u_view.x() u_scene_y = self.u_view.y() self.line.u.x = u_scene_x self.line.u.y = u_scene_y self.line.u.normalize() self.signal_moved.emit(self.line)
def __init__(self, x, y, color): super().__init__() self.x = x self.y = y self.width = 24.0 self.height = 32.0 self.color = (255, 25, 255) self.vector = QVector2D(QPointF(0.0, -1.0)) self.velocity = 10 self.score = 0 self.rotatedFor = 0 self.tournamentPlaying = PLAY_WAIT.NO self.lives = 3 self.color = color self.justSpawnedTimer = 0 self.isDead = False self.colorOfProjectile = Qt.transparent points = [ QPointF(self.x, self.y - (self.height / 2.0)), QPointF(self.x - ((self.width / 2.0)), self.y + (self.height / 2.0)), QPointF(self.x, self.y + ((self.height / 4.0))), QPointF(self.x + (self.width / 2.0), self.y + (self.height / 2.0)), ] self.points = points self.projectile = 0 self.reloadProjectile()
def moveRobot(self, Robo): #berechne neue Lenkrichtung if (abs(Robo.v_alpha) <= alpha_eps) and (abs(Robo.a_alpha) <= alpha_eps): Robo.v_alpha = 0 elif (Robo.v_alpha + Robo.a_alpha) < -v_alpha_Max: Robo.v_alpha = -v_alpha_Max elif (Robo.v_alpha + Robo.a_alpha) <= v_alpha_Max: Robo.v_alpha = (Robo.v_alpha + Robo.a_alpha) elif (Robo.v_alpha + Robo.a_alpha) >= v_alpha_Max: Robo.v_alpha = v_alpha_Max #Neue Richtung Robo.alpha = (Robo.alpha + Robo.v_alpha) % 360 #berechne geschwindigkeit v= math.sqrt(math.pow(Robo.v_vector.x(),2) + math.pow(Robo.v_vector.y(),2)) if (v + Robo.a) <= 0: v = 0 Robo.a = 0 elif (v + Robo.a) < vMax: v += Robo.a elif (v + Robo.a) >= vMax: v = vMax #X-Y Geschwindigkeit GesX = math.cos(math.radians(Robo.alpha)) * v GesY = - math.sin(math.radians(Robo.alpha)) * v #setze neue Geschwindigkeit Robo.v_vector = QVector2D(GesX,GesY) #berechne neue Position Robo.position.__iadd__(Robo.v_vector)
def emitRobotSensorData(self, robot): cone = robot.view_cone() robotsInView = {} timestamp = QDateTime.currentMSecsSinceEpoch() # Special case for runner robot: He sees everything: if isinstance(robot, robots.RunnerRobot): ids = self.robots.keys() wallsInView = self.obstacles else: # Get ids of all robots that are in view, i.e. that intersect with the view cone ids = filter(lambda id: cone.intersects(self.robots[id].shape()), self.robots) wallsInView = filter(cone.intersects, self.obstacles) for id in ids: other = self.robots[id] dist = (robot.pos - other.pos).length() angle = math.degrees( math.atan2(other.y - robot.y, other.x - robot.x)) robotsInView[id] = { 'x': other.x, 'y': other.y, 'id': other.id, 'pos': QVector2D(other.x, other.y), 'dist': dist, 'angle': angle, 'timestamp': timestamp } robot.robotsInViewSignal.emit(robotsInView) robot.wallsInViewSignal.emit(list(wallsInView))
def moveRobot(self, Robo): # berechne neue Lenkrichtung if (Robo.v_alpha + Robo.a_alpha) < -v_alpha_Max: Robo.v_alpha = -v_alpha_Max elif (Robo.v_alpha + Robo.a_alpha) <= v_alpha_Max: Robo.v_alpha = (Robo.v_alpha + Robo.a_alpha) elif (Robo.v_alpha + Robo.a_alpha) >= v_alpha_Max: Robo.v_alpha = v_alpha_Max # Neue Richtung Robo.alpha = (Robo.alpha + Robo.v_alpha) % 360 # berechne geschwindigkeit if (Robo.v + Robo.a) <= -vMax: Robo.v = -vMax elif (Robo.v + Robo.a) < vMax: Robo.v += Robo.a elif (Robo.v + Robo.a) >= vMax: Robo.v = vMax # X-Y Geschwindigkeit GesX = math.cos(math.radians(Robo.alpha)) * Robo.v GesY = - math.sin(math.radians(Robo.alpha)) * Robo.v # setze neue Geschwindigkeit Robo.v_vector = QVector2D(GesX, GesY) # berechne neue Position Robo.position.__iadd__(Robo.v_vector)
def roboCollision(self, robo, target): for robot in self.robots: if robot != robo: distance = self.distance(robot, robo) if distance <= robot.radius + robo.radius: dx = (robot.position - robo.position).x() dy = (robot.position - robo.position).y() tangent = math.atan2(dy, dx) robo.alpha = 2 * tangent - robo.alpha angle = 0.5 * math.pi + tangent overlap = distance - (robot.radius - robo.radius) roboX = math.sin(angle) * overlap roboY = math.cos(angle) * overlap newVel = QVector2D(roboX, roboY).normalized() robo.position.__iadd__(newVel) robot.position.__iadd__(newVel * (-1))
def mouseMoveEvent(self, event): if self.__dragActive: mousePos = self.view.mapToScene(self.view.mapFromGlobal(QCursor.pos())) vec1 = QVector2D(mousePos) vec1.normalize() trans = QTransform() trans.rotate(self.rotation()) vec2 = QVector2D(self.p2 * trans) vec2.normalize() angle = math.acos(max(-1, min(1, QVector2D.dotProduct(vec1, vec2)))) * 180 / math.pi # clockwise rotation if vec1.y() * vec2.x() < vec1.x() * vec2.y(): angle *= -1 angle = (self.rotation() + angle) % 360 self.setRotation(angle)