From 20cb2f371e98d1cf866b8116e163ca8599e22939 Mon Sep 17 00:00:00 2001 From: Bryan Schumaker Date: Sat, 1 Jan 2011 21:16:21 -0500 Subject: [PATCH] engine: Reposition functions These functions can set GameObjects on the edges of their boundaries or in the middle of the boundary region. Additionally, movement in the x or y direction should be through the closest integer and not a float (because pygame rects only work for integers) --- engine/object.py | 22 ++++++++++++++++++++-- games/spaceblaster/arcade.py | 4 +++- games/spaceblaster/ship.py | 4 ++-- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/engine/object.py b/engine/object.py index fdefa890..2f1c3656 100644 --- a/engine/object.py +++ b/engine/object.py @@ -24,7 +24,7 @@ class GameObject: self.max_y = h - self.h def move_x(self, do_move, ms): - dx = do_move * self.dx * ms + dx = int(do_move * self.dx * ms) self.x += dx if self.allow_offscreen == False: if self.x < self.min_x: @@ -34,7 +34,7 @@ class GameObject: self.rect.move_ip(dx, 0) def move_y(self, do_move, ms): - dy = do_move * self.dy * ms + dy = int(do_move * self.dy * ms) self.y += dy if self.allow_offscreen == False: if self.y < self.min_y: @@ -50,6 +50,24 @@ class GameObject: self.y = y self.rect.move_ip(dx, dy) + def center_x(self): + self.reposition(self.max_x / 2, self.y) + + def left_x(self): + self.reposition(0, self.y) + + def right_x(self): + self.reposition(self.max_x, self.y) + + def center_y(self): + self.reposition(self.x, self.max_y / 2) + + def top_y(self): + self.reposition(self.x, 0) + + def bottom_y(self): + self.reposition(self.x, self.max_y) + def is_offscreen(self): if self.allow_offscreen == False: return False diff --git a/games/spaceblaster/arcade.py b/games/spaceblaster/arcade.py index ca3a7afd..73549a3a 100644 --- a/games/spaceblaster/arcade.py +++ b/games/spaceblaster/arcade.py @@ -18,7 +18,9 @@ B = pygame.K_b class ArcadeState(games.GameState): def __init__(self, GAME): - self.ship = ship.CSE1670(GAME, (10, 10), (0.25, 0.25)) + self.ship = ship.CSE1670(GAME, (0.25, 0.25)) + self.ship.center_x() + self.ship.bottom_y() self.stars = tiled_image(GAME, "stars.png") self.fire_interval = 100 self.beams = [] diff --git a/games/spaceblaster/ship.py b/games/spaceblaster/ship.py index d3a41159..64228eff 100644 --- a/games/spaceblaster/ship.py +++ b/games/spaceblaster/ship.py @@ -24,8 +24,8 @@ class ArcadeShip(GO): class CSE1670(ArcadeShip): - def __init__(self, GAME, pos, speed): - ArcadeShip.__init__(self, GAME, "cse_1670.png", pos, speed) + def __init__(self, GAME, speed): + ArcadeShip.__init__(self, GAME, "cse_1670.png", (0, 0), speed) self.game = GAME self.weapon_level = 0