Commit before breaking everything
[matches/honours.git] / research / transmission_spectroscopy / simulator / pgu-0.18 / examples / engine1.py
1 """<title>an example of engine usage</title>"""
2
3 import pygame
4 from pygame.locals import *
5
6 # the following line is not needed if pgu is installed
7 import sys; sys.path.insert(0, "..")
8
9 from pgu import engine
10
11 pygame.font.init()
12
13 screen = pygame.display.set_mode((320,240),SWSURFACE)
14
15 class Red(engine.State):
16     def paint(self,s): 
17         s.fill((255,0,0))
18         pygame.display.flip()
19     def event(self,e): 
20         if e.type is KEYDOWN: return Green(self.game)
21         
22 class Green(engine.State):
23     def paint(self,s): 
24         s.fill((0,255,0))
25         pygame.display.flip()
26     def event(self,e): 
27         if e.type is KEYDOWN: return Blue(self.game)
28
29 ##A state may subclass engine.State.
30 ##::
31 class Blue(engine.State):
32     ##
33     ##The init method should load data, etc.  The __init__ method
34     ##should do nothing but record the parameters.  If the init method
35     ##returns a value, it becomes the new state.
36     ##::
37     def init(self):
38         self.image = pygame.image.load("cuzco.png")
39         self.pos = 0,0
40         self._pos = self.pos
41     ##
42     ##The paint method is called once.  If you call repaint(), it
43     ##will be called again.
44     ##::
45     def paint(self,s): 
46         s.fill((0,0,255))
47         s.blit(self.image,self.pos)
48         pygame.display.flip()
49     ##
50     ##Every time an event occurs, event is called.  If the event method
51     ##returns a value, it will become the new state.
52     ##::
53     def event(self,e): 
54         if e.type is KEYDOWN: return Red(self.game)
55     ##
56     ##Loop is called once a frame.  It should contain all the
57     ##logic.  If the loop method returns a value it will become the
58     ##new state.
59     ##::
60     def loop(self):
61         self._pos = self.pos
62         self.pos = self.pos[0]+1,self.pos[1]+1
63     ##
64     ##Update is called once a frame.  It should update the display.
65     ##::
66     def update(self,screen):
67         screen.fill((0,0,255),pygame.Rect(self._pos[0],self._pos[1],self.image.get_width(),self.image.get_height()))
68         screen.blit(self.image,self.pos)
69         pygame.display.flip() #better to do updates
70     ##
71         
72 game = engine.Game()
73 game.run(Red(game),screen)

UCC git Repository :: git.ucc.asn.au