Skip to content

Commit

Permalink
Merge pull request #96 from PeanutbutterWarrior/Add-red-mushroom
Browse files Browse the repository at this point in the history
Add red mushroom
  • Loading branch information
mx0c authored Nov 1, 2020
2 parents 4fbf81f + 0dfbca2 commit 326dd86
Show file tree
Hide file tree
Showing 15 changed files with 296 additions and 38 deletions.
2 changes: 2 additions & 0 deletions classes/Collider.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def checkX(self):
rows = [
self.level[self.entity.getPosIndex().y],
self.level[self.entity.getPosIndex().y + 1],
self.level[self.entity.getPosIndex().y + 2],
]
except Exception:
return
Expand All @@ -34,6 +35,7 @@ def checkY(self):
rows = [
self.level[self.entity.getPosIndex().y],
self.level[self.entity.getPosIndex().y + 1],
self.level[self.entity.getPosIndex().y + 2],
]
except Exception:
try:
Expand Down
13 changes: 8 additions & 5 deletions classes/Input.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ def checkForKeyboardInput(self):
pressedKeys = pygame.key.get_pressed()

if pressedKeys[K_LEFT] or pressedKeys[K_h] and not pressedKeys[K_RIGHT]:
self.entity.traits["GoTrait"].direction = -1
self.entity.traits["goTrait"].direction = -1
elif pressedKeys[K_RIGHT] or pressedKeys[K_l] and not pressedKeys[K_LEFT]:
self.entity.traits["GoTrait"].direction = 1
self.entity.traits["goTrait"].direction = 1
else:
self.entity.traits['GoTrait'].direction = 0
self.entity.traits['goTrait'].direction = 0

isJumping = pressedKeys[K_SPACE] or pressedKeys[K_UP] or pressedKeys[K_k]
self.entity.traits['JumpTrait'].jump(isJumping)
self.entity.traits['jumpTrait'].jump(isJumping)

self.entity.traits['GoTrait'].boost = pressedKeys[K_LSHIFT]
self.entity.traits['goTrait'].boost = pressedKeys[K_LSHIFT]

def checkForMouseInput(self, events):
mouseX, mouseY = pygame.mouse.get_pos()
Expand All @@ -39,6 +39,9 @@ def checkForMouseInput(self, events):
self.entity.levelObj.addGoomba(
mouseY / 32, mouseX / 32 - self.entity.camera.pos.x
)
self.entity.levelObj.addRedMushroom(
mouseY / 32, mouseX / 32 - self.entity.camera.pos.x
)
if self.isLeftMouseButtonPressed(events):
self.entity.levelObj.addCoin(
mouseX / 32 - self.entity.camera.pos.x, mouseY / 32
Expand Down
27 changes: 25 additions & 2 deletions classes/Level.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
from entities.Coin import Coin
from entities.CoinBrick import CoinBrick
from entities.Goomba import Goomba
from entities.Mushroom import RedMushroom
from entities.Koopa import Koopa
from entities.CoinBox import CoinBox
from entities.RandomBox import RandomBox


Expand All @@ -30,11 +32,12 @@ def loadLevel(self, levelname):

def loadEntities(self, data):
try:
[self.addRandomBox(x, y) for x, y in data["level"]["entities"]["randomBox"]]
[self.addCoinBox(x, y) for x, y in data["level"]["entities"]["CoinBox"]]
[self.addGoomba(x, y) for x, y in data["level"]["entities"]["Goomba"]]
[self.addKoopa(x, y) for x, y in data["level"]["entities"]["Koopa"]]
[self.addCoin(x, y) for x, y in data["level"]["entities"]["coin"]]
[self.addCoinBrick(x, y) for x, y in data["level"]["entities"]["coinBrick"]]
[self.addRandomBox(x, y, item) for x, y, item in data["level"]["entities"]["RandomBox"]]
except:
# if no entities in Level
pass
Expand Down Expand Up @@ -146,16 +149,31 @@ def addBushSprite(self, x, y):
except IndexError:
return

def addRandomBox(self, x, y):
def addCoinBox(self, x, y):
self.level[y][x] = Tile(None, pygame.Rect(x * 32, y * 32 - 1, 32, 32))
self.entityList.append(
CoinBox(
self.screen,
self.sprites.spriteCollection,
x,
y,
self.sound,
self.dashboard,
)
)

def addRandomBox(self, x, y, item):
self.level[y][x] = Tile(None, pygame.Rect(x * 32, y * 32 - 1, 32, 32))
self.entityList.append(
RandomBox(
self.screen,
self.sprites.spriteCollection,
x,
y,
item,
self.sound,
self.dashboard,
self
)
)

Expand Down Expand Up @@ -184,3 +202,8 @@ def addKoopa(self, x, y):
self.entityList.append(
Koopa(self.screen, self.sprites.spriteCollection, x, y, self, self.sound)
)

def addRedMushroom(self, x, y):
self.entityList.append(
RedMushroom(self.screen, self.sprites.spriteCollection, x, y, self, self.sound)
)
2 changes: 2 additions & 0 deletions classes/Sound.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def __init__(self):
self.death = mixer.Sound("./sfx/death.wav")
self.kick = mixer.Sound("./sfx/kick.ogg")
self.brick_bump = mixer.Sound("./sfx/brick-bump.ogg")
self.powerup = mixer.Sound('./sfx/powerup.ogg')
self.powerup_appear = mixer.Sound('./sfx/powerup_appears.ogg')

def play_sfx(self, sfx):
if self.allowSFX:
Expand Down
10 changes: 8 additions & 2 deletions classes/Sprites.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def __init__(self):
"./sprites/Animations.json",
"./sprites/BackgroundSprites.json",
"./sprites/ItemAnimations.json",
"./sprites/RedMushroom.json"
]
)

