Skip to content
Browse files

added anim and sound class code

  • Loading branch information...
1 parent 7bd21ec commit 2e4b4242eba78745dd2ee26fd62a6d63a844e42e Alec Goebel committed Apr 16, 2012
View
51 classcode/anim_and_sound/coin.py
@@ -0,0 +1,51 @@
+from random import randrange
+
+from pygame import Surface
+from pygame.sprite import Sprite, Group
+
+
+## Coin
+class Coin(Sprite):
+ def __init__(self, loc):
+ Sprite.__init__(self)
+
+ self.image = self.create_image()
+ self.rect = self.image.get_rect()
+ self.rect.center = loc
+
+ def create_image(self):
+ image = Surface((15, 15))
+ rect = image.get_rect()
+ image.fill((0,0,0), rect)
+ image.fill((255,255,0), rect.inflate(-2,-2))
+ return image
+
+ def update(self, dt):
+ pass
+
+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
+
View
BIN classcode/anim_and_sound/data/images/coin.bmp
Binary file not shown.
View
BIN classcode/anim_and_sound/data/images/grass.bmp
Binary file not shown.
View
BIN classcode/anim_and_sound/data/images/infantry.bmp
Binary file not shown.
View
BIN classcode/anim_and_sound/data/music/maintheme.ogg
Binary file not shown.
View
BIN classcode/anim_and_sound/data/sfx/coin.ogg
Binary file not shown.
View
47 classcode/anim_and_sound/game.py
@@ -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()
+
View
47 classcode/anim_and_sound/level.py
@@ -0,0 +1,47 @@
+"""
+level.py
+
+"""
+
+import os
+
+import pygame
+from pygame import Rect, Surface
+from pygame.sprite import Group, spritecollide
+
+from player import Player
+from coin import CoinGroup
+
+class Level(object):
+ initial_coins = 20
+
+ def __init__(self, size):
+ self.bounds = Rect((0,0), size)
+
+ def draw(self, surf):
+ self.draw_background(surf)
+ self.coins.draw(surf)
+ surf.blit(self.player.image, self.player.rect)
+
+ def draw_background(self, surf):
+ surf.fill((80, 80, 80))
+
+ 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()
+
+ def update(self, dt):
+ self.player.update(dt)
+ self.coins.update(dt)
+
+ # lock player in bounds
+ self.player.rect.clamp_ip(self.bounds)
+
+ spritecollide(self.player, self.coins, True)
+
View
18 classcode/anim_and_sound/main.py
@@ -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()
View
52 classcode/anim_and_sound/player.py
@@ -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 comments on commit 2e4b424

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