-
Notifications
You must be signed in to change notification settings - Fork 0
/
piece.py
153 lines (134 loc) · 5.84 KB
/
piece.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
import pygame
from meter import *
from pygame import Rect
from pygame import Surface
class Piece(object):
''' Animated object/ custom sprite stuff. '''
def __init__(self, filename, framesperdir, initpos = [0,0], initdir = None, speed = None, stats = None, id = None, width = 32, height = 64):
self._images = []
self._start = pygame.time.get_ticks()
self._delay = 250
self._last_update = 0
self.pos = initpos
self.speed = speed
self.moving = False
self.direction = initdir
self.framesperdir = framesperdir
self.animoffset = initdir
self.frame = 0
self._lastPos = initpos
self._lastFacing = initdir
self.stats = stats
self.id = id
self.width = width
self.height = height
self.colrect = pygame.Rect(initpos[0]+2, initpos[1]+36, 30,28)
# fullrect is for checking full body collision to geometry system
self.fullrect = pygame.Rect(initpos[0], initpos[1], 32, 64)
self.exploding_image = pygame.image.load("guts.png").convert()
self.exploding_image.set_colorkey((0, 0, 0))
self.exploding = False
self.exploding_seq = range(1, 640, 4)
self.exploding_iterator = -1
self.exploded = False
self.damage_iterator = 0
self.damage_threshhold = 30
self.hp_meter = meter(0, self.stats.hp, self.stats.hp, 1, width = 30, height = 4, x = initpos[0], y = initpos[1])
#Based on 32x64 sprites
self.rect = (initpos[0], initpos[1], 32,64)
image = pygame.image.load(filename).convert()
for j in range(0, (image.get_height()/64)):
for i in range(0, (image.get_width()/32)):
self._images.append(image.subsurface(Rect((i*32, j*32, 32, 64))).convert())
self._images[len(self._images) - 1].set_colorkey((0, 0, 0))
def __getitem__(self, key):
return self.id
def clear_death(self, hp):
self.exploded = False
self.exploding = False
self.exploding_iterator = -1
self.stats.hp = hp
def update(self, direction, redrawOnly = False):
'''Move the object '''
#self.direction = direction
#self.speed = howfast
if self.stats.hp <= 0:
self.exploding = True
if redrawOnly == False:
if direction == 0:
self.pos[1] = self.pos[1] - self.speed
#self.animoffset = 0
elif direction == 1:
self.pos[0] = self.pos[0] - self.speed
#self.animoffset = 1
elif direction == 2:
self.pos[1] = self.pos[1] + self.speed
#self.animoffset = 2
elif direction == 3:
self.pos[0] = self.pos[0] + self.speed
#self.animoffset = 3
#Based on 32x64 sprites
self.rect = (self.pos[0], self.pos[1], 32, 64)
self.colrect = pygame.Rect(self.pos[0]+8, self.pos[1]+36, 18, 26)
def draw(self, surface, offsx, offsy):
self.hp_meter.update_pos(self.pos[0] - offsx, self.pos[1] - offsy + 8)
self.fullrect = pygame.Rect(self.pos[0] - offsx, self.pos[1] - offsy, 32, 64)
if (self.rect[0]+32 - offsx < 0) or (self.rect[1]+64 - offsy < 0) or (self.rect[0] - offsx > 672) or (self.rect[1] - offsy > 480):
return
elif self.exploding == True:
# still need to touch this up more
surface.blit(self.exploding_image, (self.rect[0] - offsx - self.exploding_seq[self.exploding_iterator], self.rect[1] - offsy - self.exploding_seq[self.exploding_iterator], self.rect[2], self.rect[3]))
surface.blit(self.exploding_image, (self.rect[0] - offsx + self.exploding_seq[self.exploding_iterator], self.rect[1] - offsy - self.exploding_seq[self.exploding_iterator], self.rect[2], self.rect[3]))
surface.blit(self.exploding_image, (self.rect[0] - offsx - self.exploding_seq[self.exploding_iterator], self.rect[1] - offsy + self.exploding_seq[self.exploding_iterator], self.rect[2], self.rect[3]))
surface.blit(self.exploding_image, (self.rect[0] - offsx + self.exploding_seq[self.exploding_iterator], self.rect[1] - offsy + self.exploding_seq[self.exploding_iterator], self.rect[2], self.rect[3]))
surface.blit(self.exploding_image, (self.rect[0] - offsx - self.exploding_seq[self.exploding_iterator], self.rect[1] - offsy, self.rect[2], self.rect[3]))
surface.blit(self.exploding_image, (self.rect[0] - offsx + self.exploding_seq[self.exploding_iterator], self.rect[1] - offsy, self.rect[2], self.rect[3]))
surface.blit(self.exploding_image, (self.rect[0] - offsx, self.rect[1] - offsy + self.exploding_seq[self.exploding_iterator], self.rect[2], self.rect[3]))
surface.blit(self.exploding_image, (self.rect[0] - offsx, self.rect[1] - offsy - self.exploding_seq[self.exploding_iterator], self.rect[2], self.rect[3]))
if(self.exploding_iterator < len(self.exploding_seq) - 1):
self.exploding_iterator = self.exploding_iterator + 1
else:
self.exploded = True
else:
t = pygame.time.get_ticks()
if (t - self._last_update) > self._delay:
if(self.moving == True):
self.frame += 1
self._last_update = t
elif(self.moving == False):
self.frame = 1
self._last_update = t
if self.frame > self.framesperdir - 1:
self.frame = 0
surface.blit(self._images[(self.animoffset*self.framesperdir)+self.frame], (self.rect[0] - offsx, self.rect[1] - offsy, self.rect[2], self.rect[3]))
def set_speed(self, speed):
self.speed = speed
def returnToLastPos(self):
self.direction = self._lastFacing
self.pos[0] = self._lastPos[0]
self.pos[1] = self._lastPos[1]
self.rect = (self.pos[0], self.pos[1], 32, 64)
def doesCollide(self, otherPiece):
if otherPiece.exploding == False:
if self.colrect.colliderect(otherPiece.colrect):
return True
else:
return False
else:
return False
def explode(self):
self.exploding = True
def damage(self, amount):
if self.damage_iterator < self.damage_threshhold:
self.damage_iterator = self.damage_iterator + 1
else:
self.stats.hp = self.stats.hp - amount
self.damage_iterator = 0
self.hp_meter.sub(amount)
def heal(self, amount):
if self.damage_iterator < self.damage_threshhold:
self.damage_iterator = self.damage_iterator + 1
else:
self.stats.hp = self.stats.hp + amount
self.damage_iterator = 0
self.hp_meter.add(amount)