forked from teamglendy/glendy-game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
97 lines (88 loc) · 4.07 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
import pygame as pg
import math
import env
from tkinter import messagebox
import time
class Glendy():
def __init__(self, difficulty):
self.env = env.GlendyEnv()
self.n_rows = 11
self.n_columns = 11
self.board_state = [[0 for i in range(11)] for i in range(11)]
self.glenda = self.env.start
self.board_state[self.glenda[0]][self.glenda[1]] = 2
self.env.arrange_blocks(difficulty)
for block in self.env.blocks:
self.board_state[block[0]][block[1]] = 1
self.done = False
self.circle_color = (42, 98, 154)
self.glenda_color = (255, 218, 120)
self.block_color = (255, 127, 62)
self.size = (625, 550)
self.circle_diameter = self.size[1]/11
self.circle_radius = self.circle_diameter/2
self.offset = self.circle_radius/5
pg.display.init()
self.screen = pg.display.set_mode(self.size)
self.screen.fill((255, 255, 255))
pg.display.set_caption("Glendy")
self.draw_board()
pg.display.update()
def draw_board(self):
y = self.circle_radius
for c in range(self.n_columns):
x = self.circle_radius
if c%2!=0:
x += self.circle_radius
for r in range(self.n_rows):
if self.board_state[c][r] == 0:
pg.draw.circle(self.screen, self.circle_color, (x, y), self.circle_radius)
elif self.board_state[c][r] == 1:
pg.draw.circle(self.screen, self.block_color, (x, y), self.circle_radius)
elif self.board_state[c][r] == 2:
pg.draw.circle(self.screen, self.glenda_color, (x, y), self.circle_radius)
x += self.circle_diameter+self.offset
y += self.circle_diameter
def start(self):
while not self.done:
time.sleep(0.1)
for event in pg.event.get():
if event.type == pg.QUIT:
pg.display.quit()
return
if event.type == pg.MOUSEBUTTONDOWN:
x_axis = event.pos[0]
y_axis = event.pos[1]
row = math.floor(y_axis/self.circle_diameter)
if row % 2 != 0:
if x_axis >= self.circle_radius:
column = math.floor((x_axis-self.circle_radius)/(self.circle_diameter+self.offset))
else:
column = None
else:
if x_axis <= self.size[0]-self.circle_radius:
column = math.floor(x_axis/(self.circle_diameter+self.offset))
else:
column = None
if column is not None and (row, column) not in self.env.blocks and (row, column) != self.glenda:
self.board_state[row][column] = 1
self.env.blocks.append((row, column))
if (row, column) in self.env.exits:
self.env.exits.remove((row, column))
glenda_next = self.env.get_glenda_move(self.glenda)
if self.env.result == "win":
self.draw_board()
pg.display.update()
messagebox.showinfo('Win','You Won!')
pg.display.quit()
return
self.board_state[self.glenda[0]][self.glenda[1]] = 0
self.board_state[glenda_next[0]][glenda_next[1]] = 2
self.glenda = glenda_next
self.draw_board()
pg.display.update()
self.env.check_lose(self.glenda)
if self.env.result == "lose":
messagebox.showinfo('Lose','Gameover.')
pg.display.quit()
return