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)
This commit is contained in:
Bryan Schumaker 2011-01-01 21:16:21 -05:00
parent b000093330
commit 20cb2f371e
3 changed files with 25 additions and 5 deletions

View File

@ -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

View File

@ -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 = []

View File

@ -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