Expand Down Expand Up @@ -69,15 +70,20 @@ def loadSprites(self, urlList):
colorkey = sprite["colorKey"]
except KeyError:
colorkey = None
try:
xSize = sprite['xsize']
ySize = sprite['ysize']
except KeyError:
xSize, ySize = data['size']
dic[sprite["name"]] = Sprite(
mySpritesheet.image_at(
sprite["x"],
sprite["y"],
sprite["scalefactor"],
colorkey,
True,
xTileSize=data["size"][0],
yTileSize=data["size"][1],
xTileSize=xSize,
yTileSize=ySize,
),
sprite["collision"],
)
Expand Down
6 changes: 2 additions & 4 deletions classes/Spritesheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ class Spritesheet(object):
def __init__(self, filename):
try:
self.sheet = pygame.image.load(filename)
if self.sheet.get_alpha():
self.sheet = self.sheet.convert_alpha()
else:
self.sheet = self.sheet.convert()
self.sheet = pygame.image.load(filename)
if not self.sheet.get_alpha():
self.sheet.set_colorkey((0, 0, 0))
except pygame.error:
print("Unable to load spritesheet image:", filename)
Expand Down
39 changes: 39 additions & 0 deletions entities/CoinBox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from copy import copy

from entities.EntityBase import EntityBase
from entities.Item import Item


class CoinBox(EntityBase):
def __init__(self, screen, spriteCollection, x, y, sound, dashboard, gravity=0):
super(CoinBox, self).__init__(x, y, gravity)
self.screen = screen
self.spriteCollection = spriteCollection
self.animation = copy(self.spriteCollection.get("CoinBox").animation)
self.type = "Block"
self.triggered = False
self.time = 0
self.maxTime = 10
self.sound = sound
self.dashboard = dashboard
self.vel = 1
self.item = Item(spriteCollection, screen, self.rect.x, self.rect.y)

def update(self, cam):
if self.alive and not self.triggered:
self.animation.update()
else:
self.animation.image = self.spriteCollection.get("empty").image
self.item.spawnCoin(cam, self.sound, self.dashboard)
if self.time < self.maxTime:
self.time += 1
self.rect.y -= self.vel
else:
if self.time < self.maxTime * 2:
self.time += 1
self.rect.y += self.vel
self.screen.blit(
self.spriteCollection.get("sky").image,
(self.rect.x + cam.x, self.rect.y + 2),
)
self.screen.blit(self.animation.image, (self.rect.x + cam.x, self.rect.y - 1))
65 changes: 49 additions & 16 deletions entities/Mario.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,46 @@
from classes.Input import Input
from classes.Sprites import Sprites
from entities.EntityBase import EntityBase
from entities.Mushroom import RedMushroom
from traits.bounce import bounceTrait
from traits.go import GoTrait
from traits.jump import JumpTrait
from classes.Pause import Pause

