Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/DerechosSprites.py
blob: 92e03416973e440e6f9e1d03dd87726a42d7a591 (plain)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import pygame
import os
import random
import math
from AnimatedSprite import AnimatedSprite

STOP = 0
LEFT = 1
RIGHT = 2
UP = 3
DOWN = 4
TALKING = 5

class MainSprite(AnimatedSprite):
    """
    MainSprite is an AnimatedSprite class for the main character

    images: list with sprite images
    frames: list with image# for every frame
    next_frames: list with next frame from current one
    dxs: list with dx for current frame
    dys: list with dy for current frame
    xini: initial x position
    yini: initial y position
    fps: fps
    """

    def __init__(self, images, frames, next_frames, 
                 dxs, dys, xini, yini, fps = 10):

        AnimatedSprite.__init__(self, images, frames, next_frames, 
                                dxs, dys, xini, yini, fps)
        self.move = STOP


    def do_move(self,grid):

        if self.move == RIGHT:
            if self.x+62<1200:
                if grid[(self.y+78)/50][(self.x+62)/50] == 0 and \
                        grid[(self.y+40)/50][(self.x+62)/50] == 0:
                    self.x += 8
                    self._frame = self._next_frames[self._frame]
                else:
                    self.x += 8 - (self.x+62)%50 -1
            else:
                self.x = 1146
        elif self.move == LEFT:
            if self.x+8 > 0:
                if grid[(self.y+78)/50][(self.x+8)/50] == 0 and \
                        grid[(self.y+40)/50][(self.x+8)/50] == 0:
                    self.x -= 8
                    self._frame = self._next_frames[self._frame]
                else:
                    self.x += -8 + 50 - (self.x+8)%50 +1
            else:
                self.x = -16
        elif self.move == UP:
            if self.y+16 > 0:
                if grid[(self.y+34)/50][(self.x+16)/50] == 0 and \
                        grid[(self.y+34)/50][(self.x+54)/50] == 0:
                    self.y -= 6
                    self._frame = self._next_frames[self._frame]
                else:
                    self.y += -6 + 50 - (self.y+34)%50 +1
            else:
                self.y = -22
        elif self.move == DOWN:
            if self.y+84 < 900:
                if grid[(self.y+84)/50][(self.x+16)/50] == 0 and \
                        grid[(self.y+84)/50][(self.x+54)/50] == 0:
                    self.y += 6
                    self._frame = self._next_frames[self._frame]
                else:
                    self.y += 6 - (self.y+84)%50 -1
            else:
                self.y = 900-78
        elif self.move == TALKING:
            pass


