1 """<title>tutorial on how to load, display, and use Sprites</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 ##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.
24 def player_new(g,t,value):
25 g.clayer[t.ty][t.tx] = 0
26 s = tilevid.Sprite(g.images['player'],t.rect)
33 keys = pygame.key.get_pressed()
36 if keys[K_DOWN]: dy+=1
37 if keys[K_LEFT]: dx-=1
38 if keys[K_RIGHT]: dx+=1
41 s.rect.clamp_ip(g.view)
43 def enemy_new(g,t,value):
44 g.clayer[t.ty][t.tx] = 0
45 s = tilevid.Sprite(g.images['enemy'],t.rect)
50 if s.rect.right < g.view.left:
53 ##Here I initialize the image data. The columns are (name,file_name,shape)
56 ('player','player.tga',(4,4,24,24)),
57 ('enemy','enemy.tga',(4,4,24,24)),
58 ('shot','shot.tga',(1,2,6,4)),
61 ##Here I initialize the code data. The columns are (function, config).
74 g.screen = pygame.display.set_mode((SW,SH),SWSURFACE)
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)
80 ##In init(), loading in the sprite images.
85 ##In init(), running the codes for the initial screen.
87 g.run_codes(cdata,(0,0,25,17))
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
107 ##In run(), each frame I make sure to run the codes that are on the far
108 ##right of the screen.
110 g.run_codes(cdata,(g.view.right/TW,0,1,17))
114 updates = g.update(g.screen)
115 pygame.display.update(updates)