##### pygame documentation | Pygame Home || Help Contents || Reference Index Most useful stuff: Color | display | draw | event | font | image | key | locals | mixer | mouse | Rect | Surface | time | music | pygame Advanced stuff: cursors | joystick | mask | sprite | transform | BufferProxy | freetype | gfxdraw | midi | Overlay | PixelArray | pixelcopy | sndarray | surfarray | math Other: camera | cdrom | examples | fastevent | scrap | tests | touch | version

??====================================Author:YoungwookKim(Korean)========================================================================Contact:rumia0601@gmail.com========================================================================withAdvancedOUTPUT Buttons====================================EvenifKEYDOWNeventisused,itseemslikethisisnotentirelyGUIgamebecauseGUIofthisgameisonlyusedforoutput(=print),notinput.InputforGUImeanscaringmouseeventforspecificlocation.HowaboutmakingtwobuttonstoincreaseordecreaseHP?..image::AdvancedInputOutput1.gif:class:inlined-right..code-block::python:linenos:importsys,pygamepygame.init()size=width,height=320,240speed=[2,2]black=0,0,0screen=pygame.display.set_mode(size)ball=pygame.image.load("AdvancedInputOutput1.gif")ballrect=ball.get_rect()while1:foreventinpygame.event.get():ifevent.type==pygame.QUIT:sys.exit()ballrect=ballrect.move(speed)ifballrect.left<0orballrect.right>width:speed[0]=-speed[0]ifballrect.top<0orballrect.bottom>height:speed[1]=-speed[1]screen.fill(black)screen.blit(ball,ballrect)pygame.display.flip()..image::AdvancedInputOutput2.gif:class:inlined-right..code-block::python:linenos:importsys,pygamepygame.init()size=width,height=320,240speed=[2,2]black=0,0,0screen=pygame.display.set_mode(size)ball=pygame.image.load("AdvancedInputOutput2.gif")ballrect=ball.get_rect()while1:foreventinpygame.event.get():ifevent.type==pygame.QUIT:sys.exit()ballrect=ballrect.move(speed)ifballrect.left<0orballrect.right>width:speed[0]=-speed[0]ifballrect.top<0orballrect.bottom>height:speed[1]=-speed[1]screen.fill(black)screen.blit(ball,ballrect)pygame.display.flip()Okay,makingtwobuttonsissimple.Lookatthebutton,theyhaveuniquevisualshape.Howcanitbe?Simpleaspreviousidea:First,draw**bigsquare**.Second,draw**smallersquare**whichhassmallwidth(thissquaredoesn thaveinnercolorsocolorofbigsquarecanbedisplayed)soinnersquareandoutersquareseemslikeseparated.Butthesebuttonsarestillforoutputonly.Weneedtomakeclickareaforthis...image::AdvancedInputOutput3.gif:class:inlined-right..code-block::python:linenos:importsys,pygamepygame.init()size=width,height=320,240speed=[2,2]black=0,0,0screen=pygame.display.set_mode(size)ball=pygame.image.load("AdvancedInputOutput3.gif")ballrect=ball.get_rect()while1:foreventinpygame.event.get():ifevent.type==pygame.QUIT:sys.exit()ballrect=ballrect.move(speed)ifballrect.left<0orballrect.right>width:speed[0]=-speed[0]ifballrect.top<0orballrect.bottom>height:speed[1]=-speed[1]screen.fill(black)screen.blit(ball,ballrect)pygame.display.flip()..image::AdvancedInputOutput4.gif:class:inlined-right..code-block::python:linenos:importsys,pygamepygame.init()size=width,height=320,240speed=[2,2]black=0,0,0screen=pygame.display.set_mode(size)ball=pygame.image.load("AdvancedInputOutput4.gif")ballrect=ball.get_rect()while1:foreventinpygame.event.get():ifevent.type==pygame.QUIT:sys.exit()ballrect=ballrect.move(speed)ifballrect.left<0orballrect.right>width:speed[0]=-speed[0]ifballrect.top<0orballrect.bottom>height:speed[1]=-speed[1]screen.fill(black)screen.blit(ball,ballrect)pygame.display.flip()..image::AdvancedInputOutput5.gif:class:inlined-right..code-block::python:linenos:importsys,pygamepygame.init()size=width,height=320,240speed=[2,2]black=0,0,0screen=pygame.display.set_mode(size)ball=pygame.image.load("AdvancedInputOutput5.gif")ballrect=ball.get_rect()while1:foreventinpygame.event.get():ifevent.type==pygame.QUIT:sys.exit()ballrect=ballrect.move(speed)ifballrect.left<0orballrect.right>width:speed[0]=-speed[0]ifballrect.top<0orballrect.bottom>height:speed[1]=-speed[1]screen.fill(black)screen.blit(ball,ballrect)pygame.display.flip()Nownewevent``MOUSEBUTTONUP``isaddedatEventstatement.Noticethatupofmouseissameasdownofkey.IfMOUSEBUTTONUPisactivated,``event.pos``willberecordedasxandy.Whichmeans,clickedpoint.So,determiningclickedpointiswhetherinsideofcertainRectareaornotisneededbychecking``collidepoint``.Ifinside,itmeans

