def __init__(self,master,**kwarg): self.master = master self.width = 50 self.height = 20 self.borderwidth = 1 self.bordercolor = Color("Grey") self.framefont = "Arial" self.frameborderwidth = 2 self.framefontsize = 12 self.framebold = False self.frameitatic = False self.framealias = True self.colour = Color("White") self.image = None self.value = 0 self.step = 1 self.isRendering = False self._priority = 3 for key in kwarg.keys(): if key == "width": self.width = kwarg[key] elif key == "height": self.height = kwarg[key] elif key == "colour": self.colour = kwarg[key] elif key == "image": self.image = kwarg[key] elif key == "colour": self.colour = kwarg[key] elif key == "borderwidth": self.borderwidth = kwarg[key] elif key == "bordercolor": self.bordercolor = kwarg[key] elif key == "framefont": self.framefont = kwarg[key] elif key == "framefontsize": self.framefontsize = kwarg[key] elif key == "framebold": self.framebold = kwarg[key] elif key == "frameitatic": self.frameitatic = kwarg[key] elif key == "framealias": self.framealias = kwarg[key] elif key == "value": self.value = kwarg[key] elif key == "step": self.step = kwarg[key] else: raise ValueError("Invalid Arguement") self._frame = StaticFrame(self.master,width=self.width+self.borderwidth*4,height=self.height+self.borderwidth*4,\ borderwidth=0,bordercolor=self.bordercolor,\ header=0) self._entry = Entry(self._frame,width=self.width//2,height=self.height,\ font=self.framefont,fontsize=self.framefontsize,bold=self.framebold,\ italic=self.frameitatic,antialias=self.framealias,borderwidth=self.frameborderwidth) self._up = Button(self._frame,width=self.width//2,height=self.height//2-self.borderwidth*2,target=self.stepUp,\ font=self.framefont,fontsize=self.framefontsize//2,bold=self.framebold,text="+",\ italic=self.frameitatic,antialias=self.framealias,borderwidth=self.frameborderwidth) self._down = Button(self._frame,width=self.width//2,height=self.height//2-self.borderwidth*2,target=self.stepDown,\ font=self.framefont,fontsize=self.framefontsize//2,bold=self.framebold,text="-",\ italic=self.frameitatic,antialias=self.framealias,borderwidth=self.frameborderwidth) self._entry.place((0,0)) self._entry.set(str(self.value)) self._up.place((self.width//2,0)) self._down.place((self.width//2,self._entry.height//2+self.borderwidth*2)) _widgets.append(self)
def createMessageBox(screen,message): def close(): nonlocal tmpFrame tmpFrame.kill() tmpFrame = Frame(screen,htitle="Message",width=300,height=100) t = TypableSurface((190,90),text=message) tmpButton = Button(tmpFrame,width=50,height=20,text="Close",target=close) tmpFrame.blit(t,(10,10)) tmpButton.place((122,74)) tmpFrame.place((screen.get_width()//2-150,screen.get_height()//2-50)) return tmpFrame
def createInputBox(screen,message): def close(): nonlocal tmpFrame,tmpEntry nonlocal var var.set(tmpEntry.get()) var.isClosed = True tmpFrame.kill() tmpFrame = Frame(screen,htitle="Message",width=300,height=100) t = TypableSurface((190,90),text=message) tmpEntry = Entry(tmpFrame,width=234,height=20) tmpButton = Button(tmpFrame,width=50,height=18,text="Enter",target=close) var = MessageVariable() tmpFrame.blit(t,(10,10)) tmpEntry.place((2,72)) tmpButton.place((242,72)) tmpFrame.place((screen.get_width()//2-150,screen.get_height()//2-50)) return tmpFrame,var
def createWarning(screen,message): def close(): nonlocal tmpFrame tmpFrame.kill() # Creation tmpFont = font.SysFont("Arial",34) tmpText = tmpFont.render("!",1,(0,0,0)) tmpFrame = Frame(screen,htitle="Warning",width=300,height=100) t = TypableSurface((160,90),text=message) tmpSurface = Surface((42,36)) tmpButton = Button(tmpFrame,width=50,height=20,text="Ok",target=close) # Assembly tmpSurface.fill((255,255,255)) draw.polygon(tmpSurface,(255,255,0),[(20,0),(0,35),(40,35)]) draw.polygon(tmpSurface,(0,0,0),[(20,0),(0,35),(40,35)],3) tmpSurface.blit(tmpText,(16,0)) tmpFrame.blit(tmpSurface,(2,10)) tmpFrame.blit(t,(50,10)) tmpButton.place((122,74)) tmpFrame.place((screen.get_width()//2-150,screen.get_height()//2-50)) return tmpFrame
def Start_game(): #Initialize game's screen and its controlers global on_game, images, entry_score, can_execute, on_initial, input_from_user, Label_gen, Frame, change_button, home_button, execute_button, character_time, entryForInput, character, list_x1, list_x2, compile_button, exit_button #Load background music pygame.mixer.music.stop() pygame.mixer.music.load(const.musicpath + "Bet_On_It.wav") pygame.mixer.music.play(-1, 0.0) on_game = True on_initial = False #Loads images fo tips images.append( image.load(const.imagespath + "Ayuda1_Background.png").convert_alpha()) images.append( image.load(const.imagespath + "Ayuda2_Background.png").convert_alpha()) images.append( image.load(const.imagespath + "Ayuda3_Background.png").convert_alpha()) images.append( image.load(const.imagespath + "Ayuda4_Background.png").convert_alpha()) images.append( image.load(const.imagespath + "Ayuda5_Background.png").convert_alpha()) Frame = StaticFrame(Window, colour=const.Pblue, header=False, bordercolor=const.Pgreen, borderwidth=5, width=235, height=485) can_execute = False Frame.place((421, 0)) #For maze initialization if dificulty_level == 1: mymaze = maze(easy_maze[0], easy_maze[1]) else: mymaze = maze(hard_maze[0], hard_maze[1]) mymaze.generate_maze() character = Character(mymaze) character_time = 0 list_x1 = list_xil(WW, WH) list_x2 = fill_list_x2(list_x1) #Pygame directive pygame.display.flip() pygame.key.set_repeat(50, 55) #Side toolbar home_button = Button(Window, text="Inicio", width=50, height=20, bordercolor=const.Porange, colour=const.yellow, fontsize=12, target=Home) home_button.place((430, 10)) change_button = Button(Window, text=" Cambiar avatar ", width=80, height=20, bordercolor=const.Porange, colour=const.yellow, fontsize=11, target=Change_avatar) change_button.place((500, 10)) exit_button = Button(Window, text="Salir", width=50, height=20, bordercolor=const.white, colour=const.red, fontsize=12, target=Exit) exit_button.place((600, 10)) #Second set of widgets Label_gen = Label(Frame, width=220, height=290, htitle=" Programa ", htitlefont="Verdana", htitlesize=14, htitlecolor=Color(const.black[0], const.black[1], const.black[2]), colour=Color(const.Pgreen[0], const.Pgreen[1], const.Pgreen[2])) Label_gen.place((7, 30)) #Widget for line counter entry_line_no = Entry(Frame, width=12, height=270, textcolor=Color("blue"), bold=True) entry_line_no.place((12, 50)) line_no = "" for value in range(1, 50): line_no = line_no + str(value) + "\n\n" entry_line_no.set(line_no) #Visual of current code , non editable entryForInput = Entry(Frame, width=191, height=270) entryForInput.place((27, 50)) #Score entry_score = Entry(Frame, width=90, height=10) entry_score.place((120, 30)) #Errors output section Label_errors = Label(Frame, width=220, height=110, htitle=" Estado ", htitlefont="Verdana", htitlesize=14, htitlecolor=Color(const.black[0], const.black[1], const.black[2]), colour=Color(const.Pgreen[0], const.Pgreen[1], const.Pgreen[2])) Label_errors.place((7, 330)) global errors errors = Entry(Frame, text="No hay errores ", textcolor=Color("Red"), width=206, height=90, fontsize=11) errors.place((12, 350)) #Buttons compile_button = Button(Window, text="Paso 1", width=95, height=20, bordercolor=const.white, colour=const.Porange, fontsize=16, target=Compile_instruction) compile_button.place((435, 460)) execute_button = Button(Window, text="Paso 2", width=95, height=20, bordercolor=const.white, colour=const.green, fontsize=16, target=Execute_instruction) execute_button.place((545, 460))
Create_input() render_widgets() pygame.display.flip() #If the character is on the finish line if character.x == character.maze.w - 1 and character.y == character.maze.h - 1: Restart() elif (on_initial): ## Main page Window.fill(const.black) # Buttons # Levels section start_button = Button(Window, text="Principiante", width=95, height=30, bordercolor=const.Porange, colour=const.yellow, fontsize=18, target=Start_game) start_button.place((555, 25)) expert_button = Button(Window, text="Experto", width=95, height=30, bordercolor=const.white, colour=const.red, fontsize=18, target=Expert_mode) expert_button.place((555, 64)) ## Background image img_background.set_colorkey(RLEACCEL)
def Start_game():#Initialize game's screen and its controlers global on_game,images,entry_score,can_execute,on_initial,input_from_user,Label_gen,Frame,change_button,home_button,execute_button,character_time,entryForInput,character,list_x1,list_x2, compile_button,exit_button #Load background music pygame.mixer.music.stop() pygame.mixer.music.load(const.musicpath+"Bet_On_It.wav") pygame.mixer.music.play(-1,0.0) on_game = True on_initial = False #Loads images fo tips images.append(image.load(const.imagespath+"Ayuda1_Background.png").convert_alpha()) images.append(image.load(const.imagespath+"Ayuda2_Background.png").convert_alpha()) images.append(image.load(const.imagespath+"Ayuda3_Background.png").convert_alpha()) images.append(image.load(const.imagespath+"Ayuda4_Background.png").convert_alpha()) images.append(image.load(const.imagespath+"Ayuda5_Background.png").convert_alpha()) Frame = StaticFrame(Window, colour = const.Pblue, header = False, bordercolor = const.Pgreen, borderwidth = 5, width = 235, height = 485) can_execute = False Frame.place((421, 0)) #For maze initialization if dificulty_level == 1: mymaze = maze(easy_maze[0],easy_maze[1]) else: mymaze = maze(hard_maze[0],hard_maze[1]) mymaze.generate_maze() character = Character(mymaze) character_time = 0 list_x1 = list_xil(WW, WH) list_x2 = fill_list_x2(list_x1) #Pygame directive pygame.display.flip() pygame.key.set_repeat(50, 55) #Side toolbar home_button= Button(Window, text = "Inicio", width = 50, height = 20, bordercolor = const.Porange, colour = const.yellow, fontsize = 12, target = Home) home_button.place((430, 10)) change_button= Button(Window, text = " Cambiar avatar ", width = 80, height = 20, bordercolor = const.Porange, colour = const.yellow, fontsize = 11, target = Change_avatar) change_button.place((500, 10)) exit_button= Button(Window, text = "Salir", width = 50, height = 20, bordercolor = const.white, colour = const.red, fontsize = 12, target = Exit) exit_button.place((600, 10)) #Second set of widgets Label_gen = Label(Frame, width = 220, height = 290, htitle = " Programa ", htitlefont = "Verdana", htitlesize = 14, htitlecolor = Color(const.black[0], const.black[1], const.black[2]), colour = Color(const.Pgreen[0], const.Pgreen[1], const.Pgreen[2])) Label_gen.place((7, 30)) #Widget for line counter entry_line_no = Entry(Frame,width=12,height=270,textcolor = Color("blue"),bold=True) entry_line_no.place((12,50)) line_no = "" for value in range(1,50): line_no = line_no+str(value)+"\n\n" entry_line_no.set(line_no) #Visual of current code , non editable entryForInput = Entry(Frame,width=191,height=270) entryForInput.place((27,50)) #Score entry_score = Entry(Frame,width=90,height=10) entry_score.place((120,30)) #Errors output section Label_errors = Label(Frame, width = 220, height = 110, htitle = " Estado ", htitlefont = "Verdana", htitlesize = 14, htitlecolor = Color(const.black[0], const.black[1], const.black[2]), colour = Color(const.Pgreen[0], const.Pgreen[1], const.Pgreen[2])) Label_errors.place((7, 330)) global errors errors = Entry(Frame,text = "No hay errores ",textcolor=Color("Red"), width=206,height=90,fontsize=11) errors.place((12,350)) #Buttons compile_button= Button(Window, text = "Paso 1", width = 95, height = 20, bordercolor = const.white, colour = const.Porange, fontsize = 16, target = Compile_instruction) compile_button.place((435, 460)) execute_button= Button(Window, text = "Paso 2", width = 95, height = 20, bordercolor = const.white, colour = const.green, fontsize = 16, target = Execute_instruction) execute_button.place((545, 460))
##Renders rest of display character.show(Window) if not input_initialized: Create_input() render_widgets() pygame.display.flip() #If the character is on the finish line if character.x == character.maze.w - 1 and character.y == character.maze.h - 1: Restart() elif(on_initial): ## Main page Window.fill(const.black) # Buttons # Levels section start_button= Button(Window, text = "Principiante", width = 95, height = 30, bordercolor = const.Porange, colour = const.yellow, fontsize = 18, target = Start_game) start_button.place((555, 25)) expert_button= Button(Window, text = "Experto", width = 95, height = 30, bordercolor = const.white, colour = const.red, fontsize = 18, target = Expert_mode) expert_button.place((555, 64)) ## Background image img_background.set_colorkey(RLEACCEL) rect = Rect((0,0), (0, 0)) Window.blit(img_background, rect) ## Character img_character.set_colorkey(RLEACCEL) rect = Rect((-10,115), (101, 171)) Window.blit(img_character, rect) ## Bubble message img_bubble.set_colorkey(RLEACCEL) rect = Rect((75,70), (101, 171)) Window.blit(img_bubble, rect)
class Counter: def __init__(self,master,**kwarg): self.master = master self.width = 50 self.height = 20 self.borderwidth = 1 self.bordercolor = Color("Grey") self.framefont = "Arial" self.frameborderwidth = 2 self.framefontsize = 12 self.framebold = False self.frameitatic = False self.framealias = True self.colour = Color("White") self.image = None self.value = 0 self.step = 1 self.isRendering = False self._priority = 3 for key in kwarg.keys(): if key == "width": self.width = kwarg[key] elif key == "height": self.height = kwarg[key] elif key == "colour": self.colour = kwarg[key] elif key == "image": self.image = kwarg[key] elif key == "colour": self.colour = kwarg[key] elif key == "borderwidth": self.borderwidth = kwarg[key] elif key == "bordercolor": self.bordercolor = kwarg[key] elif key == "framefont": self.framefont = kwarg[key] elif key == "framefontsize": self.framefontsize = kwarg[key] elif key == "framebold": self.framebold = kwarg[key] elif key == "frameitatic": self.frameitatic = kwarg[key] elif key == "framealias": self.framealias = kwarg[key] elif key == "value": self.value = kwarg[key] elif key == "step": self.step = kwarg[key] else: raise ValueError("Invalid Arguement") self._frame = StaticFrame(self.master,width=self.width+self.borderwidth*4,height=self.height+self.borderwidth*4,\ borderwidth=0,bordercolor=self.bordercolor,\ header=0) self._entry = Entry(self._frame,width=self.width//2,height=self.height,\ font=self.framefont,fontsize=self.framefontsize,bold=self.framebold,\ italic=self.frameitatic,antialias=self.framealias,borderwidth=self.frameborderwidth) self._up = Button(self._frame,width=self.width//2,height=self.height//2-self.borderwidth*2,target=self.stepUp,\ font=self.framefont,fontsize=self.framefontsize//2,bold=self.framebold,text="+",\ italic=self.frameitatic,antialias=self.framealias,borderwidth=self.frameborderwidth) self._down = Button(self._frame,width=self.width//2,height=self.height//2-self.borderwidth*2,target=self.stepDown,\ font=self.framefont,fontsize=self.framefontsize//2,bold=self.framebold,text="-",\ italic=self.frameitatic,antialias=self.framealias,borderwidth=self.frameborderwidth) self._entry.place((0,0)) self._entry.set(str(self.value)) self._up.place((self.width//2,0)) self._down.place((self.width//2,self._entry.height//2+self.borderwidth*2)) _widgets.append(self) def get(self,x,y): return int(self._entry.get()) def set(self,value,x,y): self._entry.set(value) def stepUp(self): self._entry.set(str(int(self._entry.get())+self.step)) self.value = int(self._entry.get())+self.step def stepDown(self): self._entry.set(str(int(self._entry.get())-self.step)) self.value = int(self._entry.get())-self.step def place(self,pos): self.pos = pos self._frame.pos = pos self.isRendering = True _widgets.sort(key=get_priority,reverse=True) if type(self.master) in (DropFrame,Frame,StaticFrame,Label,XFrame): self.master.placed_obj.append(self._frame) def render(self): try: if self.master and self.isRendering: self.master.blit(self._frame.image,self.pos) except NameError: raise Exception("Position was not set.") def forget_place(self): """Stop this widget from rendering""" self.isRendering = False for widget in self.placed_obj: widget.forget_place() def remember_place(self): """Restarts this widget to begin rendering""" self.isRendering = True for widget in self.placed_obj: widget.remember_place() def move(self,pos): self.pos = pos def kill(self): global _widgets self.isRendering = False _widgets.remove(self) for widget in self.placed_obj: widget.kill()