Permalink
Browse files

tie fighter examples

  • Loading branch information...
1 parent dac9a6b commit e55c084c44fcb5b9d993fd05d334e705ce20ad6d Alec Goebel committed Apr 6, 2012
@@ -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)
@@ -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
+
@@ -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))
Oops, something went wrong. Retry.

0 comments on commit e55c084

Please sign in to comment.