Permalink
Browse files

added basic level tiles

  • Loading branch information...
1 parent 1dee0ef commit ac50b2ebf7f3005dcc247ebb36f8bca6c330d357 Alec Goebel committed Apr 6, 2012
@@ -3,14 +3,14 @@
from pygame import Surface
from pygame.sprite import Sprite, Group
-from settings import COIN_COLOR, COIN_SPAWN_RATE
+from settings import COIN_COLOR, COIN_SPAWN_RATE, COIN_SIZE
class Coin(Sprite):
def __init__(self, loc):
Sprite.__init__(self)
- self.image = Surface((30, 30))
+ self.image = Surface(COIN_SIZE)
self.rect = self.image.get_rect()
self.image.fill((0,0,0))
@@ -9,8 +9,10 @@
from camera import Camera
from coin import CoinGroup
+from images import ImageManager
from level import Level
from player import Player
+from tilesheet import TileSheet
from settings import FPS, COIN_N_STARTING
@@ -20,7 +22,8 @@ class Game(object):
def __init__(self, screen):
self.screen = screen
- self.level = Level()
+ tilesheet = TileSheet(ImageManager().load("tiles"), (32, 32))
+ self.level = Level("test_level", tilesheet)
self.player = Player(self.level.bounds)
self.coins = CoinGroup(self.level.bounds)
@@ -49,6 +52,10 @@ def update(self):
self.player.update(dt)
self.coins.update(dt)
+ # check if the player is on the path
+ onpath = spritecollide(self.player, self.level.path, False)
+ self.player.on_path(onpath)
+
# collide coins
for coin in spritecollide(self.player, self.coins, True):
self.score += 1
@@ -0,0 +1,36 @@
+"""
+images.py
+
+Handles images don'tcha know
+"""
+
+import os
+
+import pygame
+
+from settings import DATA_DIR
+
+class ImageManager(object):
+ _instance = None
+
+ # image manager is a singleton
+ def __new__(cls, *a, **kw):
+ if cls._instance is None:
+ cls._instance = object.__new__(cls, *a, **kw)
+ return cls._instance
+
+ def __init__(self):
+ self._images = {}
+
+ def load(self, name, colorkey=None):
+ if name not in self._images:
+ path = os.path.join(DATA_DIR, "images", name) + ".bmp"
+ image = pygame.image.load(path).convert()
+ if colorkey:
+ if colorkey == -1:
+ colorkey = image.get_at((0,0))
+ image.set_colorkey(colorkey)
+
+ self._images[name] = image
+
+ return self._images[name].convert()
@@ -3,27 +3,48 @@
"""
-from pygame import Rect, Surface
+import os
-from settings import LEVEL_SIZE
+from pygame import Rect, Surface
+from pygame.sprite import Group
+from settings import DATA_DIR
class Level(object):
- def __init__(self):
- self.bounds = Rect((0,0), LEVEL_SIZE)
+ _map = {
+ "%": "flower",
+ "~": "grass",
+ ".": "path"
+ }
+
+ def __init__(self, name, tilesheet):
+ # load level data
+ path = os.path.join(DATA_DIR, "levels", name) + ".lvl"
+ with open(path, "r") as f:
+ data = f.read().strip().split("\n")
+
+ # parse level data
+ data = [ [ self._map.get(c) for c in row ] for row in data ]
+ # build the level
+ self.tiles, size = tilesheet.build(data)
+ self.bounds = Rect((0,0), size)
+
+ # find path
+ self.path = Group()
+ for tile in self.tiles:
+ if tile.type == "path":
+ self.path.add(tile)
+
+ # render
self.render_background()
+
def render_background(self):
self.background = Surface(self.bounds.size)
+ self.tiles.draw(self.background)
+
- # temporary background rendered
- rows = self.bounds.height/20
- cols = self.bounds.width/20
- self.background.fill((50,50,50))
- for y in range(rows):
- for x in range(cols):
- self.background.fill((80,80,80), (x*20 + 2, y*20 + 2, 18, 18))
@@ -7,7 +7,7 @@
from pygame import Surface
from pygame.sprite import Sprite
-from settings import PLAYER_COLOR, PLAYER_SPEED
+from settings import PLAYER_COLOR, PLAYER_SPEED, PLAYER_SIZE
class Player(Sprite):
@@ -16,16 +16,24 @@ def __init__(self, bounds):
self.bounds = bounds
- self.image = Surface((80, 80))
+ self.image = Surface(PLAYER_SIZE)
self.rect = self.image.get_rect()
- self.image.fill((0,0,0))
- self.image.fill(PLAYER_COLOR, self.rect.inflate(-4,-4))
-
self.x, self.y = bounds.center
self.vx = PLAYER_SPEED
self.vy = PLAYER_SPEED
+ def on_path(self, b):
+ if b:
+ color = PLAYER_COLOR
+ else:
+ color = 255, 0, 0
+
+ self.image.fill((0,0,0))
+ rect = self.image.get_rect().inflate(-4, -4)
+ self.image.fill(color, rect)
+
+
def update(self, dt):
dt = dt / 1000.0
keys = pygame.key.get_pressed()
@@ -1,16 +1,23 @@
+## FILE
+import os
+ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+DATA_DIR = os.path.join(ROOT_DIR, "data")
+
## GAME
FPS = 60
-SCREEN_SIZE = scr_w, scr_h = 800,800
+SCREEN_SIZE = scr_w, scr_h = 400,400
## LEVEL
LEVEL_SIZE = int(1.25 * scr_w), int(1.25 * scr_h)
## PLAYER
+PLAYER_SIZE = 30, 30
PLAYER_COLOR = 0,150,0
PLAYER_SPEED = 240
## COINS
+COIN_SIZE = 10, 10
COIN_COLOR = 255, 255, 0
COIN_SPAWN_RATE = 3
COIN_N_STARTING = 30
@@ -0,0 +1,53 @@
+"""
+tilesheet.py
+
+"""
+
+from pygame import Surface
+from pygame.sprite import Sprite, Group
+
+class Tile(Sprite):
+ def __init__(self, typ, image, loc, collides):
+ Sprite.__init__(self)
+ self.type = typ
+ self.image = image
+ self.rect = image.get_rect()
+ self.rect.topleft = loc
+ self.collides = collides
+
+
+class TileSheet(object):
+ _map = {
+ # name loc collides
+ "grass": [ (20, 30), False ],
+ "flower": [ (20, 150), True ],
+ "path": [ (210, 50), False ]
+ }
+
+ def __init__(self, image, size):
+ self.image = image
+ self.w, self.h = size
+
+ self.tilemap = {}
+ for name, data in self._map.items():
+ x,y = data[0]
+
+ tile = {}
+ tile["image"] = image.subsurface(x, y, self.w, self.h)
+ tile["collides"] = data[1]
+ self.tilemap[name] = tile
+
+
+ def build(self, data):
+ rows = len(data)
+ cols = len(data[0])
+
+ tiles = Group()
+ for y, row in enumerate(data):
+ for x, cell in enumerate(row):
+ data = self.tilemap.get(cell)
+ if data:
+ tile = Tile(cell, data["image"], (x*self.w, y*self.h), data["collides"])
+ tiles.add(tile)
+
+ return tiles, (cols * self.w, rows * self.h)

0 comments on commit ac50b2e

Please sign in to comment.