Blocks, depth, holding and releasing objects, etc.
This commit is contained in:
parent
90a11e9ed3
commit
2ba27dff1f
Binary file not shown.
After Width: | Height: | Size: 418 B |
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
#
|
||||||
|
# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
|
||||||
|
#
|
||||||
|
# block.py
|
||||||
|
# --------------------
|
||||||
|
# date created : Tue Aug 7 2012
|
||||||
|
# copyright : (C) 2012 Sakse Dalum
|
||||||
|
# maintained by : Sakse Dalum <don_s@hongabar.org>
|
||||||
|
|
||||||
|
"""
|
||||||
|
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]))
|
|
@ -26,27 +26,40 @@ The first level.
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import pygame
|
import pygame
|
||||||
|
import random
|
||||||
|
|
||||||
import level
|
import level
|
||||||
import player
|
import player
|
||||||
import tile
|
import tile
|
||||||
|
import block
|
||||||
|
|
||||||
class Level1(level.Level):
|
class Level1(level.Level):
|
||||||
def __init__(self, graphics_dir):
|
def __init__(self, graphics_dir):
|
||||||
self.__dict__.update(locals())
|
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.tiles = []
|
||||||
|
self.objects = []
|
||||||
|
|
||||||
self.imgs = {}
|
self.imgs = {}
|
||||||
|
|
||||||
self.load()
|
self.load()
|
||||||
|
|
||||||
for i in range(0, 10):
|
for i in range(self.dimensions[0]):
|
||||||
for j in range(0, 10):
|
for j in range(self.dimensions[1]):
|
||||||
self.tiles.append(
|
self.tiles.append(
|
||||||
tile.Tile(i*64, j*48,
|
tile.Tile(self, i*64, j*48, self.imgs['ground%d'
|
||||||
self.imgs['ground%d' % (((i + j) % 2) + 1)]))
|
% (((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):
|
def load(self):
|
||||||
"""Load all resources used in the level."""
|
"""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.imgs[tile] = pygame.image.load(os.path.join(
|
||||||
self.graphics_dir, 'tiles', '%s.png' % tile))
|
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):
|
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):
|
def draw(self, window):
|
||||||
for tile in self.tiles:
|
for tile in self.tiles:
|
||||||
tile.draw(window)
|
tile.draw(window)
|
||||||
|
|
||||||
self.player.draw(window)
|
for obj in sorted(self.objects, key=lambda obj: (obj.y + obj.z)):
|
||||||
|
obj.draw(window)
|
||||||
|
|
|
@ -29,21 +29,47 @@ import pygame
|
||||||
import worldobject
|
import worldobject
|
||||||
|
|
||||||
class Player(worldobject.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):
|
def update(self, e, t, dt):
|
||||||
worldobject.WorldObject.update(self, e, t, dt)
|
worldobject.WorldObject.update(self, e, t, dt)
|
||||||
|
|
||||||
for event in e:
|
for event in e:
|
||||||
if event.type == pygame.KEYDOWN:
|
if event.type == pygame.KEYDOWN:
|
||||||
if event.key == pygame.K_UP:
|
if event.key == pygame.K_UP:
|
||||||
|
if not self.holding:
|
||||||
|
self.direction = (0, -1)
|
||||||
self.move(0, -1)
|
self.move(0, -1)
|
||||||
if event.key == pygame.K_DOWN:
|
if event.key == pygame.K_DOWN:
|
||||||
|
if not self.holding:
|
||||||
|
self.direction = (0, 1)
|
||||||
self.move(0, 1)
|
self.move(0, 1)
|
||||||
if event.key == pygame.K_RIGHT:
|
if event.key == pygame.K_RIGHT:
|
||||||
|
if not self.holding:
|
||||||
|
self.direction = (1, 0)
|
||||||
self.move(1, 0)
|
self.move(1, 0)
|
||||||
if event.key == pygame.K_LEFT:
|
if event.key == pygame.K_LEFT:
|
||||||
|
if not self.holding:
|
||||||
|
self.direction = (-1, 0)
|
||||||
self.move(-1, 0)
|
self.move(-1, 0)
|
||||||
|
|
||||||
|
if event.key == pygame.K_SPACE:
|
||||||
|
self.touch(*self.direction)
|
||||||
|
|
||||||
def draw(self, window):
|
def draw(self, window):
|
||||||
pygame.draw.circle(window, (255, 255, 255), (self.x + self.tile_x / 2,
|
pygame.draw.circle(window, (255, 0, 255),
|
||||||
self.y + self.tile_y / 2),
|
(self.x + self.tile_x / 2,
|
||||||
20)
|
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)
|
||||||
|
|
|
@ -14,22 +14,23 @@
|
||||||
#
|
#
|
||||||
# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
|
# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
|
||||||
#
|
#
|
||||||
# worldobject.py
|
# tile.py
|
||||||
# --------------------
|
# --------------------
|
||||||
# date created : Tue Aug 7 2012
|
# date created : Tue Aug 7 2012
|
||||||
# copyright : (C) 2012 Sakse Dalum
|
# copyright : (C) 2012 Sakse Dalum
|
||||||
# maintained by : Sakse Dalum <don_s@hongabar.org>
|
# maintained by : Sakse Dalum <don_s@hongabar.org>
|
||||||
|
|
||||||
"""
|
"""
|
||||||
A generic world object.
|
A generic tile.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import worldobject
|
import worldobject
|
||||||
|
|
||||||
class Tile(worldobject.WorldObject):
|
class Tile(worldobject.WorldObject):
|
||||||
def __init__(self, x, y, img):
|
def __init__(self, level, x, y, img):
|
||||||
self.__dict__.update(locals())
|
self.__dict__.update(locals())
|
||||||
worldobject.WorldObject.__init__(self, x, y)
|
worldobject.WorldObject.__init__(self, level, x, y)
|
||||||
|
|
||||||
def draw(self, window):
|
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))
|
||||||
|
|
|
@ -25,17 +25,44 @@ A generic world object.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
class WorldObject(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.__dict__.update(locals())
|
||||||
|
|
||||||
self.move_x = self.x = self.x - (self.x % self.tile_x)
|
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.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):
|
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_x += move_x * self.tile_x
|
||||||
self.move_y += move_y * self.tile_y
|
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):
|
def update(self, e, t, dt):
|
||||||
if self.x > self.move_x:
|
if self.x > self.move_x:
|
||||||
self.x -= min(self.speed * dt * self.tile_x,
|
self.x -= min(self.speed * dt * self.tile_x,
|
||||||
|
|
Loading…
Reference in New Issue