1 """<title>tutorial on how to add in tile hit handlers, set up groups and agroups</title>"""
4 from pygame.rect import Rect
5 from pygame.locals import *
7 # the following line is not needed if pgu is installed
8 import sys; sys.path.insert(0, "..")
10 from pgu import tilevid, timer
17 def player_new(g,t,value):
18 g.clayer[t.ty][t.tx] = 0
19 s = tilevid.Sprite(g.images['player'],t.rect)
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.
24 s.groups = g.string2groups('player')
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.
33 if s.rect.right < g.view.left: g.quit = 1
38 keys = pygame.key.get_pressed()
41 if keys[K_DOWN]: dy+=1
42 if keys[K_LEFT]: dx-=1
43 if keys[K_RIGHT]: dx+=1
46 s.rect.clamp_ip(g.view)
48 def enemy_new(g,t,value):
49 g.clayer[t.ty][t.tx] = 0
50 s = tilevid.Sprite(g.images['enemy'],t.rect)
55 if s.rect.right < g.view.left:
58 ##A few functions are added to handle player/tile hits
60 def tile_block(g,t,a):
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
80 ('player','player.tga',(4,4,24,24)),
81 ('enemy','enemy.tga',(4,4,24,24)),
82 ('shot','shot.tga',(1,2,6,4)),
92 ##Here I initialize the tile data. The columns are (groups,function,config)
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),
104 g = tilevid.Tilevid()
106 g.screen = pygame.display.set_mode((SW,SH),SWSURFACE)
108 ##In init(), this line has been changed to load the tiles with their properties (groups, functions, and config.
110 g.tga_load_tiles('tiles.tga',(TW,TH),tdata)
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)
115 g.run_codes(cdata,(0,0,25,17))
117 g.font = pygame.font.SysFont('helvetica',16)
127 pygame.display.flip()
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
135 g.run_codes(cdata,(g.view.right/TW,0,1,17))
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.
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()