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 __init__(self, pos): self.img_url = "https://raw.githubusercontent.com/CougarTasker/twenty-four/master/proto/images/carol.png" self.row = 1 self.column = 5 self.img = simplegui.load_image(self.img_url) self.bdim = Vector(self.img.get_width(), self.img.get_height()) self.adim = (self.bdim.x / self.column, self.bdim.y / self.row) self.center = Vector(self.adim[0] / 2, self.adim[1] / 2) self.pos = pos self.ratio = 0.2 self.frame_index = [1, 1] self.count = 0
def keydown(self, key): if key == simplegui.KEY_MAP['down']: self.snake.snakeScalar = Vector(0, 0.5) if key == simplegui.KEY_MAP['up']: self.snake.snakeScalar = Vector(0, -0.5) if key == simplegui.KEY_MAP['left']: self.snake.snakeScalar = Vector(-0.5, 0) if key == simplegui.KEY_MAP['right']: self.snake.snakeScalar = Vector(0.5, 0)
class Carol(object): def __init__(self, pos): self.img_url = "https://raw.githubusercontent.com/CougarTasker/twenty-four/master/proto/images/carol.png" self.row = 1 self.column = 5 self.img = simplegui.load_image(self.img_url) self.bdim = Vector(self.img.get_width(), self.img.get_height()) self.adim = (self.bdim.x / self.column, self.bdim.y / self.row) self.center = Vector(self.adim[0] / 2, self.adim[1] / 2) self.pos = pos self.ratio = 0.2 self.frame_index = [1, 1] self.count = 0 def draw_canvas(self, canvas): self.count += 1 if self.count % 5 == 0: self.frame_index = self.next_frame() canvas.draw_image( self.img, (self.center.get_p()[0] * (2 * self.frame_index[1] - 1), self.center.get_p()[1] * (2 * self.frame_index[0] - 1)), self.adim, self.pos.get_p(), (self.adim[0] * self.ratio, self.adim[1] * self.ratio)) def next_frame(self): self.frame_index[1] += 1 if self.frame_index[1] > self.column: self.frame_index[0] += 1 self.frame_index[1] = 1 if self.frame_index[0] > self.row: self.frame_index = [1, 1] return self.frame_index
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 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 update(self): global score, lives if gameRun: #used to increase the speed + size of the snake self.snakeX += self.snakeScalar.x * self.speedScalar self.snakeY += self.snakeScalar.y * self.speedScalar #enemy snake hit wall then it turns around for x in self.snakePos: if (x[0] <= 0): self.snakeScalar = Vector(0.5, 0) elif (x[1] <= 0): self.snakeScalar = Vector(0, 0.5) elif (x[0] >= CANVAS_WIDTH): self.snakeScalar = Vector(-0.5, 0) elif (x[1] >= CANVAS_HEIGHT): self.snakeScalar = Vector(0, -0.5)
def __init__(self, snakeX, snakeY, snakeRad, snakeCol): self.snakeX = snakeX self.snakeY = snakeY self.snakePos = [[self.snakeX, self.snakeY]] self.snakeRad = snakeRad self.snakeScalar = Vector(0, 0) self.snakeTails = 40 self.speedScalar = 1 self.x = 0 self.snakeCol = snakeCol
def draw(self, canvas, center=(-1, -1), size=(-1, -1), rotation=0): self.fno = round((time.time() % (self.time / 1000)) / (self.time / 1000) * (self.framecount - 1)) x = self.fno % self.size[0] y = (self.fno - x) / self.size[0] if center[0] < 0: center = self.pos.get_p() if size[0] < 0: size = (self.adim[0] * self.scale, self.adim[1] * self.scale) loc = Vector(x * self.adim[0], y * self.adim[1]) canvas.draw_image(self.img, (self.cent + loc).get_p(), self.adim, center, size, rotation)
def __init__(self, count, dim): self.fish = [] random.seed(time.time()) self.imgr = SpriteSheet( "https://raw.githubusercontent.com/CougarTasker/twenty-four/master/proto/images/right.png", (2, 2), time=400, scale=0.2) self.imgl = SpriteSheet( "https://raw.githubusercontent.com/CougarTasker/twenty-four/master/proto/images/left.png", (2, 2), time=400, scale=0.2) for i in range(count): self.fish.append( Fsh( Vector(random.random() * dim[0], random.random() * dim[1]), Bounds(Vector(0, 0.325 * dim[1]), Vector(dim[0], dim[1] * 0.675)), self.imgr, self.imgl))
def __init__(self, url, size=(1, 1), pos=Vector(), framecount=-1, time=1000, scale=1): if framecount == -1: framecount = size[0] * size[1] self.framecount = framecount self.url = url self.size = size self.img = simplegui.load_image(url) self.bdim = Vector(self.img.get_width(), self.img.get_height()) # before dimentions self.adim = (self.bdim.x / self.size[0], self.bdim.y / self.size[1] ) # after dimaentions self.cent = Vector(self.adim[0] / 2, self.adim[1] / 2) self.fno = 0 self.time = time self.pos = pos self.scale = scale
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)
def __init__(self, dimensions): self.lastFrameTime = time.time() self.dimensions = dimensions self.clouds = simplegui.load_image( "https://raw.githubusercontent.com/CougarTasker/twenty-four/master/proto/images/background_clouds.png" ) self.sun = simplegui.load_image( "https://raw.githubusercontent.com/CougarTasker/twenty-four/master/proto/images/sun.png" ) self.water_world = simplegui.load_image( "https://raw.githubusercontent.com/CougarTasker/twenty-four/master/proto/images/underwater-seamless-landscape-cartoon-background-vector-7524975.png" ) self.morebubble = simplegui.load_image( "https://raw.githubusercontent.com/CougarTasker/twenty-four/master/proto/images/bubble2.png" ) self.bubbles = SpriteSheet( "https://raw.githubusercontent.com/CougarTasker/twenty-four/master/proto/images/bubble2.png", (15, 5), time=1250, scale=0.04, pos=Vector(659, 200)) self.pearl = Pearl(Vector(695, 700)) self.carol = Carol(Vector(227, 700)) self.carolbubble = bubblesheet(Vector(227, 680))
def draw(self, canvas): #calls the local class update method self.update() #displays the score and the number of lives so the user can see the value of both variables canvas.draw_text(('Score: ' + str(score)), (0, 40), 40, 'Red') canvas.draw_text(('Lives: ' + str(lives)), (650, 40), 40, 'Red') #calls the draw method for each object from their class self.snake.draw(canvas) self.food.draw(canvas) self.enemy.draw(canvas) #draws the line to the canvas for b in self.obstacle: b.draw(canvas) #when user has no lives left if (dead): self.snake.snakeScalar = Vector(0, 0)
def correct(self, fish): return Vector(self.cord(self.pos.x, self.pos.x + self.dim.x, fish), self.cora(self.pos.y, self.pos.y + self.dim.y, fish))
def update(self): global score, lives, dead, B1, B2, E1, E2, Up, Down, Left, Right, alongline, SNAKE_STARTING_POS #makes a call to the update methods for each object snake and enemy self.snake.update() self.enemy.update() #check if snake eats food if (self.food.pos.x - 2.5 * self.snake.snakeRad <= self.snake.snakeX and self.food.pos.x + 2.5 * self.snake.snakeRad >= self.snake.snakeX and self.food.pos.y - self.snake.snakeRad < self.snake.snakeY and self.food.pos.y + self.snake.snakeRad > self.snake.snakeY): #increment the score by 1 if the player snake is in contact with the food score += 1 #increase the speed + size of snake when it consumes food self.snake.speedScalar += 0.1 self.food.pos = Vector(random.randint(1, CANVAS_WIDTH), random.randint(1, CANVAS_HEIGHT)) Down = False Up = False Left = False Right = False #check if snake touches border if (self.snake.snakeX > CANVAS_WIDTH or self.snake.snakeY > CANVAS_HEIGHT or self.snake.snakeX < 0 or self.snake.snakeY < 0): #checks users current amount of lives and adjusts them appropriatly as well as reseting the snake back to its starting position if (lives == 3): lives = 2 self.snake.snakeX = SNAKE_STARTING_POS self.snake.snakeY = SNAKE_STARTING_POS elif (lives == 2): lives = 1 self.snake.snakeX = SNAKE_STARTING_POS self.snake.snakeY = SNAKE_STARTING_POS elif (lives == 1): lives = 0 dead = True open_scorepage() #code runs for the obstacle which is the line for line in self.obstacle: HalfW = line.width / 2 + 10 #food so its not between border if (self.food.pos.y > line.p1[1] - HalfW and self.food.pos.y < line.p1[1] + HalfW) and (self.food.pos.x > line.p1[0] and self.food.pos.x < line.p2[0]): self.food.pos = Vector(random.randint(10, CANVAS_WIDTH), random.randint(10, CANVAS_HEIGHT)) #player touches the line if (self.snake.snakeY > line.p1[1] - HalfW and self.snake.snakeY < line.p1[1] + HalfW) and ( self.snake.snakeX > line.p1[0] and self.snake.snakeX < line.p2[0]): lives -= 1 self.snake.snakeX = SNAKE_STARTING_POS self.snake.snakeY = SNAKE_STARTING_POS if lives == 0: dead = True open_scorepage() #enemy not hitting line if (self.enemy.snakeY > line.p1[1] - HalfW and self.enemy.snakeY < line.p1[1] + HalfW) and ( self.enemy.snakeX > line.p1[0] and self.enemy.snakeX < line.p2[0]): if self.enemy.snakeY < CANVAS_HEIGHT / 2: if self.enemy.snakeX < B1: self.enemy.snakeScalar = Vector(0.5, 0) elif self.enemy.snakeX > E1: self.enemy.snakeScalar = Vector(-0.5, 0) elif self.enemy.snakeY > CANVAS_HEIGHT / 2: if self.enemy.snakeX < B2: self.enemy.snakeScalar = Vector(0.5, 0) elif self.enemy.snakeX > E2: self.enemy.snakeScalar = Vector(-0.5, 0) alongline = True Down = False Up = False Left = False Right = False if alongline and self.enemy.snakeY < CANVAS_HEIGHT / 2: if (self.enemy.snakeX == B1 or self.enemy.snakeX == E1): alongline = False elif alongline and self.enemy.snakeY > CANVAS_HEIGHT / 2: if (self.enemy.snakeX == B2 or self.enemy.snakeX == E2): alongline = False #enemy trying to get the food if not alongline: if not Down and (self.enemy.snakeY - self.food.pos.y) > 0: self.enemy.snakeScalar = Vector(0, -0.5) Up = True elif not Up and (self.enemy.snakeY - self.food.pos.y) < 0: self.enemy.snakeScalar = Vector(0, 0.5) Down = True else: if not Right and (self.enemy.snakeX - self.food.pos.x) > 0: self.enemy.snakeScalar = Vector(-0.5, 0) Left = True elif not Left and (self.enemy.snakeX - self.food.pos.x) < 0: self.enemy.snakeScalar = Vector(0.5, 0) Right = True #reposition food if the enemy gets it if (self.food.pos.x - 2.5 * self.enemy.snakeRad <= self.enemy.snakeX and self.food.pos.x + 2.5 * self.enemy.snakeRad >= self.enemy.snakeX and self.food.pos.y - self.enemy.snakeRad < self.enemy.snakeY and self.food.pos.y + self.enemy.snakeRad > self.enemy.snakeY): self.food.pos = Vector(random.randint(10, CANVAS_WIDTH), random.randint(10, CANVAS_HEIGHT)) Down = False Up = False Left = False Right = False
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)
def __init__(self): self.pos = Vector(random.randint(1, CANVAS_WIDTH), random.randint(1, CANVAS_HEIGHT)) self.rad = 5