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
|
|
|
|
|
|
|
|
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-08 14:52:08 +02:00
|
|
|
self.load()
|
|
|
|
|
2012-08-08 13:26:55 +02:00
|
|
|
def load(self):
|
|
|
|
self.imgs = {}
|
|
|
|
|
|
|
|
for anim, directory in (
|
|
|
|
[('idle_up', os.path.join('robot_idle', 'up')),
|
|
|
|
('idle_down', os.path.join('robot_idle', 'down')),
|
|
|
|
('idle_right', os.path.join('robot_idle', 'right')),
|
2012-08-08 19:11:43 +02:00
|
|
|
('idle_left', os.path.join('robot_idle', 'right')),
|
|
|
|
('carry_up', os.path.join('robot_carry', 'up')),
|
|
|
|
('carry_down', os.path.join('robot_carry', 'down')),
|
|
|
|
('carry_right', os.path.join('robot_carry', 'right')),
|
|
|
|
('carry_left', os.path.join('robot_carry', 'right'))]
|
2012-08-08 13:26:55 +02:00
|
|
|
):
|
|
|
|
|
|
|
|
self.imgs[anim] = []
|
|
|
|
|
|
|
|
# Find all image files for the given animation
|
|
|
|
anim_files = []
|
|
|
|
for root, dirs, files in os.walk(os.path.join(
|
|
|
|
self.level.graphics_dir, directory)):
|
|
|
|
for f in files:
|
|
|
|
if re.match(r"^.*\.(png)$", '/'.join([root, f])):
|
|
|
|
anim_files.append('/'.join([root, f]))
|
|
|
|
|
|
|
|
# Sort and load the files
|
|
|
|
for f in sorted(anim_files):
|
|
|
|
img = pygame.image.load(f)
|
|
|
|
|
|
|
|
# Special treatment:
|
2012-08-08 19:11:43 +02:00
|
|
|
if anim in ['idle_left', 'carry_left']:
|
2012-08-08 13:26:55 +02:00
|
|
|
img = pygame.transform.flip(img, 1, 0)
|
|
|
|
|
|
|
|
self.imgs[anim].append(img)
|
|
|
|
|
2012-08-08 19:11:43 +02:00
|
|
|
self.img = self.imgs[self.anim_root + '_' + self.anim][int(self.frame)]
|
2012-08-08 13:26:55 +02:00
|
|
|
|
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):
|
|
|
|
obj.use(self)
|
|
|
|
|
2012-08-07 19:59:52 +02:00
|
|
|
def update(self, e, t, dt):
|
|
|
|
for event in e:
|
|
|
|
if event.type == pygame.KEYDOWN:
|
2012-08-07 23:21:44 +02:00
|
|
|
if event.key == pygame.K_SPACE:
|
|
|
|
self.touch(*self.direction)
|
|
|
|
|
2012-08-08 15:22:27 +02:00
|
|
|
keys = pygame.key.get_pressed()
|
|
|
|
if keys[pygame.K_UP]:
|
2012-08-09 14:04:50 +02:00
|
|
|
if not self.holding and not self.is_moving:
|
2012-08-08 15:22:27 +02:00
|
|
|
self.direction = (0, -1)
|
2012-08-08 19:11:43 +02:00
|
|
|
self.anim = 'up'
|
2012-08-08 15:22:27 +02:00
|
|
|
self.move(0, -1)
|
2012-08-09 14:04:50 +02:00
|
|
|
elif keys[pygame.K_DOWN]:
|
|
|
|
if not self.holding and not self.is_moving:
|
2012-08-08 15:22:27 +02:00
|
|
|
self.direction = (0, 1)
|
2012-08-08 19:11:43 +02:00
|
|
|
self.anim = 'down'
|
2012-08-08 15:22:27 +02:00
|
|
|
self.move(0, 1)
|
2012-08-09 14:04:50 +02:00
|
|
|
elif keys[pygame.K_RIGHT]:
|
|
|
|
if not self.holding and not self.is_moving:
|
2012-08-08 15:22:27 +02:00
|
|
|
self.direction = (1, 0)
|
2012-08-08 19:11:43 +02:00
|
|
|
self.anim = 'right'
|
2012-08-08 15:22:27 +02:00
|
|
|
self.move(1, 0)
|
2012-08-09 14:04:50 +02:00
|
|
|
elif keys[pygame.K_LEFT]:
|
|
|
|
if not self.holding and not self.is_moving:
|
2012-08-08 15:22:27 +02:00
|
|
|
self.direction = (-1, 0)
|
2012-08-08 19:11:43 +02:00
|
|
|
self.anim = 'left'
|
2012-08-08 15:22:27 +02:00
|
|
|
self.move(-1, 0)
|
|
|
|
|
2012-08-08 13:26:55 +02:00
|
|
|
# Update the animation
|
|
|
|
self.frame = ((self.frame + self.anim_speed * dt) %
|
2012-08-08 19:11:43 +02:00
|
|
|
len(self.imgs[self.anim_root + '_' + self.anim]))
|
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.anim_root = 'carry' if self.holding else 'idle'
|
|
|
|
|
|
|
|
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-08 13:50:48 +02:00
|
|
|
window.blit(self.img, (self.x - 32 - self.level.camera_x,
|
|
|
|
self.y - self.img.get_size()[1] + 24
|
|
|
|
- self.level.camera_y))
|
2012-08-08 13:26:55 +02:00
|
|
|
|