diff options
author | Gabriel Eirea <geirea@gmail.com> | 2011-01-13 19:50:03 (GMT) |
---|---|---|
committer | Gabriel Eirea <geirea@gmail.com> | 2011-01-13 19:54:03 (GMT) |
commit | 14e3c6da673467618060b8813bad7b97e492f978 (patch) | |
tree | c18e8f10aa1bf0675700b1678ec9d37d8f5b0b4b | |
parent | da9b951219f8d9955ec4c85caef6058fa0577b38 (diff) |
Improved do_move code removing magic numbers
-rw-r--r-- | DerechosSprites.py | 115 | ||||
-rw-r--r-- | DialogSprite.py | 2 |
2 files changed, 64 insertions, 53 deletions
diff --git a/DerechosSprites.py b/DerechosSprites.py index 99b8056..4cbbf9b 100644 --- a/DerechosSprites.py +++ b/DerechosSprites.py @@ -35,46 +35,51 @@ class MainSprite(AnimatedSprite): def do_move(self,grid): + (xcoll, ycoll, wcoll, hcoll) = self.collision_rect() 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 + xright = xcoll + wcoll + self._dxs[self._frame] + if xright < 1200: + if grid[(ycoll+hcoll)/50][xright/50] == 0 \ + and grid[ycoll/50][xright/50] == 0: + self.x += self._dxs[self._frame] self._frame = self._next_frames[self._frame] else: - self.x += 8 - (self.x+62)%50 -1 + self.x += self._dxs[self._frame] - xright%50 - 1 else: - self.x = 1146 + self.x = 1200 - self.width + (xcoll-self.x) 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 + xleft = xcoll + self._dxs[self._frame] + if xleft > 0: + if grid[(ycoll+hcoll)/50][xleft/50] == 0 and \ + grid[ycoll/50][xleft/50] == 0: + self.x += self._dxs[self._frame] self._frame = self._next_frames[self._frame] else: - self.x += -8 + 50 - (self.x+8)%50 +1 + self.x += self._dxs[self._frame] + 50 - xleft%50 + 1 else: - self.x = -16 + self.x = -(xcoll-self.x) 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 + ytop = ycoll + self._dys[self._frame] + if ytop > 0: + if grid[ytop/50][(xcoll+wcoll)/50] == 0 and \ + grid[ytop/50][xcoll/50] == 0: + self.y += self._dys[self._frame] self._frame = self._next_frames[self._frame] else: - self.y += -6 + 50 - (self.y+34)%50 +1 + self.y += self._dys[self._frame] + 50 - ytop%50 + 1 else: - self.y = -22 + self.y = -(ycoll-self.y) 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 + ybot = ycoll + hcoll + self._dys[self._frame] + if ybot < 900: + if grid[ybot/50][xcoll/50] == 0 and \ + grid[ybot/50][(xcoll+wcoll)/50] == 0: + self.y += self._dys[self._frame] self._frame = self._next_frames[self._frame] else: - self.y += 6 - (self.y+84)%50 -1 + self.y += self._dys[self._frame] - ybot%50 - 1 else: - self.y = 900-78 + self.y = 900 - (ycoll-self.y) elif self.move == TALKING: pass @@ -105,76 +110,81 @@ class BoySprite(AnimatedSprite): def do_move(self,grid): + (xcoll, ycoll, wcoll, hcoll) = self.collision_rect() if self.move == RIGHT: + xright = xcoll + wcoll + self._dxs[self._frame] 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 + elif xright < 1200: + if grid[(ycoll+hcoll)/50][xright/50] == 0 \ + and grid[ycoll/50][xright/50] == 0: + self.x += self._dxs[self._frame] self._frame = self._next_frames[self._frame] else: - self.x += 8 - (self.x+62)%50 -1 + self.x += self._dxs[self._frame] - xright%50 - 1 self.move = random.randint(1,4) self.startup_frame() else: - self.x = 1146 + self.x = 1200 - self.width + (xcoll-self.x) self.move = random.randint(1,4) self.startup_frame() elif self.move == LEFT: + xleft = xcoll + self._dxs[self._frame] 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 + elif xleft > 0: + if grid[(ycoll+hcoll)/50][xleft/50] == 0 and \ + grid[ycoll/50][xleft/50] == 0: + self.x += self._dxs[self._frame] self._frame = self._next_frames[self._frame] else: - self.x += -8 + 50 - (self.x+8)%50 +1 + self.x += self._dxs[self._frame] + 50 - xleft%50 + 1 self.move = random.randint(1,4) self.startup_frame() else: - self.x = -16 + self.x = -(xcoll-self.x) self.move = random.randint(1,4) self.startup_frame() elif self.move == UP: + ytop = ycoll + self._dys[self._frame] 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 + elif ytop > 0: + if grid[ytop/50][(xcoll+wcoll)/50] == 0 and \ + grid[ytop/50][xcoll/50] == 0: + self.y += self._dys[self._frame] self._frame = self._next_frames[self._frame] else: - self.y += -6 + 50 - (self.y+34)%50 +1 + self.y += self._dys[self._frame] + 50 - ytop%50 + 1 self.move = random.randint(1,4) self.startup_frame() else: - self.y = -22 + self.y = -(ycoll-self.y) self.move = random.randint(1,4) self.startup_frame() elif self.move == DOWN: + ybot = ycoll + hcoll + self._dys[self._frame] 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 + elif ybot < 900: + if grid[ybot/50][xcoll/50] == 0 and \ + grid[ybot/50][(xcoll+wcoll)/50] == 0: + self.y += self._dys[self._frame] self._frame = self._next_frames[self._frame] else: - self.y += 6 - (self.y+84)%50 -1 + self.y += self._dys[self._frame] - ybot%50 - 1 self.move = random.randint(1,4) self.startup_frame() else: - self.y = 900-78 + self.y = 900 - (ycoll-self.y) self.move = random.randint(1,4) self.startup_frame() elif self.move == STOP: @@ -289,8 +299,8 @@ if __name__ == "__main__": 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"] + txts = ["Hola, yo soy Pancho", + "No creo que consigas\nnada siguiendome"] dia = DialogSprite(txts, font24, 100, 100) playing = True @@ -322,13 +332,11 @@ if __name__ == "__main__": dia.restart(sp2.rect()) sp.update_move(TALKING) sp2.update_move(TALKING) - print("Talking...") else: 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: @@ -417,6 +425,9 @@ if __name__ == "__main__": sb.render(screen) elif talking: dia.render(screen) + sb.update(time_passed,grid) + sb.render(screen) + # sp.draw_rect(screen) diff --git a/DialogSprite.py b/DialogSprite.py index a44aebf..fc19f95 100644 --- a/DialogSprite.py +++ b/DialogSprite.py @@ -61,7 +61,7 @@ class DialogSprite(pygame.sprite.Sprite): def restart(self, rect): self.x = rect[0] + int(rect[2]/2) - int(self.width/2) - self.y = rect[1] - self.height + self.y = rect[1] - self.height - 10 self.current_text = 0 self.update() |