Add some other features
This commit is contained in:
parent
a5d25ada57
commit
9af04d3c30
|
|
@ -1,8 +1,9 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import re
|
import re
|
||||||
|
import os
|
||||||
import csv
|
import csv
|
||||||
from random import random, randint, choice
|
from random import random, randint, choice
|
||||||
from datetime import timedelta
|
from datetime import timedelta, datetime
|
||||||
from kivy.app import App
|
from kivy.app import App
|
||||||
from kivy.uix.widget import Widget
|
from kivy.uix.widget import Widget
|
||||||
from kivy.uix.button import Button, ButtonBehavior
|
from kivy.uix.button import Button, ButtonBehavior
|
||||||
|
|
@ -21,7 +22,7 @@ from kivy.uix.textinput import TextInput
|
||||||
|
|
||||||
PASS_PENALTY = 4
|
PASS_PENALTY = 4
|
||||||
DRAWING_TIME = 60
|
DRAWING_TIME = 60
|
||||||
WORDS_FILE = "words.txt"
|
WORDS_FILE = "mots.csv"
|
||||||
|
|
||||||
Builder.load_string('''
|
Builder.load_string('''
|
||||||
<ScrollableLabel>:
|
<ScrollableLabel>:
|
||||||
|
|
@ -66,6 +67,7 @@ class MyPaintWidget(Label):
|
||||||
pen_characteristics = {"current_color": (0,0,0,1),
|
pen_characteristics = {"current_color": (0,0,0,1),
|
||||||
"current_width": 4
|
"current_width": 4
|
||||||
}
|
}
|
||||||
|
current_round = 0
|
||||||
current_word = ""
|
current_word = ""
|
||||||
message_label = None
|
message_label = None
|
||||||
time = timedelta(seconds=DRAWING_TIME)
|
time = timedelta(seconds=DRAWING_TIME)
|
||||||
|
|
@ -74,22 +76,38 @@ class MyPaintWidget(Label):
|
||||||
def __init__(self, text="", **kwargs):
|
def __init__(self, text="", **kwargs):
|
||||||
super().__init__(**kwargs)
|
super().__init__(**kwargs)
|
||||||
self._keyboard = None
|
self._keyboard = None
|
||||||
|
|
||||||
|
with self.canvas.before:
|
||||||
|
Color(1,1,1, mode='rgb')
|
||||||
|
self.bg = Rectangle(pos=self.pos, size=self.size)
|
||||||
|
def resize_bg(self, obj):
|
||||||
|
self.bg.size = self.size
|
||||||
|
self.bg.pos = self.pos
|
||||||
|
self.bind(size=resize_bg, pos=resize_bg)
|
||||||
|
|
||||||
self._get_keyboard()
|
self._get_keyboard()
|
||||||
self._load_words()
|
self._load_words()
|
||||||
|
self._output_dir = datetime.now().isoformat(timespec='minutes').replace(":","-") + "_creations"
|
||||||
|
self._fullscreen = False
|
||||||
self.reset_game()
|
self.reset_game()
|
||||||
|
|
||||||
def _load_words(self):
|
def _load_words(self):
|
||||||
if WORDS_FILE.endswith(".csv"):
|
try:
|
||||||
# read a CSV file
|
if WORDS_FILE.endswith(".csv"):
|
||||||
with open(WORDS_FILE, "r", newline='') as wordfile:
|
# read a CSV file
|
||||||
my_reader = csv.reader(wordfile)
|
with open(WORDS_FILE, "r", newline='', encoding="utf-8") as wordfile:
|
||||||
self._game_wordlists["possible_wordlist"] = {line[0]:
|
my_reader = csv.reader(wordfile)
|
||||||
line[1] for line in my_reader}
|
self._game_wordlists["possible_wordlist"] = {line[0]:
|
||||||
else:
|
line[1] for line in my_reader}
|
||||||
# read a plain text file
|
else:
|
||||||
with open(WORDS_FILE, "r") as wordfile:
|
# read a plain text file
|
||||||
self._wordlist = (word.strip() for word in wordfile.readlines())
|
with open(WORDS_FILE, "r", encoding="utf-8") as wordfile:
|
||||||
self._game_wordlists["possible_wordlist"] = dict.fromkeys(self._wordlist)
|
self._wordlist = (word.strip() for word in wordfile.readlines())
|
||||||
|
self._game_wordlists["possible_wordlist"] = dict.fromkeys(self._wordlist)
|
||||||
|
except OSError:
|
||||||
|
Popup(title="Error", content=Label(text=f"Erreur de lecture du fichier '{WORDS_FILE}'")).open()
|
||||||
|
self._game_wordlists["possible_wordlist"] = dict()
|
||||||
|
|
||||||
|
|
||||||
def _get_keyboard(self):
|
def _get_keyboard(self):
|
||||||
if self._keyboard is None:
|
if self._keyboard is None:
|
||||||
|
|
@ -125,14 +143,22 @@ class MyPaintWidget(Label):
|
||||||
elif keycode[0] == 13:
|
elif keycode[0] == 13:
|
||||||
self.pass_word()
|
self.pass_word()
|
||||||
elif keycode[1] == 'z' and "ctrl" in modifiers:
|
elif keycode[1] == 'z' and "ctrl" in modifiers:
|
||||||
if self.canvas.before.children:
|
if self.canvas.after.children:
|
||||||
self.canvas.before.remove(self.canvas.before.children[-1])
|
self.canvas.after.remove(self.canvas.after.children[-1])
|
||||||
self.canvas.before.remove(self.canvas.before.children[-1])
|
self.canvas.after.remove(self.canvas.after.children[-1])
|
||||||
self.canvas.before.remove(self.canvas.before.children[-1])
|
self.canvas.after.remove(self.canvas.after.children[-1])
|
||||||
elif self._game_state["game_finished"]:
|
elif self._game_state["game_finished"]:
|
||||||
if keycode[0] == 8:
|
if keycode[0] == 8:
|
||||||
self.reset_game()
|
self.reset_game()
|
||||||
|
|
||||||
|
if keycode[0] == 292:
|
||||||
|
print(Window.fullscreen)
|
||||||
|
if self._fullscreen:
|
||||||
|
Window.fullscreen = False
|
||||||
|
else:
|
||||||
|
Window.fullscreen = "auto"
|
||||||
|
self._fullscreen = not self._fullscreen
|
||||||
|
|
||||||
# Keycode is composed of an integer + a string
|
# Keycode is composed of an integer + a string
|
||||||
# If we hit escape, let the input through to leave the app
|
# If we hit escape, let the input through to leave the app
|
||||||
if keycode[1] == 'escape':
|
if keycode[1] == 'escape':
|
||||||
|
|
@ -149,6 +175,7 @@ class MyPaintWidget(Label):
|
||||||
self.text=""
|
self.text=""
|
||||||
self._game_state["game_started"] = True
|
self._game_state["game_started"] = True
|
||||||
self._game_state["game_paused"] = False
|
self._game_state["game_paused"] = False
|
||||||
|
self.current_round += 1
|
||||||
self.next_word(True)
|
self.next_word(True)
|
||||||
|
|
||||||
def next_word(self, passed=False):
|
def next_word(self, passed=False):
|
||||||
|
|
@ -161,7 +188,10 @@ class MyPaintWidget(Label):
|
||||||
else:
|
else:
|
||||||
self.message_label.color = '#5BB834'
|
self.message_label.color = '#5BB834'
|
||||||
self.message_label.text = f'"{self.current_word}" a été trouvé !'
|
self.message_label.text = f'"{self.current_word}" a été trouvé !'
|
||||||
|
if not self._game_wordlists["possible_wordlist"]:
|
||||||
|
self.time = timedelta(0)
|
||||||
|
return
|
||||||
|
self.save_frame()
|
||||||
self.current_word = choice(list(self._game_wordlists["possible_wordlist"]))
|
self.current_word = choice(list(self._game_wordlists["possible_wordlist"]))
|
||||||
value = self._game_wordlists["possible_wordlist"].pop(self.current_word)
|
value = self._game_wordlists["possible_wordlist"].pop(self.current_word)
|
||||||
if value:
|
if value:
|
||||||
|
|
@ -177,6 +207,14 @@ class MyPaintWidget(Label):
|
||||||
else:
|
else:
|
||||||
self.time = timedelta(0)
|
self.time = timedelta(0)
|
||||||
|
|
||||||
|
def save_frame(self):
|
||||||
|
if self.current_word:
|
||||||
|
if not os.path.isdir(self._output_dir):
|
||||||
|
os.mkdir(self._output_dir)
|
||||||
|
filename = os.path.join(self._output_dir,
|
||||||
|
f"round-{self.current_round}_" + os.path.normcase(self.current_word) + ".png")
|
||||||
|
self.export_to_png(filename)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_game_playing(self):
|
def is_game_playing(self):
|
||||||
return (self._game_state["game_started"]
|
return (self._game_state["game_started"]
|
||||||
|
|
@ -189,7 +227,7 @@ class MyPaintWidget(Label):
|
||||||
|
|
||||||
def on_touch_down(self, touch):
|
def on_touch_down(self, touch):
|
||||||
if self.is_game_playing:
|
if self.is_game_playing:
|
||||||
with self.canvas.before:
|
with self.canvas.after:
|
||||||
if self.collide_point(touch.x, touch.y):
|
if self.collide_point(touch.x, touch.y):
|
||||||
Color(*self.pen_characteristics["current_color"], mode='rgba')
|
Color(*self.pen_characteristics["current_color"], mode='rgba')
|
||||||
diameter = self.pen_characteristics["current_width"]
|
diameter = self.pen_characteristics["current_width"]
|
||||||
|
|
@ -259,6 +297,7 @@ class MyPaintWidget(Label):
|
||||||
"-"+ "\n-".join(found_words) if found_words else "",
|
"-"+ "\n-".join(found_words) if found_words else "",
|
||||||
f"Passés: [color=#FF2A40]{len(unfound_words)}[/color]",
|
f"Passés: [color=#FF2A40]{len(unfound_words)}[/color]",
|
||||||
"- "+ "\n- ".join(unfound_words) if unfound_words else ""]
|
"- "+ "\n- ".join(unfound_words) if unfound_words else ""]
|
||||||
|
self.save_frame()
|
||||||
self.clear_drawing()
|
self.clear_drawing()
|
||||||
self._popup = Popup(title='Fini !',
|
self._popup = Popup(title='Fini !',
|
||||||
content=ScrollableLabel(text="[size=24]"+"\n".join(lines)+"[/size]",
|
content=ScrollableLabel(text="[size=24]"+"\n".join(lines)+"[/size]",
|
||||||
|
|
@ -278,12 +317,16 @@ class MyPaintWidget(Label):
|
||||||
self.message_label.text = ""
|
self.message_label.text = ""
|
||||||
self.color = (0,0,0,1)
|
self.color = (0,0,0,1)
|
||||||
self.text = "Appuyer sur 'espace' pour commencer à jouer"
|
self.text = "Appuyer sur 'espace' pour commencer à jouer"
|
||||||
|
self.current_word = ""
|
||||||
|
self._game_wordlists["found_words"] = set()
|
||||||
|
self._game_wordlists["unfound_words"] = set()
|
||||||
|
|
||||||
self.time = timedelta(seconds=DRAWING_TIME)
|
self.time = timedelta(seconds=DRAWING_TIME)
|
||||||
self._game_state["game_finished"] = False
|
self._game_state["game_finished"] = False
|
||||||
self._game_state["game_started"] = False
|
self._game_state["game_started"] = False
|
||||||
|
|
||||||
def clear_drawing(self):
|
def clear_drawing(self):
|
||||||
self.canvas.before.clear()
|
self.canvas.after.clear()
|
||||||
|
|
||||||
class SizeButton(ButtonBehavior, Widget):
|
class SizeButton(ButtonBehavior, Widget):
|
||||||
def __init__(self, callback, size=1, **kwargs):
|
def __init__(self, callback, size=1, **kwargs):
|
||||||
|
|
@ -305,7 +348,7 @@ class SizeButton(ButtonBehavior, Widget):
|
||||||
def on_release(self):
|
def on_release(self):
|
||||||
self.callback(self.dot_size)
|
self.callback(self.dot_size)
|
||||||
|
|
||||||
class MyPaintApp(App):
|
class GuessWhatIDrawApp(App):
|
||||||
state = False
|
state = False
|
||||||
init = True
|
init = True
|
||||||
|
|
||||||
|
|
@ -387,6 +430,9 @@ class MyPaintApp(App):
|
||||||
|
|
||||||
if self.state:
|
if self.state:
|
||||||
Window.clearcolor = (1, 1, 1, 1)
|
Window.clearcolor = (1, 1, 1, 1)
|
||||||
|
with self.painter.canvas.before:
|
||||||
|
Color(1,1,1, mode='rgb')
|
||||||
|
self.painter.bg = Rectangle(pos=self.painter.pos, size=self.painter.size)
|
||||||
self.painter.pen_characteristics["current_color"] = (0,0,0,1)
|
self.painter.pen_characteristics["current_color"] = (0,0,0,1)
|
||||||
self.right_row.children[-1].background_color = self.painter.current_color
|
self.right_row.children[-1].background_color = self.painter.current_color
|
||||||
self.word_label.color = (0,0,0,1)
|
self.word_label.color = (0,0,0,1)
|
||||||
|
|
@ -394,6 +440,9 @@ class MyPaintApp(App):
|
||||||
self.state = False
|
self.state = False
|
||||||
else:
|
else:
|
||||||
Window.clearcolor = (0,0,0,1)
|
Window.clearcolor = (0,0,0,1)
|
||||||
|
with self.painter.canvas.before:
|
||||||
|
Color(0,0,0, mode='rgb')
|
||||||
|
self.painter.bg = Rectangle(pos=self.painter.pos, size=self.painter.size)
|
||||||
self.painter.pen_characteristics["current_color"] = (1,1,1,1)
|
self.painter.pen_characteristics["current_color"] = (1,1,1,1)
|
||||||
self.right_row.children[-1].background_color = self.painter.current_color
|
self.right_row.children[-1].background_color = self.painter.current_color
|
||||||
self.word_label.color = (1,1,1,1)
|
self.word_label.color = (1,1,1,1)
|
||||||
|
|
@ -406,4 +455,4 @@ class MyPaintApp(App):
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
MyPaintApp().run()
|
GuessWhatIDrawApp().run()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue