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: s.groups = g.string2groups('player')
24: s.score = 0
25: g.player = s
In player_loop(), I now check if the player has gone off screen (due to blocks
in the players way. If that happens, the game quits.
32: if s.rect.right < g.view.left: g.quit = 1
A few functions are added to handle player/tile hits
59:def tile_block(g,t,a):
60: c = t.config
61: if (c['top'] == 1 and a._rect.bottom <= t._rect.top and a.rect.bottom > t.rect.top):
62: a.rect.bottom = t.rect.top
63: if (c['left'] == 1 and a._rect.right <= t._rect.left and a.rect.right > t.rect.left):
64: a.rect.right = t.rect.left
65: if (c['right'] == 1 and a._rect.left >= t._rect.right and a.rect.left < t.rect.right):
66: a.rect.left = t.rect.right
67: if (c['bottom'] == 1 and a._rect.top >= t._rect.bottom and a.rect.top < t.rect.bottom):
68: a.rect.top = t.rect.bottom
69:
70:def tile_coin(g,t,a):
71: a.score += 100
72: g.set((t.tx,t.ty),0)
73:
74:def tile_fire(g,t,a):
75: g.quit = 1
Here I initialize the tile data. The columns are (groups,function,config)
93:tdata = {
94: 0x01:('player',tile_block,{'top':1,'bottom':1,'left':1,'right':1}),
95: 0x02:('player',tile_block,{'top':1,'bottom':1,'left':1,'right':1}),
96: 0x20:('player',tile_coin,None),
97: 0x30:('player',tile_fire,None),
98: }
In init(), this line has been changed to load the tiles with their properties (groups, functions, and config.
109: g.tga_load_tiles('tiles.tga',(TW,TH),tdata)
In run(), I have changed the update function to paint, so that
I can display the player's score on the screen at all times.
141: g.paint(g.screen)
142: img = g.font.render('%05d'%g.player.score,1,(0,0,0))
143: g.screen.blit(img,(0+1,SH-img.get_height()+1))
144: img = g.font.render('%05d'%g.player.score,1,(255,255,255))
145: g.screen.blit(img,(0,SH-img.get_height()))
146: pygame.display.flip()