Mercurial > libervia-backend
diff frontends/src/wix/quiz_game.py @ 361:141eeb7cd9e6
Quizz game: first draft
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 12 Jun 2011 16:28:33 +0200 |
parents | |
children | 208107419b17 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/frontends/src/wix/quiz_game.py Sun Jun 12 16:28:33 2011 +0200 @@ -0,0 +1,171 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +""" +wix: a SAT frontend +Copyright (C) 2009, 2010, 2011 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 pdb +from logging import debug, info, error +from sat.tools.jid import JID +from time import time +from math import sin, cos, pi + +CARD_WIDTH = 74 +CARD_HEIGHT = 136 +WIDTH = 800 +HEIGHT = 600 + +class GraphicElement(): + """This class is used to represent a card, graphically and logically""" + + def __init__(self, file, x=0, y=0, zindex=10, transparent=True): + """ Image used to build the game visual + @param file: path of the PNG file + @param zindex: layer of the element (0=background; the bigger, the more in the foreground)""" + self.bitmap = wx.Image(file).ConvertToBitmap() + self.x = x + self.y = y + self.zindex = zindex + self.transparent = transparent + + def __cmp__(self, other): + return self.zindex.__cmp__(other.zindex) + + def draw(self, dc, x=None, y=None): + """Draw the card on the device context + @param dc: device context + @param x: abscissa + @param y: ordinate""" + dc.DrawBitmap(self.bitmap, x or self.x, y or self.y, self.transparent) + +class BaseWindow(wx.Window): + """This is the panel where the game is drawed, under the other widgets""" + + def __init__(self, parent): + wx.Window.__init__(self, parent, pos=(0,0), size=(WIDTH, HEIGHT)) + self.parent = parent + self.SetMinSize(wx.Size(WIDTH, HEIGHT)) + self.Bind(wx.EVT_PAINT, self.onPaint) + self.graphic_elts = {} + self.loadImages("images/quiz/") + + def loadImages(self, dir): + """Load all the images needed for the game + @param dir: directory where the PNG files are""" + for name, sub_dir, filename, x, y, zindex, transparent in [("background", "backgrounds", "blue_background.png", 0, 0, 0, False), + ("joueur0", "characters/zombie", "zombie.png", 24, 170, 5, True), + ("joueur1", "characters/nerd", "nerd2.png", 209, 170, 5, True), + ("joueur2", "characters/zombie", "zombie.png", 392, 170, 5, True), + ("joueur3", "characters/zombie", "zombie.png", 578, 170, 5, True), + ("foreground", "foreground", "foreground.png", 0, 0, 10, True)]: + self.graphic_elts[name] = GraphicElement(os.path.join(dir, sub_dir, filename), x = x, y = y, zindex=zindex, transparent=transparent) + + def fullPaint(self, device_context): + """Paint all the game on the given dc + @param device_context: wx.DC""" + elements = self.graphic_elts.values() + elements.sort() + for elem in elements: + elem.draw(device_context) + #device_context.DrawArc(39,127, 39, 127, + + _font = wx.Font(65, wx.FONTFAMILY_TELETYPE, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL) + device_context.SetFont(_font) + device_context.SetTextForeground(wx.BLACK) + + x = 100 + for score in [0,1,4,9]: + device_context.DrawText("%d" % score, x, 355) + x+=184 + + + if self.parent.time_origin: + device_context.SetPen(wx.BLACK_PEN) + radius = 20 + center_x = 760 + center_y = 147 + origin = self.parent.time_origin + current = time() + limit = self.parent.time_limit + print "limit:", limit + total = limit - origin + left = self.parent.time_left = max(0,limit - current) + device_context.SetBrush(wx.RED_BRUSH if left/total < 1/4.0 else wx.WHITE_BRUSH) + print "left:",left + if left: + #we now draw the timer + print "total - left:", total - left + angle = ((-2*pi)*((total-left)/total) + (pi/2)) + print "angle:", angle*57.3 + x = center_x + radius * cos(angle) + y = center_y - radius * sin(angle) + print "x: %s, y:%s" % (x, y) + device_context.DrawArc(center_x, center_y-radius, x, y, center_x, center_y) + + + + def onPaint(self, event): + dc = wx.PaintDC(self) + self.fullPaint(dc) + + + +class QuizPanel(wx.Panel): + """This class is used to display the quiz game""" + + def __init__(self, parent, referee, players, player_nick): + wx.Panel.__init__(self, parent) + self.time_origin = None #set to unix time when the timer start + self.time_limit = None + self.time_left = None + self.parent = parent + self.SetMinSize(wx.Size(WIDTH, HEIGHT)) + self.SetSize(wx.Size(WIDTH, HEIGHT)) + self.base = BaseWindow(self) + self.question = wx.TextCtrl(self.base, -1, "", pos=(168,17), size=(613, 94), style=wx.TE_MULTILINE | wx.TE_READONLY) + self.reponse = wx.TextCtrl(self.base, -1, pos=(410,569), size=(342, 21), style=wx.TE_PROCESS_ENTER) + self.parent.host.bridge.quizGameReady(player_nick, referee, profile_key = self.parent.host.profile) + self.state = None + + def startTimer(self, timer=60): + """Start the timer to answer the question""" + def _refresh(): + self.Refresh() + if self.time_left: + wx.CallLater(1000, _refresh) + self.time_left = timer + self.time_origin = time() + self.time_limit = self.time_origin + timer + _refresh() + + def quizGameNew(self, data): + """Start a new game, with given hand""" + if data.has_key('instructions'): + self.question.ChangeValue(data['instructions']) + self.Refresh() + + def quizGameQuestion(self, question_id, question, timer): + """Called when a new question is available + @param question: question to ask""" + self.question.ChangeValue(question) + self.startTimer(timer) +