class Wheel: def __init__(self, pos, radius=10): self.pos = pos self.vel = Vector() self.radius = max(radius, 10) self.colour = 'White' self.rot = 0 self.on_ground = True def draw(self, canvas): Image(self).draw(canvas) def jump(self): if self.on_ground: self.vel.add(Vector(0, -40)) self.on_ground = False def update(self): global scroll self.pos.add(self.vel) scroll.add(self.vel) self.vel.x *= 0.85 self.rot = self.pos.x / (self.radius) if self.pos.x < 0: self.pos.add(Vector(WIDTH, 0)) elif self.pos.x > WIDTH: self.pos.add(Vector(-WIDTH, 0)) if not self.on_ground: self.vel.add(Vector(0, 2)) if self.pos.y + self.radius > HEIGHT: self.pos.y = HEIGHT - self.radius self.vel.y *= -0.6 if self.vel.length() < 10: self.on_ground = True self.vel.y = 0
class Player: def __init__(self, dimensions, time): self.time = time self.lastFrameTime = self.time.time() self.canvas_dim = dimensions #address needed to gain local directory to access images folder below addr = os.getcwd() ##image info + dimensions (constants) self.img = simplegui.load_image("file:///" + addr + "/images/boat2.png") self.dim = (4096, 4096) self.cen = (self.dim[0] / 2, self.dim[1] / 2) self.draw_dim = (140, 160) self.y_offset = 80 ##player position and velocity self.pos = Vector( self.draw_dim[0] / 2, self.draw_dim[1] / 5 + self.canvas_dim[1] * 0.3 - self.y_offset) self.vel = Vector(0, 0) def getPos(self): return self.pos def setPos(self, newPos): self.pos = newPos def getVel(self): return self.vel #setter for velocity not needed, so replaced by add velocity method def addVel(self, velocity): self.vel.add(velocity) #updates position based on velocity and frame rate def update(self): delta = self.time.time() - self.lastFrameTime self.lastFrameTime = self.time.time() self.pos.add(self.vel * delta) self.vel.multiply(0.85) #checks whether the user is within the horizontal bounds of the screen def inBounds(self): return ((self.draw_dim[0] / 2 <= self.pos.x) and (self.canvas_dim[0] - self.draw_dim[0] / 2 >= self.pos.x)) #sets users positions when they are reaching outer bounds def set(self): self.vel *= -1 if (self.pos.get_p()[0] < self.canvas_dim[0] / 2): self.pos = Vector(self.draw_dim[0] / 2 + 1, self.pos.y) else: self.pos = Vector(self.canvas_dim[0] - self.draw_dim[0] / 2 - 1, self.pos.y) #draws player ship sprite to canvas def draw(self, canvas): canvas.draw_image(self.img, self.cen, self.dim, self.pos.get_p(), self.draw_dim)
class Fsh: def __init__(self,pos,bounds,imgr,imgl): self.bounds = bounds self.max_vel = 75 self.imgr = imgr self.imgl = imgl self.size = 25 self.per = self.size*3 self.pos = pos angle = random.random()*math.pi*2 self.vel = Vector(math.cos(angle),math.sin(angle)) * 20 def draw(self,canvas): #canvas.draw_circle(self.pos.get_p(),4,1,"red","red") a = self.vel.angle(Vector(0,1)) if self.vel.x > 0: a *= -1 img = self.imgr a += math.pi/2 else: a -= math.pi/2 img = self.imgl img.pos = self.pos img.draw(canvas,rotation=a) #canvas.draw_line(self.pos.get_p(),(self.pos+self.vel).get_p(),2,"blue") #canvas.draw_circle(self.pos.get_p(),self.size,1,"black") def allign(self,fish): if len(fish) < 1: return # calculate the average velocities of the other boids avg = Vector() count = 0 for boid in fish: if(boid.pos - self.pos).length()<self.per: count+=1 avg += boid.vel if count>0: avg = avg.divide(count) # set our velocity towards the others return (avg).normalize() else: return Vector() def cohesion(self,fish): if len(fish) < 1: return # calculate the average distances from the other boids com = Vector() count = 0 for boid in fish: if boid.pos == self.pos: continue elif (boid.pos - self.pos).length()<self.per: com += (self.pos - boid.pos) count+=1 if count>0: com = com.divide(count) return -com.normalize() else: return Vector() def seperation(self,fish, minDistance): if len(fish) < 1: return Vector() distance = 0 numClose = 0 distsum = Vector() for boid in fish: distance = (self.pos-boid.pos).length() if distance < minDistance and distance != 0: numClose += 1 distsum += (boid.pos-self.pos).divide(distance**2) if numClose == 0: return Vector() return (-distsum.divide(numClose)).normalize() def update(self,delta,fish): self.vel = self.vel.add(self.allign(fish).multiply(self.max_vel/50)) self.vel = self.vel.add(self.cohesion(fish).multiply(self.max_vel/50)) self.vel = self.vel.add(self.seperation(fish,self.per*3/5).multiply(self.max_vel/30)) self.vel = self.vel.normalize().multiply(self.max_vel) self.vel.rotate((random.random()*2-1)*delta*20) self.pos = self.pos.add(self.vel * delta) self.pos = self.bounds.correct(self)