forked from thomasballinger/Molltris
/
MakeTetromino.py
192 lines (166 loc) · 7.49 KB
/
MakeTetromino.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
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
#!/usr/bin/python
#-*- coding: utf-8 -*-
## =====================================================================
## Tetromino creator (helper)
## Copyright (C) 2014 Jonas Møller <jonasmo441@gmail.com>
##
## This program 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.
##
## This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
## =====================================================================
import Jobs
import Core
import Shared
import Log
import Matrix
import Save
import Load
import pygame as Pygame
from Globals import *
from pygame.locals import *
def islands(dictionary):
class Block(object):
def __init__(self, x, y):
self.x = x
self.y = y
self.neighbours = []
def neighbours(x, y):
positions = [(x+1, y+1), (x-1, y-1), (x+1, y-1), (x-1, y+1),
(x, y-1), (x-1, y), (x, y+1), (x+1, y), ]
return (pos for pos in positions if pos in dictionary)
def traverse(block, visited):
if (block.x, block.y) in visited:
return
visited.add((block.x, block.y))
for block in block.neighbours:
traverse(block, visited)
blocks = {}
for x, y in dictionary:
blocks[(x, y)] = Block(x, y)
for block in blocks:
blocks[block].neighbours = set([blocks[pos] for pos in neighbours(blocks[block].x, blocks[block].y)])
visited = set()
traverse(blocks[block], visited)
return set(blocks).difference(visited)
class MakeTetromino(Core.Game):
def __init__(self, *args, **kwargs):
self.id = "MakeTetromino"
super(MakeTetromino, self).__init__(
self.id, *args, fill=False, **kwargs)
self.running = self.mainLoop
## TODO: The user should be able to change the color, create a "color palette thingy"
self.color = (0xbb,0xbb,0xbb)
self.addJob(
"board",
Jobs.Board(
self,
x=SPACER,
y=SPACER,
height=BOARD_HEIGHT,
width=BOARD_WIDTH,
blockwidth=BOARD_BLOCKWIDTH,
level=0,
bgcolor=self.bgcolor,
)
)
## TODO: Make it less cumbersome and repeditive to spawn a row of buttons
self.addJob(
"save_button",
Jobs.TextBox(self, "Save", x=self.jobs.board.x + (self.jobs.board.blocks_width*self.jobs.board.blockwidth) + 5,
y=self.jobs.board.y,
textfit=True, underline=True, colors={"background":(0x22,0x22,0x22), "font":(0xaa,0xaa,0xaa)},
font=TETRIS_STATUSBOX_FONT, onmouseclick=self.getName,
fill=MENU_BACKGROUND,
)
)
self.addJob(
"clear_button",
Jobs.TextBox(self, "Clear", x=self.jobs.board.x + (self.jobs.board.blocks_width*self.jobs.board.blockwidth) + 5,
y=self.jobs.board.y + self.jobs.save_button.height + MAKETETROMINO_OPTION_SPACER,
textfit=True, underline=True, colors={"background":(0x22,0x22,0x22), "font":(0xaa,0xaa,0xaa)},
font=TETRIS_STATUSBOX_FONT, onmouseclick=self.clear,
fill=MENU_BACKGROUND,
)
)
self.addJob(
"exit_button",
Jobs.TextBox(self, "Exit", x=self.jobs.board.x + (self.jobs.board.blocks_width*self.jobs.board.blockwidth) + 5,
y=self.jobs.clear_button.y + self.jobs.clear_button.height + MAKETETROMINO_OPTION_SPACER,
textfit=True, underline=True, colors={"background":(0x22,0x22,0x22), "font":(0xaa,0xaa,0xaa)},
font=TETRIS_STATUSBOX_FONT, onmouseclick=self.quitGame,
fill=MENU_BACKGROUND,
)
)
self.addJob(
"color_button",
Jobs.TextBox(self, "Change color", x=self.jobs.board.x + (self.jobs.board.blocks_width*self.jobs.board.blockwidth) + 5,
y=self.jobs.exit_button.y + self.jobs.exit_button.height + MAKETETROMINO_OPTION_SPACER,
textfit=True, underline=True, colors={"background":(0x22,0x22,0x22), "font":(0xaa,0xaa,0xaa)},
font=TETRIS_STATUSBOX_FONT, onmouseclick=self.changeColor,
fill=MENU_BACKGROUND,
)
)
def getName(self):
if not self.jobs.board.blocks:
Log.log("No blocks present, will not save tetromino")
self.addJob("no_blocks_present", Jobs.Notification(self, "no_blocks_present", "Invalid: No blocks present"))
return
if any(islands(self.jobs.board.blocks)):
Log.log("Islands present, will not save tetromino")
self.addJob( "islands_present_window", Jobs.Notification(self, "islands_present_window", "Invalid: Islands present"))
return
self.running = self.getNameLoop
self.addJob("name_inputbox", Jobs.InputBox(self, "Name: "))
def getNameLoop(self):
if not self.jobs.name_inputbox.update_required:
self.save(self.jobs.name_inputbox.value)
self.removeJob("name_inputbox")
self.running = self.mainLoop
def clear(self):
self.jobs.board.blocks = {}
self.jobs.board.emptyBlocks()
def changeColor(self):
pass
def eventHandler(self, event):
if event.type == QUIT:
self.quit()
if event.type == MOUSEBUTTONDOWN:
if event.button in (1, 3):
x, y = self.jobs.board.getBlockInPos(*Pygame.mouse.get_pos())
if x > self.jobs.board.blocks_width-1 or y > self.jobs.board.height-1 or x < 0 or y < 0:
pass
elif not self.jobs.board.blocks.get((x, y)):
self.jobs.board.blocks[(x, y)] = self.color
self.jobs.board.drawCube(x, y, self.color)
else:
self.jobs.board.blocks.pop((x, y))
self.jobs.board.emptyBlocks()
def save(self, name):
xlow = min(x for x, y in self.jobs.board.blocks)
xhigh = max(x for x, y in self.jobs.board.blocks)
ylow = min(y for x, y in self.jobs.board.blocks)
yhigh = max(y for x, y in self.jobs.board.blocks)
matrix = [
[(x, y) in self.jobs.board.blocks for x in xrange(xlow, xhigh+1)]
for y in xrange(ylow, yhigh+1)
]
Log.log("Created new tetromino, displaying below")
Matrix.put(matrix)
Save.saveTetromino(self.color, name, matrix)
Shared.tetrominos = Load.loadTetrominos()
return True
def mainLoop(self):
pass
class Browse(Core.Game):
def __init__(self, *args, **kwargs):
super(Browse, self).__init__(
"Browse", *args, fill=True, **kwargs)