##### 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========================================================================withAdvancedPROCESS-Functionalization====================================First,Let sprintvisualizedgeometry,nottext.HowaboutHPbar?IfmaxHPofgameisfixedandcurrentHPofgamecanvaryfrom0tomaxHP,whatwillbesimplestwaytoprintbothtwodata?..image::AdvancedOutputProcess1.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("AdvancedOutputProcess1.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::AdvancedOutputProcess2.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("AdvancedOutputProcess2.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::AdvancedOutputProcess3.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("AdvancedOutputProcess3.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()Yeah,justre-renderingtextwhencertainvariableischanged.Howtochangevariable?ThatlogicisinsideofEventstatement.(PressingupordowntoadjustHP.)Samemethodasbefore.Buttheyarestilltext,whichmeanstheyarenotvisualizedenough.Howtovisualizethesetwodatamoredetail(maxHP,currentHP)?Wecanuseideaofmagazine(gun smagazine).HPisintegervalue,whichisdiscrete.So,itcanbeprintedasbelow:..image::AdvancedOutputProcess4.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("AdvancedOutputProcess4.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::AdvancedOutputProcess5.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("AdvancedOutputProcess5.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::AdvancedOutputProcess6.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("AdvancedOutputProcess6.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()Muchbetter,isn tit?Drawinglogicisinsideof#6.Logicissimple,first,drawa**bigrectangular**whichcolorisblack.Then,checkwhetherdrawa**smallerrectangular**whichcolorisredornotdependingoncurrentHP.Finally,draw**margin**ofcurrentsmallrectangular.Marginofcoloriswhite.Wecanfindthereare4parametersforlocationdatain``pygame.draw.rect``(Imean,thirddata.Firstdataiscanvasdata,seconddataiscolordataandfourthdataiswidth.)Theeasiestwaytounderstand4parametersischangethem.Changeonevalueinto10or30whileothersare20!Thenitcanbeunderstood.Needlesstoexplain.Furthermore,nowit stimetofunctionalizespecifically.IpushAlwaysstatementandEventstatementintomainfunction.(#7isneededtofindmainfunctionandexecuteit.)AndmadenewfunctionfordrawHP.FunctionalizationideaforgameisnotfarawayfromthatofnormalGUIprogram.Forexample,itisbettertomake**singleprintfunctionforeverysinglevalue**whichhastobedisplayed.Ofcourse,settinglocationforeachvaluehastobedonewhileyouaredesigningthescreen.<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("HPbarProject")width=640#1height=480#2myScreen=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)#3fpsClock=pygame.time.Clock()defmain():#4HP=5whileTrue:myText=myTextFont.render((str(HP)+"/"+str(maxHP)),True,red,gray)myScreen.fill(gray)myScreen.blit(myText,myTextArea)drawHP(HP)#5foreventinpygame.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-1pygame.display.update()fpsClock.tick(60)defdrawHP(HP):#6r=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)returnif__name__=='__main__':#7main()

Edit on GitHub