diff --git a/resources/graphics/blocks/block1.png b/resources/graphics/blocks/block1.png
new file mode 100644
index 0000000..107c07f
Binary files /dev/null and b/resources/graphics/blocks/block1.png differ
diff --git a/robotgame/block.py b/robotgame/block.py
new file mode 100644
index 0000000..3565d8f
--- /dev/null
+++ b/robotgame/block.py
@@ -0,0 +1,44 @@
+# This file is part of ROBOTGAME
+#
+# ROBOTGAME is free software: you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation, either version 3 of the License, or (at your option) any later
+# version.
+#
+# ROBOTGAME is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along with
+# ROBOTGAME. If not, see .
+#
+# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
+#
+# block.py
+# --------------------
+# date created : Tue Aug 7 2012
+# copyright : (C) 2012 Sakse Dalum
+# maintained by : Sakse Dalum
+
+"""
+A generic block.
+"""
+
+import worldobject
+
+class Block(worldobject.WorldObject):
+ def __init__(self, level, x, y, img, movable=False):
+ self.__dict__.update(locals())
+ worldobject.WorldObject.__init__(self, level, x, y, movable=movable)
+
+ def use(self, obj):
+ if obj == self.holder:
+ obj.holding = None
+ self.holder = None
+ else:
+ obj.holding = self
+ self.holder = obj
+
+ def draw(self, window):
+ window.blit(self.img, (self.x - 32,
+ self.y - self.img.get_size()[1]))
diff --git a/robotgame/level1.py b/robotgame/level1.py
index bfac077..296aa86 100644
--- a/robotgame/level1.py
+++ b/robotgame/level1.py
@@ -26,27 +26,40 @@ The first level.
import os
import pygame
+import random
import level
import player
import tile
+import block
class Level1(level.Level):
def __init__(self, graphics_dir):
self.__dict__.update(locals())
- self.player = player.Player(200, 200)
+ self.player = player.Player(self, 5*64, 5*48)
+
+ self.dimensions = 20, 20
self.tiles = []
+ self.objects = []
+
self.imgs = {}
self.load()
- for i in range(0, 10):
- for j in range(0, 10):
+ for i in range(self.dimensions[0]):
+ for j in range(self.dimensions[1]):
self.tiles.append(
- tile.Tile(i*64, j*48,
- self.imgs['ground%d' % (((i + j) % 2) + 1)]))
+ tile.Tile(self, i*64, j*48, self.imgs['ground%d'
+ % (((i + j) % 2) + 1)]))
+
+ self.objects.append(self.player)
+ for i in range(100):
+ self.objects.append(block.Block(self, random.randint(0, 20)*64,
+ random.randint(0, 20)*48,
+ self.imgs['block1'],
+ movable=True))
def load(self):
"""Load all resources used in the level."""
@@ -56,11 +69,18 @@ class Level1(level.Level):
self.imgs[tile] = pygame.image.load(os.path.join(
self.graphics_dir, 'tiles', '%s.png' % tile))
+ block_list = ['block1']
+ for block in block_list:
+ self.imgs[block] = pygame.image.load(os.path.join(
+ self.graphics_dir, 'blocks', '%s.png' % block))
+
def update(self, e, t, dt):
- self.player.update(e, t, dt)
+ for obj in self.objects:
+ obj.update(e, t, dt)
def draw(self, window):
for tile in self.tiles:
tile.draw(window)
- self.player.draw(window)
+ for obj in sorted(self.objects, key=lambda obj: (obj.y + obj.z)):
+ obj.draw(window)
diff --git a/robotgame/player.py b/robotgame/player.py
index 4ea04e6..6c172f9 100644
--- a/robotgame/player.py
+++ b/robotgame/player.py
@@ -29,21 +29,47 @@ import pygame
import worldobject
class Player(worldobject.WorldObject):
+ def __init__(self, level, x, y, z=1, movable=True):
+ self.__dict__.update(locals())
+ worldobject.WorldObject.__init__(self, level, x, y,
+ z=z, movable=movable)
+
+ def touch(self, touch_x, touch_y):
+ for obj in self.level.objects:
+ if (obj.x == self.x + touch_x * self.tile_x
+ and obj.y == self.y + touch_y * self.tile_y
+ and obj != self):
+ obj.use(self)
+
def update(self, e, t, dt):
worldobject.WorldObject.update(self, e, t, dt)
for event in e:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
+ if not self.holding:
+ self.direction = (0, -1)
self.move(0, -1)
if event.key == pygame.K_DOWN:
+ if not self.holding:
+ self.direction = (0, 1)
self.move(0, 1)
if event.key == pygame.K_RIGHT:
+ if not self.holding:
+ self.direction = (1, 0)
self.move(1, 0)
if event.key == pygame.K_LEFT:
+ if not self.holding:
+ self.direction = (-1, 0)
self.move(-1, 0)
+ if event.key == pygame.K_SPACE:
+ self.touch(*self.direction)
+
def draw(self, window):
- pygame.draw.circle(window, (255, 255, 255), (self.x + self.tile_x / 2,
- self.y + self.tile_y / 2),
- 20)
+ pygame.draw.circle(window, (255, 0, 255),
+ (self.x + self.tile_x / 2,
+ self.y - self.tile_y / 2), 20)
+ pygame.draw.circle(window, (255, 255, 0),
+ (self.x + self.tile_x / 2 + self.direction[0]*10,
+ self.y - self.tile_y / 2 + self.direction[1]*10), 5)
diff --git a/robotgame/tile.py b/robotgame/tile.py
index 7edea35..63ccb35 100644
--- a/robotgame/tile.py
+++ b/robotgame/tile.py
@@ -14,22 +14,23 @@
#
# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
#
-# worldobject.py
+# tile.py
# --------------------
# date created : Tue Aug 7 2012
# copyright : (C) 2012 Sakse Dalum
# maintained by : Sakse Dalum
"""
-A generic world object.
+A generic tile.
"""
import worldobject
class Tile(worldobject.WorldObject):
- def __init__(self, x, y, img):
+ def __init__(self, level, x, y, img):
self.__dict__.update(locals())
- worldobject.WorldObject.__init__(self, x, y)
+ worldobject.WorldObject.__init__(self, level, x, y)
def draw(self, window):
- window.blit(self.img, (self.x, self.y), (0, 0, self.tile_x, self.tile_y))
+ window.blit(self.img, (self.x, self.y),
+ (0, 0, self.tile_x, self.tile_y))
diff --git a/robotgame/worldobject.py b/robotgame/worldobject.py
index e48ab52..0f97c6a 100644
--- a/robotgame/worldobject.py
+++ b/robotgame/worldobject.py
@@ -25,17 +25,44 @@ A generic world object.
"""
class WorldObject(object):
- def __init__(self, x, y, speed=5, tile_x=64, tile_y=48):
+ def __init__(self, level, x, y, z=0, direction=(1, 0), speed=5,
+ tile_x=64, tile_y=48,
+ 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.holding = None
+ self.holder = None
+
+ 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:
+ if (obj.x == self.x + move_x * self.tile_x
+ and obj.y == self.y + move_y * self.tile_y
+ and obj != self and obj != self.holder
+ and obj != self.holding):
+ return False
+ return True
+ return False
+
def move(self, move_x, move_y):
- if self.move_x == self.x and self.move_y == self.y:
+ if self.check_move(move_x, move_y):
+
+ if self.holding:
+ if not self.holding.check_move(move_x, move_y):
+ return False
+
self.move_x += move_x * self.tile_x
self.move_y += move_y * self.tile_y
+ if self.holding:
+ self.holding.move(move_x, move_y)
+
+ def use(self):
+ pass
+
def update(self, e, t, dt):
if self.x > self.move_x:
self.x -= min(self.speed * dt * self.tile_x,