comparison 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
comparison
equal deleted inserted replaced
360:6b5626c37909 361:141eeb7cd9e6
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 """
5 wix: a SAT frontend
6 Copyright (C) 2009, 2010, 2011 Jérôme Poisson (goffi@goffi.org)
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """
21
22
23
24 import wx
25 import os.path, glob
26 import pdb
27 from logging import debug, info, error
28 from sat.tools.jid import JID
29 from time import time
30 from math import sin, cos, pi
31
32 CARD_WIDTH = 74
33 CARD_HEIGHT = 136
34 WIDTH = 800
35 HEIGHT = 600
36
37 class GraphicElement():
38 """This class is used to represent a card, graphically and logically"""
39
40 def __init__(self, file, x=0, y=0, zindex=10, transparent=True):
41 """ Image used to build the game visual
42 @param file: path of the PNG file
43 @param zindex: layer of the element (0=background; the bigger, the more in the foreground)"""
44 self.bitmap = wx.Image(file).ConvertToBitmap()
45 self.x = x
46 self.y = y
47 self.zindex = zindex
48 self.transparent = transparent
49
50 def __cmp__(self, other):
51 return self.zindex.__cmp__(other.zindex)
52
53 def draw(self, dc, x=None, y=None):
54 """Draw the card on the device context
55 @param dc: device context
56 @param x: abscissa
57 @param y: ordinate"""
58 dc.DrawBitmap(self.bitmap, x or self.x, y or self.y, self.transparent)
59
60 class BaseWindow(wx.Window):
61 """This is the panel where the game is drawed, under the other widgets"""
62
63 def __init__(self, parent):
64 wx.Window.__init__(self, parent, pos=(0,0), size=(WIDTH, HEIGHT))
65 self.parent = parent
66 self.SetMinSize(wx.Size(WIDTH, HEIGHT))
67 self.Bind(wx.EVT_PAINT, self.onPaint)
68 self.graphic_elts = {}
69 self.loadImages("images/quiz/")
70
71 def loadImages(self, dir):
72 """Load all the images needed for the game
73 @param dir: directory where the PNG files are"""
74 for name, sub_dir, filename, x, y, zindex, transparent in [("background", "backgrounds", "blue_background.png", 0, 0, 0, False),
75 ("joueur0", "characters/zombie", "zombie.png", 24, 170, 5, True),
76 ("joueur1", "characters/nerd", "nerd2.png", 209, 170, 5, True),
77 ("joueur2", "characters/zombie", "zombie.png", 392, 170, 5, True),
78 ("joueur3", "characters/zombie", "zombie.png", 578, 170, 5, True),
79 ("foreground", "foreground", "foreground.png", 0, 0, 10, True)]:
80 self.graphic_elts[name] = GraphicElement(os.path.join(dir, sub_dir, filename), x = x, y = y, zindex=zindex, transparent=transparent)
81
82 def fullPaint(self, device_context):
83 """Paint all the game on the given dc
84 @param device_context: wx.DC"""
85 elements = self.graphic_elts.values()
86 elements.sort()
87 for elem in elements:
88 elem.draw(device_context)
89 #device_context.DrawArc(39,127, 39, 127,
90
91 _font = wx.Font(65, wx.FONTFAMILY_TELETYPE, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL)
92 device_context.SetFont(_font)
93 device_context.SetTextForeground(wx.BLACK)
94
95 x = 100
96 for score in [0,1,4,9]:
97 device_context.DrawText("%d" % score, x, 355)
98 x+=184
99
100
101 if self.parent.time_origin:
102 device_context.SetPen(wx.BLACK_PEN)
103 radius = 20
104 center_x = 760
105 center_y = 147
106 origin = self.parent.time_origin
107 current = time()
108 limit = self.parent.time_limit
109 print "limit:", limit
110 total = limit - origin
111 left = self.parent.time_left = max(0,limit - current)
112 device_context.SetBrush(wx.RED_BRUSH if left/total < 1/4.0 else wx.WHITE_BRUSH)
113 print "left:",left
114 if left:
115 #we now draw the timer
116 print "total - left:", total - left
117 angle = ((-2*pi)*((total-left)/total) + (pi/2))
118 print "angle:", angle*57.3
119 x = center_x + radius * cos(angle)
120 y = center_y - radius * sin(angle)
121 print "x: %s, y:%s" % (x, y)
122 device_context.DrawArc(center_x, center_y-radius, x, y, center_x, center_y)
123
124
125
126 def onPaint(self, event):
127 dc = wx.PaintDC(self)
128 self.fullPaint(dc)
129
130
131
132 class QuizPanel(wx.Panel):
133 """This class is used to display the quiz game"""
134
135 def __init__(self, parent, referee, players, player_nick):
136 wx.Panel.__init__(self, parent)
137 self.time_origin = None #set to unix time when the timer start
138 self.time_limit = None
139 self.time_left = None
140 self.parent = parent
141 self.SetMinSize(wx.Size(WIDTH, HEIGHT))
142 self.SetSize(wx.Size(WIDTH, HEIGHT))
143 self.base = BaseWindow(self)
144 self.question = wx.TextCtrl(self.base, -1, "", pos=(168,17), size=(613, 94), style=wx.TE_MULTILINE | wx.TE_READONLY)
145 self.reponse = wx.TextCtrl(self.base, -1, pos=(410,569), size=(342, 21), style=wx.TE_PROCESS_ENTER)
146 self.parent.host.bridge.quizGameReady(player_nick, referee, profile_key = self.parent.host.profile)
147 self.state = None
148
149 def startTimer(self, timer=60):
150 """Start the timer to answer the question"""
151 def _refresh():
152 self.Refresh()
153 if self.time_left:
154 wx.CallLater(1000, _refresh)
155 self.time_left = timer
156 self.time_origin = time()
157 self.time_limit = self.time_origin + timer
158 _refresh()
159
160 def quizGameNew(self, data):
161 """Start a new game, with given hand"""
162 if data.has_key('instructions'):
163 self.question.ChangeValue(data['instructions'])
164 self.Refresh()
165
166 def quizGameQuestion(self, question_id, question, timer):
167 """Called when a new question is available
168 @param question: question to ask"""
169 self.question.ChangeValue(question)
170 self.startTimer(timer)
171