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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
|
# boxes.py -- sundry boxes and box-like things for the game
# Copyright 2004 Joe Wreschnig <piman@sacredchao.net>
# Released under the terms of the GNU GPL v2.
__revision__ = "$Id: boxes.py 286 2004-09-04 03:51:59Z piman $"
import pygame
from pygame import transform
from pygame.sprite import Sprite
import textfx
import random
import config
import load
from constants import *
# Abstract class from which other block types inherit.
class AbstractBox(object):
def __init__(self, color, topleft):
self.size = [1, 1] # How big are we; [1,1] except for Box.
self.color = color
self.x, self.y = topleft
self._ntime = None
self.crashed = False # If we've been hit by a crash gem/diamond.
# Accessors for coordinates. FIXME: Make these properties.
def _get_top(self): return self.y
def _get_bottom(self): return self.y + self.size[1]
def _get_left(self): return self.x
def _get_right(self): return self.x + self.size[0]
# Blocks adjacent to it on the top...
def _adj_top(self, field):
adj = []
if self.y != 0:
for x in range(self.x, self._get_right()):
adj.append(field[self.y - 1][x])
return list(filter(None, adj))
# And so on.
def _adj_bottom(self, field):
adj = []
b = self._get_bottom()
if b != field.height:
for x in range(self.x, self._get_right()):
adj.append(field[b][x])
return list(filter(None, adj))
def _adj_left(self, field):
adj = []
if self.x != 0:
for y in range(self.y, self._get_bottom()):
adj.append(field[y][self.x - 1])
return list(filter(None, adj))
def _adj_right(self, field):
adj = []
r = self._get_right()
if r != field.width:
for y in range(self.y, self._get_bottom()):
adj.append(field[y][r])
return list(filter(None, adj))
# All blocks adjacent to this one.
def adjacent(self, field):
adj = self._adj_left(field)
adj.extend(self._adj_right(field))
adj.extend(self._adj_top(field))
adj.extend(self._adj_bottom(field))
return adj
# Kill the sprite, and remove it from the field map.
def remove_from(self, field):
self.crashed = True
for x in range(self.x, self._get_right()):
for y in range(self.y, self._get_bottom()):
field[y][x] = None
# Check if something is below us on the field (or we're at the bottom).
def is_blocked_down(self, field):
bot = self._get_bottom()
if bot == field.height: return True
else:
for x in range(self.x, self._get_right()):
if field[bot][x] is not None: return True
return False
# Actually move down space.
def fall(self, field):
bot = self._get_bottom()
for x in range(self.x, self._get_right()):
field[self.y][x] = None
field[bot][x] = self
self.y += 1
# Move to an entirely new location. This is done when blocks are
# moved from a FallingBlock onto the field.
def move(self, xy):
self.x, self.y = xy
# Destroy self if appropriate; return the value of the gems
# destroyed.
def crash(self, field, gem, immed = False):
broken = 0
if not self.crashed and gem.color == self.color:
broken += self.size[0] * self.size[1] * (sum(self.size) // 2)
self.crashed = True
for box in self.adjacent(field):
broken += box.crash(field, self)
if self.size != [1, 1]:
field.max_gemsize = max(field.max_gemsize,
self.size[0] * self.size[1])
return broken
# A generator for random boxes.
class BoxGen(object):
def __init__(self, rand, colors, single):
self.rand = rand
self.combat = (config.getboolean("settings", "combat") and not single)
self.colors = colors
def get(self, topleft):
f = self.rand.random()
color = self.rand.choice(self.colors)
if self.combat:
if f <= 0.004: return DiamondSprite("diamond", topleft)
elif f <= 0.20: return BreakBoxSprite(color, topleft)
elif f <= 0.27: return SpecialSprite(color, topleft)
else: return BoxSprite(color, topleft)
else:
if f <= 0.006: return DiamondSprite("diamond", topleft)
elif f <= 0.20: return BreakBoxSprite(color, topleft)
else: return BoxSprite(color, topleft)
# Find all instances of a given type in a sequence.
def instances_in(sequence, typ):
return list(filter(lambda x: isinstance(x, typ), sequence))
class SpriteBox(Sprite):
def __init__(self, color, type = ""):
Sprite.__init__(self)
self._image = load.block(color, type)
self._image.set_colorkey(self._image.get_at([0, 0]), RLEACCEL)
self.image = transform.scale(self._image,
[self.size[0] * 32, self.size[1] * 32])
self._ckey = self._image.get_colorkey()
self.rect = self.image.get_rect(topleft = [self.x * 32, self.y * 32])
self._btime = 0
def fall(self, field): self.rect.topleft = [self.x * 32, self.y * 32]
def move(self, xy): self.rect.topleft = [xy[0] * 32, xy[1] * 32]
def update(self, time):
if self.crashed and time > self._btime:
for i in range(15):
start = random.randrange(4)
end = random.randrange(4)
if end == start: end = (end + 1) % 4
if start == 1: start = [random.randrange(32), 0]
elif start == 2: start = [random.randrange(32), 31]
elif start == 3: start = [0, random.randrange(32)]
elif start == 0: start = [31, random.randrange(32)]
if end == 1: end = [random.randrange(32), 0]
elif end == 2: end = [random.randrange(32), 31]
elif end == 3: end = [0, random.randrange(32)]
elif end == 0: end = [31, random.randrange(32)]
pygame.draw.line(self._image, self._ckey, start, end)
self.image = transform.scale(self._image,
[self.size[0] * 32,
self.size[1] * 32])
self._btime = time + 20
# A basic box type; grows when put near other boxes in the field.
class Box(AbstractBox):
# Resize ourselve, and put ourself in the right place in the
# field. Note that our topleft doesn't change, since the most
# topleft gem is always the one resized.
def set_size(self, size, field):
self.size = size
for x in range(self.x, self._get_right()):
for y in range(self.y, self._get_bottom()):
field[y][x] = self
# Try to form larger blocks; return the blocks removed from merging.
def try_merge(self, field):
if self.size == [1,1]:
# Special case the 1,1 size, because we don't want 2x1 or 1x2
# blocks forming.
if self.x == field.width - 1 or self.y == field.height - 1:
return []
b1 = field[self.y][self.x + 1]
b2 = field[self.y + 1][self.x]
b3 = field[self.y + 1][self.x + 1]
if (isinstance(b1, Box) and isinstance(b2, Box) and
isinstance(b3, Box) and
self.color == b1.color == b2.color == b3.color and
b1.size == b2.size == b3.size == [1, 1]):
# Remove the sprites from the field and its sprite group
b1.remove_from(field)
b2.remove_from(field)
b3.remove_from(field)
self.set_size([2, 2], field)
return [b1, b2, b3]
else:
# test all four directions
blocks = self._adj_top(field)
# there are blocks above us, and they are not special blocks,
# and they are the same color, and they all share the same
# top location, the leftmost border and rightmost border
# match up with ours. We need these last checks to avoid
# oddly shaped structures like
# XXYY merging into XZZY
# ZZ ZZ
if (len(blocks) == self.size[0] and
len(instances_in(blocks, Box)) == len(blocks) and
[b.color for b in blocks].count(self.color) == len(blocks) and
not [b for b in blocks if b.y != blocks[0].y] and
blocks[0].x == self.x and
blocks[-1]._get_right() == self._get_right()):
dead = []
for b in blocks:
b.remove_from(field)
dead.append(b)
bot = self._get_bottom()
self.y = blocks[0].y
self.set_size([self.size[0], bot - self.y], field)
return dead
blocks = self._adj_bottom(field)
if (len(blocks) == self.size[0] and
len(instances_in(blocks, Box)) == len(blocks) and
[b.color for b in blocks].count(self.color) == len(blocks) and
not [b for b in blocks if
b._get_bottom() != blocks[0]._get_bottom()] and
blocks[0].x == self.x and
blocks[-1]._get_right() == self._get_right()):
dead = []
for b in blocks:
b.remove_from(field)
dead.append(b)
bot = blocks[0]._get_bottom()
self.set_size([self.size[0], bot - self.y], field)
return dead
blocks = self._adj_left(field)
if (len(blocks) == self.size[1] and
len(instances_in(blocks, Box)) == len(blocks) and
[b.color for b in blocks].count(self.color) == len(blocks) and
not [b for b in blocks if b.x != blocks[1].x] and
blocks[0].y == self.y and
blocks[-1]._get_bottom() == self._get_bottom()):
dead = []
for b in blocks:
b.remove_from(field)
dead.append(b)
right = self._get_right()
self.x = blocks[0].x
self.set_size([right - self.x, self.size[1]], field)
return dead
blocks = self._adj_right(field)
if (len(blocks) == self.size[1] and
len(instances_in(blocks, Box)) == len(blocks) and
[b.color for b in blocks].count(self.color) == len(blocks) and
not [b for b in blocks if
b._get_right() != blocks[0]._get_right()] and
blocks[0].y == self.y and
blocks[-1]._get_bottom() == self._get_bottom()):
dead = []
for b in blocks:
b.remove_from(field)
dead.append(b)
right = blocks[0]._get_right()
self.set_size([right - self.x, self.size[1]], field)
return dead
return []
# A version of the above that can be displayed on the screen.
class BoxSprite(Box, SpriteBox):
def __init__(self, color, topleft):
Box.__init__(self, color, topleft)
SpriteBox.__init__(self, color, "")
def move(self, xy):
Box.move(self, xy)
self.rect.topleft = [self.x * 32, self.y * 32]
def fall(self, field):
Box.fall(self, field)
SpriteBox.fall(self, field)
def set_size(self, size, field):
Box.set_size(self, size, field)
self.image = load.gem(self.color, self.size[0], self.size[1])
self.rect = self.image.get_rect(topleft = [self.x * 32, self.y * 32])
# A gem that breaks gems of the same color when it lands by them.
class BreakBox(AbstractBox):
# Called by the field to see if we can break anything.
# THIS DOESN'T BREAK SELF! self will break when its
# crashed method gets called by the gem *it* breaks.
def try_crash(self, field):
broken = 0
for box in self.adjacent(field):
broken += box.crash(field, self, True)
return broken
class BreakBoxSprite(BreakBox, SpriteBox):
def __init__(self, color, topleft):
BreakBox.__init__(self, color, topleft)
SpriteBox.__init__(self, color, "-crash")
def fall(self, field):
BreakBox.fall(self, field)
SpriteBox.fall(self, field)
def move(self, xy):
BreakBox.move(self, xy)
SpriteBox.move(self, xy)
# Now when you say special...
# These are the blocks used in "combat" mode, they don't form crystals,
# and when they break you get the 'item' in them.
class Special(AbstractBox):
names = ["", "cleared", "reversed", "flipped", "blinking",
"incoming", "scrambled"]
def __init__(self, color, topleft, special = None):
AbstractBox.__init__(self, color, topleft)
self.special = (special or random.randint(1, 6))
def crash(self, field, gem, immed = False):
if not self.crashed:
v = AbstractBox.crash(self, field, gem, immed)
if self.crashed: field.pick_up(self.special)
else: v = 0
return v
class SpecialSprite(Special, SpriteBox):
def load(cls, type):
if type == SCRAMBLE:
return textfx.shadow("? ?", 20, [255, 255, 255])
else:
fn = ["", "clear", "reverse", "flip", "blink", "gray"][type]
return load.image("special-%s.png" % fn)
load = classmethod(load)
def __init__(self, color, topleft):
Special.__init__(self, color, topleft)
SpriteBox.__init__(self, color, "")
self.image.blit(SpecialSprite.load(self.special), [6, 6])
def fall(self, field):
Special.fall(self, field)
SpriteBox.fall(self, field)
def move(self, xy):
Special.move(self, xy)
SpriteBox.move(self, xy)
# Diamonds break all of the color they land on, but "specially" -
# no bonuses for larger gems. So we just count the number of
# spaces of that color on the field, and then remove them.
# Extend BreakBox so that we find Diamonds when we filter for
# break boxes to crash.
class Diamond(BreakBox):
def try_crash(self, field):
broken = 0
self.crashed = True
if self.y == field.height - 1:
field.tech_bonus()
return 0
else: color = field[self.y + 1][self.x].color
for y in range(field.height):
for x in range(field.width):
box = field[y][x]
if (isinstance(box, Box) or isinstance(box, BreakBox) or
isinstance(box, Special)):
if box.color == color:
broken += 1
box.crashed = True
elif isinstance(box, TickBox):
if box.color == color:
broken += 0.5
box.crashed = True
return broken
class DiamondSprite(Diamond, SpriteBox):
def __init__(self, color, topleft):
Diamond.__init__(self, color, topleft)
SpriteBox.__init__(self, "diamond", "")
def fall(self, field):
Diamond.fall(self, field)
SpriteBox.fall(self, field)
def move(self, xy):
Diamond.move(self, xy)
SpriteBox.move(self, xy)
# Box with a counter on it. When it counts down, it replaces itself
# with a normal Box.
class TickBox(AbstractBox):
def __init__(self, color, topleft):
AbstractBox.__init__(self, color, topleft)
self._time_left = 5
def _get_time_left(self): return self._time_left
def _set_time_left(self, timeleft):
self._time_left = timeleft
self._render()
time_left = property(_get_time_left, _set_time_left)
def tick(self):
self.time_left -= 1
def _render(self): pass
def crash(self, field, gem, immed = False):
# Tick boxes don't crash adjacent boxes, though they do
# get destroyed themselves. They are also destroyed irrespective
# of color.
if not self.crashed and not immed:
self.crashed = True
return 0.5
else: return 0.0
class TickBoxSprite(TickBox, SpriteBox):
numerals = []
def __init__(self, color, topleft):
TickBox.__init__(self, color, topleft)
SpriteBox.__init__(self, color, "")
if not TickBoxSprite.numerals:
f = pygame.font.Font(None, 32)
for i in range(6):
img = textfx.shadow(str(i + 1), f, [200, 200, 200])
TickBoxSprite.numerals.append(img)
self._render()
def move(self, xy):
TickBox.move(self, xy)
SpriteBox.move(self, xy)
def fall(self, field):
TickBox.fall(self, field)
SpriteBox.fall(self, field)
def _render(self):
self.image = pygame.Surface([32, 32])
self.image.blit(self._image, [0, 0])
t = TickBoxSprite.numerals[self._time_left - 1]
self.image.blit(t, t.get_rect(center = [15, 15]))
self.image.set_colorkey(self.image.get_at([0, 0]))
self.rect = self.image.get_rect(topleft = [self.x * 32, self.y * 32])
|