## mu pygame - Kojiverse Productions
## Snapshot :: 0.2
## Will work on any numworks OS
## (for now, just worked on mu 1.4.3)
################## imports ################
fromkandinskyimportfill_rectasfl,fill_circleasfc,draw_stringasdr,draw_lineasdl,wait_vblankfromionimport*importtimeassys_time################## const ##################
K_OK=KEY_OK;K_BACK=KEY_BACK;K_LEFT=KEY_LEFT;K_RIGHT=KEY_RIGHT;K_UP=KEY_UP;K_DOWN=KEY_DOWNLOC_KEYS_=(K_OK,K_DOWN,K_UP,K_RIGHT,K_LEFT)FULLSCREEN=0x00000001NOFRAME=0x00000010QUIT=0x10000000KEYDOWN=0x00000100KEYUP=0x00000200__all__=["Display","Display.set_mode","Display.set_caption","Display.flip","Surface","Surface.fill""Rect","Rect.colliderect","Rect.move_to","Rect.move","draw","draw.rect","draw.line","draw.circle","time","time.delay","time.Clock","Clock","Clock.tick","Clock.get_fps""key","key.get_pressed",]################## utils ##################
presses={key:[None,None]forkeyinLOC_KEYS_}defpresses_update():forkeyinpresses.keys():presses[key][-2]=presses[key][-1]presses[key][-1]=keydown(key)click=lambdakey:presses[key][-1]andnotpresses[key][-2]release=lambdakey:presses[key][-2]andnotpresses[key][-1]################## class ##################
classinit:def__init__(self)->None:""" Init but idk what the fuck this func is supposed to do in the original pygame """returnclassSurface:def__init__(self,width:int,height:int)->None:""" Create an surface for draw, blit, ..."""self.width=widthself.height=heightself.to_do={"rect":list(),"line":list(),"circle":list(),"polygon":list(),}returndeffill(self,color:str|int)->None:""" Fill itself into specified color """Check._color(color)fl(0,display.YPLUS,self.width,self.height,color)returnclassCheck:def__init__()->None:""" Private class to check args type """returndef_int(*args)->None:""" Check if args are integers """foriinargs:iftype(i)!=int:raiseValueError("An integer was expected instead of %s"%i)returndef_color(*args)->None:""" Check if args are color """foriinargs:ifnot(type(i)==strandlen(i)==7andi[0]=="#"ortype(i)==tuple):raiseValueError("A tuple was expected instead of %s"%i)returndef_rect(*args)->None:""" Check if args are Rects """foriinargs:iftype(i)!=Rect:raiseValueError("A pygame.Rect object was expected instead of %s"%i)returndef_tuple(*args)->None:""" Check if args are tuples """foriinargs:iftype(i)!=tuple:raiseValueError("A tuple was expected instead of %s"%i)returndef_surface(*args)->None:foriinargs:iftype(i)!=Surface:raiseValueError("A pygame.Surface object was expected instead of %s"%i)returnclassDisplay:def__init__(self)->None:""" Display class for pygame numworks """self.width,self.height=None,Noneself.caption="Pygame window"self.YPLUS=18self.surface=Nonereturndef__refresh_screen(self)->None:""" Refresh window (size, caption) """ifnotself.surface:fl(0,self.YPLUS,self.width,self.height,"#000000")ifself.YPLUS:fl(0,0,self.width,self.YPLUS,"#ffffff")dr(self.caption[:self.width//10-1],5,0)returndefset_caption(self,caption:str)->None:""" Set display's caption """self.caption=captionself.__refresh_screen()returndefset_mode(self,size:tuple,flags:int=0)->Surface:""" Create an Surface object """self.width,self.height=sizeCheck._int(self.width,self.height)ifflags&NOFRAME:self.YPLUS=0ifflags&FULLSCREEN:self.width=320self.height=222-self.YPLUSifself.height+self.YPLUS>222:self.height=222self.surface=Surface(self.width,self.height)self.__refresh_screen()returnself.surfacedefflip(self)->None:""" Update display """self.__refresh_screen()wait_vblank()[fl(*rect)forrectinself.surface.to_do["rect"]][fc(*circle)forcircleinself.surface.to_do["circle"]][dl(*line)forlineinself.surface.to_do["line"]]self.surface.to_do={"rect":list(),"circle":list(),"line":list(),"polygon":list()}returndefget_surface(self)->__Surface:""" Return self.surface """returnself.surfaceclassdraw:@staticmethoddefrect(surface:Surface,color:tuple|str,rect:Rect|tuple)->None:""" Drawing a rect on a surface """Check._color(color)Check._rect(rect)surface.to_do["rect"].append(rect._get_infos(display.YPLUS)+(color,))return@staticmethoddefcircle(surface:Surface,color:tuple|str,pos:tuple,radius:int,width:int=0)->None:""" Drawing a circle on a surface """Check._color(color)Check._int(radius,width)Check._tuple(pos)surface.to_do["circle"].append((pos[0],pos[1]+display.YPLUS,radius,color))return@staticmethoddefline(surface:Surface,color:tuple|str,startpos:tuple,endpos:tuple)->None:""" Drawing a line on a surface """Check._color(color)Check._tuple(startpos,endpos)surface.to_do["line"].append((startpos[0],startpos[1]+display.YPLUS,endpos[0],endpos[1]+display.YPLUS,color))returnclassRect:def__init__(self,x:int,y:int,width:int,height:int)->None:""" Class for Rect objects """self.x,self.y=x,yself.width,self.height=width,heightdef__str__(self)->None:""" Return Rect """return"Rect({%s}, {%s}, {%s}, {%s})"%(self.x,self.y,self.width,self.height)def_get_infos(self,yplus:int=0)->tuple:""" Return tuplize rect """Check._int(yplus)return (self.x,self.y+yplus,self.width,self.height)defmove(self,x:int,y:int)->Rect:""" Move itself by x,y """Check._int(x,y)returnRect(self.x+x,self.y+y,self.width,self.height)defmove_to(self,x:int,y:int)->None:""" Move itself at x,y """Check._int(x,y)self.x,self.y=x,yreturndefcolliderect(self,other:Rect)->bool:""" Return if itself is in collision with another rect """returnmu.colliderect((self.x,self.y,self.width,self.height),(other.x,other.y,other.width,other.height))classkey:@staticmethoddefget_pressed()->dict:""" Get all keys and their status """return{key:keydown(key)forkeyinLOC_KEYS_}classtime:@staticmethoddefClock()->Clock:""" Returning pygame.Clock object """returnClock()@staticmethoddefdelay(delay:int)->None:""" Stop for speciefieds milliseconds """sys_time.sleep(delay/1000)returnclassClock:def__init__(self)->None:""" Class for pygame.clock objec """self.start_time=sys_time.monotonic()self.last_elapse=Noneself.elapsed_time=[]returndeftick(self,fps:int=60)->None:""" Wait for targeted fps """ifnotself.last_elapse:self.last_elapse=self.start_timewhilesys_time.monotonic()-self.last_elapse<1/fps:passself.elapsed_time.append(sys_time.monotonic()-self.last_elapse)iflen(self.elapsed_time)>30:delself.elapsed_time[0]self.last_elapse=sys_time.monotonic()returndefget_fps(self)->float:""" Returning average clock FPS (based on last 30 values)"""return1/((sum(self.elapsed_time)/len(self.elapsed_time)+0.00001)+0.00001)classEvents:def__init__(self)->None:""" Class for pygame.event """self.events=list()def__handle_events(self)->None:""" Handle events and adding them to self.events """presses_update()forkeyinpresses.keys():ifclick(key):self.events.append((key,KEYDOWN))ifrelease(key):self.events.append((key,KEYUP))returndefget(self)->list:""" Get an list of events """self.__handle_events()returnself.events################## assignements ###########
display=Display()event=Events()
During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:
Ensure the proper functioning of the site (essential cookies); and
Track your browsing to send you personalized communications if you have created a professional account on the site and can be contacted (audience measurement cookies).
With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.