> **userclickedsomepointwhichispartofcertainarea**

>   then,adequateprocess(updatingvalue)isneeded.

Noticethattherearetwoareaforinput:**(270,425,45,45)**and**(325,425,45,45)**atEventStatement.Noticethatalsotherearetwoareaforoutput.**(margin,height-r-10,r,r)**and**(margin+r+r_margin,height-r-10,r,r)**atdrawButtons.Inthecaseofbutton,inputandoutputareaforbuttonmustbe**identical**.(Otherwise,thisbuttonwillbedeceptive!)Itwillbebestideatosetthisdataassamevaluewithoutcalculatingexactlocationoffunction(foroutput)intoconstant(forinput).Thereisnospecificfunctiontotiethisarea,soyouhavetocareaboutthis.<ReferenceCode>::importpygame,sysfrompygame.localsimport*maxHP=10white=(255,255,255)gray=(127,127,127)black=(0,0,0)red=(255,0,0)green=(0,255,0)blue=(0,0,255)pygame.init()pygame.display.set_caption("ArraybuttonsProject")width=640height=480myScreen=pygame.display.set_mode((width,height))myTextFont=pygame.font.Font("HoonWhitecatR.ttf",32)myText=myTextFont.render((str(maxHP)+"/"+str(maxHP)),True,red,gray)myTextArea=myText.get_rect()myTextArea.center=(width/2,height/2)fpsClock=pygame.time.Clock()defmain():HP=5whileTrue:myText=myTextFont.render((str(HP)+"/"+str(maxHP)),True,red,gray)myScreen.fill(gray)myScreen.blit(myText,myTextArea)drawHP(HP)drawButtons()foreventinpygame.event.get():ifevent.type==QUIT:pygame.quit()sys.exit()elifevent.type==KEYDOWN:ifevent.key==K_UP:ifHP!=10:HP=HP+1elifevent.key==K_DOWN:ifHP!=0:HP=HP-1elifevent.type==MOUSEBUTTONUP:#1x,y=event.posifpygame.Rect(270,425,45,45).collidepoint(x,y):ifHP!=10:HP=HP+1elifpygame.Rect(325,425,45,45).collidepoint(x,y):ifHP!=0:HP=HP-1pygame.display.update()fpsClock.tick(60)defdrawHP(HP):r=int((height-40)/maxHP)pygame.draw.rect(myScreen,black,(20,20,20,20+((maxHP-0.5)*r)))foriinrange(maxHP):ifHP>=(maxHP-i):pygame.draw.rect(myScreen,red,(20,20+(i*r),20,r))pygame.draw.rect(myScreen,white,(20,20+(i*r),20,r),1)returndefdrawButtons():r=45r_margin=10colors=[red,black]num=2margin=int((width-((r*num)+(r_margin*(num-1))))/2)foriinrange(0,num):left=margin+(i*r)+(i*r_margin)up=height-r-10pygame.draw.rect(myScreen,colors[i],(left,up,r,r))pygame.draw.rect(myScreen,gray,(left+2,up+2,r-4,r-4),2)if__name__=='__main__':main()

Edit on GitHub