# -*- coding: utf-8 -*- import pygame import random import MenuState import api.Image as Image from api.Game import CGame from api.GameState import CGameState from api.Button import CButton from api.Globe import Globe from api.Sprite2 import CSprite from api.MultiLabel import CMultiLabel from api.Points import CPoints from gettext import gettext as _ from assets.data import area1game4_data STATE_ANSWER = 1 STATE_ANIM = 2 TIME_ANIM = 60 class CArea1Game4(CGameState): def __init__(self): CGameState.__init__(self) self.mLocalState = STATE_ANSWER self.mAnimCounter = 0 self.mBackground = None self.mButtonBackA1G4 = None self.mCurrentQuestion = 0 self.mQuestions = [] self.mButtonList = [] self.mBad = 0 self.mGood = 0 self.mPoints = CPoints() self.mPoints.setXY(250, 10) CGame().addChild(self.mPoints) for i in area1game4_data.ITEMS.keys(): t = area1game4_data.ITEMS[i] q = (unicode(t[0], 'UTF-8'), unicode(t[1], 'UTF-8')) self.mQuestions.append(q) button = CButton() button.set_bgColor((255, 125, 0)) button.font = pygame.font.Font('assets/fonts/DejaVuSans.ttf', 26) #button.set_center((110, 650)) button.set_size((230, 40)) button.set_text(q[1]) self.mButtonList.append(button) CGame().addChild(button) self.mBackground = Image.loadImage('assets/images/a1g4/a1game4.png', False) CGame().setBackground(self.mBackground) self.mBook = CSprite() self.mBook.setXY(316, 100) self.mBook.loadImage('assets/images/a1g4/A1G4-libro.png') CGame().addChild(self.mBook) self.mGoodSprite = CSprite() self.mGoodSprite.setXY(300, 100) self.mGoodSprite.loadImage('assets/images/a1g4/good.png') self.mBadSprite = CSprite() self.mBadSprite.setXY(300, 100) self.mBadSprite.loadImage('assets/images/a1g4/bad.png') self.mButtonBackA1G4 = CButton() self.mButtonBackA1G4.set_bgColor((0x99, 0x99, 0x66)) self.mButtonBackA1G4.font = pygame.font.Font('assets/fonts/DejaVuSans.ttf', 20) self.mButtonBackA1G4.set_center((120, 40)) self.mButtonBackA1G4.set_size((200, 40)) self.mButtonBackA1G4.set_text(_('Volver')) CGame().addChild(self.mButtonBackA1G4) self.mButtonList[0].set_center((350, 550)) self.mButtonList[1].set_center((600, 550)) self.mButtonList[2].set_center((850, 550)) self.mButtonList[3].set_center((350, 600)) self.mButtonList[4].set_center((600, 600)) self.mButtonList[5].set_center((850, 600)) self.mQuestionsL = range(6) random.shuffle(self.mQuestionsL) self.mLabelQuestion = CMultiLabel() self.mLabelQuestion.bgColor = (252, 175, 23) self.mLabelQuestion.set_font(fontSize=28) self.mLabelQuestion.set_center((600, 250)) self.mLabelQuestion.set_size((380, 150)) self.mLabelQuestion.set_text(self.mQuestions[self.mQuestionsL[self.mCurrentQuestion]][0]) CGame().addChild(self.mLabelQuestion) t = unicode(_('Ăšltima pregunta'), 'UTF-8') self.mLabelEnd = Globe((400, 100), pico='None', text=t) self.mLabelEnd.set_center((600, 400)) def update(self): CGameState.update(self) if self.mButtonBackA1G4.clicked(): ms = MenuState.CMenuState() CGame().setState(ms) return if self.mLocalState == STATE_ANSWER: answer = -1 for i in range(6): b = self.mButtonList[i] if b.clicked(): answer = i break if not(answer == -1): n = -1 if self.mCurrentQuestion < 6: n = self.mQuestionsL[self.mCurrentQuestion] if (answer == n): b = self.mButtonList[answer] b.mclicked = False CGame().removeChild(b) self.mGood = self.mGood + 1 self.mPoints.set_goods(self.mGood) CGame().addChild(self.mGoodSprite) else: self.mBad = self.mBad + 1 self.mPoints.set_bads(self.mBad) CGame().addChild(self.mBadSprite) self.mLocalState = STATE_ANIM else: print 'fin preguntas' else: self.mAnimCounter = self.mAnimCounter + 1 if self.mAnimCounter > TIME_ANIM: self.mAnimCounter = 0 CGame().removeChild(self.mGoodSprite) CGame().removeChild(self.mBadSprite) self.mLocalState = STATE_ANSWER self.mCurrentQuestion = self.mCurrentQuestion + 1 #print 'estado de preguntas', self.mCurrentQuestion if not(self.mCurrentQuestion == 6): self.mLabelQuestion.set_text(self.mQuestions[self.mQuestionsL[self.mCurrentQuestion]][0]) else: CGame().addChild(self.mLabelEnd) CGame().removeChild(self.mBook) CGame().removeChild(self.mLabelQuestion) print 'ultima' def destroy(self): CGameState.destroy(self) CGame().removeChild(self.mButtonBackA1G4) CGame().removeChild(self.mLabelQuestion) CGame().removeChild(self.mPoints) CGame().removeChild(self.mBook) CGame().removeChild(self.mGoodSprite) CGame().removeChild(self.mBadSprite) CGame().removeChild(self.mLabelEnd) self.mButtonBackA1G4 = None self.mBackground = None self.mLabelDefinition = None self.mBook = None for i in range(6): b = self.mButtonList[i] CGame().removeChild(b) b = None print "CArea1Game4 destroy"