def __init__(self, entity, level): self.direction = -1 if randint(0, 1) == 0 else 1 self.entity = entity self.collDetection = Collider(self.entity, level) self.speed = 1 self.entity.vel.x = self.speed * self.direction self.level = level
def __init__(self, x, y, level, screen, dashboard, sound, gravity=0.75): super(Mario, self).__init__(x, y, gravity) self.spriteCollection = Sprites().spriteCollection self.camera = Camera(self.rect, self) self.sound = sound self.input = Input(self) self.inAir = False self.inJump = False self.animation = Animation( [ self.spriteCollection["mario_run1"].image, self.spriteCollection["mario_run2"].image, self.spriteCollection["mario_run3"].image, ], self.spriteCollection["mario_idle"].image, self.spriteCollection["mario_jump"].image, ) self.traits = { "jumpTrait": jumpTrait(self), "goTrait": goTrait(self.animation, screen, self.camera, self), "bounceTrait": bounceTrait(self), } self.levelObj = level self.collision = Collider(self, level) self.screen = screen self.EntityCollider = EntityCollider(self) self.dashboard = dashboard self.restart = False self.pause = False self.pauseObj = Pause(screen, self, dashboard)
def __init__(self, entity, level): self.direction = -1 if randint(0, 1) == 0 else 1 self.entity = entity self.collDetection = Collider(self.entity, level) self.speed = 1 self.entity.vel.x = self.speed * self.direction self.level = level self.vertical_speed = -12 self.jumpHeight = 120 self.entity = entity self.initalHeight = 384 self.deaccelerationHeight = self.jumpHeight - ((self.vertical_speed*self.vertical_speed)/(2*self.entity.gravity))
def __init__(self, x, y, level, gravity=1.25): super(MarioHeadless, self).__init__(x, y, gravity) self.input = Input(self) self.traits = { "jumpTrait": jumpTrait(self), "goTrait": goTraitHeadless(self), "bounceTrait": bounceTrait(self) } self.levelObj = level self.collision = Collider(self, level) self.EntityCollider = EntityCollider(self) self.restart = False
class LacyFight: def __init__(self, entity, level): self.direction = -1 if randint(0, 1) == 0 else 1 self.entity = entity self.collDetection = Collider(self.entity, level) self.speed = 1 self.entity.vel.x = self.speed * self.direction self.level = level self.vertical_speed = -12 self.jumpHeight = 120 self.entity = entity self.initalHeight = 384 self.deaccelerationHeight = self.jumpHeight - ((self.vertical_speed*self.vertical_speed)/(2*self.entity.gravity)) def update(self): if self.entity.rect.x > (54*32): self.direction = -1 elif self.entity.vel.x == 0: self.direction = 1 self.entity.vel.x = self.speed * self.direction self.moveEntity() self.jump(self.entity.inJump) if abs(self.entity.rect.x - self.level.mario.rect.x) < 150 and not self.entity.inJump: if randint(0, 250) == 100 and self.level.mario.rect.y == 384: self.jump(True) def moveEntity(self): self.entity.rect.y += self.entity.vel.y self.collDetection.checkY() self.entity.rect.x += self.entity.vel.x self.collDetection.checkX() def jump(self, jumping): if jumping: if not self.entity.inAir and not self.entity.inJump: self.entity.vel.y = self.vertical_speed self.entity.inAir = True self.initalHeight = self.entity.rect.y self.entity.inJump = True if self.entity.inJump: if (self.initalHeight-self.entity.rect.y) >= self.deaccelerationHeight or self.entity.vel.y==0: self.entity.inJump = False self.reset() def reset(self): self.entity.inAir = False self.level.mario.stun()
class LeftRightWalkTrait: def __init__(self, entity, level): self.direction = -1 self.entity = entity self.collDetection = Collider(self.entity, level) self.speed = 5 self.entity.vel.x = self.speed * self.direction def update(self): if self.entity.vel.x == 0: self.direction *= -1 self.entity.vel.x = self.speed * self.direction self.moveEntity() def moveEntity(self): self.entity.rect.y += self.entity.vel.y self.collDetection.checkY() self.entity.rect.x += self.entity.vel.x self.collDetection.checkX()
def __init__(self, x, y, level, screen, dashboard, sound, gravity=0.75): super(Mario, self).__init__(x, y, gravity) self.x = x self.spriteCollection = Sprites().spriteCollection self.CT = CollisionTester() self.camera = Camera(self.rect, self) self.sound = sound self.level = level self.OI = Input(self) self.closest_mob = None self.closest_object = None self.output =0 self.inAir = False self.brain = Model().share_memory() self.fitness = 0 self.animation = Animation( [ self.spriteCollection["mario_run1"].image, self.spriteCollection["mario_run2"].image, self.spriteCollection["mario_run3"].image, ], self.spriteCollection["mario_idle"].image, self.spriteCollection["mario_jump"].image, ) self.traits = { "jumpTrait": jumpTrait(self), "goTrait": goTrait(self.animation, screen, self.camera, self), "bounceTrait": bounceTrait(self), } self.levelObj = level self.collision = Collider(self, level) self.screen = screen self.EntityCollider = EntityCollider(self) self.dashboard = dashboard self.restart = False self.pause = False self.pauseObj = Pause(screen, self, dashboard)
class HenriettaFight: def __init__(self, entity, level): self.direction = -1 if randint(0, 1) == 0 else 1 self.entity = entity self.collDetection = Collider(self.entity, level) self.speed = 1 self.entity.vel.x = self.speed * self.direction self.level = level self.vertical_speed = -12 self.jumpHeight = 120 self.entity = entity self.initalHeight = 384 self.deaccelerationHeight = self.jumpHeight - ((self.vertical_speed*self.vertical_speed)/(2*self.entity.gravity)) def update(self): self.moveEntity() def moveEntity(self): self.entity.rect.y += self.entity.vel.y self.collDetection.checkY() self.entity.rect.x += self.entity.vel.x self.collDetection.checkX()
def __init__(self, x, y, level, screen, dashboard, sound, gravity=0.75): super(Mario, self).__init__(x, y, gravity) self.camera = Camera(self.rect, self) self.sound = sound self.input = Input(self) self.inAir = False self.inJump = False self.powerUpState = 0 self.invincibilityFrames = 0 self.traits = { "jumpTrait": JumpTrait(self), "goTrait": GoTrait(smallAnimation, screen, self.camera, self), "bounceTrait": bounceTrait(self), } self.levelObj = level self.collision = Collider(self, level) self.screen = screen self.EntityCollider = EntityCollider(self) self.dashboard = dashboard self.restart = False self.pause = False self.pauseObj = Pause(screen, self, dashboard)
def __init__(self, screen, spriteColl, x, y, level, sound): super(RedMushroom, self).__init__(y, x - 1, 1.25) self.spriteCollection = spriteColl self.animation = Animation([ self.spriteCollection.get("mushroom").image, ]) self.screen = screen self.leftrightTrait = LeftRightWalkTrait(self, level) self.type = "Mob" self.dashboard = level.dashboard self.collision = Collider(self, level) self.EntityCollider = EntityCollider(self) self.levelObj = level self.sound = sound
class SalFight: def __init__(self, entity, level): self.direction = -1 if randint(0, 1) == 0 else 1 self.entity = entity self.collDetection = Collider(self.entity, level) self.speed = 1 self.entity.vel.x = self.speed * self.direction self.level = level def update(self): if self.entity.rect.x > 1700: self.direction = -1 if self.entity.rect.x < 1340: self.direction = 1 if randint(0, 20) == 3: self.speed = randint(1, 3) self.entity.vel.x = self.speed * self.direction self.moveEntity() def moveEntity(self): self.entity.rect.y += self.entity.vel.y self.collDetection.checkY() self.entity.rect.x += self.entity.vel.x self.collDetection.checkX()
def __init__(self, x, y, level, screen, dashboard, gravity=1.25): super(Mario, self).__init__(x, y, gravity) self.spriteCollection = Sprites().spriteCollection self.camera = Camera(self.rect, self) self.sound = Sound() self.animation = Animation([ self.spriteCollection["mario_run1"].image, self.spriteCollection["mario_run2"].image, self.spriteCollection["mario_run3"].image ], self.spriteCollection["mario_idle"].image, self.spriteCollection["mario_jump"].image) self.traits = { "jumpTrait": jumpTrait(self), "goTrait": goTrait(self.animation, screen, self.camera, self), "bounceTrait": bounceTrait(self) } self.levelObj = level self.collision = Collider(self, level) self.screen = screen self.EntityCollider = EntityCollider(self) self.dashboard = dashboard self.restart = False
def __init__(self, screen, spriteColl, x, y, level): super(Koopa, self).__init__(y - 1, x, 1.25) self.spriteCollection = spriteColl self.animation = Animation([ self.spriteCollection.get("koopa-1").image, self.spriteCollection.get("koopa-2").image, ]) self.screen = screen self.leftrightTrait = LeftRightWalkTrait(self, level) self.timer = 0 self.timeAfterDeath = 35 self.type = "Mob" self.dashboard = level.dashboard self.collision = Collider(self, level) self.EntityCollider = EntityCollider(self) self.levelObj = level
class Mario(EntityBase): def __init__(self, x, y, level, screen, dashboard, sound, gravity=1.25): super(Mario, self).__init__(x, y, gravity) self.spriteCollection = Sprites().spriteCollection self.camera = Camera(self.rect, self) self.sound = sound self.input = Input(self) self.animation = Animation([ self.spriteCollection["mario_run1"].image, self.spriteCollection["mario_run2"].image, self.spriteCollection["mario_run3"].image ], self.spriteCollection["mario_idle"].image, self.spriteCollection["mario_jump"].image) self.traits = { "jumpTrait": jumpTrait(self), "goTrait": goTrait(self.animation, screen, self.camera, self), "bounceTrait": bounceTrait(self) } self.levelObj = level self.collision = Collider(self, level) self.screen = screen self.EntityCollider = EntityCollider(self) self.dashboard = dashboard self.restart = False def update(self): self.updateTraits() self.moveMario() self.camera.move() self.applyGravity() self.checkEntityCollision() self.input.checkForInput() def moveMario(self): self.rect.y += self.vel.y self.collision.checkY() self.rect.x += self.vel.x self.collision.checkX() def checkEntityCollision(self): for ent in self.levelObj.entityList: collisionState = self.EntityCollider.check(ent) if collisionState.isColliding: if (ent.type == "Item"): self._onCollisionWithItem(ent) elif (ent.type == "Block"): self._onCollisionWithBlock(ent) elif (ent.type == "Mob"): self._onCollisionWithMob(ent, collisionState) def _onCollisionWithItem(self, item): self.levelObj.entityList.remove(item) self.dashboard.points += 100 self.dashboard.coins += 1 self.sound.play_sfx(self.sound.coin) def _onCollisionWithBlock(self, block): if (not block.triggered): self.sound.play_sfx(self.sound.bump) block.triggered = True def _onCollisionWithMob(self, mob, collisionState): if collisionState.isTop and (mob.alive is True or mob.alive == "shellBouncing"): self.sound.play_sfx(self.sound.stomp) self.rect.bottom = mob.rect.top self.bounce() self.killEntity(mob) elif collisionState.isTop and mob.alive == "sleeping": self.sound.play_sfx(self.sound.stomp) self.rect.bottom = mob.rect.top mob.timer = 0 self.bounce() mob.alive = False elif collisionState.isTop and mob.alive == "sleeping": if (mob.rect.x < self.rect.x): mob.leftrightTrait.direction = -1 else: mob.leftrightTrait.direction = 1 mob.alive = "shellBouncing" elif collisionState.isColliding and mob.alive == True: self.gameOver() def bounce(self): self.traits['bounceTrait'].jump = True def killEntity(self, ent): if ent.__class__.__name__ != "Koopa": ent.alive = False else: ent.timer = 0 ent.alive = "sleeping" self.dashboard.points += -1000 def gameOver(self): srf = pygame.Surface((640, 480)) srf.set_colorkey((255, 255, 255), pygame.RLEACCEL) srf.set_alpha(128) self.sound.music_channel.stop() self.sound.music_channel.play(self.sound.death) # for i in range(500, 20, -2): # srf.fill((0, 0, 0)) # pygame.draw.circle( # srf, (255, 255, 255), (int( # self.camera.x + self.rect.x) + 2, self.rect.y + 2), i) # self.screen.blit(srf, (0, 0)) # pygame.display.update() # self.input.checkForInput() # while(self.sound.music_channel.get_busy()): # pygame.display.update() # self.input.checkForInput() self.restart = True def getPos(self): return (self.camera.x + self.rect.x, self.rect.y) def doRandomMove(self): moves = ['moveLeft', 'moveRight', 'jump', 'doNothing'] random_move = random.choice(moves) if random_move == 'moveLeft': self.traits['goTrait'].direction = -1 elif random_move == 'moveRight': self.traits['goTrait'].direction = 1 elif random_move == 'jump': self.traits['jumpTrait'].start() elif random_move == 'doNothing': self.traits['goTrait'].direction = 0
class Mario(EntityBase): def __init__(self, x, y, level, screen, dashboard, gravity=1.25): super(Mario, self).__init__(x, y, gravity) self.spriteCollection = Sprites().spriteCollection self.camera = Camera(self.rect, self) self.sound = Sound() self.animation = Animation([ self.spriteCollection["mario_run1"].image, self.spriteCollection["mario_run2"].image, self.spriteCollection["mario_run3"].image ], self.spriteCollection["mario_idle"].image, self.spriteCollection["mario_jump"].image) self.traits = { "jumpTrait": jumpTrait(self), "goTrait": goTrait(self.animation, screen, self.camera, self), "bounceTrait": bounceTrait(self) } self.levelObj = level self.collision = Collider(self, level) self.screen = screen self.EntityCollider = EntityCollider(self) self.dashboard = dashboard self.restart = False def update(self): self.updateTraits() self.moveMario() self.camera.move() self.applyGravity() self.checkEntityCollision() def moveMario(self): self.rect.y += self.vel.y self.collision.checkY() self.rect.x += self.vel.x self.collision.checkX() def checkEntityCollision(self): for ent in self.levelObj.entityList: collission = self.EntityCollider.check(ent) if (collission != False): if (ent.type == "Item"): self.levelObj.entityList.remove(ent) self.dashboard.points += 100 self.dashboard.coins += 1 self.sound.play_sfx(self.sound.coin) elif (ent.type == "Block"): if (not ent.triggered): self.sound.play_sfx(self.sound.bump) ent.triggered = True elif (ent.type == "Mob"): if (collission == "top"): self.sound.play_sfx(self.sound.bump) if collission == "top" and (ent.alive == True or ent.alive == "shellBouncing"): self.rect.bottom = ent.rect.top self.bounce() self.killEntity(ent) elif collission == "top" and ent.alive == "sleeping": self.rect.bottom = ent.rect.top self.bounce() ent.alive = False elif collission and ent.alive == "sleeping": if (ent.rect.x < self.rect.x): ent.leftrightTrait.direction = -1 else: ent.leftrightTrait.direction = 1 ent.alive = "shellBouncing" elif collission and ent.alive == True: self.gameOver() def bounce(self): self.traits['bounceTrait'].jump = True def killEntity(self, ent): if ent.__class__.__name__ != "Koopa": ent.alive = False else: ent.timer = 0 ent.alive = "sleeping" self.dashboard.points += 100 def gameOver(self): srf = pygame.Surface((640, 480)) srf.set_colorkey((255, 255, 255), pygame.RLEACCEL) for i in range(500, 20, -3): srf.fill((0, 0, 0)) pygame.draw.circle(srf, (255, 255, 255), (int(self.camera.pos.x * 32 + self.rect.x) + 16, self.rect.y + 16), i) self.screen.blit(srf, (0, 0)) pygame.display.update() self.restart = True def getPos(self): return (self.camera.pos.x * 32 + self.rect.x, self.rect.y)
def __init__(self, entity, level): self.direction = -1 self.entity = entity self.collDetection = Collider(self.entity, level) self.speed = 5 self.entity.vel.x = self.speed * self.direction
class Mario(EntityBase): def __init__(self, x, y, level, screen, dashboard, sound, gravity=0.75): super(Mario, self).__init__(x, y, gravity) self.x = x self.spriteCollection = Sprites().spriteCollection self.CT = CollisionTester() self.camera = Camera(self.rect, self) self.sound = sound self.level = level self.OI = Input(self) self.closest_mob = None self.closest_object = None self.output =0 self.inAir = False self.brain = Model().share_memory() self.fitness = 0 self.animation = Animation( [ self.spriteCollection["mario_run1"].image, self.spriteCollection["mario_run2"].image, self.spriteCollection["mario_run3"].image, ], self.spriteCollection["mario_idle"].image, self.spriteCollection["mario_jump"].image, ) self.traits = { "jumpTrait": jumpTrait(self), "goTrait": goTrait(self.animation, screen, self.camera, self), "bounceTrait": bounceTrait(self), } self.levelObj = level self.collision = Collider(self, level) self.screen = screen self.EntityCollider = EntityCollider(self) self.dashboard = dashboard self.restart = False self.pause = False self.pauseObj = Pause(screen, self, dashboard) def getInputs(self): map = [] input = [] for x in range(60): map.append(self.CT.test(x, 11, self.level, self)) if map[x] == 3: map[x] = 0 for x in range(int(self.rect.x/32), int(self.rect.x/32) + 8): input.append(map[x]) return torch.FloatTensor(input) def update(self): self.updateTraits() self.moveMario() self.camera.move() self.applyGravity() self.checkEntityCollision() self.output = self.brain.forward(self.getInputs()) #print(self.output) self.OI.checkForInput(self.output) def moveMario(self): self.rect.y += self.vel.y self.collision.checkY() self.rect.x += self.vel.x self.collision.checkX() def checkEntityCollision(self): for ent in self.levelObj.entityList: collisionState = self.EntityCollider.check(ent) if collisionState.isColliding: if ent.type == "Item": self._onCollisionWithItem(ent) elif ent.type == "Block": self._onCollisionWithBlock(ent) elif ent.type == "Mob": self._onCollisionWithMob(ent, collisionState) def _onCollisionWithItem(self, item): self.levelObj.entityList.remove(item) self.dashboard.points += 100 self.dashboard.coins += 1 self.sound.play_sfx(self.sound.coin) def _onCollisionWithBlock(self, block): if not block.triggered: self.dashboard.coins += 1 self.sound.play_sfx(self.sound.bump) block.triggered = True def _onCollisionWithMob(self, mob, collisionState): if collisionState.isTop and (mob.alive or mob.alive == "shellBouncing"): self.sound.play_sfx(self.sound.stomp) self.rect.bottom = mob.rect.top self.bounce() self.killEntity(mob) elif collisionState.isTop and mob.alive == "sleeping": self.sound.play_sfx(self.sound.stomp) self.rect.bottom = mob.rect.top mob.timer = 0 self.bounce() mob.alive = False elif collisionState.isColliding and mob.alive == "sleeping": if mob.rect.x < self.rect.x: mob.leftrightTrait.direction = -1 mob.rect.x += -5 else: mob.rect.x += 5 mob.leftrightTrait.direction = 1 mob.alive = "shellBouncing" elif collisionState.isColliding and mob.alive: self.gameOver() def bounce(self): self.traits["bounceTrait"].jump = True def killEntity(self, ent): if ent.__class__.__name__ != "Koopa": ent.alive = False else: ent.timer = 0 ent.alive = "sleeping" self.dashboard.points += 100 def gameOver(self): self.restart = True def getPos(self): return self.camera.x + self.rect.x, self.rect.y def setPos(self,x,y): self.rect.x = x self.rect.y = y
class Mario(EntityBase): def __init__(self, x, y, level, screen, dashboard, sound, gravity=0.75): super(Mario, self).__init__(x, y, gravity) self.spriteCollection = Sprites().spriteCollection self.camera = Camera(self.rect, self) self.sound = sound self.input = Input(self) self.inAir = False self.inJump = False self.animation = Animation( [ self.spriteCollection["mario_run1"].image, self.spriteCollection["mario_run2"].image, self.spriteCollection["mario_run3"].image, ], self.spriteCollection["mario_idle"].image, self.spriteCollection["mario_jump"].image, ) self.traits = { "jumpTrait": jumpTrait(self), "goTrait": goTrait(self.animation, screen, self.camera, self), "bounceTrait": bounceTrait(self), } self.levelObj = level self.collision = Collider(self, level) self.screen = screen self.EntityCollider = EntityCollider(self) self.dashboard = dashboard self.restart = False self.pause = False self.pauseObj = Pause(screen, self, dashboard) def update(self): self.updateTraits() self.moveMario() self.camera.move() self.applyGravity() self.checkEntityCollision() self.input.checkForInput() def moveMario(self): self.rect.y += self.vel.y self.collision.checkY() self.rect.x += self.vel.x self.collision.checkX() def checkEntityCollision(self): for ent in self.levelObj.entityList: collisionState = self.EntityCollider.check(ent) if collisionState.isColliding: if ent.type == "Item": self._onCollisionWithItem(ent) elif ent.type == "Block": self._onCollisionWithBlock(ent) elif ent.type == "Mob": self._onCollisionWithMob(ent, collisionState) def _onCollisionWithItem(self, item): self.levelObj.entityList.remove(item) self.dashboard.points += 100 self.dashboard.coins += 1 self.sound.play_sfx(self.sound.coin) def _onCollisionWithBlock(self, block): if not block.triggered: self.dashboard.coins += 1 self.sound.play_sfx(self.sound.bump) block.triggered = True def _onCollisionWithMob(self, mob, collisionState): if collisionState.isTop and (mob.alive or mob.alive == "shellBouncing"): self.sound.play_sfx(self.sound.stomp) self.rect.bottom = mob.rect.top self.bounce() self.killEntity(mob) elif collisionState.isTop and mob.alive == "sleeping": self.sound.play_sfx(self.sound.stomp) self.rect.bottom = mob.rect.top mob.timer = 0 self.bounce() mob.alive = False elif collisionState.isColliding and mob.alive == "sleeping": if mob.rect.x < self.rect.x: mob.leftrightTrait.direction = -1 mob.rect.x += -5 else: mob.rect.x += 5 mob.leftrightTrait.direction = 1 mob.alive = "shellBouncing" elif collisionState.isColliding and mob.alive: self.gameOver() def bounce(self): self.traits["bounceTrait"].jump = True def killEntity(self, ent): if ent.__class__.__name__ != "Koopa": ent.alive = False else: ent.timer = 0 ent.alive = "sleeping" self.dashboard.points += 100 def gameOver(self): srf = pygame.Surface((640, 480)) srf.set_colorkey((255, 255, 255), pygame.RLEACCEL) srf.set_alpha(128) self.sound.music_channel.stop() self.sound.music_channel.play(self.sound.death) for i in range(500, 20, -2): srf.fill((0, 0, 0)) pygame.draw.circle( srf, (255, 255, 255), (int(self.camera.x + self.rect.x) + 16, self.rect.y + 16), i, ) self.screen.blit(srf, (0, 0)) pygame.display.update() self.input.checkForInput() while self.sound.music_channel.get_busy(): pygame.display.update() self.input.checkForInput() self.restart = True def getPos(self): return self.camera.x + self.rect.x, self.rect.y def setPos(self, x, y): self.rect.x = x self.rect.y = y
class Mario(EntityBase): def __init__(self, x, y, level, screen, dashboard, sound, gravity=0.8): super(Mario, self).__init__(x, y, gravity) self.camera = Camera(self.rect, self) self.sound = sound self.input = Input(self) self.inAir = False self.inJump = False self.powerUpState = 0 self.invincibilityFrames = 0 self.traits = { "jumpTrait": JumpTrait(self), "goTrait": GoTrait(smallAnimation, screen, self.camera, self), "bounceTrait": bounceTrait(self), } self.levelObj = level self.collision = Collider(self, level) self.screen = screen self.EntityCollider = EntityCollider(self) self.dashboard = dashboard self.restart = False self.pause = False self.pauseObj = Pause(screen, self, dashboard) def update(self): if self.invincibilityFrames > 0: self.invincibilityFrames -= 1 self.updateTraits() self.moveMario() self.camera.move() self.applyGravity() self.checkEntityCollision() self.input.checkForInput() def moveMario(self): self.rect.y += self.vel.y self.collision.checkY() self.rect.x += self.vel.x self.collision.checkX() def checkEntityCollision(self): for ent in self.levelObj.entityList: collisionState = self.EntityCollider.check(ent) if collisionState.isColliding: if ent.type == "Item": self._onCollisionWithItem(ent) elif ent.type == "Block": self._onCollisionWithBlock(ent) elif ent.type == "Mob": self._onCollisionWithMob(ent, collisionState) def _onCollisionWithItem(self, item): self.levelObj.entityList.remove(item) self.dashboard.points += 100 self.dashboard.coins += 1 self.sound.play_sfx(self.sound.coin) def _onCollisionWithBlock(self, block): if not block.triggered: self.dashboard.coins += 1 self.sound.play_sfx(self.sound.bump) block.triggered = True def _onCollisionWithMob(self, mob, collisionState): if isinstance(mob, RedMushroom) and mob.alive: self.powerup(1) self.killEntity(mob) self.sound.play_sfx(self.sound.powerup) elif collisionState.isTop and (mob.alive or mob.bouncing): self.sound.play_sfx(self.sound.stomp) self.rect.bottom = mob.rect.top self.bounce() self.killEntity(mob) elif collisionState.isTop and mob.alive and not mob.active: self.sound.play_sfx(self.sound.stomp) self.rect.bottom = mob.rect.top mob.timer = 0 self.bounce() mob.alive = False elif collisionState.isColliding and mob.alive and not mob.active and not mob.bouncing: mob.bouncing = True if mob.rect.x < self.rect.x: mob.leftrightTrait.direction = -1 mob.rect.x += -5 self.sound.play_sfx(self.sound.kick) else: mob.rect.x += 5 mob.leftrightTrait.direction = 1 self.sound.play_sfx(self.sound.kick) elif collisionState.isColliding and mob.alive and not self.invincibilityFrames: if self.powerUpState == 0: self.gameOver() elif self.powerUpState == 1: self.powerUpState = 0 self.traits['goTrait'].updateAnimation(smallAnimation) x, y = self.rect.x, self.rect.y self.rect = pygame.Rect(x, y + 32, 32, 32) self.invincibilityFrames = 60 self.sound.play_sfx(self.sound.pipe) def bounce(self): self.traits["bounceTrait"].jump = True def killEntity(self, ent): if ent.__class__.__name__ != "Koopa": ent.alive = False else: ent.timer = 0 ent.leftrightTrait.speed = 1 ent.alive = True ent.active = False ent.bouncing = False self.dashboard.points += 100 def gameOver(self): srf = pygame.Surface((640, 480)) srf.set_colorkey((255, 255, 255), pygame.RLEACCEL) srf.set_alpha(128) self.sound.music_channel.stop() self.sound.music_channel.play(self.sound.death) for i in range(500, 20, -2): srf.fill((0, 0, 0)) pygame.draw.circle( srf, (255, 255, 255), (int(self.camera.x + self.rect.x) + 16, self.rect.y + 16), i, ) self.screen.blit(srf, (0, 0)) pygame.display.update() self.input.checkForInput() while self.sound.music_channel.get_busy(): pygame.display.update() self.input.checkForInput() self.restart = True def getPos(self): return self.camera.x + self.rect.x, self.rect.y def setPos(self, x, y): self.rect.x = x self.rect.y = y def powerup(self, powerupID): if self.powerUpState == 0: if powerupID == 1: self.powerUpState = 1 self.traits['goTrait'].updateAnimation(bigAnimation) self.rect = pygame.Rect(self.rect.x, self.rect.y-32, 32, 64) self.invincibilityFrames = 20
class MarioHeadless(EntityBase): def __init__(self, x, y, level, gravity=1.25): super(MarioHeadless, self).__init__(x, y, gravity) self.input = Input(self) self.traits = { "jumpTrait": jumpTrait(self), "goTrait": goTraitHeadless(self), "bounceTrait": bounceTrait(self) } self.levelObj = level self.collision = Collider(self, level) self.EntityCollider = EntityCollider(self) self.restart = False def update(self): self.updateTraits() self.moveMario() self.applyGravity() self.checkEntityCollision() def moveMario(self): self.rect.y += self.vel.y self.collision.checkY() self.rect.x += self.vel.x self.collision.checkX() def checkEntityCollision(self): for ent in self.levelObj.entityList: collisionState = self.EntityCollider.check(ent) if collisionState.isColliding: if (ent.type == "Item"): self._onCollisionWithItem(ent) elif (ent.type == "Block"): self._onCollisionWithBlock(ent) elif (ent.type == "Mob"): self._onCollisionWithMob(ent, collisionState) def _onCollisionWithItem(self, item): self.levelObj.entityList.remove(item) self.levelObj.points += 100 self.levelObj.coins += 1 def _onCollisionWithBlock(self, block): block.triggered = True def _onCollisionWithMob(self, mob, collisionState): if collisionState.isTop and (mob.alive is True or mob.alive == "shellBouncing"): self.rect.bottom = mob.rect.top self.bounce() self.killEntity(mob) elif collisionState.isTop and mob.alive == "sleeping": self.rect.bottom = mob.rect.top mob.timer = 0 self.bounce() mob.alive = False elif collisionState.isTop and mob.alive == "sleeping": if (mob.rect.x < self.rect.x): mob.leftrightTrait.direction = -1 else: mob.leftrightTrait.direction = 1 mob.alive = "shellBouncing" elif collisionState.isColliding and mob.alive == True: self.gameOver() def bounce(self): self.traits['bounceTrait'].jump = True def killEntity(self, ent): if ent.__class__.__name__ != "Koopa": ent.alive = False else: ent.timer = 0 ent.alive = "sleeping" self.levelObj.points += -1000 def gameOver(self): self.restart = True def getPos(self): return (self.rect.x, self.rect.y) def doRandomMove(self): moves = ['moveLeft', 'moveRight', 'jump', 'doNothing'] random_move = random.choice(moves) if random_move == 'moveLeft': self.traits['goTrait'].direction = -1 elif random_move == 'moveRight': self.traits['goTrait'].direction = 1 elif random_move == 'jump': self.traits['jumpTrait'].start() elif random_move == 'doNothing': self.traits['goTrait'].direction = 0
def __init__(self, entity, level): self.direction = random.choice([-1, 1]) self.entity = entity self.collDetection = Collider(self.entity, level) self.speed = 1 self.entity.vel.x = self.speed * self.direction