class Carrot(): def __init__(self, direction, posX, posY, objectList = [], rabbitList = []): self.objectList = objectList self.rabbitList = rabbitList self.carrotAnim = Animation("carrot", 24) self.carrotAnim.setFrameRange(1, 12); self.rect = pygame.Rect(posX, posY, 25, 25) self.carrotAnim.setRect(self.rect) self.carrotAnim.playAnim() self.sprite = pygame.sprite.RenderPlain(self.carrotAnim) self.screen = pygame.display.get_surface() self.area = self.screen.get_rect() self.area.h += 500 self.area.y -= 550 self.area.w -= 200 self.smoked = False self.countDown = 60 self.moveX = posX self.direction = direction if self.direction == "left": self.carrotAnim.flipAnim() def update(self): if self.smoked: self.carrotAnim.setRect(self.rect) self.countDown -= 1 if self.countDown == 0: return True else: if self.direction == "right": self.moveX += 6 else: self.moveX -= 6 self.rect.x = self.moveX self.carrotAnim.setRect(self.rect) self.checkForCollision() if self.rect.x > self.area.w + 10: return True elif (self.rect.x + self.rect.w) < self.area.x - 10: return True self.sprite.update() self.sprite.draw(self.screen) self.carrotAnim.update() pygame.event.pump() return False def collisionDetection(self, obj, rabbit = False): if rabbit: if (self.rect.y + self.rect.h) > obj.rect.y and self.rect.y < (obj.rect.y + obj.rect.h): if self.direction == "right": if (self.rect.x + self.rect.w) > obj.rect.x: obj.touch() self.smoke() else: if self.rect.x < (obj.rect.x + obj.rect.w): obj.touch() self.smoke() else: if obj.isInBlock(self.rect.x + self.rect.w/2, self.rect.y + self.rect.h/2): self.smoke() def checkForCollision(self): for obj in self.objectList: self.collisionDetection(obj) for rabbit in self.rabbitList: self.collisionDetection(rabbit, True) def getAnim(self): return self.carrotAnim def smoke(self): self.smoked = True self.rect.x -= 25 self.rect.y -= 25 self.rect.w = 75 self.rect.h = 75 self.carrotAnim = Animation("carrot_smoke", 25) self.carrotAnim.setFrameRange(1, 25); self.carrotAnim.setRect(self.rect) self.sprite = pygame.sprite.RenderPlain(self.carrotAnim) self.carrotAnim.playAnim(False)
class Butterfly(): def __init__(self, x, y, color = (255, 255, 255), objectList = [], spriteList = []): self.objectList = objectList self.spriteList = spriteList self.rect = pygame.Rect(x, y, 15, 15) self.screen = pygame.display.get_surface() self.butterflyAnim = Animation("butterfly", 8) self.butterflyAnim.updateColor(color) self.butterflyAnim.setFrameRange(1, 8); self.butterflyAnim.setCurrentFrame(randint(1, 8)) self.sprite = pygame.sprite.RenderPlain(self.butterflyAnim) self.butterflyAnim.playAnim() self.area = self.screen.get_rect() self.color = color self.floorLevel = self.screen.get_height() - self.rect.h self.movingLeft = False self.movingRight = False self.movingUp = False self.movingDown = False self.degree = randint(0, 360) self.speed = 4 self.detlaUpdate = 0 self.collide = False self.movePos = [0,0.01] def update(self): if(self.detlaUpdate == 1): self.detlaUpdate = 0 limitBottom = self.degree - 20 limitTop = self.degree + 20 self.degree = randint(limitBottom, limitTop) self.degree = self.degree % 360 if(randint(0, 1) == 0): if(self.speed < 6): self.speed += 1 else: if(self.speed > 2): self.speed -= 1 self.movePos[0] = math.cos(math.radians(self.degree)) * self.speed self.movePos[1] = math.sin(math.radians(self.degree)) * self.speed #self.movePos[0] = randint(-8, 8) #self.movePos[1] = randint(-8, 8) self.checkForCollision() newpos = self.rect.move(self.movePos) if self.area.contains(newpos) and not self.collide: self.rect = newpos self.butterflyAnim.getRect().x = self.rect.x self.butterflyAnim.getRect().y = self.rect.y if self.degree >= 90 and self.degree <= 240: self.movingLeft = True self.movingRight = False else: self.movingLeft = False self.movingRight = True if self.degree >= 0 and self.degree <= 180: self.movingDown = True self.movingUp = False else: self.movingDown = False self.movingUp = True else: self.detlaUpdate += 1 self.butterflyAnim.update() self.sprite.update() self.sprite.draw(self.screen) pygame.event.pump() def collisionDetection(self, obj, rabbit = False): #FLEE BEHAVIOR !!! objCenterX = obj.rect.x + obj.rect.w/2 objCenterY = obj.rect.y + obj.rect.h/2 butterflyCenterX = self.rect.x + self.rect.w/2 butterflyCenterY = self.rect.y + self.rect.h/2 dist = math.fabs(math.sqrt(((objCenterX - butterflyCenterX) * (objCenterX - butterflyCenterX)) + ((objCenterY - butterflyCenterY) * (objCenterY - butterflyCenterY)))) if dist < 50: distX = math.fabs(objCenterX - butterflyCenterX) angle = math.degrees(math.acos(distX/dist)) if (objCenterX <= butterflyCenterX) and (objCenterY <= butterflyCenterY): angle = 240 - angle elif (objCenterX > butterflyCenterX) and (objCenterY <= butterflyCenterY): angle = 360 - angle elif (objCenterX <= butterflyCenterX) and (objCenterY > butterflyCenterY): angle = 180 - angle angle += 180 angle = angle % 360 self.degree = int(angle) #FLEE BEHAVIOR !!! # if (self.rect.x < (obj.rect.x + obj.rect.w)) and (obj.rect.x < self.rect.x): # if (self.rect.y + self.rect.h) > (obj.rect.y + 1): # if self.rect.y < (obj.rect.y + obj.rect.h): # self.movePos[0] = 5 # if (obj.rect.x < (self.rect.x + self.rect.w)) and (obj.rect.x > self.rect.x): # if (self.rect.y + self.rect.h) > (obj.rect.y + 1): # if self.rect.y < (obj.rect.y + obj.rect.h): # self.movePos[0] = -5 # if (self.rect.y <= (obj.rect.y + obj.rect.h)) and (obj.rect.y <= self.rect.y): # if (self.rect.x + self.rect.w) > (obj.rect.x + 3): # if self.rect.x < (obj.rect.x + obj.rect.w - 5): # self.movePos[1] = 5 # if ((self.rect.y + self.rect.h) >= obj.rect.y) and (self.rect.y <= obj.rect.y): # if (self.rect.x + self.rect.w) > (obj.rect.x + 3): # if self.rect.x < (obj.rect.x + obj.rect.w - 5): # if self.movePos[1] >= 0: # self.rect.y = obj.rect.y - self.rect.h # self.movePos[1] = -5 def screenCollisionDetection(self): butterflyCenterX = self.rect.x + self.rect.w/2 butterflyCenterY = self.rect.y + self.rect.h/2 distBorder = butterflyCenterX if distBorder < 10: self.degree = 0 if (butterflyCenterY) < distBorder: distBorder = butterflyCenterY if distBorder < 10: self.degree = 90 if (self.screen.get_rect().w - butterflyCenterX) < distBorder: distBorder = self.screen.get_rect().w - butterflyCenterX if distBorder < 10: self.degree = 180 if (self.screen.get_rect().h - butterflyCenterY) < distBorder: distBorder = self.screen.get_rect().h - butterflyCenterY if distBorder < 10: self.degree = 240 def checkForCollision(self): #if not self.movingLeft and not self.movingRight and self.movePos[1] == 0 and self.velocity == 0: # return for obj in self.objectList: if obj.getType() is not "carrot": self.collisionDetection(obj, False) self.screenCollisionDetection() #for rabbit in self.rabbitList: # self.collisionDetection(rabbit, True) def moveLeftStart(self): self.movingLeft = True self.movingRight = False def moveLeftStop(self): self.movingLeft = False self.movePos[0] = 0 def moveRightStart(self): self.movingRight = True self.movingLeft = False def moveRightStop(self): self.movingRight = False self.movePos[0] = 0 def getAnim(self): return self.butterflyAnim
class Rabbit(): pygame.mixer.pre_init(44100, -16, 2, 1024) pygame.mixer.init() def __init__(self, id = -1, name = "", color = (255, 255, 255), objectList = [], spriteList = [], fake = False, x = 0, y = 0): self.objectList = objectList self.spriteList = spriteList self.rabbitList = [] self.fake = fake self.color = color self.rect = pygame.Rect(x, y, 43, 32) self.rabbitAnim = Animation("rabbit", 30) self.rabbitAnim.updateColor(self.color) self.rabbitAnim.setFrameRange(1, 8); self.sprite = pygame.sprite.RenderPlain(self.rabbitAnim) self.rabbitAnim.stopAnim() self.screen = pygame.display.get_surface() self.area = self.screen.get_rect() self.area.h += 500 self.area.y -= 550 self.area.w -= 200 self.floorLevel = self.screen.get_height() - self.rect.h self.explosion = Explosion() self.movingLeft = False self.movingRight = False self.movingUp = False self.movingDown = False self.isJumping = False self.isOnBlock = False self.direction = "left" self.collide = False self.touched = False self.touchDelay = 0 self.id = id self.name = name self.jumpSound = pygame.mixer.Sound("resources/sound/jump.wav") self.splashSound = pygame.mixer.Sound("resources/sound/splash.wav") self.carrotSound = pygame.mixer.Sound("resources/sound/carrot.wav") self.jumpSound.set_volume(float(Resources.getOptionValue("sound"))/100) self.splashSound.set_volume(float(Resources.getOptionValue("sound"))/100) self.carrotSound.set_volume(float(Resources.getOptionValue("sound"))/100) self.velocity = 5 self.gravity = 0.6 self.movePos = [0,0.01] self.replaceRabbit() self.points = 0 self.carrots = 0 self.currentFriction = self.getFloorFriction() self.thrownCarrots = [] self.blood = Resources.getOptionValue("blood") def __str__(self): print "Rabbit ", self.id, ": ", self.name def update(self): if not self.fake: if self.touched: self.touchDelayCalculation() else: if self.movingLeft == True: self.movePos[0] = -self.velocity if self.movingRight == True: self.movePos[0] = self.velocity if self.movePos[1] is not 0: self.movePos[1] += self.gravity self.rect.y += self.movePos[1] if self.rect.y > self.floorLevel: self.rect.y = self.floorLevel self.movePos[1] = 0 self.isJumping = False if self.movePos[1] < 0: self.movingUp = True self.movingDown = False elif self.movePos[1] > 0: self.movingUp = False self.movingDown = True else: self.movingUp = False self.movingDown = False self.checkForCollision() newpos = self.rect.move(self.movePos) if self.area.contains(newpos) and not self.collide: self.rect = newpos if self.isJumping: self.rabbitAnim.setFrameRange(9, 15) if self.movingUp: self.rabbitAnim.stopAnim() self.rabbitAnim.setCurrentFrame(9) else: self.rabbitAnim.playAnim(False) else: self.rabbitAnim.setFrameRange(1, 8) if not self.movingLeft and not self.movingRight: self.rabbitAnim.stopAnim() self.rabbitAnim.rewind() else: self.rabbitAnim.playAnim() self.rabbitAnim.getRect().x = self.rect.x self.rabbitAnim.getRect().y = self.rect.y - 16 tmpThrownCarrots = [] for c in self.thrownCarrots: end = c.update() if not end: tmpThrownCarrots.append(c) self.thrownCarrots = tmpThrownCarrots self.sprite.update() self.sprite.draw(self.screen) self.rabbitAnim.update() self.explosion.update() pygame.event.pump() def collisionDetection(self, obj, rabbit = False): if self.movingLeft: if (self.rect.x < (obj.rect.x + obj.rect.w)) and (obj.rect.x < self.rect.x): if (self.rect.y + self.rect.h) > (obj.rect.y + 1): if self.rect.y < (obj.rect.y + obj.rect.h): self.movePos[0] = 0 if self.movingRight: if (obj.rect.x < (self.rect.x + self.rect.w)) and (obj.rect.x > self.rect.x): if (self.rect.y + self.rect.h) > (obj.rect.y + 1): if self.rect.y < (obj.rect.y + obj.rect.h): self.movePos[0] = 0 if self.movingUp: if (self.rect.y <= (obj.rect.y + obj.rect.h)) and (obj.rect.y <= self.rect.y): if (self.rect.x + self.rect.w) > (obj.rect.x + 3): if self.rect.x < (obj.rect.x + obj.rect.w - 5): self.movePos[1] = 0.01 if ((self.rect.y + self.rect.h) >= obj.rect.y) and (self.rect.y <= obj.rect.y): if (self.rect.x + self.rect.w) > (obj.rect.x + 3): if self.rect.x < (obj.rect.x + obj.rect.w - 5): if self.movePos[1] >= 0 and not self.isOnBlock: self.isJumping = False if rabbit: self.jump(5) if self.blood == 1: obj.explosion = Explosion(obj.rect.x, obj.rect.y, obj.color) obj.explosion.startExplosion() self.splashSound.play() obj.replaceRabbit() self.points += 1 elif obj.getType() == "boing": self.jump(12.7) else: self.rect.y = obj.rect.y - self.rect.h self.movePos[1] = 0 self.isOnBlock = True if self.isJumping: self.isOnBlock = False if self.isOnBlock: if (self.rect.x > (obj.rect.x + obj.rect.w)) or ((self.rect.x + self.rect.w) < obj.rect.x): self.isOnBlock = False self.movePos[1] = 0.01 def checkForCollision(self): if not self.movingLeft and not self.movingRight and self.movePos[1] == 0 and self.velocity == 0 and not self.isJumping: return for obj in self.objectList: if obj.getType() == "carrot" and obj.isInBlock(self.rect.x + self.rect.w/2, self.rect.y + self.rect.h -5): self.carrotSound.play() self.carrots += 1 self.objectList.remove(obj) self.spriteList.remove(obj) if obj.getType() is not "carrot": self.collisionDetection(obj, False) for rabbit in self.rabbitList: self.collisionDetection(rabbit, True) def jump(self, velocity = 8.1): if not self.isJumping: self.jumpSound.play() self.movePos[1] = -velocity self.isJumping = True def moveLeftStart(self): if self.rabbitAnim.getFlip(): self.rabbitAnim.flipAnim() self.rabbitAnim.playAnim() self.movingLeft = True self.movingRight = False self.direction = "left" def moveLeftStop(self): if not self.movingRight: self.rabbitAnim.stopAnim() self.rabbitAnim.rewind() self.movingLeft = False self.movePos[0] = 0 def moveRightStart(self): if not self.rabbitAnim.getFlip(): self.rabbitAnim.flipAnim() self.rabbitAnim.playAnim() self.movingRight = True self.movingLeft = False self.direction = "right" def moveRightStop(self): if not self.movingLeft: self.rabbitAnim.stopAnim() self.rabbitAnim.rewind() self.movingRight = False self.movePos[0] = 0 def appendRabbit(self, rabbit): self.rabbitList.append(rabbit) def replaceRabbit(self): self.touched = False while True: randObj = self.objectList[random.randint(1, len(self.objectList)) - 1] if self.isFloor(randObj): break self.rect.topleft = (randObj.rect.x, randObj.rect.y - randObj.rect.h) def isInBlock(self, x, y): for obj in self.objectList: if obj.isInBlock(x, y): return True return False def isFloor(self, object): if self.isInBlock(object.getX() + 5, object.getY() - 5): return False return True def getFloorFriction(self): return 0.7 def touch(self): self.touched = True self.touchDelay = 180 TOUCH = USEREVENT + 1 touchevent = pygame.event.Event(TOUCH) pygame.event.post(touchevent) def touchDelayCalculation(self): self.touchDelay -= 1 if(self.touchDelay == 0): self.touched = False UNTOUCH = USEREVENT + 2 untouchevent = pygame.event.Event(UNTOUCH) pygame.event.post(untouchevent) def isTouched(self): return self.touched def throwCarrot(self): if(self.carrots > 0): if(self.direction == "right"): self.thrownCarrots.append(Carrot(self.direction, self.rect.x + 10, self.rect.y, self.objectList, self.rabbitList)) else: self.thrownCarrots.append(Carrot(self.direction, self.rect.x, self.rect.y, self.objectList, self.rabbitList)) self.carrots -= 1 def updateColor(self, color): self.rabbitAnim.updateColor(color) def getId(self): return self.id def getName(self): return self.name def getPoints(self): return self.points def getCarrots(self): return self.carrots def getAnim(self): return self.rabbitAnim def setId(self, id): self.id = id def setName(self, name): self.name = name def setPoints(self, points): self.points = points