Add lobby
This commit is contained in:
160
lobby.py
Normal file
160
lobby.py
Normal file
@@ -0,0 +1,160 @@
|
||||
import tkinter as tk
|
||||
import color
|
||||
import main as quadraLudi
|
||||
import importlib
|
||||
|
||||
class Lobby:
|
||||
def __init__(self):
|
||||
# Win
|
||||
win = tk.Tk()
|
||||
win.title('QuadraLudi - Lobby')
|
||||
win.geometry('800x640')
|
||||
win['bg'] = color.LEVEL_1
|
||||
# win.resizable(False, False)
|
||||
self.win = win
|
||||
|
||||
# Frame
|
||||
self.frame = None
|
||||
|
||||
# Variables
|
||||
self.pseudo = tk.StringVar(win, "")
|
||||
self.message = tk.StringVar(win, "")
|
||||
|
||||
# First
|
||||
self.nameDisplay()
|
||||
|
||||
def resetFrame(self):
|
||||
if self.frame != None:
|
||||
self.frame.destroy()
|
||||
|
||||
frame = tk.Frame(self.win, bg = color.LEVEL_1)
|
||||
frame.pack(fill = tk.BOTH, expand = True, padx = 5, pady = 5)
|
||||
self.frame = frame
|
||||
|
||||
def nameDisplay(self):
|
||||
self.resetFrame()
|
||||
|
||||
# Place
|
||||
pseudoFrame = tk.LabelFrame(self.frame, text = 'Pseudo', bg = color.LEVEL_1, fg = color.PURPLE)
|
||||
pseudoFrame.place(relx = 0.5, rely = 0.5, width = 200, height = 50, anchor = 'center')
|
||||
|
||||
entry = tk.Entry(pseudoFrame, textvariable = self.pseudo, bg = color.LEVEL_2, fg = color.GHOST)
|
||||
entry.bind('<Return>', lambda e: self.nameConfirm())
|
||||
entry.pack(side = tk.LEFT, expand = True)
|
||||
entry.focus()
|
||||
tk.Button(
|
||||
pseudoFrame, text = 'Valider', bg = color.PURPLE, fg = color.LEVEL_1,
|
||||
command = lambda: self.nameConfirm()
|
||||
).pack(side = tk.LEFT, expand = True)
|
||||
|
||||
def lobbyDisplay(self):
|
||||
self.resetFrame()
|
||||
|
||||
# Configure layout
|
||||
mainFrame = self.frame
|
||||
mainFrame.columnconfigure(0, weight = 1)
|
||||
mainFrame.columnconfigure(1, weight = 4)
|
||||
mainFrame.rowconfigure(0, weight = 1)
|
||||
|
||||
# Score
|
||||
scoreFrame = tk.LabelFrame(mainFrame, text = 'Score', bg = color.LEVEL_1, fg = color.PURPLE, font = (None, 12))
|
||||
scoreFrame.grid(column = 0, row = 0, sticky = 'NSEW', padx = 5)
|
||||
self.scoreFrame = scoreFrame
|
||||
self.addScoreEntry(1, 'Eveldee', 2354)
|
||||
self.addScoreEntry(2, 'Fetyrix', 42)
|
||||
|
||||
# Chat
|
||||
chatFrame = tk.LabelFrame(mainFrame, text = 'Chat', bg = color.LEVEL_1, fg = color.PURPLE, font = (None, 12))
|
||||
chatFrame.grid(column = 1, row = 0, sticky = 'NSEW', padx = 5)
|
||||
self.chatFrame = chatFrame
|
||||
self.addChatEntry('16:45', 'Eveldee', 'Hello')
|
||||
self.addChatEntry('16:46', 'Fetyrix', 'Je suis un très long text text text text text text text text text text text text text text text text')
|
||||
|
||||
bottomFrame = tk.Frame(chatFrame, bg = color.LEVEL_5)
|
||||
bottomFrame.pack(side = tk.BOTTOM, fill = tk.X)
|
||||
entry = tk.Entry(bottomFrame, textvariable = self.message, bg = color.LEVEL_1, fg = color.GHOST, font = (None, 11))
|
||||
entry.bind('<Return>', lambda e: self.sendMessage())
|
||||
entry.pack(side = tk.LEFT, fill = tk.BOTH, expand = True, padx = 5, pady = 5)
|
||||
entry.focus()
|
||||
self.chatEntry = entry
|
||||
tk.Button(
|
||||
bottomFrame, text = 'Envoyer', bg = color.PURPLE, fg = color.LEVEL_1,
|
||||
command = lambda: self.sendMessage()
|
||||
).pack(side = tk.LEFT, padx = 5, pady = 5)
|
||||
|
||||
# Play
|
||||
tk.Button(
|
||||
mainFrame, text = 'Jouer', bg = color.PURPLE, fg = color.LEVEL_1,
|
||||
command = lambda: self.play()
|
||||
).grid(column = 0, row = 1, columnspan = 2, sticky = 'NSEW', padx = 5, pady = 5)
|
||||
|
||||
def nameConfirm(self):
|
||||
pseudo = self.pseudo.get()
|
||||
|
||||
if pseudo == '':
|
||||
return
|
||||
|
||||
self.lobbyDisplay()
|
||||
|
||||
def sendMessage(self):
|
||||
# Check empty
|
||||
message = self.message.get()
|
||||
if message == '':
|
||||
return
|
||||
|
||||
# Send
|
||||
|
||||
|
||||
# Clear
|
||||
self.message.set('')
|
||||
|
||||
def sendScore(self, score):
|
||||
pass
|
||||
|
||||
def play(self):
|
||||
# Reload
|
||||
importlib.reload(quadraLudi)
|
||||
|
||||
# Minimize
|
||||
self.win.iconify()
|
||||
|
||||
# Display
|
||||
quadraLudi.main(self.win, lambda score: self.playEnd(score))
|
||||
|
||||
def playEnd(self, score):
|
||||
# Display window, focus
|
||||
self.win.deiconify()
|
||||
self.win.focus_force()
|
||||
self.chatEntry.focus()
|
||||
|
||||
# Send score
|
||||
self.sendScore(score)
|
||||
|
||||
def addScoreEntry(self, rank, pseudo, score):
|
||||
frame = tk.Frame(self.scoreFrame)
|
||||
frame.pack(anchor = tk.W)
|
||||
font = (None, 11)
|
||||
tk.Label(frame, text = f'{rank}', bg = color.LEVEL_1, fg = color.PURPLE, font = font).pack(side = tk.LEFT)
|
||||
tk.Label(frame, text = f'>', bg = color.LEVEL_1, fg = color.BLUE, font = font).pack(side = tk.LEFT)
|
||||
tk.Label(frame, text = f'{pseudo}', bg = color.LEVEL_1, fg = color.GREEN, font = font).pack(side = tk.LEFT)
|
||||
tk.Label(frame, text = f':', bg = color.LEVEL_1, fg = color.YELLOW, font = font).pack(side = tk.LEFT)
|
||||
tk.Label(frame, text = f'{score}', bg = color.LEVEL_1, fg = color.PINK, font = font).pack(side = tk.LEFT)
|
||||
|
||||
def addChatEntry(self, time, pseudo, message):
|
||||
frame = tk.Frame(self.chatFrame, bg = color.LEVEL_1)
|
||||
frame.pack(anchor = tk.W)
|
||||
font = (None, 10)
|
||||
tk.Label(frame, text = f'[{time}]', bg = color.LEVEL_1, fg = color.PURPLE, font = font).pack(side = tk.LEFT)
|
||||
tk.Label(frame, text = f'({pseudo})', bg = color.LEVEL_1, fg = color.GREEN, font = font).pack(side = tk.LEFT)
|
||||
tk.Label(frame, text = f':', bg = color.LEVEL_1, fg = color.YELLOW, font = font).pack(side = tk.LEFT)
|
||||
tk.Label(frame, text = f'{message}', bg = color.LEVEL_1, fg = color.PINK, font = font, wraplength = 450, justify = tk.LEFT).pack(side = tk.LEFT)
|
||||
|
||||
def show(self):
|
||||
self.win.mainloop()
|
||||
|
||||
def main():
|
||||
lobby = Lobby()
|
||||
lobby.show()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
62
main.py
62
main.py
@@ -7,20 +7,17 @@ from PIL import Image, ImageTk
|
||||
from time import sleep
|
||||
from tkinter import messagebox
|
||||
|
||||
win = tk.Tk()
|
||||
win['bg'] = color.LEVEL_9
|
||||
win.title('Quadraludi')
|
||||
win.geometry('806x606')
|
||||
win.resizable(False, False)
|
||||
# Var
|
||||
win = None
|
||||
|
||||
# Res
|
||||
image_center = ImageTk.PhotoImage(Image.open('res/logo/main.png'))
|
||||
image_center = None
|
||||
|
||||
# Time
|
||||
time = None
|
||||
delta = None
|
||||
tick = 0
|
||||
score = tk.IntVar(win, 0)
|
||||
score = None
|
||||
|
||||
# Difficulty
|
||||
difficulty = 1.0
|
||||
@@ -29,6 +26,28 @@ DIFFICULTY_STEP = (MAX_DIFFICULTY - difficulty) / 5000
|
||||
|
||||
# End
|
||||
_end = False
|
||||
lobbyEnd = None
|
||||
|
||||
def ini(master):
|
||||
global win, image_center, score
|
||||
|
||||
# if run from lobby
|
||||
if (master != None):
|
||||
win = tk.Toplevel(master)
|
||||
else:
|
||||
win = tk.Tk()
|
||||
|
||||
win['bg'] = color.LEVEL_9
|
||||
win.title('Quadraludi')
|
||||
win.geometry('806x606')
|
||||
win.resizable(False, False)
|
||||
win.focus_force()
|
||||
|
||||
# Res
|
||||
image_center = ImageTk.PhotoImage(Image.open('res/logo/main.png'))
|
||||
|
||||
# Time
|
||||
score = tk.IntVar(win, 0)
|
||||
|
||||
def loop(deltaTime):
|
||||
global time, delta, tick, score, difficulty
|
||||
@@ -42,11 +61,14 @@ def loop(deltaTime):
|
||||
deltatime = (delta - time).microseconds * 1e-6
|
||||
|
||||
# Loop
|
||||
if tick > 2:
|
||||
fall.loop(deltatime, difficulty, end)
|
||||
osu.loop(deltatime, difficulty, end)
|
||||
pong.loop(deltatime, difficulty, end)
|
||||
space.loop(deltatime, difficulty, end)
|
||||
try:
|
||||
if tick > 2:
|
||||
fall.loop(deltatime, difficulty, end)
|
||||
osu.loop(deltatime, difficulty, end)
|
||||
pong.loop(deltatime, difficulty, end)
|
||||
space.loop(deltatime, difficulty, end)
|
||||
except:
|
||||
return
|
||||
|
||||
# Step tick
|
||||
tick += 1
|
||||
@@ -62,8 +84,12 @@ def loop(deltaTime):
|
||||
time = delta
|
||||
win.after(int(1000 / 60), lambda: loop(deltaTime))
|
||||
|
||||
def main():
|
||||
global time, delta
|
||||
def main(master = None, end = None):
|
||||
global time, delta, lobbyEnd
|
||||
|
||||
# Ini
|
||||
lobbyEnd = end
|
||||
ini(master)
|
||||
|
||||
# Create Canvas
|
||||
canvas_pong = tk.Canvas(win, width = 400, height = 300, bd = 0, highlightthickness = 0, relief = 'ridge', bg = color.LEVEL_1)
|
||||
@@ -82,7 +108,7 @@ def main():
|
||||
logo = tk.PhotoImage('res/logo/icon.png')
|
||||
win.tk.call('wm', 'iconphoto', win._w, logo)
|
||||
|
||||
tk.Label(win, image = image_center, bg = color.LEVEL_1).place(x = 401 - 32, y = 301 - 32)
|
||||
# tk.Label(win, image = image_center, bg = color.LEVEL_1).place(x = 401 - 32, y = 301 - 32)
|
||||
|
||||
# Score
|
||||
tk.Label(win, textvariable = score, bg = color.LEVEL_1, fg = color.BROWN, font = (None, 12)).place(x = 2, y = 2)
|
||||
@@ -124,7 +150,11 @@ def end():
|
||||
|
||||
# Terminate
|
||||
win.destroy()
|
||||
sys.exit(0)
|
||||
|
||||
if type(win) == tk.Tk:
|
||||
sys.exit(0)
|
||||
else:
|
||||
lobbyEnd(score.get())
|
||||
|
||||
# Main
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user