-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
58 lines (50 loc) · 2.2 KB
/
app.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
import pygame
from pygame.locals import *
from grid import Map
import shutil
class App:
def __init__(self):
self._running = True
self._display_surf = None
self.size = self.width, self.height = 400, 400
def on_init(self):
pygame.init()
self._display_surf = pygame.display.set_mode(self.size, pygame.HWSURFACE | pygame.DOUBLEBUF)
self._running = True
self._clock = pygame.time.Clock()
self._dt = self._clock.tick(200)
self._grid = Map(self.size)
def on_event(self, event):
if event.type == pygame.QUIT:
self._running = False
elif event.type == pygame.MOUSEBUTTONUP:
self._grid.click(int((pygame.mouse.get_pos()[0]/self.width)*8),int((pygame.mouse.get_pos()[1]/self.height)*8))
def on_loop(self):
if self._grid.ended:
self._grid = Map(self.size)
def on_render(self):
for i,l in enumerate(self._grid.grid):
for j, elem in enumerate(l):
if elem[0] == 0:
self._display_surf.blit(self._grid.greenSquare, (i*int(self.width/8), j*int(self.height/8)))
else:
self._display_surf.blit(self._grid.yellowSquare, (i*int(self.width/8), j*int(self.height/8)))
self._display_surf.blit(self._grid.pieceSurfaces[elem[1].value], (i*int(self.width/8), j*int(self.height/8)))
if elem[2]== 1:
self._display_surf.blit(self._grid.frameSurface, (i*int(self.width/8), j*int(self.height/8)))
elif elem[2]==2:
self._display_surf.blit(self._grid.circleSurface, (i*int(self.width/8) + (self.width/8)/2 - (self.width/8)/8, j*int(self.height/8) + int(self.height/8)/2 - int(self.height/8)/8))
pygame.display.flip()
def on_cleanup(self):
shutil.rmtree("__pycache__")
pygame.quit()
def on_execute(self):
if self.on_init() == False:
self._running = False
while (self._running):
self._dt = self._clock.tick(200)
for event in pygame.event.get():
self.on_event(event)
self.on_loop()
self.on_render()
self.on_cleanup()