Permalink
Browse files

classcode for menus and states

  • Loading branch information...
1 parent b4f8d06 commit 11a4ad8e88f6eeac39e0ca7426055e50f861cf6e Alec Goebel committed Apr 18, 2012
@@ -0,0 +1,53 @@
+class AnimationFrames(object):
+ def __init__(self, frames, loops=-1):
+ self._times = []
+ self._data = []
+ total = 0
+ for t, data in frames:
+ total += t
+ self._times.append(total)
+ self._data.append(data)
+
+ self.end = total
+ self.loops = loops
+
+
+ def get(self, time):
+ # if looping forever or within the number of loops, wrap time
+ if self.loops == -1 or time < self.loops * self.end:
+ time %= self.end
+
+ # return last frame if we've gone too far
+ if time > self.end:
+ return self._data[-1]
+
+ # otherwise loop until we get the right frame
+ idx = 0
+ while self._times[idx] < time:
+ idx += 1
+
+ return self._data[idx]
+
+
+
+
+class Animation(object):
+ def __init__(self, spritesheet, frames):
+ if not isinstance(frames, AnimationFrames):
+ frames = AnimationFrames(frames)
+
+ self.spritesheet = spritesheet
+ self.frames = frames
+ self.time = 0
+ self.update(0)
+
+ def get_frame_data(self, time):
+ return self.frames.get(time)
+
+ def get_current_frame(self):
+ return self.spritesheet.get(self.x, self.y)
+
+ def update(self, dt):
+ self.time += dt
+ self.x, self.y = self.get_frame_data(self.time)
+
@@ -0,0 +1,67 @@
+from random import randrange
+
+from pygame import Surface
+from pygame.sprite import Sprite, Group
+
+from anim import Animation
+from spritesheet import SpriteSheet
+
+
+
+class CoinAnimation(Animation):
+ def __init__(self, duration):
+ spritesheet = SpriteSheet("coin", (8, 1))
+
+ frames = [ (duration, (0, 0)),
+ (duration, (1, 0)),
+ (duration, (2, 0)),
+ (duration, (3, 0)),
+ (duration, (4, 0)),
+ (duration, (5, 0)),
+ (duration, (6, 0)),
+ (duration, (7, 0)) ]
+
+ Animation.__init__(self, spritesheet, frames)
+
+
+## Coin
+class Coin(Sprite):
+ def __init__(self, loc):
+ Sprite.__init__(self)
+
+ self.anim = CoinAnimation(240)
+ self.image = self.anim.get_current_frame()
+ self.rect = self.image.get_rect()
+ self.rect.center = loc
+
+
+ def update(self, dt):
+ self.anim.update(dt)
+ self.image = self.anim.get_current_frame()
+
+class CoinGroup(Group):
+ spawn_rate = 1000 # ms
+
+ def __init__(self, bounds):
+ Group.__init__(self)
+
+ self.bounds = bounds
+ 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):
+ Group.update(self, dt)
+
+ # update the spawner
+ self.spawn_timer += dt
+ if self.spawn_timer >= self.spawn_rate:
+ self.spawn()
+ self.spawn_timer = 0
+
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,47 @@
+"""
+game.py
+
+"""
+
+import pygame
+from pygame.locals import *
+from pygame.sprite import spritecollide, GroupSingle
+
+from level import Level
+
+class Game(object):
+ fps = 60
+
+ def __init__(self, screen):
+ self.screen = screen
+ self.level = Level(self.screen.get_size())
+
+ def quit(self):
+ self.done = True
+
+
+ def update(self):
+ dt = self.clock.tick(self.fps)
+ self.level.update(dt)
+
+ def draw(self):
+ self.level.draw(self.screen)
+
+
+ def run(self):
+ self.done = False
+ self.clock = pygame.time.Clock()
+ self.level.restart()
+
+ 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()
+
+ self.update()
+ self.draw()
+ pygame.display.flip()
+
@@ -0,0 +1,59 @@
+"""
+level.py
+
+"""
+
+import os
+
+import pygame
+from pygame import Rect, Surface
+from pygame.sprite import Group, spritecollide
+
+from resource import load_image, play_song
+
+from player import Player
+from coin import CoinGroup
+
+class Level(object):
+ initial_coins = 20
+ song = "maintheme"
+
+ def __init__(self, size):
+ self.bounds = Rect((0,0), size)
+ self.bg_tile = load_image("grass")
+
+ def draw_background(self, surf):
+ tw, th = self.bg_tile.get_size()
+ sw, sh = surf.get_size()
+
+ for y in range(0, sh, th):
+ for x in range(0, sw, tw):
+ surf.blit(self.bg_tile, (x,y))
+
+ def restart(self):
+ self.player = Player()
+ self.player.rect.center = self.bounds.center
+
+ self.coins = CoinGroup(self.bounds)
+
+ # create initial coins
+ for i in range(self.initial_coins):
+ self.coins.spawn()
+
+ # start the background music
+ play_song(self.song)
+
+ def update(self, dt):
+ self.player.update(dt)
+ self.coins.update(dt)
+
+ # lock player in bounds
+ self.player.rect.clamp_ip(self.bounds)
+
+ # collide player with coins
+ spritecollide(self.player, self.coins, True)
+
+ def draw(self, surf):
+ self.draw_background(surf)
+ self.coins.draw(surf)
+ surf.blit(self.player.image, self.player.rect)
@@ -0,0 +1,18 @@
+import pygame
+
+from game import Game
+
+def main():
+ # initialize pygame
+ pygame.init()
+ screen = pygame.display.set_mode((800, 800))
+
+ # create game
+ game = Game(screen)
+ try:
+ game.run()
+ except KeyboardInterrupt:
+ game.quit()
+
+if __name__ == "__main__":
+ main()
@@ -0,0 +1,52 @@
+"""
+player.py
+
+"""
+import math
+
+import pygame
+from pygame.locals import *
+from pygame import Surface
+from pygame.sprite import Sprite
+
+DIAG = 1 / math.sqrt(2)
+
+class Player(Sprite):
+ speed = 300
+
+ def __init__(self):
+ Sprite.__init__(self)
+ self.vx = 0
+ self.vy = 0
+
+ self.image = self.create_image()
+ self.rect = self.image.get_rect()
+
+ # create image is for temporary artwork
+ def create_image(self):
+ image = Surface((40, 40))
+ rect = image.get_rect()
+ image.fill((0,0,0), rect)
+ image.fill((255,0,0), rect.inflate(-4,-4))
+ return image
+
+
+ def update(self, dt):
+ self.vx, self.vy = 0, 0
+ keys = pygame.key.get_pressed()
+ if keys[K_UP]:
+ self.vy = -self.speed
+ if keys[K_DOWN]:
+ self.vy = self.speed
+ if keys[K_LEFT]:
+ self.vx = -self.speed
+ if keys[K_RIGHT]:
+ self.vx = self.speed
+
+ if self.vx and self.vy:
+ self.vx *= DIAG
+ self.vy *= DIAG
+
+ dt = dt / 1000.0
+ self.rect.x += self.vx * dt
+ self.rect.y += self.vy * dt
@@ -0,0 +1,26 @@
+import os
+from os.path import abspath, dirname
+
+import pygame
+
+ROOT_DIR = dirname(abspath(__file__))
+DATA_DIR = os.path.join(ROOT_DIR, "data")
+
+SFX_DIR = os.path.join(DATA_DIR, "sfx")
+MUSIC_DIR = os.path.join(DATA_DIR, "music")
+IMG_DIR = os.path.join(DATA_DIR, "images")
+
+
+def play_song(song, times=-1):
+ path = os.path.join(MUSIC_DIR, song + ".ogg")
+ pygame.mixer.music.load(path)
+ pygame.mixer.music.play(times)
+
+
+_images = {}
+def load_image(name):
+ if name not in _images:
+ path = os.path.join(IMG_DIR, name + ".bmp")
+ _images[name] = pygame.image.load(path)
+
+ return _images[name].convert()
@@ -0,0 +1,29 @@
+from resource import load_image
+
+class SpriteSheet(object):
+ def __init__(self, image, dimensions, colorkey=-1):
+
+ # load the image
+ if type(image) is str:
+ image = load_image(image)
+
+ if colorkey == -1:
+ colorkey = image.get_at((0,0))
+
+ if colorkey:
+ image.set_colorkey(colorkey)
+
+ cols, rows = dimensions
+ w = self.width = 1.0 * image.get_width() / cols
+ h = self.height = 1.0 * image.get_height() / rows
+
+ # build the images
+ self._images = []
+ for y in range(rows):
+ row = []
+ for x in range(cols):
+ row.append(image.subsurface((x*w, y*h, w, h)))
+ self._images.append(row)
+
+ def get(self, x, y):
+ return self._images[y][x]

0 comments on commit 11a4ad8

Please sign in to comment.