Codes
Retrouvez ici mes différents scripts et morceaux de code.
#!/usr/bin/python2.7
#-*- coding: utf-8 -*-
import time
import datetime
import serial
import threading
import random
from random import shuffle
from random import randint
from tkinter import *
from tkinter import ttk
from PIL import ImageTk, Image
from itertools import count
from interface import Glask_Chillax_V1
from interface import Settings
from glask import Players, Glask
from transforms import RGBTransform
def RGB(R, G, B):
if R > 255:
R = 255
if G > 255:
G = 255
if B > 255:
B = 255
color = Image.open("glask.png")
color = color.convert('RGBA')
color = RGBTransform().mix_with((R, G, B),factor=.30).applied_to(color)
base = Image.open("glask_t.png")
base = base.convert('RGBA')
f = Image.new("RGBA", base.size)
f = Image.alpha_composite(f, color)
f = Image.alpha_composite(f, base)
return f
def simOUT(COM, deformatOut):
comp = ""
lab1 = COM.split("#")
lab2 = deformatOut.split("#")
i = 0
for s in lab1:
u = (lab1[i]).split(";")
v = (lab2[i]).split(";")
if len(u) > 2 and len(v) > 2:
w = "#" + v[0] + ";" + v[1] + ";" + v[2] + ";" + v[3] + ";" + v[4] + ";" + u[5] + ";" + u[6] + ";" + u[7] + ";"
comp = comp + w + "\n"
i = i + 1
if len(comp.split("#")) > 1:
f = open('glask.txt','w')
f.write(comp)
f.close()
BT = False
try:
ser = serial.Serial('/dev/rfcomm0', 9600, timeout=1)
BT = True
except Exception as e:
print "Unable to BT connect"
print "Go for sim..."
playersArray = Players()
n = 4
playersName = []
potentielAffond = []
nbreAffond = []
notif = []
nLoop = 0
for i in range(1, n+1):
playersName.append("#" + str(i))
nbreAffond.append(0)
# Interface graphique
root = Tk()
top = Glask_Chillax_V1(root, n)
settings = None
def notifCenter(s):
if len(notif) == 0:
top.Message2.configure(text=s)
notif.append(s)
else:
if notif[len(notif)-1] != s:
notif.append(s)
top.Message1.configure(text=notif[len(notif)-2])
top.Message2.configure(text=notif[len(notif)-1])
def blink():
(playersArray.get(1)).RGB(100, 100, 100)
print (playersArray.get(1)).formatOut()
def settingsWindow():
sett = Tk()
global settings
settings = Settings(sett, n)
settings.Button1.configure(command=setPlayers)
def setPlayers():
#playersName[0] = top.Entry1.get()
#playersName[1] = top.Entry2.get()
#playersName[2] = top.Entry3.get()
#s = "Bonne guindaille " + playersName[0] + ", " + playersName[1] + " et " + playersName[2]
#notifCenter(s)
global settings
i = 0
st = "Bonne guindaille "
for s in settings.Entry:
if (s.get()).strip():
playersName[i] = s.get()
if i == len(settings.Entry) - 2:
st = st + s.get() + " et "
elif i == len(settings.Entry) - 1:
st = st + s.get()
else:
st = st + s.get() + ", "
i = i + 1
notifCenter(st)
def launchAffond():
for glask in playersArray.players:
glask.RGB(0, 0, 0)
select = random.randint(1, int(playersArray.numOfPlayers()))
print select
for s in range(0, 3):
for glask in sorted(playersArray.players, key=lambda L: random.random()):
glask.RGBtoRGB(255, 0, 0, 300, 0, 0, 0)
time.sleep(0.3)
time.sleep(0.3)
for glask in sorted(playersArray.players, key=lambda L: random.random()):
if int(glask.id) == int(select):
glask.RGBtoRGB(255, 0, 0, 200, 0, 0, 0)
time.sleep(0.2)
glask.RGBtoRGB(255, 0, 0, 200, 0, 0, 0)
time.sleep(0.2)
glask.RGB(0, 255, 0)
notifCenter("C'est à " + playersName[int(glask.id)-1] + " d'affoner !")
break
else:
glask.RGBtoRGB(255, 0, 0, 300, 0, 0, 0)
time.sleep(0.3)
def buttonAffond():
t = threading.Timer(0, launchAffond)
t.start()
top.Button1.configure(command=settingsWindow)
top.Button2.configure(command=buttonAffond)
# Loop
def clock():
global nLoop
if BT:
COM = ser.readline()
else:
f = open("glask.txt","r")
COM = ""
for line in f:
COM = COM + line
f.close()
# Créé le tableau de joueur à partir des données COM
for player in COM.split("#"):
if player != None:
states = player.split(";")
if len(states) > 2:
currentPlayer = Glask(states[0], states[1], states[2], states[3], states[4], states[5], states[6], states[7])
playersArray.set(currentPlayer)
# Jeu
i = 1
while i < n+1:
glask = playersArray.get(i)
if playersArray.get(i) != None:
# Gestion des players Online()
if playersArray.isOnline(i):
img = ImageTk.PhotoImage(RGB(glask.R, glask.G, glask.B))
top.Canvas[i-1].image = img
top.Canvas[i-1].create_image(150, 230, image=img)
top.Status[i-1].create_oval(0, 0, 18, 18, width=0, fill='green2')
top.LStatus[i-1].configure(text=playersName[i-1] + " is online")
top.Resulta[i-1].configure(text=str(nbreAffond[i-1]))
top.Resultb[i-1].configure(text=str(int(glask.bestTime)/1000.0))
# Gestion de l'animation isDrinking()
if glask.isDrinking:
top.Loading[i-1].place(x=top.LoadingPlace[i-1], y=200, height=40, width=40)
else:
top.Loading[i-1].place_forget()
# Gestion des isFilled()
if glask.isFilled:
img = ImageTk.PhotoImage(Image.open('glask_filled.png'))
top.Canvas[i-1].image = img
top.Canvas[i-1].create_image(150, 230, image=img)
# Gestion des affonds
# Si le verre est rempli et que le joueur boit alors il fait un potentiel affond
if glask.isFilled and glask.isDrinking and i not in potentielAffond:
potentielAffond.append(i)
# Si le verre est toujours rempli mais qu'il ne boit plus, ce n'etait pas un affond
if i in potentielAffond and glask.isFilled and not glask.isDrinking:
potentielAffond.pop(potentielAffond.index(i))
# Si le verre n'est plus rempli, qu'il ne boit plus et que c'etait un affond potentiel alors c'est un affond
if not glask.isFilled and not glask.isDrinking and i in potentielAffond:
potentielAffond.pop(potentielAffond.index(i))
s = playersName[i-1] + " vient d'affoner !"
notifCenter(s)
nbreAffond[i-1] = nbreAffond[i-1] + 1
# Notification en cas de record
if int(glask.bestTime) == int(glask.lastTime) and nbreAffond[i-1] > 0:
s = playersName[i-1] + " vient de faire un record !"
notifCenter(s)
i = i + 1
# Retourne le tableau selon ce qui a été joué
#top.Label4.configure(text=COM)
#top.Label5.configure(text=playersArray.formatOut())
if not BT:
simOUT(COM, playersArray.formatOut())
nLoop = nLoop + 1
root.after(10, clock)
clock()
root.mainloop()