"Cheat code handler" import math, os import pygame from pygame.locals import * import game, gfx, snd from basegamehandler import BaseGameHandler # text-related constants from text import * from sprites.objsstext import SubsurfaceText CHEAT_PROMPT = "Enter cheat code:" CHEAT_PROMPT_RECT = pygame.Rect((250,200,0,0)) CHEAT_CODE_RECT = pygame.Rect((250,250,0,0)) MESSAGE_RECT = pygame.Rect((250,300,0,0)) CHEAT_PROMPT_COLOR = WHITE MESSAGE_COLOR = WHITE CODE_COLOR = BLUE_GREY TRANSITION_TIME = 2000 # ticks CHEAT_CODES = {"shields":("shields",1), "morelives":("infinite_lives",1), "morefuel":("infinite_fuel",1), "morebombs":("infinite_bombs",1) } def load_game_resources(): snd.preload('select_choose') class GameCheat(BaseGameHandler): def __init__(self, prevhandler): BaseGameHandler.__init__(self) self.prevhandler = prevhandler self.done = 0 self.top = 60 self.center = gfx.rect.centerx self.text = [] self.drawnprompt = 0 self.code = '' self.message = None self.cheat_codes = CHEAT_CODES self.cheat_prompt = CHEAT_PROMPT # find longest code self.longest_code = 0 for key in self.cheat_codes.keys(): code_len = len(key) if code_len > self.longest_code: self.longest_code = code_len #print key,self.cheat_codes[key],self.longest_code #print "longest cheat code: %d" % self.longest_code self.cheatprompt_message = SubsurfaceText(gfx.surface,gfx.background,self.cheat_prompt, CHEAT_PROMPT_RECT, fontpath=BABELFISH_FONTPATH,fontsize=FONTSIZE_SMALL, fontcolor=CHEAT_PROMPT_COLOR) self.code_msg = None self.last_code_msg = self.code_msg def quit(self): gfx.update() pygame.time.delay(TRANSITION_TIME) # empty the event queue pygame.event.get() game.handler = self.prevhandler self.done = 1 snd.play('select_choose') def event(self, e): pass def get_events(self): return pygame.event.get() def run(self): if self.drawnprompt == 0: gfx.clearscreen() self.cheatprompt_message.draw() self.drawnprompt = 1 return def draw_code(self): if self.last_code_msg != None: self.last_code_msg.erase() self.last_code_msg = self.code_msg # SubsurfaceText will fail if text string is 0 length if self.code != '': self.code_msg = SubsurfaceText(gfx.surface,gfx.background,self.code,CHEAT_CODE_RECT, fontpath=BABELFISH_FONTPATH,fontsize=FONTSIZE_SMALL, fontcolor=CODE_COLOR) self.code_msg.draw() def draw_message(self,message): if self.message != None: self.message.erase() self.message = SubsurfaceText(gfx.surface,gfx.background,message,MESSAGE_RECT, fontpath=BABELFISH_FONTPATH,fontsize=FONTSIZE_SMALL, fontcolor=MESSAGE_COLOR) self.message.draw() # unlike other handlers, we get raw events (the default input translate behavior) def input(self,event): if event.type == KEYDOWN: key_pressed = event.key self.code += pygame.key.name(event.key) self.draw_code() if len(self.code) > self.longest_code or self.code == K_RETURN: self.draw_message("Sorry.") self.quit() return for key in self.cheat_codes.keys(): if self.code == key: var, value = self.cheat_codes[key] setattr(game,var,value) m = "%s = %s" % (var,value) self.draw_message(m) self.quit() return