spriteCollection = Sprites().spriteCollection
smallAnimation = Animation(
[
spriteCollection["mario_run1"].image,
spriteCollection["mario_run2"].image,
spriteCollection["mario_run3"].image,
],
spriteCollection["mario_idle"].image,
spriteCollection["mario_jump"].image,
)
bigAnimation = Animation(
[
spriteCollection["mario_big_run1"].image,
spriteCollection["mario_big_run2"].image,
spriteCollection["mario_big_run3"].image,
],
spriteCollection["mario_big_idle"].image,
spriteCollection["mario_big_jump"].image,
)


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.powerUpState = 0
self.invincibilityFrames = 0
self.traits = {
"JumpTrait": JumpTrait(self),
"GoTrait": GoTrait(self.animation, screen, self.camera, self),
"jumpTrait": JumpTrait(self),
"goTrait": GoTrait(smallAnimation, screen, self.camera, self),
"bounceTrait": bounceTrait(self),
}

Expand All @@ -48,6 +60,8 @@ def __init__(self, x, y, level, screen, dashboard, sound, gravity=0.75):
self.pauseObj = Pause(screen, self, dashboard)

def update(self):
if self.invincibilityFrames > 0:
self.invincibilityFrames -= 1
self.updateTraits()
self.moveMario()
self.camera.move()
Expand Down Expand Up @@ -85,7 +99,11 @@ def _onCollisionWithBlock(self, block):
block.triggered = True

def _onCollisionWithMob(self, mob, collisionState):
if collisionState.isTop and (mob.alive or mob.alive == "shellBouncing"):
if isinstance(mob, RedMushroom):
self.powerup(1)
self.killEntity(mob)
self.sound.play_sfx(self.sound.powerup)
elif collisionState.isTop and (mob.alive or mob.alive == "shellBouncing"):
self.sound.play_sfx(self.sound.stomp)
self.rect.bottom = mob.rect.top
self.bounce()
Expand All @@ -106,8 +124,15 @@ def _onCollisionWithMob(self, mob, collisionState):
mob.leftrightTrait.direction = 1
self.sound.play_sfx(self.sound.kick)
mob.alive = "shellBouncing"
elif collisionState.isColliding and mob.alive:
self.gameOver()
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

def bounce(self):
self.traits["bounceTrait"].jump = True
Expand Down Expand Up @@ -149,3 +174,11 @@ def getPos(self):
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
66 changes: 66 additions & 0 deletions entities/Mushroom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from classes.Animation import Animation
from classes.Maths import Vec2D
from entities.EntityBase import EntityBase
from traits.leftrightwalk import LeftRightWalkTrait
from classes.Collider import Collider
from classes.EntityCollider import EntityCollider


class RedMushroom(EntityBase):
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

def update(self, camera):
if self.alive:
self.applyGravity()
self.drawRedMushroom(camera)
self.leftrightTrait.update()
self.checkEntityCollision()
else:
self.onDead(camera)

def drawRedMushroom(self, camera):
self.screen.blit(self.animation.image, (self.rect.x + camera.x, self.rect.y))
self.animation.update()

def onDead(self, camera):
if self.timer == 0:
self.setPointsTextStartPosition(self.rect.x + 3, self.rect.y)
if self.timer < self.timeAfterDeath:
self.movePointsTextUpAndDraw(camera)
else:
self.alive = None
self.timer += 0.1

def setPointsTextStartPosition(self, x, y):
self.textPos = Vec2D(x, y)

def movePointsTextUpAndDraw(self, camera):
self.textPos.y += -0.5
self.dashboard.drawText("100", self.textPos.x + camera.x, self.textPos.y, 8)

def checkEntityCollision(self):
for ent in self.levelObj.entityList:
collisionState = self.EntityCollider.check(ent)
if collisionState.isColliding:
if ent.type == "Mob":
self._onCollisionWithMob(ent, collisionState)

def _onCollisionWithMob(self, mob, collisionState):
if collisionState.isColliding and mob.alive == "shellBouncing":
self.alive = False
self.sound.play_sfx(self.sound.brick_bump)
Loading

0 comments on commit 326dd86

Please sign in to comment.