diff --git a/src/enemies.py b/src/enemies.py index 85723ea..b62b054 100644 --- a/src/enemies.py +++ b/src/enemies.py @@ -188,6 +188,12 @@ def kickFall(self, state, kicksMain= 0, time=0): self.mX = 0 self.timesKicked += 1 + elif self.enemy == "Fly": + self.currentSetFrames = self.fallenFrames + self.timeFlipped = time + self.isFlipped = True + self.mX = 0 + elif state == "fall" and not self.isDed: # Create movement self.velY = 6 @@ -218,6 +224,7 @@ def __init__(self, enemy, collideX, collideY): self.fallenFramesAngry = [[96, 128, 16, 16], [112, 128, 16, 16]] self.currentSetFrames = self.movingFramesNormal + self.value = 100 class Crab(Enemies): @@ -230,52 +237,94 @@ def __init__(self, enemy, collideX, collideY): self.fallenFrames = [[152, 40, 16, 16], [168, 40, 16, 16]] self.currentSetFrames = self.movingFramesNormal - - # TEMPORAL - self.currframe = self.currentSetFrames[0] + self.value = 200 class Fly(Enemies): def __init__(self, enemy, collideX, collideY): super().__init__(enemy, collideX, collideY) - self.movingFrames = [[48, 56, 16, 16], [32, 56, 16, 16], [48, 56, 16, 16], [0, 56, 16, 16]] + self.movingFrames = [[48, 56, 16, 16], [0, 56, 16, 16]] self.fallenFrames = [[64, 56, 16, 16], [80, 56, 16, 16]] self.currentSetFrames = self.movingFrames self.jumping = False + self.mX = 2 + + self.value = 300 - """ def movement(self, dimX, platforms): - if pyxel.frame_count // 7 and not self.jumping: + if pyxel.frame_count % 20 == 0 and not self.jumping: self.velY = 8 self.jumping = True - if self.jumping: - self.posX += self.mX * self.direction + if not self.isFlipped: + if self.jumping: + if not self.checkIsParallel(platforms): + self.posX += self.mX * self.direction + self.currframe = self.movingFrames[1] - # Make the gravity - if not self.isFalling: - if self.checkIsUnder(platforms): - self.isFalling = True - self.velY = 0 + if 0 < self.posX < dimX: + # Make the gravity + if not self.isFalling: + if self.checkIsUnder(platforms): + self.isFalling = True + self.velY = 0 - else: - self.posY -= self.velY - self.velY -= 1 + else: + self.posY -= self.velY + self.velY -= 1 - elif self.isFalling: - if self.isOver: - self.posY = self.currPlat.positionY - self.collideY + if self.velY <= 0: + self.isFalling = True + self.velY = 0 + + elif self.isFalling: + if self.isOver: + self.posY = self.currPlat.positionY - self.collideY + self.isFalling = False + self.jumping = False + self.velY = 0 + + else: + if self.velY < 8: + self.velY += 1 + self.posY += self.velY + + elif not self.jumping: + self.currframe = self.movingFrames[0] + + if (self.posX <= 3 or self.posX >= (dimX - 6)) and not self.isDed: + # If the enemy is in the lower part of the screen, we teleport them up to the pipe + if self.posY < 144: self.isFalling = False - self.jumping = False - self.velY = 0 + self.isOver = True - else: - if self.velY < 8: - self.velY += 1 - self.posY += self.velY - """ + if self.posX >= (dimX + 20): + self.posX = 0-20 + elif self.posX <= 0-20: + self.posX = dimX + 20 + + elif self.posY > 144: + self.posY = 24 + self.direction *= 0-1 + self.isSpawning = True + + if self.posX > dimX: + self.posX = 230 + elif self.posX < 0: + self.posX = 10 + + elif self.isFlipped: + self.jumping = False + self.currframe = self.currentSetFrames[self.currentPhaseFrame] + self.currentPhaseFrame = 0 + + if pyxel.frame_count % 4 == 0: + if self.currentPhaseFrame == 1: + self.currentPhaseFrame = 0 + elif self.currentPhaseFrame != 1: + self.currentPhaseFrame += 1 def checkIsUnder(self, currplatform): for a, i in enumerate(currplatform): @@ -287,3 +336,15 @@ def checkIsUnder(self, currplatform): (i.positionX + i.width): self.posY = i.positionY + i.height return True + + def checkIsParallel(self, currplatforms): + for i in currplatforms: + if self.posY < i.positionY < (self.posY + self.collideY) or \ + self.posY < (i.positionY + i.height) < \ + (self.posY + self.collideY): + if (i.positionX + i.width - 2) <= self.posX <= \ + (i.positionX + i.width + 2) or \ + (i.positionX + 2) >= (self.posX + self.collideX) >= (i.positionX - 2): + return True + + return False diff --git a/src/main.py b/src/main.py index 1b05993..da94063 100644 --- a/src/main.py +++ b/src/main.py @@ -56,8 +56,8 @@ def __init__(self, dimx, dimy, mario: object, lives: object, screens: list, enem # First we initialize the objects that are going to be used inside the level self.currlv, self.lvType = 0, "block" self.currplatforms = self.screens[self.currlv].platforms - self.currenemies = self.enemies[self.currlv] self.currpipes = self.screens[self.currlv].pipes + self.currenemies = self.enemies[self.currlv] self.timesKickedPlatforms = 0 # Then set the location of the file with the top score @@ -65,29 +65,6 @@ def __init__(self, dimx, dimy, mario: object, lives: object, screens: list, enem with open(self.locationTopScore, "r") as file: self.score, self.topScore = 0, json.load(file)["topscore"] - # We change the spawn of every enemy inside the pipes - try: - for i, a in enumerate(self.currenemies): - # If the index is even, we spawn him in the left - if i % 2 == 0: - a.posX = self.currpipes[0][0] - a.direction = 1 - - # But if its odd, we put him in the right - elif i % 2 != 0: - a.posX = self.currpipes[1][0] - a.direction = 0 - 1 - - # And change the height for all of them - a.posY = self.currpipes[0][1] - - # If there are not 2 pipes in the level, we put everyone to spawn at the only one - # (Not used but if there was, they would spawn correctly) - except IndexError: - for a in self.currenemies: - a.posX = self.currpipes[0][0] - a.posY = self.currpipes[0][1] - # And initialize the first enemy self.activenemies = [] self.activeCoins, self.paceCoins = [], 1 @@ -97,6 +74,53 @@ def __init__(self, dimx, dimy, mario: object, lives: object, screens: list, enem # Finally we run the program pyxel.run(self.update, self.draw) + # We just used setters for the needed variables. For example, we didn't consider + # necessary setters for the objects and certain arrays. + @property + def dimX(self): + return self.__dimX + + @dimX.setter + def dimX(self, dimX): + if dimX > 500: + raise ValueError("Dimension X too big") + elif dimX < 500: + self.__dimX = dimX + + @property + def dimY(self): + return self.__dimY + + @dimY.setter + def dimY(self, dimY): + if dimY > 500: + raise ValueError("Dimension Y too big") + elif dimY < 500: + self.__dimY = dimY + + @property + def currenemies(self): + return self.__currenemies + + @currenemies.setter + def currenemies(self, setenemies): + self.__currenemies = setenemies + + # We change the spawn of every enemy inside the pipes + for i, a in enumerate(self.__currenemies): + # If the index is even, we spawn him in the left + if i % 2 == 0: + a.posX = self.currpipes[0][0] + a.direction = 1 + + # But if its odd, we put him in the right + elif i % 2 != 0: + a.posX = self.currpipes[1][0] + a.direction = 0 - 1 + + # And change the height for all of them + a.posY = self.currpipes[0][1] + def update(self): try: # We look if mario is not dead or in the endgame @@ -203,7 +227,7 @@ def draw(self): pyxel.bltm(0, 0, 1, 0, 0, 240, 200, colkey=8) # Scores at the top - pyxel.text(60, 4, f"Score: {str(self.score)}" , 7) + pyxel.text(60, 4, f"Score: {str(self.score)}", 7) pyxel.text(120, 4, f"Top score: {str(self.topScore)}", 7) # This was created for the animation of the kick @@ -284,8 +308,7 @@ def enemiesCheck(self, i): i.movement(self.dimX) elif i == "Fly": - # i.movement(self.dimX, self.currplatforms) - i.movement(self.dimX) + i.movement(self.dimX, self.currplatforms) if i.isSpawning and (float(self.temptime) - float(i.timeSpawning)) >= 0.6: i.isSpawning = False @@ -391,13 +414,13 @@ def changeLv(self): levels.Platform(0, 192, 240, 8)], [[8, 24], [220, 24], [0, 180], [240, 180]]) -# enemies1 = [enemies.Fly("Fly", 16, 16)] -enemies1 = [enemies.Turtle("Turtle", 16, 16), - enemies.Turtle("Turtle", 16, 16), - enemies.Turtle("Turtle", 16, 16), - enemies.Crab("Crab", 16, 16), - enemies.Crab("Crab", 16, 16), - enemies.Crab("Crab", 16, 16)] +enemies1 = [enemies.Fly("Fly", 16, 16)] +#enemies1 = [enemies.Turtle("Turtle", 16, 16), +# enemies.Turtle("Turtle", 16, 16), +# enemies.Turtle("Turtle", 16, 16), +# enemies.Crab("Crab", 16, 16), +# enemies.Crab("Crab", 16, 16), +# enemies.Crab("Crab", 16, 16)] # enemies2 = [enemies.Fly("Fly", 16, 16)] enemies2 = [enemies.Turtle("Turtle", 16, 16),