-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added python script, map file, and graphic resources.
- Loading branch information
Pat Richeal
committed
Apr 4, 2015
1 parent
a99b9e4
commit 93cb83a
Showing
5 changed files
with
282 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,266 @@ | ||
import pygame, pytmx | ||
|
||
#Colors | ||
BACKGROUND = (20, 20, 20) | ||
|
||
#Constants | ||
SCREEN_WIDTH = 720 | ||
SCREEN_HEIGHT = 480 | ||
|
||
#Tiled map layer of blocks that you collide with | ||
MAP_COLLISION_LAYER = 1 | ||
|
||
class Game(object): | ||
def __init__(self): | ||
self.currentLevelNumber = 0 | ||
self.levels = [] | ||
self.levels.append(Level(fileName = "resources/level1.tmx")) | ||
self.currentLevel = self.levels[self.currentLevelNumber] | ||
|
||
self.player = Player(x = 200, y = 100) | ||
self.player.currentLevel = self.currentLevel | ||
|
||
self.overlay = pygame.image.load("resources/overlay.png") | ||
|
||
def processEvents(self): | ||
for event in pygame.event.get(): | ||
if event.type == pygame.QUIT: | ||
return True | ||
elif event.type == pygame.KEYDOWN: | ||
if event.key == pygame.K_LEFT: | ||
self.player.goLeft() | ||
elif event.key == pygame.K_RIGHT: | ||
self.player.goRight() | ||
elif event.key == pygame.K_UP: | ||
self.player.jump() | ||
elif event.type == pygame.KEYUP: | ||
if event.key == pygame.K_LEFT and self.player.changeX < 0: | ||
self.player.stop() | ||
elif event.key == pygame.K_RIGHT and self.player.changeX > 0: | ||
self.player.stop() | ||
|
||
return False | ||
|
||
def runLogic(self): | ||
self.player.update() | ||
|
||
def draw(self, screen): | ||
screen.fill(BACKGROUND) | ||
self.currentLevel.draw(screen) | ||
self.player.draw(screen) | ||
screen.blit(self.overlay, [0, 0]) | ||
pygame.display.flip() | ||
|
||
class Player(pygame.sprite.Sprite): | ||
def __init__(self, x, y): | ||
pygame.sprite.Sprite.__init__(self) | ||
|
||
self.sprites = SpriteSheet("resources/player.png") | ||
|
||
self.stillRight = self.sprites.image_at((0, 0, 30, 42), colorkey = (250, 255, 0)) | ||
self.stillLeft = self.sprites.image_at((0, 42, 30, 42), colorkey = (250, 255, 0)) | ||
|
||
self.runningRight = (self.sprites.image_at((0, 84, 30, 42), colorkey = (250, 255, 0)), | ||
self.sprites.image_at((30, 84, 30, 42), colorkey = (250, 255, 0)), | ||
self.sprites.image_at((60, 84, 30, 42), colorkey = (250, 255, 0)), | ||
self.sprites.image_at((90, 84, 30, 42), colorkey = (250, 255, 0)), | ||
self.sprites.image_at((120, 84, 30, 42), colorkey = (250, 255, 0))) | ||
|
||
self.runningLeft = (self.sprites.image_at((0, 126, 30, 42), colorkey = (250, 255, 0)), | ||
self.sprites.image_at((30, 126, 30, 42), colorkey = (250, 255, 0)), | ||
self.sprites.image_at((60, 126, 30, 42), colorkey = (250, 255, 0)), | ||
self.sprites.image_at((90, 126, 30, 42), colorkey = (250, 255, 0)), | ||
self.sprites.image_at((120, 126, 30, 42), colorkey = (250, 255, 0))) | ||
|
||
self.jumpingRight = (self.sprites.image_at((30, 0, 30, 42), colorkey = (250, 255, 0)), | ||
self.sprites.image_at((60, 0, 30, 42), colorkey = (250, 255, 0)), | ||
self.sprites.image_at((90, 0, 30, 42), colorkey = (250, 255, 0))) | ||
|
||
self.jumpingLeft = (self.sprites.image_at((30, 42, 30, 42), colorkey = (250, 255, 0)), | ||
self.sprites.image_at((60, 42, 30, 42), colorkey = (250, 255, 0)), | ||
self.sprites.image_at((90, 42, 30, 42), colorkey = (250, 255, 0))) | ||
|
||
self.image = self.stillRight | ||
|
||
self.rect = self.image.get_rect() | ||
self.rect.x = x | ||
self.rect.y = y | ||
|
||
self.changeX = 0 | ||
self.changeY = 0 | ||
self.direction = "right" | ||
|
||
self.running = False | ||
self.runningFrame = 0 | ||
self.runningTime = pygame.time.get_ticks() | ||
|
||
self.currentLevel = None | ||
|
||
def update(self): | ||
self.rect.x += self.changeX | ||
tileHitList = pygame.sprite.spritecollide(self, self.currentLevel.layers[MAP_COLLISION_LAYER].tiles, False) | ||
for tile in tileHitList: | ||
if self.changeX > 0: | ||
self.rect.right = tile.rect.left | ||
else: | ||
self.rect.left = tile.rect.right | ||
|
||
if self.rect.right >= SCREEN_WIDTH - 200: | ||
difference = self.rect.right - (SCREEN_WIDTH - 200) | ||
self.rect.right = SCREEN_WIDTH - 200 | ||
self.currentLevel.shiftLevel(-difference) | ||
|
||
if self.rect.left <= 200: | ||
difference = 200 - self.rect.left | ||
self.rect.left = 200 | ||
self.currentLevel.shiftLevel(difference) | ||
|
||
self.rect.y += self.changeY | ||
tileHitList = pygame.sprite.spritecollide(self, self.currentLevel.layers[MAP_COLLISION_LAYER].tiles, False) | ||
if len(tileHitList) > 0: | ||
for tile in tileHitList: | ||
if self.changeY > 0: | ||
self.rect.bottom = tile.rect.top | ||
self.changeY = 1 | ||
|
||
if self.direction == "right": | ||
self.image = self.stillRight | ||
else: | ||
self.image = self.stillLeft | ||
else: | ||
self.rect.top = tile.rect.bottom | ||
self.changeY = 0 | ||
else: | ||
self.changeY += 0.2 | ||
if self.changeY > 0: | ||
if self.direction == "right": | ||
self.image = self.jumpingRight[1] | ||
else: | ||
self.image = self.jumpingLeft[1] | ||
|
||
if self.running and self.changeY == 1: | ||
if self.direction == "right": | ||
self.image = self.runningRight[self.runningFrame] | ||
else: | ||
self.image = self.runningLeft[self.runningFrame] | ||
|
||
if pygame.time.get_ticks() - self.runningTime > 50: | ||
self.runningTime = pygame.time.get_ticks() | ||
if self.runningFrame == 4: | ||
self.runningFrame = 0 | ||
else: | ||
self.runningFrame += 1 | ||
|
||
def jump(self): | ||
self.rect.y += 2 | ||
tileHitList = pygame.sprite.spritecollide(self, self.currentLevel.layers[MAP_COLLISION_LAYER].tiles, False) | ||
self.rect.y -= 2 | ||
|
||
if len(tileHitList) > 0: | ||
if self.direction == "right": | ||
self.image = self.jumpingRight[0] | ||
else: | ||
self.image = self.jumpingLeft[0] | ||
|
||
self.changeY = -6 | ||
|
||
def goRight(self): | ||
self.direction = "right" | ||
self.running = True | ||
self.changeX = 3 | ||
|
||
def goLeft(self): | ||
self.direction = "left" | ||
self.running = True | ||
self.changeX = -3 | ||
|
||
def stop(self): | ||
self.running = False | ||
self.changeX = 0 | ||
|
||
def draw(self, screen): | ||
screen.blit(self.image, self.rect) | ||
|
||
class Level(object): | ||
def __init__(self, fileName): | ||
self.mapObject = pytmx.load_pygame(fileName) | ||
self.layers = [] | ||
self.levelShift = 0 | ||
|
||
for layer in range(len(self.mapObject.layers)): | ||
self.layers.append(Layer(index = layer, mapObject = self.mapObject)) | ||
|
||
def shiftLevel(self, shiftX): | ||
self.levelShift += shiftX | ||
|
||
for layer in self.layers: | ||
for tile in layer.tiles: | ||
tile.rect.x += shiftX | ||
|
||
def draw(self, screen): | ||
for layer in self.layers: | ||
layer.draw(screen) | ||
|
||
class Layer(object): | ||
def __init__(self, index, mapObject): | ||
self.index = index | ||
self.tiles = pygame.sprite.Group() | ||
self.mapObject = mapObject | ||
|
||
for x in range(self.mapObject.width): | ||
for y in range(self.mapObject.height): | ||
img = self.mapObject.get_tile_image(x, y, self.index) | ||
if img: | ||
self.tiles.add(Tile(image = img, x = (x * self.mapObject.tilewidth), y = (y * self.mapObject.tileheight))) | ||
|
||
def draw(self, screen): | ||
self.tiles.draw(screen) | ||
|
||
class Tile(pygame.sprite.Sprite): | ||
def __init__(self, image, x, y): | ||
pygame.sprite.Sprite.__init__(self) | ||
|
||
self.image = image | ||
self.rect = self.image.get_rect() | ||
self.rect.x = x | ||
self.rect.y = y | ||
|
||
class SpriteSheet(object): | ||
def __init__(self, fileName): | ||
self.sheet = pygame.image.load(fileName).convert() | ||
|
||
def image_at(self, rectangle, colorkey = None): | ||
rect = pygame.Rect(rectangle) | ||
image = pygame.Surface(rect.size).convert() | ||
image.blit(self.sheet, (0, 0), rect) | ||
if colorkey is not None: | ||
if colorkey is -1: | ||
colorkey = image.get_at((0,0)) | ||
image.set_colorkey(colorkey, pygame.RLEACCEL) | ||
return image | ||
|
||
def images_at(self, rects, colorkey = None): | ||
return [self.image_at(rect, colorkey) for rect in rects] | ||
|
||
def load_strip(self, rect, image_count, colorkey = None): | ||
tups = [(rect[0]+rect[2]*x, rect[1], rect[2], rect[3]) | ||
for x in range(image_count)] | ||
return self.images_at(tups, colorkey) | ||
|
||
def main(): | ||
pygame.init() | ||
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT]) | ||
pygame.display.set_caption("Pygame Tiled Demo") | ||
clock = pygame.time.Clock() | ||
done = False | ||
game = Game() | ||
|
||
while not done: | ||
done = game.processEvents() | ||
game.runLogic() | ||
game.draw(screen) | ||
clock.tick(60) | ||
|
||
pygame.quit() | ||
|
||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<map version="1.0" orientation="orthogonal" renderorder="right-down" width="67" height="20" tilewidth="24" tileheight="24" nextobjectid="1"> | ||
<tileset firstgid="1" name="tiles" tilewidth="24" tileheight="24" spacing="1"> | ||
<image source="tiles.png" width="374" height="174"/> | ||
</tileset> | ||
<layer name="background" width="67" height="20"> | ||
<data encoding="base64" compression="zlib"> | ||
eJztlsEKgCAQRP2P6r7HDnny//8rDwYRSrs60Gg+WLo83GaKyDkde5rePQTsGb/qIoA97V6EhyC360iD8rR7WzwE7O/+7MLuIWDP+Mcunt9jMZ6HgKWLJc4aZ6s8DwFLF60eAvaMvXYhGU8K7t0rOZb7Q8D+vGcXdg8Be8YRu/DpGl68i5H/L7zSq92LYHZh38XuIWDPiOjiBFS2KPg= | ||
</data> | ||
</layer> | ||
<layer name="collision" width="67" height="20"> | ||
<data encoding="base64" compression="zlib"> | ||
eJzdmM1OAjEQxweCaPz+ums8SKIGD0Y4CYtwZXkBDBvf/yHshJns7Oy0bJXuwSb/dCltt/11ZtpuDgC5oZWnPLZOqF5sufw/pm3Tce5Sz6mjdKV+d406Wl1PW1bvj8x1nVCbtafuRP3WfeA420hNmcXwl30w62+nL6cN1Bkhi8wob5tF6oTz2JDWlCMTOdfMaQ4ljwKq9vGfWbBtSB/5hCqLAkpW3XaHnCwhC5wPM0BfWYsyZjFzmsLWPjQPjp3se70Wxn0Eu2OBL4777JhZ6JgoYweymEM9ThRGWVs+c5igT7TvhdMSqvOR9sE2gXkGpc0UIpdtD0gp02WCPnENed1575T+wSwyqO8laEcF1O3iOsE4dQrZxZPTs9NLZJ/IAjl8QP0cofeRKdg+oXUTOQYrnTmdK12I/y27aHLm8J3h5PkC56r9RNoFS7Lqw9bH+qR9stiV9hkv2DdmlC+gnKslyyaQ4yXlyOLB6RFKFmNSirTPeIEscP4yHjSxfynmgDnaB3JAHrf0jjGk47HPvUrGiZB/WPcTFvsI+4neU8fQnId1XpC+j++Qd0DrPmjdQZqcLbDslfQm8iE9D0VZiMkdSZbxOTSGhTznsHB90DcXpFTnlg69S+6jPJ8R5e/G+LSYhXXWimUxgK2f3VPOPjegPCULvS+wQix0DLVY6DM569jphHQKVZuW3yaWNDa0hYl4b2q7sNbaYoFjm4tnZmixyFX7kSoPfUNhv+A9fELCuCZjhIwLoTOF7xuGbuMbk2SxojHxPXUK1TuaZKH787HwxR9eG44RuXjfjOpZ6/fbb3lNvhNqu/gB6Buj8w== | ||
</data> | ||
</layer> | ||
</map> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.