Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

tie fighter examples

  • Loading branch information...
commit e55c084c44fcb5b9d993fd05d334e705ce20ad6d 1 parent dac9a6b
@alecgoebel alecgoebel authored
View
67 samplecode/tiefighter_example/animals.py
@@ -0,0 +1,67 @@
+#!/usr/bin/env python
+
+class Animal(object):
+ def __init__(self, name):
+ self.name = name
+
+ def can_eat(self, food):
+ pass
+
+ def eat(self, food):
+ pass
+
+ def speak(self):
+ pass
+
+ def die(self):
+ print self.name, "lived a good life"
+
+ def __str__(self):
+ return self.__class__.__name__ + ": " + self.name
+
+class Dog(Animal):
+ def can_eat(self, food):
+ return True
+
+ def eat(self, food):
+ print self.name, "gobbles", food
+
+ def speak(self):
+ print self.name + ": woof"
+
+
+class Cat(Animal):
+ def __init__(self, name):
+ Animal.__init__(self, "Mrs. "+name)
+
+ def can_eat(self, food):
+ return food.lower() == "fish"
+
+ def eat(self, food):
+ print self.name, "sniffs", food
+
+ def speak(self):
+ print self.name, "walks away"
+
+class Robot(object):
+ battery = 0
+
+ def charge(self, amnt):
+ self.battery += amnt
+
+
+ def die(self):
+ print self.name, "cannot die"
+
+class RobotDog(Robot, Dog):
+ def die(self, typ=Dog):
+ typ.die(self)
+
+charlie = RobotDog("Charlie")
+charlie.eat("stuff")
+charlie.charge(3)
+print charlie.battery
+charlie.die()
+charlie.die(Robot)
+charlie.die(typ=Dog)
+charlie.die(Dog)
View
97 samplecode/tiefighter_example/app.py
@@ -0,0 +1,97 @@
+#!/usr/bin/env python
+
+import pygame
+from pygame.locals import *
+
+class Application(object):
+ title = None
+ screen_size = 800, 600
+ screen_flags = DOUBLEBUF|HWSURFACE|SRCALPHA
+ fps = 30
+
+ def __init__(self):
+ pygame.init()
+ self.screen = pygame.display.set_mode(self.screen_size, self.screen_flags)
+ if self.title:
+ pygame.display.set_caption(self.title)
+
+ font = pygame.font.Font(None, 60)
+ self._paused_text = font.render("Press Space to Resume", True, (255,255,255), (0,0,0))
+ self._paused_text.set_colorkey((0,0,0))
+
+ self.paused = False
+
+ def pause(self):
+ self.paused = True
+ self.on_pause()
+ self._draw_pause()
+
+ def _draw_pause(self):
+ overlay = pygame.Surface(self.screen.get_size())
+ overlay.set_alpha(200)
+ overlay.fill((0,0,0))
+ self.screen.blit(overlay, (0,0))
+
+ loc = self._paused_text.get_rect()
+ loc.center = self.screen.get_rect().center
+ self.screen.blit(self._paused_text, loc)
+ pygame.display.flip()
+
+
+ def resume(self):
+ self.paused = False
+ self.on_resume()
+
+ def quit(self):
+ self.done = True
+
+ def step(self):
+ self.clock.tick(self.fps)
+
+ # pause if screen is hidden
+ if not pygame.display.get_active() and not self.paused:
+ self.pause()
+
+ # handle events
+ 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:
+ if self.paused:
+ self.resume()
+ else:
+ self.pause()
+ elif event.type == VIDEOEXPOSE:
+ print "woo"
+ else:
+ self.handle_event(event)
+
+ # update if not paused
+ if not self.paused:
+ self.update()
+ self.draw(self.screen)
+ pygame.display.flip()
+
+
+ def run(self):
+ self.done = False
+ self.clock = pygame.time.Clock()
+ self.on_start()
+ while not self.done:
+ self.step()
+ self.on_quit()
+
+ # update if not paused
+
+
+ def handle_event(self,event): pass
+ def update(self): pass
+ def draw(self, screen): pass
+
+ def on_resume(self): pass
+ def on_pause(self): pass
+ def on_start(self): pass
+ def on_quit(self): pass
+
View
44 samplecode/tiefighter_example/graphics.py
@@ -0,0 +1,44 @@
+#!/usr/bin/env python
+
+import pygame
+
+def colorkey(color):
+ if color == (0,0,0):
+ return (255,255,255)
+ else:
+ return (0,0,0)
+
+def clear(surf, color):
+ ck = colorkey(color)
+ surf.fill(ck)
+ surf.set_colorkey(ck)
+
+
+def draw_tie(surf, color):
+ clear(surf, color)
+ rect = surf.get_rect()
+
+ d = min(rect.width, rect.height)
+ wall = d/8
+
+ dw = rect.width - d
+ dh = rect.height - d
+
+ pygame.draw.rect(surf, color, (0, 0, wall, rect.height))
+ pygame.draw.rect(surf, color, (rect.right-wall, 0, wall, rect.height))
+ pygame.draw.rect(surf, color, (0, (rect.height - wall)/2, rect.width, wall))
+ pygame.draw.circle(surf, color, ( (d+dw)/2, (d+dh)/2 ), d / 4)
+
+
+def draw_ywing(surf, color):
+ clear(surf, color)
+ rect = surf.get_rect()
+
+ unit = rect.height / 16
+ engine_len = rect.width / 2 + 3 * unit
+
+ pygame.draw.rect(surf, color, (0, 0, engine_len + unit, 3 * unit))
+ pygame.draw.rect(surf, color, (0, rect.height - 3*unit, engine_len + unit, 3 * unit))
+ pygame.draw.rect(surf, color, (rect.width-engine_len, (rect.height - 3*unit) / 2, engine_len, 3 * unit))
+ pygame.draw.rect(surf, color, ( (rect.width - 3*unit)/2, 0, 3 * unit, rect.height))
+ pygame.draw.rect(surf, color, ( rect.width - 9*unit, (rect.height - 6*unit)/2, 9 * unit, 6*unit))
View
208 samplecode/tiefighter_example/main.py
@@ -0,0 +1,208 @@
+#!/usr/bin/env python
+
+import math
+from random import randrange
+
+import pygame
+from pygame import Rect, Surface
+from pygame.locals import *
+from pygame.sprite import Sprite, Group
+
+from app import Application
+from graphics import draw_tie, draw_ywing
+from ships import Ship, ShipSpawner
+from utils import *
+
+## EXPLOSIONS
+class ExplosionGroup(Group):
+ def draw(self, surf):
+ for xplo in self:
+ if xplo.radius > 0:
+ xplo.draw(surf)
+
+class Explosion(Sprite):
+ dradius = 60
+ duration = 1500
+ group = ExplosionGroup()
+
+ def __init__(self, pos, radius):
+ Sprite.__init__(self)
+ self.pos = pos
+ self.radius = radius
+
+ def update(self, dt):
+ if self.duration > 0:
+ self.duration -= dt
+ elif self.radius > 0:
+ self.radius -= self.dradius * (dt / 1000.0)
+ else:
+ self.kill()
+
+ def rand_color(self):
+ return randrange(120,256), 255, randrange(120,256)
+
+ def draw(self, surf):
+ pygame.draw.circle(surf, self.rand_color(), self.pos, int(self.radius))
+
+
+
+
+class Explodes(Sprite):
+ explosion_type = Explosion
+ explosion_radius = 60
+
+ def kill(self):
+ xplo = self.explosion_type(self.rect.center, self.explosion_radius)
+ Explosion.group.add(xplo)
+ Sprite.kill(self)
+
+
+def collide_xplo_ship(xplo, ship):
+ return collide_rect_circle(ship.rect, xplo.pos, xplo.radius)
+
+## SHIP GROUP
+class ShipGroup(Group):
+ def __init__(self, count):
+ Group.__init__(self)
+ self.count = count
+
+ def add(self, *sprites):
+ for sprite in sprites:
+ if len(self) < self.count:
+ Group.add(self, sprite)
+
+## TIE FIGHTERS
+class TieExplosion(Explosion):
+ def rand_color(self):
+ r = randrange(256)
+ return 255, r, 0
+
+class TieFighter(Explodes, Ship):
+ width = 40
+ height = 40
+
+ explosion_type = TieExplosion
+ explosion_radius = 28
+
+ def draw_image(self):
+ draw_tie(self.image, self.color)
+
+ def update(self, dt):
+ vx = self.vx
+ vy = self.vy
+
+ Ship.update(self, dt)
+
+ if vx != self.vx or vy != self.vy:
+ if vx != self.vx:
+ vx = self.vx
+ vy = -vy
+ else:
+ vx = -vx
+ vy = self.vy
+
+ tie = TieFighter(self.rect.x, self.rect.y, vx, vy, self.bounds, self.color)
+
+ for group in self.groups():
+ group.add(tie)
+
+class TieSpawner(ShipSpawner):
+ ship_type = TieFighter
+
+ def rand_vel(self):
+ vx = randint_neg(100, 250)
+ vy = randint_neg(100, 250)
+ return vx, vy
+
+ def rand_color(self):
+ r = randrange(128,256)
+ return r,0,0
+
+## Y-Wing
+class YWingExplosion(Explosion):
+ def rand_color(self):
+ r = randrange(256)
+ return r, 255, 255
+
+class YWing(Explodes, Ship):
+ width = 128
+ height = 64
+
+ explosion_type = YWingExplosion
+
+ def draw_image(self):
+ draw_ywing(self.image, self.color)
+ self.orig_image = self.image
+ self.flipped_image = pygame.transform.flip(self.image, True, False)
+
+ def update(self, dt):
+ if randrange(60) == 0:
+ self.vx = -self.vx
+
+ Ship.update(self, dt)
+
+ if self.vx > 0:
+ self.image = self.orig_image
+ else:
+ self.image = self.flipped_image
+
+class YWingSpawner(ShipSpawner):
+ ship_type = YWing
+
+ def rand_vel(self):
+ vx = randint_neg(200, 400)
+ return vx, 0
+
+ def rand_color(self):
+ r = randrange(128,256)
+ return r,r,r
+
+## GAME
+class Game(Application):
+ title = "Spaceships"
+ screen_size = 800, 600
+ min_dt = 200
+ max_ships = 600
+
+ def __init__(self):
+ Application.__init__(self)
+
+ self.bounds = self.screen.get_rect()
+ self.ships = ShipGroup(self.max_ships)
+ self.xplos = ExplosionGroup()
+ Explosion.group = self.xplos
+
+ self.spawners = [ TieSpawner(1000, self.ships, self.bounds),
+ YWingSpawner(2000, self.ships, self.bounds) ]
+
+ def handle_event(self, event):
+ if event.type == MOUSEBUTTONDOWN and event.button == 1:
+ self.xplos.add( Explosion(pygame.mouse.get_pos(), 30) )
+
+ def update(self):
+ dt = min(self.min_dt, self.clock.get_time())
+
+ self.ships.update(dt)
+ self.xplos.update(dt)
+
+ for xplo in self.xplos:
+ pygame.sprite.spritecollide(xplo, self.ships, True, collide_xplo_ship)
+
+ for spawner in self.spawners:
+ spawner.update(dt)
+
+ def draw(self, screen):
+ screen.fill((0,0,0))
+ self.ships.draw(screen)
+ self.xplos.draw(screen)
+
+if __name__ == "__main__":
+ Game().run()
+ print "ByeBye"
+
+
+
+
+
+
+
View
76 samplecode/tiefighter_example/ships.py
@@ -0,0 +1,76 @@
+#!/usr/bin/env python
+
+from random import randrange
+
+import pygame
+from pygame import Rect, Surface
+from pygame.locals import *
+from pygame.sprite import Sprite, Group
+
+class Ship(Sprite):
+ width = 20
+ height = 20
+
+ def __init__(self, x, y, vx, vy, bounds, color):
+ Sprite.__init__(self)
+
+ self.vx = vx
+ self.vy = vy
+ self.bounds = bounds
+ self.color = color
+
+ self.rect = Rect(x, y, self.width, self.height)
+ self.image = Surface(self.rect.size)
+ self.draw_image()
+
+ def draw_image(self):
+ self.image.fill(self.color)
+
+ def update(self, dt):
+ dt /= 1000.0
+ dx = int(self.vx * dt)
+ dy = int(self.vy * dt)
+ self.rect.x += dx
+ self.rect.y += dy
+
+ if self.rect.left < self.bounds.left or self.rect.right > self.bounds.right:
+ self.vx = -self.vx
+ self.rect.x += -2 * dx
+
+ if self.rect.top < self.bounds.top or self.rect.bottom > self.bounds.bottom:
+ self.vy = -self.vy
+ self.rect.y += -2 * dy
+
+
+
+class ShipSpawner(object):
+ ship_type = Ship
+
+ def __init__(self, duration, group, bounds):
+ self.group = group
+ self.bounds = bounds
+ self.duration = duration
+ self.time = duration
+
+ def rand_vel(self):
+ return 100, 100
+
+ def rand_color(self):
+ return 255, 255, 0
+
+ def spawn(self):
+ x = randrange(self.bounds.width - self.ship_type.width) + self.bounds.left
+ y = randrange(self.bounds.height - self.ship_type.height) + self.bounds.top
+ vx, vy = self.rand_vel()
+ color = self.rand_color()
+
+ ship = self.ship_type(x, y, vx, vy, self.bounds, color)
+ self.group.add(ship)
+
+ def update(self, dt):
+ self.time += dt
+ if self.time >= self.duration:
+ self.time = 0
+ self.spawn()
+
+
View
34 samplecode/tiefighter_example/utils.py
@@ -0,0 +1,34 @@
+from random import randint, random
+
+import pygame
+
+def rand_odds(n,t):
+ n = n / float(t)
+ return random() < n
+
+
+def randint_neg(lo,hi):
+ r = randint(lo,hi)
+ sign = randint(0,1) * 2 - 1
+ return r * sign
+
+
+def collide_rect_circle(rect, pos, radius):
+ cx, cy = pos
+ rx, ry = rect.center
+
+ w = rect.width/2.0
+ h = rect.height/2.0
+ cdx = abs(cx - rect.centerx)
+ cdy = abs(cy - rect.centery)
+
+ if cdx > w+radius or cdy > h+radius:
+ return False
+
+ if cdx <= w or cdy <= h:
+ return True
+
+ corner = (cdx - w)**2 + (cdy-h)**2
+
+ return corner <= radius**2
+
Please sign in to comment.
Something went wrong with that request. Please try again.