class BoySprite(AnimatedSprite):
    """
    BoySprite is an AnimatedSprite class for the boys in the game

    images: list with sprite images
    frames: list with image# for every frame
    next_frames: list with next frame from current one
    dxs: list with dx for current frame
    dys: list with dy for current frame
    xini: initial x position
    yini: initial y position
    fps: fps
    """

    def __init__(self, images, frames, next_frames, 
                 dxs, dys, xini, yini, fps = 10):

        AnimatedSprite.__init__(self, images, frames, next_frames, 
                                dxs, dys, xini, yini, fps)
        self.move = random.randint(0,4)

    def do_move(self,grid):

        if self.move == RIGHT:
            rn = random.randint(1,100)
            if rn > 97:
                self.startup_frame()
                self.move = STOP
            elif self.x+62<1200:
                if grid[(self.y+78)/50][(self.x+62)/50] == 0 and \
                        grid[(self.y+40)/50][(self.x+62)/50] == 0:
                    self.x += 8
                    self._frame = self._next_frames[self._frame]
                else:
                    self.x += 8 - (self.x+62)%50 -1
                    self.move = random.randint(1,4)
                    self.startup_frame()
            else:
                self.x = 1146
                self.move = random.randint(1,4)
                self.startup_frame()
        elif self.move == LEFT:
            rn = random.randint(1,100)
            if rn > 97:
                self.startup_frame()
                self.move = STOP
            elif self.x+8 > 0:
                if grid[(self.y+78)/50][(self.x+8)/50] == 0 and \
                        grid[(self.y+40)/50][(self.x+8)/50] == 0:
                    self.x -= 8
                    self._frame = self._next_frames[self._frame]
                else:
                    self.x += -8 + 50 - (self.x+8)%50 +1
                    self.move = random.randint(1,4)
                    self.startup_frame()
            else:
                self.x = -16
                self.move = random.randint(1,4)
                self.startup_frame()
        elif self.move == UP:
            rn = random.randint(1,100)
            if rn > 97:
                self.startup_frame()
                self.move = STOP
            elif self.y+16 > 0:
                if grid[(self.y+34)/50][(self.x+16)/50] == 0 and \
                        grid[(self.y+34)/50][(self.x+54)/50] == 0:
                    self.y -= 6
                    self._frame = self._next_frames[self._frame]
                else:
                    self.y += -6 + 50 - (self.y+34)%50 +1
                    self.move = random.randint(1,4)
                    self.startup_frame()
            else:
                self.y = -22
                self.move = random.randint(1,4)
                self.startup_frame()
        elif self.move == DOWN:
            rn = random.randint(1,100)
            if rn > 97:
                self.startup_frame()
                self.move = STOP
            elif self.y+84 < 900:
                if grid[(self.y+84)/50][(self.x+16)/50] == 0 and \
                        grid[(self.y+84)/50][(self.x+54)/50] == 0:
                    self.y += 6
                    self._frame = self._next_frames[self._frame]
                else:
                    self.y += 6 - (self.y+84)%50 -1
                    self.move = random.randint(1,4)
                    self.startup_frame()
            else:
                self.y = 900-78
                self.move = random.randint(1,4)
                self.startup_frame()
        elif self.move == STOP:
            rn = random.randint(1,100)
            if rn > 98:
                self.move = random.randint(1,4)
                self.startup_frame()
        elif self.move == TALKING:
            pass

    def startup_frame(self):
        if self.move == UP:
            self._frame = 0
        elif self.move == DOWN:
            self._frame = 8
        elif self.move == LEFT:
            self._frame = 16
        elif self.move == RIGHT:
            self._frame = 24

    def draw_talking_range(self, screen):
        pygame.draw.arc(screen, (200, 0, 0), (self.x+9, self.y, 48, 48),
                        -math.pi/4, math.pi/4, 2)
        pygame.draw.arc(screen, (200, 0, 0), (self.x+9, self.y, 48, 48),
                        math.pi/4*3, math.pi/4*5, 2)

    def draw_talking(self, screen):
        pygame.draw.arc(screen, (0, 0, 200), (self.x+9, self.y, 48, 48),
                        -math.pi/4, math.pi/4, 2)
        pygame.draw.arc(screen, (0, 0, 200), (self.x+9, self.y, 48, 48),
                        math.pi/4*3, math.pi/4*5, 2)



def draw_grid(grid,screen):
    for yi in range(18):
        for xi in range(24):
            if grid[yi][xi] == 1:
                pygame.draw.rect(screen,(100,100,100),(xi*50,yi*50,50,50),2)


