Merge branch 'master' of git.ucc.asn.au:/matches/honours
[matches/honours.git] / research / transmission_spectroscopy / simulator / pgu-0.18 / examples / tilevid4.py
1 """<title>tutorial on how to add in tile hit handlers, set up groups and agroups</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 def player_new(g,t,value):
18     g.clayer[t.ty][t.tx] = 0
19     s = tilevid.Sprite(g.images['player'],t.rect)
20     g.sprites.append(s)
21     s.loop = player_loop
22     ##In player_new() I add the player to the 'player' group, and set the score to 0. I also set the game's player to this Sprite.
23     ##::
24     s.groups = g.string2groups('player')
25     s.score = 0
26     g.player = s
27     ##
28     
29 def player_loop(g,s):
30     ##In player_loop(), I now check if the player has gone off screen (due to blocks
31     ##in the players way.  If that happens, the game quits.
32     ##::
33     if s.rect.right < g.view.left: g.quit = 1
34     ##
35     
36     s.rect.x += SPEED
37     
38     keys = pygame.key.get_pressed()
39     dx,dy = 0,0
40     if keys[K_UP]: dy-=1
41     if keys[K_DOWN]: dy+=1
42     if keys[K_LEFT]: dx-=1
43     if keys[K_RIGHT]: dx+=1
44     s.rect.x += dx*5
45     s.rect.y += dy*5
46     s.rect.clamp_ip(g.view)
47     
48 def enemy_new(g,t,value):
49     g.clayer[t.ty][t.tx] = 0
50     s = tilevid.Sprite(g.images['enemy'],t.rect)
51     g.sprites.append(s)
52     s.loop = enemy_loop
53     
54 def enemy_loop(g,s):
55     if s.rect.right < g.view.left:
56         g.sprites.remove(s)
57     
58 ##A few functions are added to handle player/tile hits
59 ##::
60 def tile_block(g,t,a):
61     c = t.config
62     if (c['top'] == 1 and a._rect.bottom <= t._rect.top and a.rect.bottom > t.rect.top):
63         a.rect.bottom = t.rect.top
64     if (c['left'] == 1 and a._rect.right <= t._rect.left and a.rect.right > t.rect.left):
65         a.rect.right = t.rect.left
66     if (c['right'] == 1 and a._rect.left >= t._rect.right and a.rect.left < t.rect.right):
67         a.rect.left = t.rect.right
68     if (c['bottom'] == 1 and a._rect.top >= t._rect.bottom and a.rect.top < t.rect.bottom):
69         a.rect.top = t.rect.bottom
70
71 def tile_coin(g,t,a):
72     a.score += 100
73     g.set((t.tx,t.ty),0)
74     
75 def tile_fire(g,t,a):
76     g.quit = 1
77 ##
78
79 idata = [
80     ('player','player.tga',(4,4,24,24)),
81     ('enemy','enemy.tga',(4,4,24,24)),
82     ('shot','shot.tga',(1,2,6,4)),
83     ]
84
85 cdata = {
86     1:(player_new,None),
87     2:(enemy_new,None),
88     3:(enemy_new,None),
89     4:(enemy_new,None),
90     }
91
92 ##Here I initialize the tile data.  The columns are (groups,function,config)
93 ##::
94 tdata = {
95     0x01:('player',tile_block,{'top':1,'bottom':1,'left':1,'right':1}),
96     0x02:('player',tile_block,{'top':1,'bottom':1,'left':1,'right':1}),
97     0x20:('player',tile_coin,None),
98     0x30:('player',tile_fire,None),
99     }
100 ##
101
102     
103 def init():
104     g = tilevid.Tilevid()
105     
106     g.screen = pygame.display.set_mode((SW,SH),SWSURFACE)
107     
108     ##In init(), this line has been changed to load the tiles with their properties (groups, functions, and config.
109     ##::
110     g.tga_load_tiles('tiles.tga',(TW,TH),tdata)
111     ##
112     g.tga_load_level('level.tga',1)
113     g.bounds = pygame.Rect(TW,TH,(len(g.tlayer[0])-2)*TW,(len(g.tlayer)-2)*TH)
114     g.load_images(idata)
115     g.run_codes(cdata,(0,0,25,17))
116     pygame.font.init()
117     g.font = pygame.font.SysFont('helvetica',16)
118
119     return g
120     
121 def run(g): 
122     g.quit = 0
123     
124     t = timer.Timer(FPS)
125     
126     g.paint(g.screen)
127     pygame.display.flip()
128     
129     while not g.quit:
130         for e in pygame.event.get():
131             if e.type is QUIT: g.quit = 1
132             if e.type is KEYDOWN and e.key == K_ESCAPE: g.quit = 1
133             
134         g.view.x += SPEED
135         g.run_codes(cdata,(g.view.right/TW,0,1,17))
136         
137         g.loop()
138
139         ##In run(), I have changed the update function to paint, so that
140         ##I can display the player's score on the screen at all times.
141         ##::
142         g.paint(g.screen)
143         img = g.font.render('%05d'%g.player.score,1,(0,0,0))
144         g.screen.blit(img,(0+1,SH-img.get_height()+1))
145         img = g.font.render('%05d'%g.player.score,1,(255,255,255))
146         g.screen.blit(img,(0,SH-img.get_height()))
147         pygame.display.flip()
148         ##
149
150         t.tick()
151     
152 run(init())

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