Tarot game: first draft

- wix: first draft of cards window
- shell script to split cards from Tarot game found on wikimedia commons
This commit is contained in:
Goffi 2010-04-18 15:47:10 +10:00
commit a441bbbd39
3 changed files with 217 additions and 1 deletions

106
frontends/wix/card_game.py Normal file
View file

@ -0,0 +1,106 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
wix: a SAT frontend
Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import wx
import os.path, glob
import random
import pdb
from logging import debug, info, error
from tools.jid import JID
from quick_frontend.quick_chat import QuickChat
from contact_list import ContactList
class Card():
"""This class is used to represent a card, graphically and logically"""
def __init__(self, file):
"""@param file: path of the PNG file"""
self.bitmap = wx.Image(file).ConvertToBitmap()
root_name = os.path.splitext(os.path.basename(file))[0]
self.family,self.value=root_name.split('_')
self.bout = True if self.family=="atout" and self.value in ["1","21","excuse"] else False
print "Carte:",self.family, self.value, self.bout
def draw(self, dc, x, y):
"""Draw the card on the device context
@param dc: device context
@param x: abscissa
@param y: ordinate"""
dc.DrawBitmap(self.bitmap, x, y, True)
class CardPanel(wx.Panel):
"""This class is used to display the cards"""
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.SetBackgroundColour(wx.GREEN)
self.Bind(wx.EVT_PAINT, self.onPaint)
self.load_cards("/home/goffi/dev/divers/images/cards/")
def load_cards(self, dir):
"""Load all the cards in memory
@param dir: directory where the PNG files are"""
self.cards={}
self.deck=[]
self.cards["atout"]={} #As Tarot is a french game, it's more handy & logical to keep french names
self.cards["pique"]={} #spade
self.cards["coeur"]={} #heart
self.cards["carreau"]={} #diamond
self.cards["trefle"]={} #club
for file in glob.glob(dir+'/*_*.png'):
card = Card(file)
self.cards[card.family, card.value]=card
self.deck.append(card)
"""for value in map(str,range(1,22))+['excuse']:
self.idx_cards.append(self.cards["atout",value])
for family in ["pique", "coeur", "carreau", "trefle"]:
for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]:
self.idx_cards.append(self.cards[family, value])""" #XXX: no need to sort the cards !
def onPaint(self, event):
print "toto", event
dc = wx.PaintDC(self)
x=0
for card in random.sample(self.deck,13):
card.draw(dc,x,0)
x+=37
class CardGame(wx.Frame, QuickChat):
"""The chat Window for one to one conversations"""
def __init__(self, host):
wx.Frame.__init__(self, None, pos=(0,0), size=(800,550))
self.host = host
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.panel = CardPanel(self)
self.sizer.Add(self.panel, 1, flag=wx.EXPAND)
self.SetSizer(self.sizer)
self.SetAutoLayout(True)
#events

View file

@ -0,0 +1,106 @@
#!/bin/sh
#This script split an image with cards, used to split cards from the Tarot deck found on Wikimedia Commons
#This script work with any resolution on the initial image
dest_dir=cards
get_face_name()
{
if [ $1 -le 10 ]
then
echo $1
else
case $1 in
11) echo valet;;
12) echo cavalier;;
13) echo dame;;
14) echo roi;;
esac
fi
}
get_card_name()
{
if [ $1 -le 21 ]
then
echo "atout_$1"
elif [ $1 -eq 22 ]; then
echo "atout_excuse"
elif [ $1 -le 36 ]; then
echo "pique_$(get_face_name $(($1-22)))"
elif [ $1 -le 50 ]; then
echo "coeur_$(get_face_name $(($1-36)))"
elif [ $1 -le 64 ]; then
echo "carreau_$(get_face_name $(($1-50)))"
else
echo "trefle_$(get_face_name $(($1-64)))"
fi
}
current=`pwd`
#TODO: check directory presence
#echo "making directory"
if test -e $dest_dir
then
if test -n "`ls -A $dest_dir`"
then
echo "$dest_dir directory exists and is not empty !"
exit 1
fi
else
mkdir $dest_dir
fi
echo "splitting $dest_dir"
convert Tarot$dest_dir.jpg -bordercolor black -crop 14x6@ -fuzz 50% -trim $dest_dir/card_%02d.png 2>/dev/null
cd $dest_dir
#POST PROCESSING
nb_files=`ls -A1 card*png | wc -l`
num=0
idx=0
max_w=0
max_h=0
deleted=""
for file in card*png
do
num=$((num+1))
size=`stat -c%s $file`
width=`identify -format "%w" $file`
height=`identify -format "%h" $file`
if [ $width -gt $max_w ]
then
max_w=$width
fi
if [ $height -gt $max_h ]
then
max_h=$height
fi
echo -n "post processing file [$file] ($num/$nb_files) | "
echo -n `echo "scale=2;$num/$nb_files*100" | bc`%
echo -n "\r"
if test $size -lt 1000
then #we delete empty files (areas without card on the initial picture)
deleted="$deleted$file\n"
rm -f $file
else
idx=$((idx+1))
#We use transparency for the round corners
mogrify -fuzz 80% -fill none -draw "matte 0,0 floodfill" \
-draw "matte $((width-1)),0 floodfill"\
-draw "matte 0,$((height-1)) floodfill"\
-draw "matte $((width-1)),$((height-1)) floodfill"\
$file
#Time to rename the cards
mv "$file" "$(get_card_name $idx).png"
fi
done
echo "\nEmpty files deleted:\n$deleted"
echo "\nBiggest size: ${max_w}X${max_h}"
cd "$current"
echo "DONE :)"

View file

@ -26,6 +26,7 @@ from quick_frontend.quick_contact_management import QuickContactManagement
import wx
from contact_list import ContactList
from chat import Chat
from card_game import CardGame
from param import Param
from form import Form
from gateways import GatewaysManager
@ -109,7 +110,10 @@ class MainWindow(wx.Frame, QuickApp):
self.profile_pan = ProfileManager(self)
#self.profile_pan.Hide() #gof:
self.sizer.Add(self.profile_pan, 1, flag=wx.EXPAND)
Tarot = CardGame(self)
Tarot.Show()#gof: temp for test
self.Show()
def plug_profile(self, profile_key='@DEFAULT@'):