diff --git a/pylletTown.py b/pylletTown.py index 4e0c54d..a457009 100644 --- a/pylletTown.py +++ b/pylletTown.py @@ -2,208 +2,208 @@ import tmx class Player(pygame.sprite.Sprite): - def __init__(self, location, orientation, *groups): - super(Player, self).__init__(*groups) - self.image = pygame.image.load('sprites/player.png') - self.imageDefault = self.image.copy() - self.rect = pygame.Rect(location, (64,64)) - self.orient = orientation - self.holdTime = 0 - self.walking = False - self.dx = 0 - self.step = 'rightFoot' - # Set default orientation - self.setSprite() - - def setSprite(self): - # Resets the player sprite sheet to its default position - # and scrolls it to the necessary position for the current orientation - self.image = self.imageDefault.copy() - if self.orient == 'up': - self.image.scroll(0, -64) - elif self.orient == 'down': - self.image.scroll(0, 0) - elif self.orient == 'left': - self.image.scroll(0, -128) - elif self.orient == 'right': - self.image.scroll(0, -192) - - def update(self, dt, game): - key = pygame.key.get_pressed() - # Setting orientation and sprite based on key input: - if key[pygame.K_UP]: - if not self.walking: - if self.orient != 'up': - self.orient = 'up' - self.setSprite() - self.holdTime += dt - elif key[pygame.K_DOWN]: - if not self.walking: - if self.orient != 'down': - self.orient = 'down' - self.setSprite() - self.holdTime += dt - elif key[pygame.K_LEFT]: - if not self.walking: - if self.orient != 'left': - self.orient = 'left' - self.setSprite() - self.holdTime += dt - elif key[pygame.K_RIGHT]: - if not self.walking: - if self.orient != 'right': - self.orient = 'right' - self.setSprite() - self.holdTime += dt - else: - self.holdTime = 0 - self.step = 'rightFoot' - # Walking mode enabled if a button is held for 0.1 seconds - if self.holdTime >= 100: - self.walking = True - lastRect = self.rect.copy() - # Walking at 8 pixels per frame in the direction the player is facing - if self.walking and self.dx < 64: - if self.orient == 'up': - self.rect.y -= 8 - elif self.orient == 'down': - self.rect.y += 8 - elif self.orient == 'left': - self.rect.x -= 8 - elif self.orient == 'right': - self.rect.x += 8 - self.dx += 8 - # Collision detection: - # Reset to the previous rectangle if player collides - # with anything in the foreground layer - if len(game.tilemap.layers['triggers'].collide(self.rect, - 'solid')) > 0: - self.rect = lastRect - # Area entry detection: - elif len(game.tilemap.layers['triggers'].collide(self.rect, - 'entry')) > 0: - entryCell = game.tilemap.layers['triggers'].find('entry')[0] - game.fadeOut() - game.initArea(entryCell['entry']) - - return - # Switch to the walking sprite after 32 pixels - if self.dx == 32: - # Self.step keeps track of when to flip the sprite so that - # the character appears to be taking steps with different feet. - if (self.orient == 'up' or - self.orient == 'down') and self.step == 'leftFoot': - self.image = pygame.transform.flip(self.image, True, False) - self.step = 'rightFoot' - else: - self.image.scroll(-64, 0) - self.step = 'leftFoot' - # After traversing 64 pixels, the walking animation is done - if self.dx == 64: - self.walking = False - self.setSprite() - self.dx = 0 - - game.tilemap.set_focus(self.rect.x, self.rect.y) + def __init__(self, location, orientation, *groups): + super(Player, self).__init__(*groups) + self.image = pygame.image.load('sprites/player.png') + self.imageDefault = self.image.copy() + self.rect = pygame.Rect(location, (64,64)) + self.orient = orientation + self.holdTime = 0 + self.walking = False + self.dx = 0 + self.step = 'rightFoot' + # Set default orientation + self.setSprite() + + def setSprite(self): + # Resets the player sprite sheet to its default position + # and scrolls it to the necessary position for the current orientation + self.image = self.imageDefault.copy() + if self.orient == 'up': + self.image.scroll(0, -64) + elif self.orient == 'down': + self.image.scroll(0, 0) + elif self.orient == 'left': + self.image.scroll(0, -128) + elif self.orient == 'right': + self.image.scroll(0, -192) + + def update(self, dt, game): + key = pygame.key.get_pressed() + # Setting orientation and sprite based on key input: + if key[pygame.K_UP]: + if not self.walking: + if self.orient != 'up': + self.orient = 'up' + self.setSprite() + self.holdTime += dt + elif key[pygame.K_DOWN]: + if not self.walking: + if self.orient != 'down': + self.orient = 'down' + self.setSprite() + self.holdTime += dt + elif key[pygame.K_LEFT]: + if not self.walking: + if self.orient != 'left': + self.orient = 'left' + self.setSprite() + self.holdTime += dt + elif key[pygame.K_RIGHT]: + if not self.walking: + if self.orient != 'right': + self.orient = 'right' + self.setSprite() + self.holdTime += dt + else: + self.holdTime = 0 + self.step = 'rightFoot' + # Walking mode enabled if a button is held for 0.1 seconds + if self.holdTime >= 100: + self.walking = True + lastRect = self.rect.copy() + # Walking at 8 pixels per frame in the direction the player is facing + if self.walking and self.dx < 64: + if self.orient == 'up': + self.rect.y -= 8 + elif self.orient == 'down': + self.rect.y += 8 + elif self.orient == 'left': + self.rect.x -= 8 + elif self.orient == 'right': + self.rect.x += 8 + self.dx += 8 + # Collision detection: + # Reset to the previous rectangle if player collides + # with anything in the foreground layer + if len(game.tilemap.layers['triggers'].collide(self.rect, + 'solid')) > 0: + self.rect = lastRect + # Area entry detection: + elif len(game.tilemap.layers['triggers'].collide(self.rect, + 'entry')) > 0: + entryCell = game.tilemap.layers['triggers'].find('entry')[0] + game.fadeOut() + game.initArea(entryCell['entry']) + + return + # Switch to the walking sprite after 32 pixels + if self.dx == 32: + # Self.step keeps track of when to flip the sprite so that + # the character appears to be taking steps with different feet. + if (self.orient == 'up' or + self.orient == 'down') and self.step == 'leftFoot': + self.image = pygame.transform.flip(self.image, True, False) + self.step = 'rightFoot' + else: + self.image.scroll(-64, 0) + self.step = 'leftFoot' + # After traversing 64 pixels, the walking animation is done + if self.dx == 64: + self.walking = False + self.setSprite() + self.dx = 0 + + game.tilemap.set_focus(self.rect.x, self.rect.y) class SpriteLoop(pygame.sprite.Sprite): - """A simple looped animated sprite. - - SpriteLoops require certain properties to be defined in the relevant - tmx tile: - - src - the source of the image that contains the sprites - width, height - the width and height of each section of the sprite that - will be displayed on-screen during animation - mspf - milliseconds per frame, or how many milliseconds must pass to - advance onto the next frame in the sprite's animation - frames - the number individual frames that compose the animation - """ - def __init__(self, location, cell, *groups): - super(SpriteLoop, self).__init__(*groups) - self.image = pygame.image.load(cell['src']) - self.defaultImage = self.image.copy() - self.width = int(cell['width']) - self.height = int(cell['height']) - self.rect = pygame.Rect(location, (self.width,self.height)) - self.frames = int(cell['frames']) - self.frameCount = 0 - self.mspf = int(cell['mspf']) # milliseconds per frame - self.timeCount = 0 + """A simple looped animated sprite. + + SpriteLoops require certain properties to be defined in the relevant + tmx tile: + + src - the source of the image that contains the sprites + width, height - the width and height of each section of the sprite that + will be displayed on-screen during animation + mspf - milliseconds per frame, or how many milliseconds must pass to + advance onto the next frame in the sprite's animation + frames - the number individual frames that compose the animation + """ + def __init__(self, location, cell, *groups): + super(SpriteLoop, self).__init__(*groups) + self.image = pygame.image.load(cell['src']) + self.defaultImage = self.image.copy() + self.width = int(cell['width']) + self.height = int(cell['height']) + self.rect = pygame.Rect(location, (self.width,self.height)) + self.frames = int(cell['frames']) + self.frameCount = 0 + self.mspf = int(cell['mspf']) # milliseconds per frame + self.timeCount = 0 - def update(self, dt, game): - self.timeCount += dt - if self.timeCount > self.mspf: - # Advance animation to the appropriate frame - self.image = self.defaultImage.copy() - self.image.scroll(-1*self.width*self.frameCount, 0) - self.timeCount = 0 - - self.frameCount += 1 - if self.frameCount == self.frames: - self.frameCount = 0 - + def update(self, dt, game): + self.timeCount += dt + if self.timeCount > self.mspf: + # Advance animation to the appropriate frame + self.image = self.defaultImage.copy() + self.image.scroll(-1*self.width*self.frameCount, 0) + self.timeCount = 0 + + self.frameCount += 1 + if self.frameCount == self.frames: + self.frameCount = 0 + class Game(object): - def __init__(self, screen): - self.screen = screen - - def fadeOut(self): - """Animate the screen fading to black for entering a new area""" - clock = pygame.time.Clock() - blackRect = pygame.Surface(self.screen.get_size()) - blackRect.set_alpha(100) - blackRect.fill((0,0,0)) - # Continuously draw a transparent black rectangle over the screen - # to create a fadeout effect - for i in range(0,5): - clock.tick(15) - self.screen.blit(blackRect, (0,0)) - pygame.display.flip() - clock.tick(15) - screen.fill((255,255,255,50)) - pygame.display.flip() - - def initArea(self, mapFile): - """Load maps and initialize sprite layers for each new area""" - self.tilemap = tmx.load(mapFile, screen.get_size()) - self.players = tmx.SpriteLayer() - self.objects = tmx.SpriteLayer() - # Initializing other animated sprites - try: - for cell in self.tilemap.layers['sprites'].find('src'): - SpriteLoop((cell.px,cell.py), cell, self.objects) - # In case there is no sprite layer for the current map - except KeyError: - pass - else: - self.tilemap.layers.append(self.objects) - # Initializing player sprite - startCell = self.tilemap.layers['triggers'].find('playerStart')[0] - self.player = Player((startCell.px, startCell.py), - startCell['playerStart'], self.players) - self.tilemap.layers.append(self.players) - self.tilemap.set_focus(self.player.rect.x, self.player.rect.y) - - def main(self): - clock = pygame.time.Clock() - self.initArea('palletTown.tmx') - - while 1: - dt = clock.tick(30) + def __init__(self, screen): + self.screen = screen + + def fadeOut(self): + """Animate the screen fading to black for entering a new area""" + clock = pygame.time.Clock() + blackRect = pygame.Surface(self.screen.get_size()) + blackRect.set_alpha(100) + blackRect.fill((0,0,0)) + # Continuously draw a transparent black rectangle over the screen + # to create a fadeout effect + for i in range(0,5): + clock.tick(15) + self.screen.blit(blackRect, (0,0)) + pygame.display.flip() + clock.tick(15) + screen.fill((255,255,255,50)) + pygame.display.flip() + + def initArea(self, mapFile): + """Load maps and initialize sprite layers for each new area""" + self.tilemap = tmx.load(mapFile, screen.get_size()) + self.players = tmx.SpriteLayer() + self.objects = tmx.SpriteLayer() + # Initializing other animated sprites + try: + for cell in self.tilemap.layers['sprites'].find('src'): + SpriteLoop((cell.px,cell.py), cell, self.objects) + # In case there is no sprite layer for the current map + except KeyError: + pass + else: + self.tilemap.layers.append(self.objects) + # Initializing player sprite + startCell = self.tilemap.layers['triggers'].find('playerStart')[0] + self.player = Player((startCell.px, startCell.py), + startCell['playerStart'], self.players) + self.tilemap.layers.append(self.players) + self.tilemap.set_focus(self.player.rect.x, self.player.rect.y) + + def main(self): + clock = pygame.time.Clock() + self.initArea('palletTown.tmx') + + while 1: + dt = clock.tick(30) - for event in pygame.event.get(): - if event.type == pygame.QUIT: - return - if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: - return + for event in pygame.event.get(): + if event.type == pygame.QUIT: + return + if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: + return - self.tilemap.update(dt, self) - screen.fill((0,0,0)) - self.tilemap.draw(self.screen) - pygame.display.flip() + self.tilemap.update(dt, self) + screen.fill((0,0,0)) + self.tilemap.draw(self.screen) + pygame.display.flip() if __name__ == '__main__': - pygame.init() - screen = pygame.display.set_mode((640, 480)) - pygame.display.set_caption("Pyllet Town") - Game(screen).main() \ No newline at end of file + pygame.init() + screen = pygame.display.set_mode((640, 480)) + pygame.display.set_caption("Pyllet Town") + Game(screen).main() \ No newline at end of file