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