1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
import pygame
import os
from AnimatedSprite import *
class Game():
def __init__(self):
# pygame.display.init()
# self.screen = pygame.display.set_mode((1200,900))
# self.clock = pygame.time.Clock()
# load images
# images_main = self.load_sprite_group(50,50,"main.png")
# load levels
# setup objects
# self.me = AnimatedSprite(images_main,30)
# start
print("initializing game...")
pygame.display.init()
self.screen = pygame.display.set_mode((1200,900))
self.clock = pygame.time.Clock()
print("loading images...")
self.images = []
master_image = pygame.image.load(os.path.join('images',
'xoxi-ninios-sprite-01.png')).convert_alpha()
master_width, master_height = master_image.get_size()
w = int(master_width/4)
h = int(master_height/4)
for i in xrange(4):
self.images.append(master_image.subsurface((i*w,0,w,h)))
for i in xrange(4):
self.images.append(master_image.subsurface((i*w,h,w,h)))
for i in xrange(4):
self.images.append(master_image.subsurface((i*w,2*h,w,h)))
for i in xrange(4):
self.images.append(master_image.subsurface((i*w,3*h,w,h)))
self.bg_image = pygame.image.load(os.path.join('images',
'derechos-plaza-fondo.jpg')).convert_alpha()
sps = [0, 1, 0, 2, 0, 1, 0, 3, 4, 5, 4, 6, 4, 5, 4, 7, 8, 9, 8, 10,
8, 9, 8, 11, 15, 13, 15, 14, 15, 13, 15, 12]
nf = [1, 2, 3, 4, 5, 6, 7, 0, 9, 10, 11, 12, 13, 14, 15, 8, 17, 18, 19,
20, 21, 22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,0,0,0,0,0,1],
[1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1]]
playing = True
sp.updateMove(0)
sp2.updateMove(random.randint(1,4))
def load_sprite_group(self, w, h, filename):
'''
Specs :
Master can be any height.
Sprites frames width must be the same width
Master width must be len(frames)*frame.width
Assuming you ressources directory is named "ressources"
'''
images = []
master_image = pygame.image.load(os.path.join('images', filename)).convert_alpha()
master_width, master_height = master_image.get_size()
for i in xrange(int(master_width/w)):
images.append(master_image.subsurface((i*w,0,w,h)))
return images
def start (self):
print("starting...")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
if event.type == pygame.MOUSEBUTTONDOWN:
pass
self.screen.fill((0,0,0))
time_passed = self.clock.tick(30)
self.me.render(self.screen)
pygame.display.flip()
def main():
# game = Game()
# game.start()
while playing:
for event in pygame.event.get():
if event.type == pygame.QUIT:
playing = False
elif event.type == pygame.MOUSEBUTTONDOWN:
pass
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
sp.updateMove(UP)
sp.updateFrame(0)
if event.key == pygame.K_DOWN:
sp.updateMove(DOWN)
sp.updateFrame(8)
if event.key == pygame.K_LEFT:
sp.updateMove(LEFT)
sp.updateFrame(16)
if event.key == pygame.K_RIGHT:
sp.updateMove(RIGHT)
sp.updateFrame(24)
elif event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
sp.updateMove(0)
sp.updateFrame(0)
if event.key == pygame.K_DOWN:
sp.updateMove(0)
sp.updateFrame(8)
if event.key == pygame.K_LEFT:
sp.updateMove(0)
sp.updateFrame(16)
if event.key == pygame.K_RIGHT:
sp.updateMove(0)
sp.updateFrame(24)
sp.domove(grilla)
sp2.ai(grilla)
screen.blit(bg_image, (0, 0))
# dibujar_grilla(grilla,screen)
time_passed = clock.tick(20)
sp.update(time_passed)
sp2.update(time_passed)
sp.render(screen)
sp2.render(screen)
pygame.display.flip()
if __name__ == "__main__":
main()
|