Made the camera follow the robot.

This commit is contained in:
Sakse Dalum 2012-08-08 13:50:48 +02:00
parent 565c2a4267
commit 82388afde3
7 changed files with 41 additions and 26 deletions

View File

@ -40,6 +40,7 @@ class Block(worldobject.WorldObject):
self.holder = obj
def draw(self, window):
window.blit(self.img, (self.x - 32,
self.y - self.img.get_size()[1]))
window.blit(self.img, (self.x - 32 - self.level.camera_x,
self.y - self.img.get_size()[1]
- self.level.camera_y))

View File

@ -24,10 +24,24 @@
A generic level.
"""
import player
class Level(object):
def __init__(self, graphics_dir, paused=False):
def __init__(self, game, graphics_dir, paused=False):
self.__dict__.update(locals())
self.tiles = []
self.objects = []
self.imgs = {}
self.load()
self.camera_x = 0
self.camera_y = 0
self.player = player.Player(self, 0, 0)
self.objects.append(self.player)
def restart(self):
pass
@ -36,6 +50,10 @@ class Level(object):
for obj in self.objects:
obj.update(e, t, dt)
screen_size = self.game.window.get_size()
self.camera_x = self.player.x - screen_size[0] / 2
self.camera_y = self.player.y - screen_size[1] / 2
def draw(self, window):
for tile in self.tiles:
tile.draw(window)

View File

@ -34,26 +34,16 @@ import tile
import block
class Level1(level.Level):
def __init__(self, graphics_dir, paused=False):
level.Level.__init__(self, graphics_dir, paused)
self.player = player.Player(self, 5*64, 5*48)
def __init__(self, game, graphics_dir, paused=False):
level.Level.__init__(self, game, graphics_dir, paused)
self.dimensions = 10, 10
self.tiles = []
self.objects = []
self.imgs = {}
self.load()
for i in range(self.dimensions[0]):
for j in range(self.dimensions[1]):
self.tiles.append(
tile.Tile(self, i*64, j*48, self.imgs['ground1']))
self.objects.append(self.player)
for i in range(10):
self.objects.append(block.Block(self, random.randint(0, 10)*64,
random.randint(0, 10)*48,
@ -74,6 +64,4 @@ class Level1(level.Level):
self.graphics_dir, 'blocks', '%s.png' % block))
def restart(self):
self.objects.remove(self.player)
self.player = player.Player(self, 5*64, 5*48)
self.objects.append(self.player)
self.player.reset_pos()

View File

@ -59,7 +59,7 @@ class MainMenu(object):
for event in e:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
self.game.goto_level(level1.Level1(self.img_dir))
self.game.goto_level(level1.Level1(self.game, self.img_dir))
self.game.objs.remove(self)
self.game.objs.append(game_menu.GameMenu(self.game,
self.img_dir))

View File

@ -40,7 +40,7 @@ class Player(worldobject.WorldObject):
self.anim = 'idle_right'
self.frame = 0
self.anim_speed = 30
self.anim_speed = 15
def load(self):
self.imgs = {}
@ -115,6 +115,7 @@ class Player(worldobject.WorldObject):
def draw(self, window):
self.img = self.imgs[self.anim][int(self.frame)]
window.blit(self.img, (self.x - 32,
self.y - self.img.get_size()[1] + 32))
window.blit(self.img, (self.x - 32 - self.level.camera_x,
self.y - self.img.get_size()[1] + 24
- self.level.camera_y))

View File

@ -32,5 +32,6 @@ class Tile(worldobject.WorldObject):
worldobject.WorldObject.__init__(self, level, x, y)
def draw(self, window):
window.blit(self.img, (self.x - 32,
self.y - self.img.get_size()[1]))
window.blit(self.img, (self.x - 32 - self.level.camera_x,
self.y - self.img.get_size()[1]
- self.level.camera_y))

View File

@ -30,12 +30,18 @@ class WorldObject(object):
movable=False):
self.__dict__.update(locals())
self.move_x = self.x = self.x - (self.x % self.tile_x)
self.move_y = self.y = self.y - (self.y % self.tile_y)
self.init_x = self.move_x = self.x = self.x - (self.x % self.tile_x)
self.init_y = self.move_y = self.y = self.y - (self.y % self.tile_y)
self.holding = None
self.holder = None
def set_init_pos(self):
self.init_x, self.init_y = self.x, self.y
def reset_pos(self):
self.x, self.y = self.init_x, self.init_y
def check_move(self, move_x, move_y):
if self.move_x == self.x and self.move_y == self.y and self.movable:
for obj in self.level.objects: