Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

sidescroller example

  • Loading branch information...
commit fb918c78febf0cc701f7dce6adbb3ffb657fda2e 1 parent 93e2269
@alecgoebel alecgoebel authored
View
4 samplecode/sidescroller/README.md
@@ -0,0 +1,4 @@
+Sidescroller Example
+============================
+
+Like the camera example but with floor collision and stuff.
View
1  samplecode/sidescroller/data/README
@@ -0,0 +1 @@
+All images, sounds, etc go in this folder
View
BIN  samplecode/sidescroller/data/images/tiles.bmp
Binary file not shown
View
16 samplecode/sidescroller/data/levels/test_level.lvl
@@ -0,0 +1,16 @@
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+~~~~........~~~~~~~~~~~~~~~~~~
+~~~~........~~~~~~~~~~~~~~~~~~
+~~~~........~~~~~~~~~~~~~~~~~~
+~~~~........~~~~~~~~~~~~~~~~~~
+~~~~........~~~~~~~~~~~~~~~~~~
+~~~~......................~~~~
+~~~~......................~~~~
+~~~~~~~~~~~~~~~~~~~~......~~~~
+~~~%%%%%%~~~~~~~~~~~......~~~~
+~~~%%%%%%~~~~~~~~~~~......~~~~
+~~~%%%%%%~~~~~~~~~~~......~~~~
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
View
6 samplecode/sidescroller/gamelib/__init__.py
@@ -0,0 +1,6 @@
+# this is the core "gamelib" module. Whenever gamelib gets imported, this file gets imported first
+# Therefore, anything you need to do to "initialize" your python module goes here.
+
+
+# import the main function from our "main.py"
+from gamelib.main import main
View
25 samplecode/sidescroller/gamelib/camera.py
@@ -0,0 +1,25 @@
+"""
+camera.py
+
+"""
+
+from pygame import Rect
+
+from util import rel_rect
+
+class Camera(object):
+
+ def __init__(self, target, bounds, size):
+ self.bounds = bounds
+ self.rect = Rect((0,0), size)
+
+ def update(self, target):
+ self.rect.center = target.center
+ self.rect.clamp_ip(self.bounds)
+
+ def draw_background(self, surf, bg):
+ surf.blit(bg, (-self.rect.x, -self.rect.y))
+
+ def draw_sprite(self, surf, sprite):
+ if self.rect.colliderect(sprite.rect):
+ surf.blit(sprite.image, rel_rect(sprite.rect, self.rect))
View
72 samplecode/sidescroller/gamelib/game.py
@@ -0,0 +1,72 @@
+"""
+game.py
+
+"""
+
+import pygame
+from pygame.locals import *
+from pygame.sprite import spritecollide
+
+from camera import Camera
+from level import Level
+from player import Player
+
+from settings import FPS
+
+
+class Game(object):
+
+ def __init__(self, screen):
+ self.screen = screen
+
+ self.level = Level()
+ self.player = Player(self.level)
+
+ self.hud = screen.subsurface((0, 0, screen.get_width(), 40))
+ self.game_area = screen.subsurface((0, 40, screen.get_width(), screen.get_height() - 40))
+
+ self.cam = Camera(self.player, self.level.bounds, self.game_area.get_size())
+
+ self.font = pygame.font.Font(None, 36)
+
+ self.score = 0
+
+
+ def quit(self):
+ self.done = True
+
+ def update(self):
+ dt = self.clock.tick(FPS)
+
+ self.player.update(dt)
+ self.cam.update(self.player.rect)
+
+
+ def draw(self):
+ # draw level
+ self.cam.draw_background(self.game_area, self.level.background)
+ self.cam.draw_sprite(self.game_area, self.player)
+
+ # draw hud
+ self.hud.fill((20,20,20))
+ text = self.font.render("Score: %04d" % self.score, True, (255,255,255), (20, 20, 20))
+ self.hud.blit(text, (15, 10))
+
+ def run(self):
+ self.done = False
+ self.clock = pygame.time.Clock()
+
+ while not self.done:
+ # input
+ for event in pygame.event.get():
+ if event.type == QUIT:
+ self.quit()
+ elif event.type == KEYDOWN and event.key == K_ESCAPE:
+ self.quit()
+ elif event.type == KEYDOWN and event.key == K_SPACE:
+ self.player.jump()
+
+ self.update()
+ self.draw()
+ pygame.display.flip()
+
View
45 samplecode/sidescroller/gamelib/level.py
@@ -0,0 +1,45 @@
+"""
+level.py
+
+"""
+
+import os
+
+from pygame import Rect, Surface
+from pygame.sprite import Sprite, Group
+
+from settings import LEVEL_SIZE
+
+class Block(Sprite):
+ def __init__(self, x, y, w, h):
+ Sprite.__init__(self)
+ self.image = Surface((w,h))
+ self.image.fill(0)
+ self.rect = Rect(x,y,w,h)
+
+class Level(object):
+
+ def __init__(self):
+ self.bounds = Rect((0,0), LEVEL_SIZE)
+
+ # make rects for level
+ self.blocks = Group(
+ Block(0,0,40,self.bounds.height), # left wall
+ Block(self.bounds.width - 40, 0, 40, self.bounds.height), # right wall
+ Block(0, self.bounds.height - 40, self.bounds.width, 40), # floor
+ Block(self.bounds.centerx + 60, self.bounds.height - 120, 40, 120), # random thing
+ Block(0, self.bounds.height - 50, 80, 50),
+ Block(self.bounds.centerx + 160, self.bounds.height - 140, self.bounds.width, 40)
+ )
+
+
+ # render
+ self.render_background()
+
+ def render_background(self):
+ self.background = Surface(self.bounds.size)
+ self.background.fill((80,80,80))
+ self.blocks.draw(self.background)
+
+
+
View
16 samplecode/sidescroller/gamelib/main.py
@@ -0,0 +1,16 @@
+import pygame
+
+from gamelib.settings import SCREEN_SIZE
+from gamelib.game import Game
+
+def main():
+ # initialize pygame
+ pygame.init()
+ screen = pygame.display.set_mode(SCREEN_SIZE)
+
+ # create game
+ game = Game(screen)
+ try:
+ game.run()
+ except KeyboardInterrupt:
+ game.quit()
View
86 samplecode/sidescroller/gamelib/player.py
@@ -0,0 +1,86 @@
+"""
+player.py
+
+"""
+import pygame
+from pygame.locals import *
+from pygame import Surface
+from pygame.sprite import Sprite, Group
+
+from settings import PLAYER_COLOR, PLAYER_SPEED, PLAYER_SIZE, PLAYER_JUMP_SPEED, GRAVITY
+
+class Player(Sprite):
+
+ def __init__(self, level):
+ Sprite.__init__(self)
+
+ self.level = level
+ self.bounds = level.bounds
+
+ self.image = Surface(PLAYER_SIZE)
+ self.rect = self.image.get_rect()
+
+ self.image.fill((0,0,0))
+ rect = self.image.get_rect().inflate(-4, -4)
+ self.image.fill((255,0,0), rect)
+
+ self.rect = self.image.get_rect()
+ self.rect.center = self.bounds.center
+
+ self.vx = 0
+ self.vy = 0
+ self.inair = True
+
+ def jump(self):
+ if not self.inair:
+ self.inair = True
+ self.vy = PLAYER_JUMP_SPEED
+
+ def touches(self, group):
+ touching = Group()
+ coll = self.rect.inflate(1,1) # grow 1px to allow for edges
+ for sprite in group:
+ if coll.colliderect(sprite.rect):
+ touching.add(sprite)
+ return touching
+
+ def update(self, dt):
+ dt = dt / 1000.0
+ keys = pygame.key.get_pressed()
+
+ self.vx = 0
+ if keys[K_LEFT]:
+ self.vx -= PLAYER_SPEED
+ if keys[K_RIGHT]:
+ self.vx += PLAYER_SPEED
+
+ self.vy -= dt * GRAVITY
+ dx = self.vx * dt
+ dy = -self.vy * dt
+
+ # update position
+ prev_rect = self.rect
+ self.rect = self.rect.move(dx, dy)
+ self.rect.clamp_ip(self.bounds) # temp error
+
+ self.inair = True
+ for sprite in self.touches(self.level.blocks):
+ rect = sprite.rect
+
+ # collide with walls
+ if self.rect.left <= rect.right and prev_rect.left >= rect.right:
+ self.rect.left = rect.right
+ if self.rect.right >= rect.left and prev_rect.right <= rect.left:
+ self.rect.right = rect.left
+
+ # handle cielings
+ if self.rect.top <= rect.bottom and prev_rect.top >= rect.bottom:
+ self.vy /= 2.0 # halve speed from hitting head
+ self.rect.top = rect.bottom
+
+ # handle landing
+ if self.rect.bottom >= rect.top and prev_rect.bottom <= rect.top:
+ self.vy = 0
+ self.rect.bottom = rect.top
+ self.inair = False
+
View
25 samplecode/sidescroller/gamelib/settings.py
@@ -0,0 +1,25 @@
+## 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 = 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
+PLAYER_JUMP_SPEED = 420
+
+GRAVITY = 600
+
+## COINS
+COIN_SIZE = 10, 10
+COIN_COLOR = 255, 255, 0
+COIN_SPAWN_RATE = 3
+COIN_N_STARTING = 30
View
10 samplecode/sidescroller/gamelib/util.py
@@ -0,0 +1,10 @@
+"""
+util.py
+
+"""
+
+from pygame import Rect
+
+def rel_rect(rect, parent):
+ return Rect((rect.x - parent.x, rect.y - parent.y), rect.size)
+
View
10 samplecode/sidescroller/run
@@ -0,0 +1,10 @@
+#!/usr/bin/env python
+
+# This sets up the game and runs it. You can do more in this file like
+# loading configuration files, loading save files, etc and THEN running
+# the main function
+
+# NOTE: import gamelib is importing the "gamelib/__init__.py" file.
+
+import gamelib
+gamelib.main()
Please sign in to comment.
Something went wrong with that request. Please try again.