-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_window.py
225 lines (189 loc) · 7.58 KB
/
game_window.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
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
from PyQt6.QtWidgets import QGridLayout, QWidget, QMainWindow, QLabel, QProgressBar, QPushButton, QTextBrowser, \
QVBoxLayout, QDialog, QHBoxLayout, QCheckBox
from PyQt6.QtCore import Qt, pyqtSignal
class ClickableLabel(QLabel):
clicked = pyqtSignal()
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def mousePressEvent(self, event):
self.clicked.emit()
class GameWindow(QMainWindow):
def __init__(self):
super().__init__()
self.settings_window = SettingsWindow(self)
self.table = None
self.falling_ball_label = None
self.level_label = None
self.drops_label = None
self.progress_bar = None
self.scores_label = None
self.setWindowTitle("PyQt6 Chain Factor Game")
self.setMinimumSize(425, 510)
self.setMaximumSize(425, 510)
self.default_background = "#FFFFFF"
self.init_ui()
def init_ui(self):
self.init_stylesheets()
central_widget = QWidget()
self.setCentralWidget(central_widget)
layout = QGridLayout()
central_widget.setLayout(layout)
self.table = []
for row in range(7):
row_labels = []
for col in range(7):
label = ClickableLabel()
label.setFixedSize(60, 60)
label.setAlignment(Qt.AlignmentFlag.AlignCenter)
label.setStyleSheet(f"background-color: {self.default_background}; border: 1px solid black")
layout.addWidget(label, row, col)
row_labels.append(label)
self.table.append(row_labels)
self.falling_ball_label = QLabel()
self.falling_ball_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
layout.addWidget(self.falling_ball_label, 9, 0, 1, 2)
self.level_label = QLabel()
self.level_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
layout.addWidget(self.level_label, 9, 2, 1, 2)
self.drops_label = QLabel()
self.drops_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
layout.addWidget(self.drops_label, 10, 3, 1, 3)
self.progress_bar = QProgressBar()
layout.addWidget(self.progress_bar, 9, 4, 1, 3)
self.scores_label = QLabel()
self.scores_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
layout.addWidget(self.scores_label, 10, 0, 1, 2)
settings_button = QPushButton("Settings")
settings_button.clicked.connect(self.open_settings)
layout.addWidget(settings_button, 11, 0, 1, 2)
rules_button = QPushButton("Rules")
rules_button.clicked.connect(self.open_rules)
layout.addWidget(rules_button, 11, 2, 1, 2)
self.restart_button = QPushButton("Restart")
layout.addWidget(self.restart_button, 11, 4, 1, 2)
def init_stylesheets(self):
dark_mode_stylesheet = """
QWidget {
background-color: #333333;
color: white;
}
QPushButton:hover {
background-color: #666666;
}
"""
bright_mode_stylesheet = """
QWidget {
background-color: #FFFFFF;
color: black;
}
QPushButton:hover {
background-color: #DDDDDD;
}
"""
self.dark_mode_stylesheet = dark_mode_stylesheet
self.bright_mode_stylesheet = bright_mode_stylesheet
def apply_dark_mode(self):
self.setStyleSheet(self.dark_mode_stylesheet)
def apply_bright_mode(self):
self.setStyleSheet(self.bright_mode_stylesheet)
def open_settings(self):
self.settings_window.exec()
def open_rules(self):
rules_window = RulesWindow(self)
rules_window.exec()
class SettingsWindow(QDialog):
def __init__(self, parent):
super().__init__(parent)
self.parent = parent
self.setWindowTitle("Settings")
layout = QVBoxLayout()
self.setLayout(layout)
self.color_label = QLabel("Color Scheme:")
layout.addWidget(self.color_label)
self.color_checkbox = QCheckBox("Dark Mode")
layout.addWidget(self.color_checkbox)
buttons_layout = QHBoxLayout()
layout.addLayout(buttons_layout)
save_button = QPushButton("Save")
save_button.clicked.connect(self.save_settings)
buttons_layout.addWidget(save_button)
cancel_button = QPushButton("Cancel")
cancel_button.clicked.connect(self.close)
buttons_layout.addWidget(cancel_button)
def save_settings(self):
color_scheme = "dark" if self.color_checkbox.isChecked() else "bright"
if color_scheme == "dark":
self.parent.default_background = "#333333"
self.apply_dark_mode()
self.parent.apply_dark_mode()
else:
self.parent.default_background = "#FFFFFF"
self.apply_bright_mode()
self.parent.apply_bright_mode()
self.close()
def apply_dark_mode(self):
for row in self.parent.table:
for label in row:
label.setStyleSheet("background-color: #333333; color: white; border: 1px solid black")
def apply_bright_mode(self):
for row in self.parent.table:
for label in row:
label.setStyleSheet("background-color: #FFFFFF; color: black; border: 1px solid black")
class RulesWindow(QDialog):
def __init__(self, parent):
super().__init__(parent)
self.setWindowTitle("Rules")
layout = QVBoxLayout()
self.setLayout(layout)
self.setMinimumSize(800, 600)
rules_text = """
<h1>Chain Factor Game Rules</h1>
<p>
Welcome to the Chain Factor! The objective of the game is to clear balls from the game board and earn points.
</p>
<h2>How to Play</h2>
<p>
1. Balls fall from the top of the game board. Each ball has a numerical value ranging from 1 to 7.
</p>
<p>
2. Click on an empty cell in the column of the game board to drop the falling ball into that column.
</p>
<p>
3. If the numerical value on the ball is the same as the count of adjacent balls horizontally or vertically, the ball will desappear.
</p>
<p>
4. Protected balls cannot be removed as other balls. You need to remove adjacent balls twice to clear a protection.
</p>
<p>
5. When a certain amount of drops happened, you'll go to the next level.
</p>
<p>
6. With each new level, amount of drops needed to proceed decreases.
</p>
<p>
7. When proceeding to a new level, new row of protected balls pushes from below. If there's not enough place, the game is over.
</p>
<h2>Scoring</h2>
<p>
- Earn points for each ball removed from the game board.
</p>
<p>
- Each new level gives 100 points.
</p>
<p>
- Additional points are awarded for clearing multiple balls in a single move.
</p>
<h2>Game Over</h2>
<p>
The game ends when you have balls in the top row when trying to go to the next level.
</p>
<p>
Enjoy the game and aim for a high score!
</p>
"""
text_browser = QTextBrowser()
text_browser.setHtml(rules_text)
layout.addWidget(text_browser)
close_button = QPushButton("Close")
close_button.clicked.connect(self.close)
layout.addWidget(close_button)