Skip to content
Browse files

better camera example

  • Loading branch information...
1 parent e2cc4da commit dac9a6b8121df4c069f11f3127677db1b01a5bb9 Alec Goebel committed Apr 5, 2012
View
25 samplecode/camera_example/gamelib/camera.py
@@ -9,24 +9,17 @@
class Camera(object):
- def __init__(self, player, level, size):
- self.player = player
- self.level = level
-
+ def __init__(self, target, bounds, size):
+ self.bounds = bounds
self.rect = Rect((0,0), size)
- def update(self):
- self.rect.center = self.player.rect.center
- self.rect.clamp_ip(self.level.bounds)
-
-
- def draw_level(self, surf):
- surf.blit(self.level.background, (-self.rect.x, -self.rect.y))
-
-
- def draw_player(self, surf):
- self.draw_sprite(surf, self.player)
+ 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):
- surf.blit(sprite.image, rel_rect(sprite.rect, self.rect))
+ if self.rect.colliderect(sprite.rect):
+ surf.blit(sprite.image, rel_rect(sprite.rect, self.rect))
View
44 samplecode/camera_example/gamelib/coin.py
@@ -0,0 +1,44 @@
+from random import randrange
+
+from pygame import Surface
+from pygame.sprite import Sprite, Group
+
+from settings import COIN_COLOR, COIN_SPAWN_RATE
+
+class Coin(Sprite):
+
+ def __init__(self, loc):
+ Sprite.__init__(self)
+
+ self.image = Surface((30, 30))
+ self.rect = self.image.get_rect()
+
+ self.image.fill((0,0,0))
+ self.image.fill(COIN_COLOR, self.rect.inflate(-2, -2))
+ self.rect.center = loc
+
+
+class CoinGroup(Group):
+ def __init__(self, bounds):
+ Group.__init__(self)
+
+ self.bounds = bounds
+
+ self.spawn_rate = COIN_SPAWN_RATE * 1000
+ self.spawn_timer = 0
+
+ def spawn(self):
+ x = randrange(self.bounds.x, self.bounds.x + self.bounds.width)
+ y = randrange(self.bounds.y, self.bounds.y + self.bounds.height)
+
+ coin = Coin((x,y))
+ coin.rect.clamp_ip(self.bounds)
+ self.add(coin)
+
+ def update(self, dt):
+ self.spawn_timer += dt
+ if self.spawn_timer >= self.spawn_rate:
+ self.spawn()
+ self.spawn_timer = 0
+
+
View
35 samplecode/camera_example/gamelib/game.py
@@ -5,47 +5,68 @@
import pygame
from pygame.locals import *
+from pygame.sprite import spritecollide
from camera import Camera
+from coin import CoinGroup
from level import Level
from player import Player
-from settings import FPS
+from settings import FPS, COIN_N_STARTING
class Game(object):
+
def __init__(self, screen):
self.screen = screen
self.level = Level()
- self.player = Player(self.level)
+ self.player = Player(self.level.bounds)
+ self.coins = CoinGroup(self.level.bounds)
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, self.game_area.get_size())
+ self.cam = Camera(self.player, self.level.bounds, self.game_area.get_size())
self.font = pygame.font.Font(None, 36)
+ self.score = 0
+
+ # create initial coins
+ for i in range(COIN_N_STARTING):
+ self.coins.spawn()
+
def quit(self):
self.done = True
def update(self):
dt = self.clock.tick(FPS)
+
+ # basic update
self.player.update(dt)
- self.cam.update()
+ self.coins.update(dt)
+
+ # collide coins
+ for coin in spritecollide(self.player, self.coins, True):
+ self.score += 1
+
+ self.cam.update(self.player.rect)
def draw(self):
# draw level
- self.cam.draw_level(self.game_area)
- self.cam.draw_player(self.game_area)
+ self.cam.draw_background(self.game_area, self.level.background)
+ self.cam.draw_sprite(self.game_area, self.player)
+
+ for coin in self.coins:
+ self.cam.draw_sprite(self.game_area, coin)
# draw hud
self.hud.fill((20,20,20))
- text = self.font.render("This is a HUD", True, (255,255,255), (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):
View
2 samplecode/camera_example/gamelib/level.py
@@ -23,7 +23,7 @@ def render_background(self):
rows = self.bounds.height/20
cols = self.bounds.width/20
- self.background.fill((0,0,0))
+ 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))
View
38 samplecode/camera_example/gamelib/player.py
@@ -2,31 +2,43 @@
player.py
"""
-
+import pygame
+from pygame.locals import *
from pygame import Surface
from pygame.sprite import Sprite
+from settings import PLAYER_COLOR, PLAYER_SPEED
+
class Player(Sprite):
- def __init__(self, level):
+ def __init__(self, bounds):
Sprite.__init__(self)
- self.level = level
+ self.bounds = bounds
- self.image = Surface((120, 80))
- self.image.fill((0,150,0))
+ self.image = Surface((80, 80))
self.rect = self.image.get_rect()
- self.x, self.y = level.bounds.center
- self.vy = 5
-
- def update(self, dt):
- self.y += self.vy
+ self.image.fill((0,0,0))
+ self.image.fill(PLAYER_COLOR, self.rect.inflate(-4,-4))
- if self.y < self.level.bounds.top or self.y > self.level.bounds.bottom:
- self.vy = -self.vy
- self.y += 2 * self.vy
+ self.x, self.y = bounds.center
+ self.vx = PLAYER_SPEED
+ self.vy = PLAYER_SPEED
+ def update(self, dt):
+ dt = dt / 1000.0
+ keys = pygame.key.get_pressed()
+ if keys[K_UP]:
+ self.y -= self.vy * dt
+ if keys[K_DOWN]:
+ self.y += self.vy * dt
+ if keys[K_LEFT]:
+ self.x -= self.vx * dt
+ if keys[K_RIGHT]:
+ self.x += self.vx * dt
# update player
+ self.rect = self.image.get_rect()
self.rect.center = int(self.x), int(self.y)
+ self.rect.clamp_ip(self.bounds)
View
16 samplecode/camera_example/gamelib/settings.py
@@ -1,6 +1,16 @@
-## GAME SETTINGS
+## GAME
FPS = 60
SCREEN_SIZE = scr_w, scr_h = 800,800
-## LEVEL SETTINGS
-LEVEL_SIZE = int(1.5 * scr_w), int(1.5 * scr_h)
+## LEVEL
+LEVEL_SIZE = int(1.25 * scr_w), int(1.25 * scr_h)
+
+## PLAYER
+PLAYER_COLOR = 0,150,0
+PLAYER_SPEED = 240
+
+
+## COINS
+COIN_COLOR = 255, 255, 0
+COIN_SPAWN_RATE = 3
+COIN_N_STARTING = 30

0 comments on commit dac9a6b

Please sign in to comment.
Something went wrong with that request. Please try again.