-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
146 lines (107 loc) · 3.52 KB
/
gui.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
import pygame
import os
import sys
import numpy as np
from grid import Grid
obj = Grid()
pygame.init()
pygame.font.init()
pygame.display .set_caption("Cross Word")
# GLOBAL CONSTANTS
BLOCK_SIZE = 20
MARGIN = 15
N_BLOCKS = obj.SIZE
# GLOBAL VARIABLES
GRID = obj.grid
WORDS = obj.words
# GAME WINDOW
WIDTH = (BLOCK_SIZE * N_BLOCKS) + (MARGIN * N_BLOCKS) + MARGIN + 300
HEIGHT = (BLOCK_SIZE * N_BLOCKS) + (MARGIN * N_BLOCKS) + MARGIN
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
# COLORS
LET_COLOR = (0, 0, 0)
WORD_COLOR = (155, 0, 0)
ACCENT = (130, 30, 30)
# LOADING IMAGES
BG = pygame.transform.scale(pygame.image.load(
os.path.join("assets/Images", "bg.jpg")), (WIDTH, HEIGHT))
RESTART = pygame.transform.scale(pygame.image.load(
os.path.join("assets/Images", "restart.png")), (75, 75))
# CIRCLE = pygame.transform.scale(pygame.image.load(
# os.path.join("assets/Images", "circle-32.png")), (32, 32))
CIRCLE = pygame.image.load(
os.path.join("assets/Images", "circle-32.png"))
# LOADING FONTS
LET_FONT = pygame.font.Font(os.path.join(
"assets/Montserrat", "Montserrat-Light.ttf"), 27)
LET_FONT_2 = pygame.font.Font(os.path.join(
"assets/Montserrat", "Montserrat-Medium.ttf"), 30)
WORD_FONT = pygame.font.Font(os.path.join(
"assets/Montserrat", "Montserrat-Light.ttf"), 35)
def get_grid():
global GRID, WORDS
obj = Grid()
GRID, WORDS = obj.grid, obj.words
def hover(rect):
if rect.collidepoint(pygame.mouse.get_pos()):
return True
return False
def show_grid(selection_grid):
for column in range(N_BLOCKS):
for row in range(N_BLOCKS):
x = (MARGIN + BLOCK_SIZE) * column + MARGIN
y = (MARGIN + BLOCK_SIZE) * row + MARGIN
text = LET_FONT.render(str(GRID[row, column]), 1, (0, 0, 0))
text_rect = text.get_rect()
text_rect.x = int(x + (BLOCK_SIZE / 2) - text_rect.width / 2)
text_rect.y = int(y + (BLOCK_SIZE / 2) - text_rect.height / 2)
if selection_grid[row, column] == 1:
WIN.blit(CIRCLE, (x-6, y-3))
if hover(text_rect):
text = LET_FONT_2.render(str(GRID[row, column]), 1, ACCENT)
WIN.blit(text, (text_rect.x, text_rect.y))
def show_words():
pygame.draw.line(WIN, (0, 0, 0), (WIDTH-250, 0), (WIDTH-250, HEIGHT), 3)
for word in WORDS:
word_lbl = WORD_FONT.render(word, 1, WORD_COLOR)
word_rect = word_lbl.get_rect()
word_rect.x = int(WIDTH/2-(word_rect.width/2)+300)
word_rect.y = int(WORDS.index(word)*word_rect.height)
WIN.blit(word_lbl, (word_rect.x, word_rect.y))
def rest_btn():
rest_rect = RESTART.get_rect()
rest_rect.x = int(WIDTH/2 - (rest_rect.width/2) + 300)
rest_rect.y = int(HEIGHT - (rest_rect.height + MARGIN))
WIN.blit(RESTART, (rest_rect.x, rest_rect.y))
return rest_rect
def main():
run = True
clock = pygame.time.Clock()
selection_grid = np.zeros((N_BLOCKS, N_BLOCKS))
get_grid()
while run:
clock.tick(60)
WIN.blit(BG, (0, 0))
show_grid(selection_grid)
show_words()
restart_btn = rest_btn()
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONUP:
pos = pygame.mouse.get_pos()
column = (pos[0]-6) // (BLOCK_SIZE + MARGIN)
row = (pos[1]-6) // (BLOCK_SIZE + MARGIN)
if row < N_BLOCKS and column < N_BLOCKS:
if selection_grid[row, column] == 1:
selection_grid[row, column] = 0
else:
selection_grid[row, column] = 1
if hover(restart_btn):
run = False
main()
if __name__ == '__main__':
main()