ec0993cb7b6bcd07ea36465b22c4c909e440fb3b
[matches/honours.git] / research / transmission_spectroscopy / simulator / pgu-0.18 / examples / tilevid3.py
1 """<title>tutorial on how to load, display, and use Sprites</title>"""
2
3 import pygame
4 from pygame.rect import Rect
5 from pygame.locals import *
6
7 # the following line is not needed if pgu is installed
8 import sys; sys.path.insert(0, "..")
9
10 from pgu import tilevid, timer
11
12 SW,SH = 320,240
13 TW,TH = 16,16
14 SPEED = 2
15 FPS = 40
16
17 ##Here are the various functions I used for the player and enemy logic.
18 ##- After creating a Sprite, I set the clayer to 0 so that more than one player / enemy is created.
19 ##- the Sprite must be added to the sprites list
20 ##- instead of using class methods, I prefer to use set functions for
21 ##the various methods -- loop and hit.
22 ##- Enemies are removed when they go off screen.
23 ##::
24 def player_new(g,t,value):
25     g.clayer[t.ty][t.tx] = 0
26     s = tilevid.Sprite(g.images['player'],t.rect)
27     g.sprites.append(s)
28     s.loop = player_loop
29     
30 def player_loop(g,s):
31     s.rect.x += SPEED
32     
33     keys = pygame.key.get_pressed()
34     dx,dy = 0,0
35     if keys[K_UP]: dy-=1
36     if keys[K_DOWN]: dy+=1
37     if keys[K_LEFT]: dx-=1
38     if keys[K_RIGHT]: dx+=1
39     s.rect.x += dx*5
40     s.rect.y += dy*5
41     s.rect.clamp_ip(g.view)
42     
43 def enemy_new(g,t,value):
44     g.clayer[t.ty][t.tx] = 0
45     s = tilevid.Sprite(g.images['enemy'],t.rect)
46     g.sprites.append(s)
47     s.loop = enemy_loop
48     
49 def enemy_loop(g,s):
50     if s.rect.right < g.view.left:
51         g.sprites.remove(s)
52 ##
53 ##Here I initialize the image data.  The columns are (name,file_name,shape)
54 ##::
55 idata = [
56     ('player','player.tga',(4,4,24,24)),
57     ('enemy','enemy.tga',(4,4,24,24)),
58     ('shot','shot.tga',(1,2,6,4)),
59     ]
60 ##
61 ##Here I initialize the code data.  The columns are (function, config).
62 ##::
63 cdata = {
64     1:(player_new,None),
65     2:(enemy_new,None),
66     3:(enemy_new,None),
67     4:(enemy_new,None),
68     }
69 ##
70     
71 def init():
72     g = tilevid.Tilevid()
73     
74     g.screen = pygame.display.set_mode((SW,SH),SWSURFACE)
75     
76     g.tga_load_tiles('tiles.tga',(TW,TH))
77     g.tga_load_level('level.tga',1)
78     g.bounds = pygame.Rect(TW,TH,(len(g.tlayer[0])-2)*TW,(len(g.tlayer)-2)*TH)
79     
80     ##In init(), loading in the sprite images.
81     ##::
82     g.load_images(idata)
83     ##
84     
85     ##In init(), running the codes for the initial screen.
86     ##::
87     g.run_codes(cdata,(0,0,25,17))
88     ##
89     
90     return g
91     
92 def run(g): 
93     g.quit = 0
94     
95     t = timer.Timer(FPS)
96     
97     g.paint(g.screen)
98     pygame.display.flip()
99     
100     while not g.quit:
101         for e in pygame.event.get():
102             if e.type is QUIT: g.quit = 1
103             if e.type is KEYDOWN and e.key == K_ESCAPE: g.quit = 1
104             
105         g.view.x += SPEED
106
107         ##In run(), each frame I make sure to run the codes that are on the far
108         ##right of the screen.
109         ##::
110         g.run_codes(cdata,(g.view.right/TW,0,1,17))
111         ##
112         
113         g.loop()
114         updates = g.update(g.screen)
115         pygame.display.update(updates)
116         
117         t.tick()
118     
119 run(init())

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