-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
118 lines (92 loc) · 3.63 KB
/
main.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
# Copyright (C) 2024 Spandan Barve
#
# 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 pygame
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
import config, utils
from font import Font
from components.common import Drawable
from views import game
class LatinSquares:
def __init__(self):
self.running = True
self.clock = pygame.time.Clock()
self.gameDisplay = None
self.info = None
self.update_function = None
self.bg = (0, 0, 0)
self.prev_update_function = None
def vw(self, x):
return (x / 100) * self.display_rect.width
def vh(self, y):
return (y / 100) * self.display_rect.height
def blit_centred(self, surf, x, y):
rect = surf.get_rect()
centered_coords = (x - rect.width // 2, y - rect.height // 2)
self.gameDisplay.blit(surf, centered_coords)
def stop(self):
self.running = False
def set_screen(self, view):
view(self)
def set_background(self, color):
self.bg = color
def show_help(self):
if self.prev_update_function is None:
self.prev_update_function = self.update_function
self.update_function = self.help_popup.update
else:
self.update_function = self.prev_update_function
def run(self):
self.gameDisplay = pygame.display.get_surface()
self.info = pygame.display.Info()
self.display_rect = self.gameDisplay.get_rect()
config.load_images()
config.font.intialize("./assets/fonts/Geist.ttf")
self.bg = config.background_color
self.set_screen(game.view)
self.help_image = config.images["misc"]["help"]
screen_ratio = self.vh(100) / self.vw(100)
help_ratio = self.help_image.get_height() / self.help_image.get_width()
if help_ratio > screen_ratio:
self.help_image = utils.scale_image_maintain_ratio(self.help_image, h=self.vh(100))
else:
self.help_image = utils.scale_image_maintain_ratio(self.help_image, w=self.vw(100))
self.help_popup = Drawable()
self.help_popup.gameDisplay = self.gameDisplay
self.help_popup.x = self.vw(0)
self.help_popup.y = self.vh(0)
self.help_popup.set_image_rect(self.help_image)
if not (self.gameDisplay):
self.gameDisplay = pygame.display.set_mode(
(self.info.current_w, self.info.current_h))
pygame.display.set_caption("Latin Squares Activity")
while self.running:
self.gameDisplay.fill(self.bg)
if self.update_function is not None:
self.update_function()
while Gtk.events_pending():
Gtk.main_iteration()
for event in pygame.event.get():
if event.type == pygame.QUIT:
break
pygame.display.update()
self.clock.tick(60)
return
if __name__ == "__main__":
pygame.init()
pygame.display.set_mode((0, 0), pygame.RESIZABLE)
game = LatinSquares()
game.run()