# -*- coding: utf-8 -*- import pygame import random import api.Image as Image from api.Game import CGame from api.GameState import CGameState from api.Button import CButton from api.Sprite2 import CSprite from api.Points import CPoints from api.Globe import Globe import HallState from gettext import gettext as _ from assets.data import area1game3_data OVER_COLOR = (255, 0, 0) NORMAL_COLOR = (255, 125, 50) GOODS = 8 BADS = 4 class CArea1Game3(CGameState): def __init__(self): CGameState.__init__(self) self.mBackground = None self.mButtonBack = None self.mLabelQuestion = None self.mButtonTrue = None self.mButtonFalse = None self.mBoy = None self.mGirl = None self.mDialogBoy1 = None self.mDialogGirl1 = None self.mDialogBoy2 = None self.mDialogGood = None self.mDialogBad = None self.mActualQuestionN = None self.mTupleQuestion = None self.mEndDialog = None self.mQuestionAnswered = False self.mQuestionShowed = False self.mQuestionCorrect = False self.mActiveDialog = None self.mAlphaSet = True self.mHelpShowed = True self.mboy_list = [] self.mgirl_list = [] self.mCurrentState = 0 self.time = 0 self.mBad = 0 self.mGood = 0 self.mBackground = Image.loadImage('assets/images/a1g3/a1game3.png', False) CGame().setBackground(self.mBackground) # get a list of the keys of dict BOY and GIRL self.mboy_list = area1game3_data.BOY.keys() self.mBoyDict = {} for i in self.mboy_list: t = area1game3_data.BOY[i] q = (unicode(t[0], 'UTF-8'), t[1]) self.mBoyDict[i] = q self.mgirl_list = area1game3_data.GIRL.keys() self.mGirlDict = {} for i in self.mgirl_list: t = area1game3_data.GIRL[i] q = (unicode(t[0], 'UTF-8'), t[1]) self.mGirlDict[i] = q self.mCharDialog = [] for e in area1game3_data.DIALOG: self.mCharDialog.append(unicode(e, 'UTF-8')) self.mInitB = unicode(area1game3_data.INITB, 'UTF-8') self.mInitG = unicode(area1game3_data.INITG, 'UTF-8') self.mGoodA = unicode(area1game3_data.GOOD, 'UTF-8') self.mBadA = unicode(area1game3_data.BAD, 'UTF-8') # randomize it random.shuffle(self.mboy_list) random.shuffle(self.mgirl_list) self.mBBsetted = False self.mNBsetted = True self.mBHsetted = False self.mNHsetted = True self.mBackImageN = Image.loadImage('assets/images/back.png') self.mBackImageB = Image.loadImage('assets/images/back_big.png') self.mButtonBack = CSprite(1170, 30) self.mButtonBack.setRegistrationPointOffset(22, 22) self.mButtonBack.setImage(self.mBackImageN) CGame().addChild(self.mButtonBack) # help button self.mHelpImageN = Image.loadImage('assets/images/help.png') self.mHelpImageB = Image.loadImage('assets/images/help_big.png') self.mButtonHelp = CSprite(1110, 30) self.mButtonHelp.setRegistrationPointOffset(22, 22) self.mButtonHelp.setImage(self.mHelpImageN) CGame().addChild(self.mButtonHelp) self.mLabelQuestion = Globe((500, 250), pico='None', text=' ') self.mLabelQuestion.set_center((600, 250)) font28 = pygame.font.Font('assets/fonts/DejaVuSans.ttf', 28) self.mButtonTrue = CButton() #self.mButtonTrue.bgColor = (125, 255, 125) self.mButtonTrue.bgColor = NORMAL_COLOR self.mButtonTrue.font = font28 self.mButtonTrue.center = (460, 400) self.mButtonTrue.size = (200, 40) self.mButtonTrue.set_text(_('Verdadero')) self.mButtonFalse = CButton() #self.mButtonFalse.bgColor = (125, 255, 125) self.mButtonFalse.bgColor = NORMAL_COLOR self.mButtonFalse.font = font28 self.mButtonFalse.center = (740, 400) self.mButtonFalse.size = (200, 40) self.mButtonFalse.set_text(_('Falso')) self.mButtonNext = CButton() #self.mButtonNext.bgColor = (125, 255, 125) self.mButtonNext.bgColor = NORMAL_COLOR self.mButtonNext.font = font28 self.mButtonNext.center = (600, 500) self.mButtonNext.size = (200, 40) self.mButtonNext.set_text(_('Siguiente')) self.mBoy = CSprite(100, 400) self.mBoy.loadImage('assets/images/a1g3/Juan-chico-costado-parado.png') CGame().addChild(self.mBoy) elisa = Image.loadImage('assets/images/a1g3/Elisa-chica-costado-parada.png') elisa_rotated = pygame.transform.flip(elisa, True, False) self.mGirl = CSprite(900, 400) self.mGirl.setImage(elisa_rotated) CGame().addChild(self.mGirl) # sabes que le pasa self.mDialogBoy1 = Globe((300, 170), text=self.mCharDialog[0]) self.mDialogBoy1.set_center((300, 300)) # mmm.. no... self.mDialogGirl1 = Globe((200, 100), pico='right', text=self.mCharDialog[1]) self.mDialogGirl1.set_center((900, 300)) # a ver.. self.mDialogBoy2 = Globe((200, 100), pico='left', text=self.mCharDialog[2]) self.mDialogBoy2.set_center((300, 300)) self.mDialogGood = Globe((200, 75), pico='left', text=self.mGoodA) self.mDialogGood.set_center((300, 300)) self.mDialogBad = Globe((200, 75), pico='left', text=self.mBadA) self.mDialogBad.set_center((300, 300)) self.mPoints = CPoints(20, 20) CGame().addChild(self.mPoints) alphasurface = pygame.Surface((1200,900)) alphasurface.convert() alphasurface.fill((100,100,100)) alphasurface.set_alpha(200) self.mAlpha = CSprite(0, 0) self.mAlpha.setImage(alphasurface) CGame().addChild(self.mAlpha) self.mButtonAccept = CButton() self.mButtonAccept.bgColor = NORMAL_COLOR self.mButtonAccept.font = font28 self.mButtonAccept.center = (600, 400) self.mButtonAccept.size = (200, 40) self.mButtonAccept.set_text(_('Aceptar')) CGame().addChild(self.mButtonAccept) msg = area1game3_data.HELP self.mInstructions = Globe((450, 150), text=unicode(msg, 'UTF-8'), pico=None) self.mInstructions.set_center((600, 300)) CGame().addChild(self.mInstructions) self.mCurrentState = 0 self.time = 0 def update(self): CGameState.update(self) if not(self.mAlphaSet): if self.mButtonBack.mouseOver(): if not(self.mBBsetted): self.mBBsetted = True self.mNBsetted = False self.mButtonBack.setRegistrationPointOffset(27, 27) self.mButtonBack.setImage(self.mBackImageB) else: if not(self.mNBsetted): self.mBBsetted = False self.mNBSetted = True self.mButtonBack.setRegistrationPointOffset(22, 22) self.mButtonBack.setImage(self.mBackImageN) if self.mButtonHelp.mouseOver(): if not(self.mBHsetted): self.mBHsetted = True self.mNHsetted = False self.mButtonHelp.setRegistrationPointOffset(27, 27) self.mButtonHelp.setImage(self.mHelpImageB) else: if not(self.mNHsetted): self.mBHsetted = False self.mNHSetted = True self.mButtonHelp.setRegistrationPointOffset(22, 22) self.mButtonHelp.setImage(self.mHelpImageN) if self.mButtonBack.clicked(): #print "clicked back of a1g3" hs = HallState.CHallState() CGame().setState(hs) hs.setCurrentGame(2) return if self.mButtonHelp.clicked(): self.mAlphaSet = True self.mHelpShowed = True CGame().addChild(self.mAlpha) CGame().addChild(self.mInstructions) CGame().addChild(self.mButtonAccept) return if self.mCurrentState == 0: CGame().addChild(self.mDialogBoy1) CGame().addChild(self.mButtonNext) self.mCurrentState = 1 if self.mCurrentState < 5: if self.mButtonNext.mouseOver(): self.mButtonNext.set_bgColor(OVER_COLOR) else: self.mButtonNext.set_bgColor(NORMAL_COLOR) if self.mButtonNext.clicked(): self.mCurrentState = self.mCurrentState + 1 if self.mCurrentState == 2: CGame().removeChild(self.mDialogBoy1) CGame().addChild(self.mDialogGirl1) elif self.mCurrentState == 3: CGame().removeChild(self.mDialogGirl1) CGame().addChild(self.mDialogBoy2) elif self.mCurrentState == 4: CGame().removeChild(self.mDialogBoy2) self.mCurrentState = 5 CGame().removeChild(self.mButtonNext) # Pregunta elif self.mCurrentState == 5: self.time = self.time + 1 if self.mQuestionShowed == False: if self.next_questionB(): CGame().addChild(self.mLabelQuestion) CGame().addChild(self.mButtonTrue) CGame().addChild(self.mButtonFalse) self.mQuestionShowed = True else: self.mCurrentState = 6 elif (self.mQuestionAnswered == False): if self.mButtonTrue.mouseOver(): self.mButtonTrue.set_bgColor(OVER_COLOR) else: self.mButtonTrue.set_bgColor(NORMAL_COLOR) if self.mButtonFalse.mouseOver(): self.mButtonFalse.set_bgColor(OVER_COLOR) else: self.mButtonFalse.set_bgColor(NORMAL_COLOR) if self.mButtonTrue.clicked(): CGame().removeChild(self.mLabelQuestion) CGame().removeChild(self.mButtonTrue) CGame().removeChild(self.mButtonFalse) self.mButtonTrue.set_bgColor(NORMAL_COLOR) if self.mTupleQuestion[1] == True: CGame().addChild(self.mDialogGood) self.mGood = self.mGood + 1 self.mPoints.set_goods(self.mGood) else: CGame().addChild(self.mDialogBad) self.mBad = self.mBad + 1 self.mPoints.set_bads(self.mBad) self.mQuestionAnswered = True self.time = 0 elif self.mButtonFalse.clicked(): CGame().removeChild(self.mLabelQuestion) CGame().removeChild(self.mButtonTrue) CGame().removeChild(self.mButtonFalse) self.mButtonFalse.set_bgColor(NORMAL_COLOR) if self.mTupleQuestion[1] == False: CGame().addChild(self.mDialogGood) self.mGood = self.mGood + 1 self.mPoints.set_goods(self.mGood) else: CGame().addChild(self.mDialogBad) self.mBad = self.mBad + 1 self.mPoints.set_bads(self.mBad) self.mQuestionAnswered = True self.time = 0 elif self.mQuestionAnswered == True: if self.time > 2*30: CGame().removeChild(self.mDialogGood) CGame().removeChild(self.mDialogBad) self.mQuestionShowed = False self.mQuestionAnswered = False self.mActiveDialog = None if (self.mGood == GOODS) or (self.mBad > BADS): self.mCurrentState = 6 if (self.mGood == GOODS): CGame().setEndGameState(1, 2, 1) elif self.mCurrentState == 6: self.mAlphaSet = True CGame().addChild(self.mAlpha) msg = _('Has terminado con %s respuestas bien') % self.mGood self.mEndDialog = Globe((450, 100), text=unicode(msg, 'UTF-8'), pico=None) self.mEndDialog.set_center((600, 300)) CGame().addChild(self.mEndDialog) CGame().addChild(self.mButtonAccept) else: if self.mButtonAccept.mouseOver(): self.mButtonAccept.set_bgColor(OVER_COLOR) else: self.mButtonAccept.set_bgColor(NORMAL_COLOR) if self.mButtonAccept.clicked(): if self.mHelpShowed: self.mAlphaSet = False self.mHelpShowed = False self.mButtonAccept.set_bgColor(NORMAL_COLOR) CGame().removeChild(self.mAlpha) CGame().removeChild(self.mInstructions) CGame().removeChild(self.mButtonAccept) else: hs = HallState.CHallState() CGame().setState(hs) hs.setCurrentGame(2) return def next_questionB(self): if not(self.mboy_list == []): self.mActualQuestionN = self.mboy_list.pop() self.mTupleQuestion = self.mBoyDict[self.mActualQuestionN] text = self.mInitB + '\n' + self.mTupleQuestion[0] self.mLabelQuestion.set_text(text) return True else: return False def next_questionG(self): if not(self.mgirl_list == []): self.mActualQuestionN = self.mgirl_list.pop() self.mTupleQuestion = self.mGirlDict[self.mActualQuestionN] text = self.mInitG + '\n' + self.mTupleQuestion[0] self.mLabelQuestion.setText(text) return True else: return False def destroy(self): CGameState.destroy(self) CGame().removeChild(self.mEndDialog) CGame().removeChild(self.mButtonBack) CGame().removeChild(self.mButtonHelp) CGame().removeChild(self.mLabelQuestion) CGame().removeChild(self.mButtonTrue) CGame().removeChild(self.mButtonFalse) CGame().removeChild(self.mButtonNext) CGame().removeChild(self.mBoy) CGame().removeChild(self.mGirl) CGame().removeChild(self.mDialogBoy1) CGame().removeChild(self.mDialogGirl1) CGame().removeChild(self.mDialogBoy2) CGame().removeChild(self.mDialogGood) CGame().removeChild(self.mDialogBad) CGame().removeChild(self.mPoints) CGame().removeChild(self.mAlpha) CGame().removeChild(self.mInstructions) CGame().removeChild(self.mButtonAccept) self.mButtonBack = None self.mButtonHelp = None self.mLabelQuestion = None self.mButtonTrue = None self.mButtonFalse = None self.mBoy = None self.mGirl = None self.mDialogBoy1 = None self.mDialogGirl1 = None self.mDialogBoy2 = None self.mDialogGood = None self.mDialogBad = None self.mPoints = None self.mAlpha = None self.mInstructions = None self.mButtonAccept = None self.mDialog = None