-
Notifications
You must be signed in to change notification settings - Fork 0
/
PieceManager.py
59 lines (49 loc) · 1.5 KB
/
PieceManager.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
#
# This class keeps track of the current pieces and works the rng of creating new
# pieces.
#
import random
from Settings import *
from Piece import Piece, piece_data
class PieceManager(object):
def __init__(self, clock):
self.pieces = []
self.clock = clock
self.history = ['s', 's', 'z', 'z']
for i in range(num_next_pieces):
self.addNew()
self.reposition()
def getPieces(self):
return self.pieces
def addNew(self):
self.pieces.append(Piece(self.clock, name=self.randPiece()))
# Based on the TGM randomizer
def randPiece(self):
tries = 5
# Try a certain number of times to pick a new piece.
for unused in range(tries):
curr = random.choice(list(piece_data.keys()))
if curr not in self.history:
break
self.history.pop(0)
self.history.append(curr)
return curr
def next(self, gravity):
old = self.pieces.pop(0)
self.addNew()
self.reposition()
if gravity:
old.setGravity(gravity)
return old, self.pieces[-1]
def reposition(self):
for y, piece in enumerate(self.pieces):
piece.moveToNext(y)
# "random generator" method - not in use
def lazyBag(self):
bag = []
while len(bag) < bag_size:
names = list(piece_data.keys())
random.shuffle(names)
for name in names:
bag.append(name)
self.bag = bag