-
Notifications
You must be signed in to change notification settings - Fork 4
/
player.py
163 lines (144 loc) · 5.32 KB
/
player.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# -*- coding: utf-8 -*-
import pygame
import consts
import math
import move
import sprite
class Player(sprite.DynamicSprite):
def __init__(self, options):
super(Player, self).__init__()
# Spriteset parameters
self.spriteset = consts.tiles[options['tilesGroup']]
self.characterSizeX, self.characterSizeY =\
self.spriteset['width'], self.spriteset['height']
# Animation parameters
self._start = pygame.time.get_ticks()
self._delay = 10000 / consts.FPS
self._last_update = 0
self.frame = 0
self.animation = .5
# Handle sprite set
self.build_spriteset()
self.direction = 'down'
self.updateDirection()
self.rect = self.image.get_rect()
self.xPos, self.yPos = options['x'], options['y']
self.rect.center = [dimension / 2 for dimension in consts.RESOLUTION]
self.moveX, self.moveY = 0, 0
self.speed = 3
self.moving = False
self._createHitBox()
self.axismove = False
if 'movePattern' not in options:
self.movePattern = None
else:
if 'type' not in options['movePattern']:
raise move.exception('A move pattern type is required')
if 'attributes' not in options['movePattern']:
raise move.exception('Move pattern attributes are required')
self.movePattern = options['movePattern']
self.movePattern['attributes']['topLeft'] = (self.xPos, self.yPos)
def updateDirection(self):
direction = self.spritesetDirections[self.direction][self.frame]
self.image = direction.convert()
def _createHitBox(self):
self.hitBox = pygame.sprite.Sprite()
self.hitBox.image = pygame.Surface(self.spriteset['hitbox']['size'])
self.hitBox.image.fill((0, 0, 0))
#handle other hitbox types
self.hitBox.rect = self.hitBox.image.get_rect()
def calculatePosition(self, mapSize):
if self.moveX == 0 or self.moveY == 0 or self.axismove:
x_velocity = int(self.moveX * self.speed)
y_velocity = int(self.moveY * self.speed)
else:
x_velocity = int(self.moveX * self.speed / math.sqrt(2))
y_velocity = int(self.moveY * self.speed / math.sqrt(2))
return (
min(
mapSize[0] - self.characterSizeX / 2,
max(
self.characterSizeX / 2,
self.xPos + x_velocity
)
),
min(
mapSize[1] - self.characterSizeY / 2,
max(
self.characterSizeY / 2,
self.yPos + y_velocity
)
)
)
def updatePosition(self, position):
self.xPos, self.yPos = position
# Set moving
self.moving = not (self.moveX, self.moveY) == (0, 0)
def draw(self, x, y):
self.rect.center = (self.xPos - x, self.yPos - y)
def drawHitBox(self):
self.hitBox.rect.center = map(
sum,
zip(
(self.xPos, self.yPos),
self.spriteset['hitbox']['positionInSprite']
)
)
def getPosition(self):
return (self.xPos, self.yPos)
def updateFrame(self, tick):
if not self.moving:
self.frame = 0
elif tick - self._last_update > self._delay:
self._last_update = tick
# frame should go 0, 1, 0, 2, 0, 1, ...
if not self.animation == int(self.animation):
self.frame = 0
elif self.animation / 2 == int(self.animation / 2):
self.frame = 1
else:
self.frame = 2
self.animation += .5
if self.moveX < 0:
self.direction = 'left'
if self.moveX > 0:
self.direction = 'right'
if self.moveY < 0:
self.direction = 'up'
if self.moveY > 0:
self.direction = 'down'
self.updateDirection()
def build_spriteset(self):
"Cut and build sprite set"
width = self.spriteset['width']
height = self.spriteset['height']
fond = pygame.image.load(self.spriteset['name']).convert()
pixel = self.spriteset['map'][0]
transparent_pixel = (pixel[0] * height, pixel[1] * width)
fond.set_colorkey(
fond.get_at(transparent_pixel)
)
# use map to cut parts
spriteset = list()
for (left, top) in self.spriteset['map']:
rect = pygame.Rect(left * width, top * height, width, height)
spriteset.append(fond.subsurface(rect).convert())
# build direction there
self.spritesetDirections = {
'up': (spriteset[0], spriteset[8], spriteset[7]),
'down': (spriteset[9], spriteset[10], spriteset[11]),
'left': (spriteset[2], spriteset[1], spriteset[3]),
'right': (spriteset[4], spriteset[5], spriteset[6])
}
def moveVertical(self, coef=1, absolute=False):
if absolute:
self.moveY = coef
else:
self.moveY += coef
def moveHorizontal(self, coef=1, absolute=False):
if absolute:
self.moveX = coef
else:
self.moveX += coef
def stop(self):
self.moveX = self.moveY = 0