From da9b951219f8d9955ec4c85caef6058fa0577b38 Mon Sep 17 00:00:00 2001 From: Gabriel Eirea Date: Thu, 13 Jan 2011 18:41:07 +0000 Subject: Added dialog --- diff --git a/AnimatedSprite.py b/AnimatedSprite.py index 76960c8..a7824f4 100644 --- a/AnimatedSprite.py +++ b/AnimatedSprite.py @@ -1,6 +1,4 @@ import pygame -#import os -#import random class AnimatedSprite(pygame.sprite.Sprite): """ diff --git a/AnimatedSprite.pyc b/AnimatedSprite.pyc index 85c3645..decb905 100644 --- a/AnimatedSprite.pyc +++ b/AnimatedSprite.pyc Binary files differ diff --git a/DerechosSprites.py b/DerechosSprites.py index 1d383ec..99b8056 100644 --- a/DerechosSprites.py +++ b/DerechosSprites.py @@ -3,6 +3,7 @@ import os import random import math from AnimatedSprite import AnimatedSprite +from DialogSprite import DialogSprite STOP = 0 LEFT = 1 @@ -32,7 +33,6 @@ class MainSprite(AnimatedSprite): dxs, dys, xini, yini, fps) self.move = STOP - def do_move(self,grid): if self.move == RIGHT: @@ -195,12 +195,6 @@ class BoySprite(AnimatedSprite): elif self.move == RIGHT: self._frame = 24 - 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 collision_rect(self): return (self.x+15, self.y+38, 38, 38) @@ -214,7 +208,7 @@ def draw_grid(grid,screen): if __name__ == "__main__": - pygame.display.init() + pygame.init() screen = pygame.display.set_mode((1200,900)) clock = pygame.time.Clock() @@ -262,13 +256,13 @@ if __name__ == "__main__": images.append(master_image.subsurface((0, 0, w, h))) images.append(master_image.subsurface((0, h, w, h))) fms = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 1, 1, 1, 1] + 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1] nf = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, - 16, 17, 18, 19, 20, 21, 22, 23, 0] + 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 0] dxs = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0] + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] dys = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0] + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] sb = AnimatedSprite(images, fms, nf, dxs, dys, 0, 0, 10) bg_image = pygame.image.load(os.path.join('images', @@ -293,6 +287,12 @@ if __name__ == "__main__": [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]] + font24 = pygame.font.Font(None, 24) + + txts = ["Una prueba\ndel poder de las palabras", + "Y al que no le guste\nque se vaya\na donde ya sabe"] + dia = DialogSprite(txts, font24, 100, 100) + playing = True talking_range = False talking = False @@ -319,14 +319,16 @@ if __name__ == "__main__": elif event.key == pygame.K_SPACE and talking_range: if not talking: talking = True + dia.restart(sp2.rect()) sp.update_move(TALKING) sp2.update_move(TALKING) print("Talking...") else: - talking = False - sp.update_move(STOP) - sp2.update_move(STOP) - print("Not talking...") + if not dia.update(): + talking = False + sp.update_move(STOP) + sp2.update_move(STOP) + print("Not talking...") elif event.key == pygame.K_ESCAPE: playing = False elif event.type == pygame.KEYUP: @@ -414,7 +416,7 @@ if __name__ == "__main__": sb.update(time_passed,grid) sb.render(screen) elif talking: - sp2.draw_talking(screen) + dia.render(screen) # sp.draw_rect(screen) diff --git a/DialogSprite.py b/DialogSprite.py new file mode 100644 index 0000000..a44aebf --- /dev/null +++ b/DialogSprite.py @@ -0,0 +1,72 @@ +import pygame + +class DialogSprite(pygame.sprite.Sprite): + """ + DialogSprite is a custom Sprite class for text dialogs + + text: list with text to display; each element can have \n to separate lines + font: font to use + xini: initial x position + yini: initial y position + """ + + def __init__(self, text, font, xini, yini): + + pygame.sprite.Sprite.__init__(self) + self._font = font + self._text = text + self._ntexts = len(text) + self.current_text = 0 + self.x = xini + self.y = yini + self.width = 0 + self.height = 0 + for t in self._text: + lines = t.split("\n") + lh = 0 + for l in lines: + (w, h) = self._font.size(l) + if w > self.width: + self.width = w + lh += h + if lh > self.height: + self.height = lh + self.width += 10 + self.height += 10 + self.bg = pygame.Surface((self.width, self.height)) + self.bg.fill((255, 255, 255)) + + def update(self): + + if self.current_text == self._ntexts: + return False + self.image = self.bg.copy() + yline = 5 + for l in self._text[self.current_text].split("\n"): + text_img = self._font.render(l, 1, (0, 0, 0)) + textrect = text_img.get_rect() + textrect.left = 5 + textrect.top = yline + self.image.blit(text_img, textrect) + yline += self._font.get_height() + self.current_text += 1 + return True + + def render(self, screen): + + screen.blit(self.image, (self.x, self.y)) + + def rect(self): + return (self.x, self.y, self.width, self.height) + + def restart(self, rect): + self.x = rect[0] + int(rect[2]/2) - int(self.width/2) + self.y = rect[1] - self.height + self.current_text = 0 + self.update() + +if __name__ == "__main__": + + print("You can't run this file.") + print("You have to import DialogSprite from another Python file.") + -- cgit v0.9.1