2012-08-07 19:59:52 +02:00
|
|
|
# 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/>.
|
|
|
|
#
|
|
|
|
# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
|
|
|
|
#
|
|
|
|
# player.py
|
|
|
|
# --------------------
|
|
|
|
# date created : Tue Aug 7 2012
|
|
|
|
# copyright : (C) 2012 Sakse Dalum
|
|
|
|
# maintained by : Sakse Dalum <don_s@hongabar.org>
|
|
|
|
|
|
|
|
"""
|
2012-08-08 19:29:26 +02:00
|
|
|
The player.
|
2012-08-07 19:59:52 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
import pygame
|
2012-08-08 13:26:55 +02:00
|
|
|
import re
|
|
|
|
import os
|
2012-08-07 19:59:52 +02:00
|
|
|
|
|
|
|
import worldobject
|
2012-08-09 15:22:42 +02:00
|
|
|
import boulder
|
2012-08-10 14:43:08 +02:00
|
|
|
import lever
|
2012-08-07 19:59:52 +02:00
|
|
|
|
|
|
|
class Player(worldobject.WorldObject):
|
2012-08-07 23:21:44 +02:00
|
|
|
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)
|
|
|
|
|
2012-08-08 19:11:43 +02:00
|
|
|
self.anim_root = 'idle'
|
|
|
|
self.anim = 'right'
|
2012-08-08 13:26:55 +02:00
|
|
|
self.frame = 0
|
2012-08-08 13:50:48 +02:00
|
|
|
self.anim_speed = 15
|
2012-08-08 13:26:55 +02:00
|
|
|
|
2012-08-10 14:43:08 +02:00
|
|
|
self.working = False
|
|
|
|
|
2012-08-09 15:22:42 +02:00
|
|
|
self.ignore_list.append(boulder.Boulder)
|
|
|
|
|
2012-08-10 18:54:48 +02:00
|
|
|
self.imgs = dict([(k[6:], v) for (k, v) in self.level.imgs.iteritems()
|
|
|
|
if k.startswith('robot_')])
|
2012-08-08 13:26:55 +02:00
|
|
|
|
2012-08-10 14:43:08 +02:00
|
|
|
def direction_as_string(self):
|
|
|
|
if self.direction == (1, 0):
|
|
|
|
return "right"
|
|
|
|
if self.direction == (-1, 0):
|
|
|
|
return "left"
|
|
|
|
if self.direction == (0, -1):
|
|
|
|
return "up"
|
|
|
|
if self.direction == (0, 1):
|
|
|
|
return "down"
|
|
|
|
|
2012-08-07 23:21:44 +02:00
|
|
|
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):
|
|
|
|
|
2012-08-10 14:43:08 +02:00
|
|
|
# Concerning levers
|
|
|
|
if type(obj) == lever.Lever:
|
2012-08-10 16:57:22 +02:00
|
|
|
if not obj.at_rest:
|
|
|
|
return
|
|
|
|
|
|
|
|
obj.use(self)
|
2012-08-10 14:43:08 +02:00
|
|
|
self.working = True
|
|
|
|
|
|
|
|
# List all possible combinations ...
|
|
|
|
lever_direction = (
|
2012-08-10 15:57:09 +02:00
|
|
|
'up' if obj.anim == 'lever_updown' and not obj.setting
|
2012-08-10 14:43:08 +02:00
|
|
|
else
|
2012-08-10 15:57:09 +02:00
|
|
|
'right' if obj.anim == 'lever_leftright'
|
|
|
|
and not obj.setting
|
2012-08-10 14:43:08 +02:00
|
|
|
else
|
2012-08-10 15:57:09 +02:00
|
|
|
'down' if obj.anim == 'lever_updown' and obj.setting
|
2012-08-10 14:43:08 +02:00
|
|
|
else
|
|
|
|
'left')
|
|
|
|
|
|
|
|
self.anim_root = 'lever'
|
|
|
|
self.frame = 0
|
|
|
|
self.anim = (
|
|
|
|
self.direction_as_string() + '_' + lever_direction)
|
2012-08-10 16:57:22 +02:00
|
|
|
else:
|
|
|
|
obj.use(self)
|
2012-08-10 14:43:08 +02:00
|
|
|
|
2012-08-07 19:59:52 +02:00
|
|
|
def update(self, e, t, dt):
|
2012-08-10 14:43:08 +02:00
|
|
|
if not self.working:
|
|
|
|
self.anim_root = 'carry' if self.holding else 'idle'
|
|
|
|
|
|
|
|
keys = pygame.key.get_pressed()
|
|
|
|
if keys[pygame.K_UP]:
|
|
|
|
if not self.holding and not self.is_moving:
|
|
|
|
self.direction = (0, -1)
|
|
|
|
self.anim = self.direction_as_string()
|
|
|
|
self.move(0, -1)
|
|
|
|
elif keys[pygame.K_DOWN]:
|
|
|
|
if not self.holding and not self.is_moving:
|
|
|
|
self.direction = (0, 1)
|
|
|
|
self.anim = self.direction_as_string()
|
|
|
|
self.move(0, 1)
|
|
|
|
elif keys[pygame.K_RIGHT]:
|
|
|
|
if not self.holding and not self.is_moving:
|
|
|
|
self.direction = (1, 0)
|
|
|
|
self.anim = self.direction_as_string()
|
|
|
|
self.move(1, 0)
|
|
|
|
elif keys[pygame.K_LEFT]:
|
|
|
|
if not self.holding and not self.is_moving:
|
|
|
|
self.direction = (-1, 0)
|
|
|
|
self.anim = self.direction_as_string()
|
|
|
|
self.move(-1, 0)
|
|
|
|
|
2012-08-10 15:57:09 +02:00
|
|
|
for event in e:
|
|
|
|
if event.type == pygame.KEYDOWN:
|
|
|
|
if event.key == pygame.K_SPACE:
|
|
|
|
self.touch(*self.direction)
|
|
|
|
|
2012-08-10 14:43:08 +02:00
|
|
|
else:
|
|
|
|
self.working = not (self.frame == len(
|
|
|
|
self.imgs[self.anim_root + '_' + self.anim]) - 1)
|
|
|
|
if not self.working:
|
|
|
|
self.anim = self.direction_as_string()
|
|
|
|
self.anim_root = 'carry' if self.holding else 'idle'
|
2012-08-08 15:22:27 +02:00
|
|
|
|
2012-08-08 13:26:55 +02:00
|
|
|
# Update the animation
|
2012-08-10 14:43:08 +02:00
|
|
|
if not self.working:
|
|
|
|
self.frame = ((self.frame + self.anim_speed * dt) %
|
|
|
|
len(self.imgs[self.anim_root + '_' + self.anim]))
|
|
|
|
else:
|
|
|
|
self.frame = min(
|
|
|
|
self.frame + self.anim_speed * dt,
|
|
|
|
len(self.imgs[self.anim_root + '_' + self.anim]) - 1)
|
|
|
|
|
2012-08-08 13:26:55 +02:00
|
|
|
|
2012-08-08 15:03:39 +02:00
|
|
|
worldobject.WorldObject.update(self, e, t, dt)
|
|
|
|
|
2012-08-07 19:59:52 +02:00
|
|
|
def draw(self, window):
|
2012-08-08 19:11:43 +02:00
|
|
|
self.img = self.imgs[self.anim_root + '_' + self.anim][int(self.frame)]
|
2012-08-08 14:52:08 +02:00
|
|
|
self.img.set_alpha(128)
|
2012-08-10 15:57:09 +02:00
|
|
|
offset_x = (96 if self.anim_root == 'lever' and self.anim in
|
|
|
|
['left_up', 'left_left', 'left_down', 'left_right',
|
|
|
|
'down_right', 'up_left']
|
|
|
|
else 32)
|
|
|
|
window.blit(self.img, (self.x - offset_x - self.level.camera_x,
|
2012-08-08 13:50:48 +02:00
|
|
|
self.y - self.img.get_size()[1] + 24
|
|
|
|
- self.level.camera_y))
|
2012-08-08 13:26:55 +02:00
|
|
|
|