def normalize_vector(vector): v = Vector(vector) vect_mag = v.vector_magnitude() for i in range(len(vector)): vector[i] = vector[i] / vect_mag return vector
def second_level(self): while len(self.monsters) != 0: time.sleep(1) self.change_level(([ Vector(0, 200), Vector(400, 200), Vector(400, 500), Vector(1000, 500) ], )) tree = 'Tree' skeleton = 'Skeleton' golem = 'Golem' time.sleep(7) for i in range(10): self.lock.acquire() self.monsters.append(Monster(self.road[0], tree)) self.lock.release() time.sleep(0.5) self.lock.acquire() self.monsters.append(Monster(self.road[0], skeleton)) self.lock.release() time.sleep(1) self.lock.acquire() self.monsters.append(Boss(self.road[0], golem)) self.lock.release() time.sleep(5) for i in range(10): self.lock.acquire() self.monsters.append(Monster(self.road[0], tree)) self.lock.release() time.sleep(1) self.lock.acquire() self.monsters.append(Monster(self.road[0], skeleton)) self.lock.release() self.monsters.append(Boss(self.road[0], golem))
def projection_onto_orthonorm_subspace(vector, orthonorm_basis): for i in range(len(orthonorm_basis)): scalar = dot_product(vector, orthonorm_basis[i]) v = Vector(orthonorm_basis[i]) scal_vect_product = v.vectorScalarProduct(scalar) if i == 0: result_vector = scal_vect_product else: result_vector = vector_addition(result_vector, scal_vect_product) return result_vector
def least_square_approx(matrix_A, vecotr_b): m = Matrix(matrix_A) trans_A = m.transpose_of_a_matrix() # A^T Atrans_x_A = matrix_multiplication(trans_A, matrix_A) # A^T * A v = Vector(vecotr_b) Atrans_x_bvect = v.matrix_vector_product(trans_A) # A^T * vector_b # finding solution for A^TA x* = A^T b resultant_vector = gauss_jordan_elimination(Atrans_x_A, Atrans_x_bvect) # return x* return resultant_vector
def __init__(self): path = Game.conf() config.create_config(path) self.config = config.get_config(path) self.bullets = [] self.road = ([Vector(0, 350), Vector(1000, 350)], ) self.player = Player() self.level = 0 self.tower = None self.game_level = 0 self.monsters = [] self.lock = threading.Lock() self.spawn = threading.Thread(target=self.create, daemon=True) self.spell = None
def third_level(self): tree = 'Tree' skeleton = 'Skeleton' dragon = 'Dragon' golem = 'Golem' while len(self.monsters) != 0: time.sleep(1) self.change_level(([Vector(0, 600), Vector(1000, 600)], [Vector(1000, 0), Vector(1000, 600)])) self.player.get_coins(100) time.sleep(10) for i in range(8): self.lock.acquire() self.monsters.append(Monster(self.road[0], tree)) self.lock.release() self.lock.acquire() self.monsters.append(Monster(self.road[0], tree)) self.monsters.append(Monster(self.road[1], tree)) self.lock.release() time.sleep(3) for i in range(6): self.lock.acquire() self.monsters.append(Monster(self.road[0], skeleton)) self.lock.release() time.sleep(1) self.lock.acquire() self.monsters.append(Monster(self.road[1], skeleton)) self.lock.release() for i in range(8): self.lock.acquire() self.monsters.append(Monster(self.road[0], skeleton)) self.lock.release() time.sleep(1) self.lock.acquire() self.monsters.append(Monster(self.road[1], skeleton)) self.lock.release() time.sleep(3) self.lock.acquire() self.monsters.append(Boss(self.road[1], dragon)) self.lock.release() while len(self.monsters) != 0: time.sleep(1) self.lock.acquire() self.monsters.append(Boss(self.road[1], golem)) self.monsters.append(Boss(self.road[0], golem)) self.lock.release()
def move(self): vector = self.target.location - self.location if self.target.is_dead(): self.hit = True if vector.length < self.target.hitbox: if self.target not in self.visited: self.target.get_damage(self) self.visited.append(self.target) self.pounce -= 1 else: self.hit = True if self.pounce == 0: self.hit = True for creature in self.enemies: if creature in self.visited: continue vector = creature.location - self.location if vector.length < self.pounce_range: self.target = creature break self.direction = vector.angle delta = Vector( math.cos(self.direction) * self.speed, self.speed * math.sin(self.direction)) self.location = self.location + delta
def update(self): if not obj_user_car.Collisonwall(): if self.keyboard.up and self.keyboard.right: self.user_car.vel.add(Vector((0.05, -0.05))) elif self.keyboard.down and self.keyboard.right: self.user_car.vel.add(Vector((0.05, 0.05))) elif self.keyboard.left: self.user_car.vel.add(Vector((-0.05, 0))) elif self.keyboard.right: self.user_car.vel.add(Vector((0.05, 0))) else: self.user_car.vel = Vector( (1, 0)) #if nothing is done then keep moving forward else: #call game crash #then game over interface obj_spriteS.collision = True
def __init__(self, start, damage, enemy): self.location = Vector(start.X, start.Y) self.damage = damage self.target = enemy self.direction = math.pi / 2 self.hit = False self.speed = int(config.get(Game.conf(), 'Archers', 'speed')) self.image = 'arrow'
def __init__(self, damage, location, enemy, slow): self.damage = damage self.location = Vector(location.X, location.Y) self.target = enemy self.slow = slow self.speed = int(config.get(Game.conf(), 'Poison', 'speed')) self.hit = False self.direction = math.pi / 2 self.image = 'poison'
def move(self): vector = self.target.location - self.location if vector.length < self.target.hitbox: self.target.get_damage(self) self.hit = True self.direction = vector.angle self.location += Vector( math.cos(self.direction) * self.speed, math.sin(self.direction) * self.speed)
def fourth_level(self): tree = 'Tree' skeleton = 'Skeleton' dragon = 'Dragon' golem = 'Golem' while len(self.monsters) != 0: time.sleep(1) self.change_level(([Vector(0, 700), Vector(1000, 700)], [ Vector(1000, 0), Vector(1000, 400), Vector(520, 400), Vector(520, 700), Vector(1000, 700) ])) self.player.get_coins(100) time.sleep(10) for i in range(10): self.lock.acquire() self.monsters.append(Monster(self.road[0], skeleton)) self.lock.release() time.sleep(0.4) for i in range(10): self.lock.acquire() self.monsters.append(Monster(self.road[1], skeleton)) self.lock.release() time.sleep(0.4) self.lock.acquire() self.monsters.append(Monster(self.road[1], tree)) self.lock.release() time.sleep(0.8) self.lock.acquire() self.monsters.append(Boss(self.road[0], dragon)) self.lock.release() time.sleep(3) self.lock.acquire() self.monsters.append(Boss(self.road[1], dragon)) self.lock.release() time.sleep(5) self.monsters.append(Boss(self.road[1], golem)) for i in range(10): self.lock.acquire() self.monsters.append(Monster(self.road[0], skeleton)) self.lock.release() time.sleep(0.2) for i in range(10): self.lock.acquire() self.monsters.append(Monster(self.road[1], skeleton)) self.lock.release() time.sleep(0.3) self.lock.acquire() self.monsters.append(Monster(self.road[1], tree)) self.lock.release() time.sleep(0.5) self.lock.acquire() self.monsters.append(Boss(self.road[1], golem)) self.monsters.append(Boss(self.road[0], golem)) self.lock.release()
def __init__(self, location): self.location = Vector(location.X, location.Y) self.damage = int(config.get(Game.conf(), 'Archers', 'damage_1')) self.level = 0 self.price = int(config.get(Game.conf(), 'Archers', 'price')) self.range = int(config.get(Game.conf(), 'Archers', 'range_1')) self.upg_price = int(config.get(Game.conf(), 'Archers', 'lvl_up')) self.attack_speed = int( config.get(Game.conf(), 'Archers', 'attack_speed_1')) self.iter = 1 self.image = 'archers'
class Coin(PhysicalObject): ''' A class that implements the methods for drawing a coin. ''' def __init__(self, radius, position, velocity, scaling, relWidth, relHeight, fillColour, massP, radiusP, muP, omegaP, positionXP, positionYP, centre): self.fillColour = fillColour self.radius = radius self.onRoundabout = True self.centre = centre self.massP = massP self.radiusP = radiusP self.muP = muP self.omegaP = omegaP self.positionXP = positionXP self.positionYP = positionYP self.initialPosition = position self.initialVelocity = velocity PhysicalObject.__init__(self, position, velocity, None, scaling, relWidth, relHeight) def display(self): stroke(0, 0, 0) fill(self.fillColour) relXEllipse(self.position.x * self.scaling, self.position.y * self.scaling, self.relWidth * self.scaling, self.relHeight * self.scaling) def initialise(self): # Just call the __init__ function to reset everything. self.__init__(self.radius, self.initialPosition, self.initialVelocity, self.scaling, self.relWidth, self.relHeight, self.fillColour, self.massP, self.radiusP, self.muP, self.omegaP, self.positionXP, self.positionYP, self.centre) def updateFromInput(self): self.mass = self.massP.value # Update mass from parameter requiredVelocity = self.omegaP.value * self.radiusP.value if self.onRoundabout: # Update position and velocity from radius, only before it has slipped self.position = self.centre + (self.position - self.centre).scaleAbs(self.radiusP.value) self.velocity = Vector(-(self.position - self.centre).y, (self.position - self.centre).x).scaleAbs(requiredVelocity) # Always scale up the speed. else: self.velocity = self.velocity.scaleAbs(requiredVelocity) self.mu = self.muP.value def updateToOutput(self): self.positionXP.setParameter((self.position - self.centre).x) self.positionYP.setParameter((self.centre - self.position).y) # As up is negative, calculate centre - position def findForce(self): if self.onRoundabout and g * self.mu > self.omegaP.value ** 2 * self.radiusP.value: return (self.centre - self.position).scaleAbs(abs(self.velocity) ** 2 / self.radiusP.value * self.mass) else: self.onRoundabout = False return Vector(0, 0) # Not very realistic, but works well for demonstrations
def __init__(self, location): self.location = Vector(location.X, location.Y) self.damage = int(config.get(Game.conf(), 'Poison', 'damage_1')) self.level = 0 self.price = int(config.get(Game.conf(), 'Poison', 'price')) self.range = int(config.get(Game.conf(), 'Poison', 'range_1')) self.attack_speed = int( config.get(Game.conf(), 'Poison', 'attack_speed_1')) self.slow = float(config.get(Game.conf(), 'Poison', 'slow_1')) self.iter = 1 self.upg_price = int(config.get(Game.conf(), 'Poison', 'lvl_up')) self.image = 'poisontw'
def __init__(self, location, damage, target, enemies, pounce): self.location = Vector(location.X, location.Y) self.damage = damage self.target = target self.enemies = enemies self.pounce = pounce self.speed = int(config.get(Game.conf(), 'Magic', 'speed')) self.direction = math.pi / 2 self.hit = False self.pounce_range = int( config.get(Game.conf(), 'Magic', 'pounce_range')) self.visited = [] self.image = 'magic'
def move(self): vector = self.target.location - self.location if self.target.is_dead(): self.hit = True if vector.length < self.target.hitbox: self.target.get_damage(self) self.target.slow_down(self.slow) self.hit = True self.direction = vector.angle delta = Vector( math.cos(self.direction) * self.speed, self.speed * math.sin(self.direction)) self.location = self.location + delta
def updateFromInput(self): self.mass = self.massP.value # Update mass from parameter requiredVelocity = self.omegaP.value * self.radiusP.value if self.onRoundabout: # Update position and velocity from radius, only before it has slipped self.position = self.centre + (self.position - self.centre).scaleAbs(self.radiusP.value) self.velocity = Vector(-(self.position - self.centre).y, (self.position - self.centre).x).scaleAbs(requiredVelocity) # Always scale up the speed. else: self.velocity = self.velocity.scaleAbs(requiredVelocity) self.mu = self.muP.value
def move(self, player): vector = self.points[self.next] - self.location if self.next == len(self.points) - 1: self.range = self.range_2 if vector.length < self.range: self.next += 1 if self.next == len(self.points): self.is_completed = True player.health -= self.damage return self.dir = vector.angle delta = Vector( math.cos(self.dir) * self.speed * self.speed_percent, math.sin(self.dir) * self.speed * self.speed_percent) self.location += delta
def move(self, player): if not self.finish: vector = self.points[self.next] - self.location if self.next == len(self.points) - 1: self.range = self.range_2 if vector.length < self.range: self.next += 1 if self.next == len(self.points): self.finish = True self.image = self.attack_images self.animation = 5 return self.dir = vector.angle df = Vector( math.cos(self.dir) * self.speed * self.speed_percent, math.sin(self.dir) * self.speed * self.speed_percent) self.location += df else: if self.animation == 36: player.health -= self.damage
def _get_row(matrix, row): return Vector(*matrix.entries[row])
if __name__ == "__main__": from Vectors import Vector import matplotlib.pyplot as plt v1, v2, = Vector(10, 0), Vector(0, 10) v1.plot(plt, c="r") v2.plot(plt, c="g") v1 += v2 v1.plot(plt, c="b") plt.show() pass
def __init__(self, pos): self.pos = pos self.vel = Vector() self.dodged = 0 self.score = 0
(self.frameWidth, self.frameHeight), ((posX, posY)), (self.dimX, self.dimY)) def nextFrame( self): # value can be passed to change the animation stating point # refers to the current frame index(assigning a value to it initially) self.frameIndex[0] = (self.frameIndex[0] + 1) % self.columns if self.frameIndex[0] == 0: self.frameIndex[1] = (self.frameIndex[1] + 1 ) % self.rows # y is referred to by the rows # instead of using this the value should be bracketed to mean that it's a single arguement value #instances of all types, without brackets it points to a class but doesnt make an instance obj_user_car = User_Car(Vector((75, random.randrange(150, 530)))) obj_Enemey_car = Enemy_Car( Vector((random.randrange(0, 400), random.randrange(150, 450))), Vector((3, 0)), img_cars_array[random.randrange(0, 5)]) obj_Tree = TreeAndWall() obj_Kbd = keyboard() obj_Int = Interaction(obj_user_car, obj_Kbd) obj_spriteS = Spritesheet() #parameter passed in as canvas
def _get_column(matrix, column): return Vector(*[m[column] for m in matrix])
from Vectors import Vector import math testv = Vector(20, 20) testv.setMag(10 * math.sqrt(2)) print(testv)