if __name__ == "__main__":

    pygame.display.init()
    screen = pygame.display.set_mode((1200,900))
    clock = pygame.time.Clock()

    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):
        images.append(master_image.subsurface((i*w,0,w,h)))
    for i in xrange(4):
        images.append(master_image.subsurface((i*w,h,w,h)))
    for i in xrange(4):
        images.append(master_image.subsurface((i*w,2*h,w,h)))
    for i in xrange(4):
        images.append(master_image.subsurface((i*w,3*h,w,h)))

    bg_image = pygame.image.load(os.path.join('images', 
                   'derechos-plaza-fondo.jpg')).convert_alpha()

    fms = [0, 1, 0, 2, 0, 1, 0, 3, # up
           4, 5, 4, 6, 4, 5, 4, 7, # down
           8, 9, 8, 10, 8, 9, 8, 11, # left
           15, 13, 15, 14, 15, 13, 15, 12] # right
    nf = [1, 2, 3, 4, 5, 6, 7, 0,
          9, 10, 11, 12, 13, 14, 15, 8,
          17, 18, 19, 20, 21, 22, 23, 16,
          25, 26, 27, 28, 29, 30, 31, 24]
    dxs = [0, 0, 0, 0, 0, 0, 0, 0,
           0, 0, 0, 0, 0, 0, 0, 0, 
           -8, -8, -8, -8, -8, -8, -8, -8,
           8, 8, 8, 8, 8, 8, 8, 8]
    dys = [-6, -6, -6, -6, -6, -6, -6, -6,
            6, 6, 6, 6, 6, 6, 6, 6,
            0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0]

    sp = MainSprite(images, fms, nf, dxs, dys, 30, 370, 10)
    sp2 = BoySprite(images, fms, nf, dxs, dys, 150, 470, 10)

    grid = [[0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1],
              [0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0],
              [0,0,1,1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,0,0,0,0],
              [0,0,1,1,1,1,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,1],
              [0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,1],
              [0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,0,0,1,1,1,0,0,1,1],
              [1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,0,0,1,1,1,0,0,1,1],
              [0,0,0,0,0,0,0,1,1,1,1,0,0,1,1,0,0,1,1,1,0,0,1,1],
              [0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0],
              [0,0,1,1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0],
              [0,0,1,0,0,0,0,1,1,1,1,0,0,1,1,1,1,0,0,1,1,0,0,0],
              [0,0,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,1,1,1,0,0],
              [0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,1,1,1,0,0],
              [0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1],
              [1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1],
              [1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
              [1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,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
    talking_range = False
    talking = False
    
    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 and not talking:
                    sp.update_move(UP)
                    sp.update_frame(0)
                elif event.key == pygame.K_DOWN and not talking:
                    sp.update_move(DOWN)
                    sp.update_frame(8)
                elif event.key == pygame.K_LEFT and not talking:
                    sp.update_move(LEFT)
                    sp.update_frame(16)
                elif event.key == pygame.K_RIGHT and not talking:
                    sp.update_move(RIGHT)
                    sp.update_frame(24)
                elif event.key == pygame.K_SPACE and talking_range:
                    if not talking:
                        talking = True
                        sp.update_move(TALKING)
                        sp2.update_move(TALKING)
                        print("Talking...")
                    else:
                        talking = False
                        sp.update_move(STOP)
                        sp2.update_move(STOP)
                        print("Not talking...")
            elif event.type == pygame.KEYUP:
                keys_pressed = pygame.key.get_pressed()
                if event.key == pygame.K_UP and not talking:
                    if keys_pressed[pygame.K_LEFT]:
                        sp.update_move(LEFT)
                        sp.update_frame(16)
                    elif keys_pressed[pygame.K_RIGHT]:
                        sp.update_move(RIGHT)
                        sp.update_frame(24)
                    elif keys_pressed[pygame.K_DOWN]:
                        sp.update_move(DOWN)
                        sp.update_frame(8)
                    else:
                        sp.update_move(0)
                        sp.update_frame(0)
                elif event.key == pygame.K_DOWN and not talking:
                    if keys_pressed[pygame.K_LEFT]:
                        sp.update_move(LEFT)
                        sp.update_frame(16)
                    elif keys_pressed[pygame.K_RIGHT]:
                        sp.update_move(RIGHT)
                        sp.update_frame(24)
                    elif keys_pressed[pygame.K_UP]:
                        sp.update_move(UP)
                        sp.update_frame(0)
                    else:
                        sp.update_move(0)
                        sp.update_frame(8)
                elif event.key == pygame.K_LEFT and not talking:
                    if keys_pressed[pygame.K_UP]:
                        sp.update_move(UP)
                        sp.update_frame(0)
                    elif keys_pressed[pygame.K_DOWN]:
                        sp.update_move(DOWN)
                        sp.update_frame(8)
                    elif keys_pressed[pygame.K_RIGHT]:
                        sp.update_move(RIGHT)
                        sp.update_frame(24)
                    else:
                        sp.update_move(0)
                        sp.update_frame(16)
                elif event.key == pygame.K_RIGHT and not talking:
                    if keys_pressed[pygame.K_UP]:
                        sp.update_move(UP)
                        sp.update_frame(0)
                    elif keys_pressed[pygame.K_DOWN]:
                        sp.update_move(DOWN)
                        sp.update_frame(8)
                    elif keys_pressed[pygame.K_LEFT]:
                        sp.update_move(LEFT)
                        sp.update_frame(16)
                    else:
                        sp.update_move(0)
                        sp.update_frame(24)
        
        screen.blit(bg_image, (0, 0))
#        draw_grid(grid,screen)

        time_passed = clock.tick(20)

        sp.update(time_passed, grid)
        sp2.update(time_passed, grid)

        (ulx, uly) = sp.upper_left_tile()
        (ulx2, uly2) = sp2.upper_left_tile()

        if ulx-ulx2 >= -1 and ulx-ulx2 <=1 \
                and uly-uly2 >= -1 and uly-uly2<=1:
            talking_range = True
        else:
            talking_range = False

        if sp.y < sp2.y:
            sp.render(screen)
            sp2.render(screen)
        else:
            sp2.render(screen)
            sp.render(screen)
        
        if talking_range and not talking:
            sp2.draw_talking_range(screen)
        elif talking:
            sp2.draw_talking(screen)
        
        pygame.display.flip()