-
Notifications
You must be signed in to change notification settings - Fork 0
/
onlineGameInterface.py
207 lines (181 loc) · 6.24 KB
/
onlineGameInterface.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
from tkinter import messagebox
import random
import tkinter as tk
import time
import math
import copy
from PIL import ImageTk, Image, ImageEnhance
from gamelogic import GameLogic
from timer import Timer
from solve import Solver
from tile import Tile
# Provides online App class limited ofline app with multiplayer support
class App(tk.Toplevel):
def __init__(self, master, seed, friend, comm):
super().__init__(master)
self.master = master
self.type = 1
self.n = 4
self.w = 500
self.h = self.w+100
self.canvas = tk.Canvas(self, width=self.w, height=self.h)
self.seed = seed
self.comm = comm
self.friend = friend
self.canvas.pack()
self.loadSources()
self.f = False
self.startGame()
def loadSources(self):
self.icons = {}
self.textures = self.loadTextures("")
backgrounds = ["gyr.jpg", "gyr.jpg"]
bgPath = random.choice(backgrounds)
im = Image.open("textures/bg/"+bgPath)
self.bg = ImageTk.PhotoImage(im)
def startGame(self):
self.started = True
self.model = GameLogic(self.n)
self.gameOver = False
self.onPause = True
self.master.after(0, self.draw)
self.numberOfMoves = 0
self.shuffleBoard()
self.canvas.bind("<Button-1>", self.mouse)
self.t = Timer(self.master, self.canvas)
def setBG(self):
self.canvas.create_image(0, 0, image=self.bg, anchor='nw')
def crop(self, infile):
im = Image.open(infile)
width, height = im.size
imgwidth = self.w
imgheight = imgwidth * height // width
im = im.resize((imgwidth, imgheight), Image.ANTIALIAS)
cropped = []
for i in range(self.n):
for j in range(self.n):
box = (j*125, i*125, (j+1)
* 125, (i+1)*125)
piece = im.crop(box)
img = Image.new('RGB', (125, 125), 255)
img.paste(piece)
cropped.append(img)
return cropped
@staticmethod
def reduce_opacity(im, opacity):
"""Returns an image with reduced opacity."""
# from https://stackoverflow.com/questions/61271072/how-can-i-solve-python-3-pil-putalpha-problem
assert opacity >= 0 and opacity <= 1
if im.mode != 'RGBA':
im = im.convert('RGBA')
else:
im = im.copy()
alpha = im.split()[3]
alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
im.putalpha(alpha)
return im
def loadTextures(self, infile):
infile = "textures/wood.jpg"
alpha = int(0.5 * 255)
fill = "#0269A4"
fill = self.master.winfo_rgb(fill) + (alpha,)
im = Image.new('RGBA', (123, 123), fill)
# im = im.putalpha(128)
ph = ImageTk.PhotoImage(im)
number = self.n * self.n
return [ph] * number
def shuffleBoard(self):
# self.seed = self.model.getShuffleSeed()
self.shuffleAnimaion(self.seed)
def shuffleAnimaion(self, seed):
self.draw()
if not seed:
self.onPause = False
return
self.model.moveDirection(seed[0])
seed = seed[1:]
self.master.after(50, lambda seed=seed: self.shuffleAnimaion(seed))
def handleGameOver(self):
# actions when game is over
if self.gameOver:
self.timeFinished = round(self.t.getTotal(), 2)
self.comm.sendMessage(self.friend, "W"+str(self.timeFinished))
tk.messagebox.showinfo(message="CONGRATSSSSSSS")
self.destroy()
def draw(self):
self.tiles = self.getTiles(self.n)
self.drawTiles()
self.putNumberOfMoves()
if self.model.isGameOver() and not self.onPause:
self.gameOver = True
self.t.stop()
self.handleGameOver()
def getPos(self, x):
pos = math.floor(x / 125)
if pos < 0:
pos = 0
if pos >= self.n:
pos -= 1
return pos
def drawMoveDirection(self, direction):
if self.onPause:
return
res = self.model.moveDirection(direction)
self.master.after(0, self.draw)
return res
def putNumberOfMoves(self):
# puts number of moves into the screen
self.canvas.create_text(
100, 520, text="Your #moves:" + str(self.numberOfMoves), font="Helvetica 16")
self.canvas.create_text(
100, 540, text="Opponent #moves:", font="Helvetica 16")
def setFriendStat(self, s):
# get's number of moves from friend
if self.f:
try:
self.canvas.delete(self.f)
except Exception:
pass
self.f = self.canvas.create_text(200, 540, text=s, font="Helvetica 16")
def mouse(self, event):
# handles mouse input
y = self.getPos(event.x)
x = self.getPos(event.y) if event.y < self.w else -1
if x < 0:
return
if not self.onPause and (x, y != self.model.empty):
if self.model.moveByBlock(x, y):
if self.numberOfMoves == 0:
self.t.run()
self.numberOfMoves += 1
self.putNumberOfMoves()
self.master.after(0, self.draw)
def getTiles(self, n):
# returns tiles as a class objects
board = self.model.getBoard()
tiles = copy.deepcopy(board) # copying to have exact same dimensions
for i in range(len(board)):
for j in range(len(board[i])):
newTile = Tile(
self.canvas, board[i][j], self.type, j*125, i*125, 125, self.textures)
tiles[i][j] = newTile
return tiles
def drawTiles(self):
# draws tiles on the screen
self.canvas.delete("all")
for row in self.tiles:
for tile in row:
tile.display()
def getMoves(self):
# returns number of moves
return self.numberOfMoves
def getCanvas(self):
# return canvas object
return self.canvas
if __name__ == "__main__":
root = tk.Tk()
seed = "DDLLRDURLUDDURLDURL"
friend = "User"
comm = ""
app = App(root, seed, friend, comm)
root.mainloop()