diff --git a/classes/Camera.py b/classes/Camera.py index e05f64a8..30cf13cc 100644 --- a/classes/Camera.py +++ b/classes/Camera.py @@ -1,9 +1,9 @@ -from classes.Maths import vec2D +from classes.Maths import Vec2D class Camera: def __init__(self, pos, entity): - self.pos = vec2D(pos.x, pos.y) + self.pos = Vec2D(pos.x, pos.y) self.entity = entity self.x = self.pos.x * 32 self.y = self.pos.y * 32 diff --git a/classes/Collider.py b/classes/Collider.py index f7eb3092..86395347 100644 --- a/classes/Collider.py +++ b/classes/Collider.py @@ -52,8 +52,8 @@ def checkY(self): self.entity.vel.y = 0 # reset jump on bottom if self.entity.traits is not None: - if "jumpTrait" in self.entity.traits: - self.entity.traits["jumpTrait"].reset() + if "JumpTrait" in self.entity.traits: + self.entity.traits["JumpTrait"].reset() if "bounceTrait" in self.entity.traits: self.entity.traits["bounceTrait"].reset() if self.entity.vel.y < 0: diff --git a/classes/Font.py b/classes/Font.py index 5ca28d13..f775e864 100644 --- a/classes/Font.py +++ b/classes/Font.py @@ -24,8 +24,8 @@ def loadFont(self): row, 2, colorkey=pygame.color.Color(0, 0, 0), - xTileSize = 8, - yTileSize = 8 + xTileSize=8, + yTileSize=8 ) } ) diff --git a/classes/GaussianBlur.py b/classes/GaussianBlur.py index 9de5ec34..c2bb2173 100644 --- a/classes/GaussianBlur.py +++ b/classes/GaussianBlur.py @@ -1,14 +1,15 @@ import pygame from scipy.ndimage.filters import * + class GaussianBlur: - def __init__(self,kernelsize=7): + def __init__(self, kernelsize=7): self.kernel_size = kernelsize def filter(self, srfc, xpos, ypos, width, height): - nSrfc = pygame.Surface((width,height)) + nSrfc = pygame.Surface((width, height)) pxa = pygame.surfarray.array3d(srfc) - blurred = gaussian_filter(pxa,sigma=(self.kernel_size, self.kernel_size, 0)) + blurred = gaussian_filter(pxa, sigma=(self.kernel_size, self.kernel_size, 0)) pygame.surfarray.blit_array(nSrfc, blurred) del pxa return nSrfc diff --git a/classes/Input.py b/classes/Input.py index e8aa4ffe..935b31ec 100644 --- a/classes/Input.py +++ b/classes/Input.py @@ -19,18 +19,18 @@ 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): + def checkForMouseInput(self, events): mouseX, mouseY = pygame.mouse.get_pos() if self.isRightMouseButtonPressed(events): self.entity.levelObj.addKoopa( @@ -44,7 +44,7 @@ def checkForMouseInput(self,events): mouseX / 32 - self.entity.camera.pos.x, mouseY / 32 ) - def checkForQuitAndRestartInputEvents(self,events): + def checkForQuitAndRestartInputEvents(self, events): for event in events: if event.type == pygame.QUIT: pygame.quit() @@ -55,20 +55,13 @@ def checkForQuitAndRestartInputEvents(self,events): self.entity.pauseObj.createBackgroundBlur() def isLeftMouseButtonPressed(self, events): - return self.checkMouse(events,1) - - + return self.checkMouse(events, 1) def isRightMouseButtonPressed(self, events): - return self.checkMouse(events,3) - + return self.checkMouse(events, 3) def checkMouse(self, events, button): for e in events: - if e.type == pygame.MOUSEBUTTONUP: - if e.button == button: - return True - else: - return False - - + if e.type == pygame.MOUSEBUTTONUP and e.button == button: + return True + return False diff --git a/classes/Level.py b/classes/Level.py index fdbe22db..b5933fbc 100644 --- a/classes/Level.py +++ b/classes/Level.py @@ -4,10 +4,11 @@ from classes.Sprites import Sprites from classes.Tile import Tile from entities.Coin import Coin +from entities.CoinBrick import CoinBrick from entities.Goomba import Goomba from entities.Koopa import Koopa from entities.RandomBox import RandomBox -from entities.CoinBrick import CoinBrick + class Level: def __init__(self, screen, sound, dashboard): @@ -111,7 +112,7 @@ def addCloudSprite(self, x, y): def addPipeSprite(self, x, y, length=2): try: - # add Pipe Head + # add pipe head self.level[y][x] = Tile( self.sprites.spriteCollection.get("pipeL"), pygame.Rect(x * 32, y * 32, 32, 32), @@ -120,7 +121,7 @@ def addPipeSprite(self, x, y, length=2): self.sprites.spriteCollection.get("pipeR"), pygame.Rect((x + 1) * 32, y * 32, 32, 32), ) - # add pipe Body + # add pipe body for i in range(1, length + 20): self.level[y + i][x] = Tile( self.sprites.spriteCollection.get("pipe2L"), diff --git a/classes/Maths.py b/classes/Maths.py index fc0b6d3a..b669b34f 100644 --- a/classes/Maths.py +++ b/classes/Maths.py @@ -1,4 +1,4 @@ -class vec2D: +class Vec2D: def __init__(self, x=0, y=0): self.x = x self.y = y diff --git a/classes/Menu.py b/classes/Menu.py index 9ae3b592..57d8f687 100644 --- a/classes/Menu.py +++ b/classes/Menu.py @@ -35,7 +35,7 @@ def __init__(self, screen, dashboard, level, sound): 0, 150, 2, colorkey=[255, 0, 220], ignoreTileSize=True ) self.menu_dot2 = self.spritesheet.image_at( - 20, 150, 2, colorkey=[255, 0, 220], ignoreTileSize=True + 20, 150, 2, colorkey=[255, 0, 220], ignoreTileSize=True ) self.loadSettings("./settings.json") @@ -98,7 +98,7 @@ def drawMenu(self): self.dashboard.drawText("SETTINGS", 180, 320, 24) self.dashboard.drawText("EXIT", 180, 360, 24) - def drawMenuBackground(self,withBanner=True): + def drawMenuBackground(self, withBanner=True): for y in range(0, 13): for x in range(0, 20): self.screen.blit( @@ -111,7 +111,7 @@ def drawMenuBackground(self,withBanner=True): self.level.sprites.spriteCollection.get("ground").image, (x * 32, y * 32), ) - if(withBanner): + if withBanner: self.screen.blit(self.menu_banner, (150, 80)) self.screen.blit( self.level.sprites.spriteCollection.get("mario_idle").image, @@ -132,7 +132,7 @@ def drawMenuBackground(self,withBanner=True): self.screen.blit( self.level.sprites.spriteCollection.get("bush_3").image, (18 * 32, 12 * 32) ) - self.screen.blit(self.level.sprites.spriteCollection.get("goomba-1").image,(18.5*32,12*32)) + self.screen.blit(self.level.sprites.spriteCollection.get("goomba-1").image, (18.5*32, 12*32)) def drawSettings(self): self.drawDot() @@ -154,11 +154,11 @@ def chooseLevel(self): self.levelNames = self.loadLevelNames() self.drawLevelChooser() - def drawBorder(self,x,y,width,height,color,thickness): - pygame.draw.rect(self.screen,color,(x,y,width,thickness)) - pygame.draw.rect(self.screen,color,(x,y+width,width,thickness)) - pygame.draw.rect(self.screen,color,(x,y,thickness,width)) - pygame.draw.rect(self.screen,color,(x+width,y,thickness,width+thickness)) + def drawBorder(self, x, y, width, height, color, thickness): + pygame.draw.rect(self.screen, color, (x, y, width, thickness)) + pygame.draw.rect(self.screen, color, (x, y+width, width, thickness)) + pygame.draw.rect(self.screen, color, (x, y, thickness, width)) + pygame.draw.rect(self.screen, color, (x+width, y, thickness, width+thickness)) def drawLevelChooser(self): j = 0 @@ -166,16 +166,16 @@ def drawLevelChooser(self): textOffset = 90 for i, levelName in enumerate(self.loadLevelNames()): if self.currSelectedLevel == i+1: - color = (255,255,255) + color = (255, 255, 255) else: - color = (150,150,150) + color = (150, 150, 150) if i < 3: - self.dashboard.drawText(levelName,175*i+textOffset,100,12) - self.drawBorder(175*i+offset,55,125,75,color,5) + self.dashboard.drawText(levelName, 175*i+textOffset, 100, 12) + self.drawBorder(175*i+offset, 55, 125, 75, color, 5) else: - self.dashboard.drawText(levelName,175*j+textOffset,250,12) - self.drawBorder(175*j+offset,210,125,75,color,5) - j+=1 + self.dashboard.drawText(levelName, 175*j+textOffset, 250, 12) + self.drawBorder(175*j+offset, 210, 125, 75, color, 5) + j += 1 def loadLevelNames(self): files = [] diff --git a/classes/Pause.py b/classes/Pause.py index 69b9b550..3905a88b 100644 --- a/classes/Pause.py +++ b/classes/Pause.py @@ -16,11 +16,11 @@ def __init__(self, screen, entity, dashboard): 0, 150, 2, colorkey=[255, 0, 220], ignoreTileSize=True ) self.gray_dot = self.spritesheet.image_at( - 20, 150, 2, colorkey=[255, 0, 220], ignoreTileSize=True + 20, 150, 2, colorkey=[255, 0, 220], ignoreTileSize=True ) def update(self): - self.screen.blit(self.pause_srfc,(0,0)) + self.screen.blit(self.pause_srfc, (0, 0)) self.dashboard.drawText("PAUSED", 120, 160, 68) self.dashboard.drawText("CONTINUE", 150, 280, 32) self.dashboard.drawText("BACK TO MENU", 150, 320, 32) diff --git a/classes/Spritesheet.py b/classes/Spritesheet.py index 9384b73c..1e7c4bf3 100644 --- a/classes/Spritesheet.py +++ b/classes/Spritesheet.py @@ -1,5 +1,6 @@ import pygame + class Spritesheet(object): def __init__(self, filename): try: diff --git a/compile.py b/compile.py index de8541c2..59d5df76 100644 --- a/compile.py +++ b/compile.py @@ -1,6 +1,7 @@ from distutils.core import setup -import py2exe, glob +import py2exe +import glob setup( # this is the file that is run when you start the game from the command line. diff --git a/entities/EntityBase.py b/entities/EntityBase.py index 58d7d445..4815fade 100644 --- a/entities/EntityBase.py +++ b/entities/EntityBase.py @@ -1,11 +1,11 @@ import pygame -from classes.Maths import vec2D +from classes.Maths import Vec2D class EntityBase(object): def __init__(self, x, y, gravity): - self.vel = vec2D() + self.vel = Vec2D() self.rect = pygame.Rect(x * 32, y * 32, 32, 32) self.gravity = gravity self.traits = None @@ -14,10 +14,10 @@ def __init__(self, x, y, gravity): self.timer = 0 self.type = "" self.onGround = False - self.obeygravity = True + self.obeyGravity = True def applyGravity(self): - if self.obeygravity: + if self.obeyGravity: self.vel.y += self.gravity def updateTraits(self): @@ -28,7 +28,7 @@ def updateTraits(self): pass def getPosIndex(self): - return vec2D(int(self.rect.x / 32), int(self.rect.y / 32)) + return Vec2D(int(self.rect.x / 32), int(self.rect.y / 32)) def getPosIndexAsFloat(self): - return vec2D(self.rect.x / 32.0, self.rect.y / 32.0) + return Vec2D(self.rect.x / 32.0, self.rect.y / 32.0) diff --git a/entities/Goomba.py b/entities/Goomba.py index 70824c33..15cd3ade 100644 --- a/entities/Goomba.py +++ b/entities/Goomba.py @@ -1,9 +1,10 @@ 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 +from classes.Maths import Vec2D +from entities.EntityBase import EntityBase +from traits.leftrightwalk import LeftRightWalkTrait + class Goomba(EntityBase): def __init__(self, screen, spriteColl, x, y, level, sound): @@ -23,6 +24,7 @@ def __init__(self, screen, spriteColl, x, y, level, sound): self.EntityCollider = EntityCollider(self) self.levelObj = level self.sound = sound + self.textPos = Vec2D(0, 0) def update(self, camera): if self.alive: @@ -54,7 +56,7 @@ def drawFlatGoomba(self, camera): ) def setPointsTextStartPosition(self, x, y): - self.textPos = vec2D(x, y) + self.textPos = Vec2D(x, y) def movePointsTextUpAndDraw(self, camera): self.textPos.y += -0.5 diff --git a/entities/Item.py b/entities/Item.py index 2977ac00..941aab89 100644 --- a/entities/Item.py +++ b/entities/Item.py @@ -1,14 +1,14 @@ from copy import copy from classes.Dashboard import Dashboard -from classes.Maths import vec2D +from classes.Maths import Vec2D class Item(Dashboard): def __init__(self, collection, screen, x, y): super(Item, self).__init__("./img/font.png", 8, screen) - self.ItemPos = vec2D(x, y) - self.itemVel = vec2D(0, 0) + self.ItemPos = Vec2D(x, y) + self.itemVel = Vec2D(0, 0) self.screen = screen self.coin_animation = copy(collection.get("coin-item").animation) self.sound_played = False diff --git a/entities/Koopa.py b/entities/Koopa.py index 410dbb1a..5c7372f4 100644 --- a/entities/Koopa.py +++ b/entities/Koopa.py @@ -1,11 +1,11 @@ import pygame 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 +from classes.Maths import Vec2D +from entities.EntityBase import EntityBase +from traits.leftrightwalk import LeftRightWalkTrait class Koopa(EntityBase): @@ -30,7 +30,7 @@ def __init__(self, screen, spriteColl, x, y, level, sound): self.sound = sound def update(self, camera): - if self.alive == True: + if self.alive: self.updateAlive(camera) self.checkEntityCollision() elif self.alive == "sleeping": @@ -38,7 +38,7 @@ def update(self, camera): self.checkEntityCollision() elif self.alive == "shellBouncing": self.shellBouncing(camera) - elif self.alive == False: + elif not self.alive: self.die(camera) def drawKoopa(self, camera): @@ -61,10 +61,10 @@ def shellBouncing(self, camera): def die(self, camera): if self.timer == 0: - self.textPos = vec2D(self.rect.x + 3, self.rect.y - 32) + self.textPos = Vec2D(self.rect.x + 3, self.rect.y - 32) if self.timer < self.timeAfterDeath: self.textPos.y += -0.5 - self.textPos = vec2D(self.rect.x + 3, self.rect.y - 32) + self.textPos = Vec2D(self.rect.x + 3, self.rect.y - 32) self.dashboard.drawText("100", self.textPos.x + camera.x, self.textPos.y, 8) self.vel.y -= 0.5 self.rect.y += self.vel.y @@ -113,4 +113,4 @@ def checkEntityCollision(self): def _onCollisionWithMob(self, mob, collisionState): if collisionState.isColliding and mob.alive == "shellBouncing": self.alive = False - self.sound.play_sfx(self.sound.brick_bump) \ No newline at end of file + self.sound.play_sfx(self.sound.brick_bump) diff --git a/entities/Mario.py b/entities/Mario.py index 4b37ca5f..1b2f7061 100644 --- a/entities/Mario.py +++ b/entities/Mario.py @@ -8,8 +8,8 @@ from classes.Sprites import Sprites from entities.EntityBase import EntityBase from traits.bounce import bounceTrait -from traits.go import goTrait -from traits.jump import jumpTrait +from traits.go import GoTrait +from traits.jump import JumpTrait from classes.Pause import Pause @@ -33,8 +33,8 @@ def __init__(self, x, y, level, screen, dashboard, sound, gravity=0.75): ) self.traits = { - "jumpTrait": jumpTrait(self), - "goTrait": goTrait(self.animation, screen, self.camera, self), + "JumpTrait": JumpTrait(self), + "GoTrait": GoTrait(self.animation, screen, self.camera, self), "bounceTrait": bounceTrait(self), } @@ -146,6 +146,6 @@ def gameOver(self): def getPos(self): return self.camera.x + self.rect.x, self.rect.y - def setPos(self,x,y): + def setPos(self, x, y): self.rect.x = x self.rect.y = y diff --git a/main.py b/main.py index 52e09e8d..7f59e5c1 100644 --- a/main.py +++ b/main.py @@ -6,7 +6,9 @@ from entities.Mario import Mario -windowSize = (640,480) +windowSize = (640, 480) + + def main(): pygame.mixer.pre_init(44100, -16, 2, 4096) pygame.init() diff --git a/traits/go.py b/traits/go.py index 08e9eeeb..ae6af604 100644 --- a/traits/go.py +++ b/traits/go.py @@ -1,7 +1,7 @@ from pygame.transform import flip -class goTrait: +class GoTrait: def __init__(self, animation, screen, camera, ent): self.animation = animation self.direction = 0 diff --git a/traits/jump.py b/traits/jump.py index 63531df0..208ffff6 100644 --- a/traits/jump.py +++ b/traits/jump.py @@ -1,29 +1,25 @@ - -class jumpTrait: +class JumpTrait: def __init__(self, entity): - self.vertical_speed = -12 #jump speed - self.jumpHeight = 120 #jump height in pixels + self.vertical_speed = -12 + self.jumpHeight = 120 self.entity = entity - self.initalHeight = 384 #stores the position of mario at jump + self.initalHeight = 384 self.deaccelerationHeight = self.jumpHeight - ((self.vertical_speed*self.vertical_speed)/(2*self.entity.gravity)) - def jump(self,jumping): - #print(self.entity.rect.y) + def jump(self, jumping): if jumping: - if not self.entity.inAir and not self.entity.inJump: #only jump when mario is on ground and not in a jump. redundant check + if not self.entity.inAir and not self.entity.inJump: # redundant check self.entity.sound.play_sfx(self.entity.sound.jump) self.entity.vel.y = self.vertical_speed self.entity.inAir = True self.initalHeight = self.entity.rect.y self.entity.inJump = True - self.entity.obeygravity = False #dont obey gravity in jump so as to reach jump height no matter what the speed + self.entity.obeyGravity = False # always reach maximum height - if self.entity.inJump: #check vertical distance travelled while mario is in a jump - if (self.initalHeight-self.entity.rect.y) >= self.deaccelerationHeight or self.entity.vel.y==0: - print("reached") + if self.entity.inJump: + if (self.initalHeight-self.entity.rect.y) >= self.deaccelerationHeight or self.entity.vel.y == 0: self.entity.inJump = False - self.entity.obeygravity = True #mario obeys gravity again and continues normal play - + self.entity.obeyGravity = True def reset(self): self.entity.inAir = False