Skip to content

Commit

Permalink
Allow mushroom to come out of box
Browse files Browse the repository at this point in the history
Renamed RandomBox to Coin Box and RandomBox now creates items
  • Loading branch information
PeanutbutterWarrior authored and PeanutbutterWarrior committed Oct 29, 2020
1 parent bdad528 commit be4c55f
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 17 deletions.
1 change: 1 addition & 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 Down
10 changes: 5 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 Down
21 changes: 19 additions & 2 deletions classes/Level.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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 @@ -31,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 @@ -147,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
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))
4 changes: 2 additions & 2 deletions entities/Mushroom.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from classes.Animation import Animation
from classes.Maths import vec2D
from classes.Maths import Vec2D
from entities.EntityBase import EntityBase
from traits.leftrightwalk import LeftRightWalkTrait
from classes.Collider import Collider
Expand Down Expand Up @@ -47,7 +47,7 @@ def onDead(self, camera):
self.timer += 0.1

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

def movePointsTextUpAndDraw(self, camera):
self.textPos.y += -0.5
Expand Down
12 changes: 7 additions & 5 deletions entities/RandomBox.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
from copy import copy

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


class RandomBox(EntityBase):
def __init__(self, screen, spriteCollection, x, y, sound, dashboard, gravity=0):
def __init__(self, screen, spriteCollection, x, y, item, sound, dashboard, level, gravity=0):
super(RandomBox, self).__init__(x, y, gravity)
self.screen = screen
self.spriteCollection = spriteCollection
self.animation = copy(self.spriteCollection.get("randomBox").animation)
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)
self.item = item
self.level = level

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.item == 'RedMushroom':
self.level.addRedMushroom(self.rect.y // 32 - 1, self.rect.x // 32)
self.item = None
if self.time < self.maxTime:
self.time += 1
self.rect.y -= self.vel
Expand Down
7 changes: 5 additions & 2 deletions levels/Level1-1.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@
}
},
"entities": {
"randomBox":[
"CoinBox":[
[4,8],
[4,3],
[42,5],
[56,2]
],
Expand Down Expand Up @@ -111,6 +110,10 @@
[12,22],
[12,33],
[12,40]
],
"RandomBox":[
[4, 3, "RedMushroom"],
[52, 2, "RedMushroom"]
]
}

Expand Down
2 changes: 1 addition & 1 deletion sprites/Animations.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type":"animation",
"sprites":[
{
"name":"randomBox",
"name":"CoinBox",
"images":[
{
"x":24,
Expand Down

0 comments on commit be4c55f

Please sign in to comment.