??====================================Author:YoungwookKim(Korean)========================================================================Contact:rumia0601@gmail.com========================================================================BasicPROCESS========================================================================-SettingforAnimation====================================Previousprojectlookslikeasingleimageinsteadofgame.Becausethereisnoinputneitherprocesstocontroloutput.Ofcourse,clickingexitbuttononwindowisnotcountedbecauseitisjustshuttingdowntheentireprogram.First,wewilllettext

HelloWorld!

tomoveautomatically(andnowprojectwillbelookslikeananimationratherthansingleimage),whichmeansaddingfirstprocessinglogiconthisproject.Howtomovetext?WeknowthatlocationoftextisinitializedinInitialstatement.So,locationoftextshouldbeupdatedinAlwaysstatement,withaddingsomevariabletoprocesssomething.

..image::Bagic-PROCESS-sourcecode.png:class:inlined-right..code-block::python:linenos:importsys,pygamepygame.init()size=width,height=220,140speed=[2,2]black=0,0,0screen=pygame.display.set_mode(size)ball=pygame.image.load("Bagic-PROCESS-sourcecode.png")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::Bagic-PROCESS-resultscreen.png:class:inlined-right..code-block::python:linenos:importsys,pygamepygame.init()size=width,height=220,140speed=[2,2]black=0,0,0screen=pygame.display.set_mode(size)ball=pygame.image.load("Bagic-PROCESS-resultscreen.png")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()(SourceCodeforMovingWorldProjectanditsresultscreen)(NottheentiresourcecodeofMovingWorldProject,butpart)(MovingWorld!movesautomaticallyjustlikeArkanoidballorDVDscreensaver.)Newline#1-#5wereappendedattheendofInitialstatement.Also,multipleif-elsephases(#6-#9)wereinsertedatthebeginningofAlwaysstatement,withline#11attheendofalwaysstatements.Wecanunderstandwhatcommandsof#2-#10do.Theyjustchangevariablesforpositionof

MovingWorld

whenAlwaysstatementbeingstarted.Butthereisaproblem.Howfastis

MovingWorld?

Itissurethatdisplacementof

MovingWorld

issqrt(2)(simplePythagoreanequation).Buthowoftendisplacementof

MovingWorld

ischanged?Itcan tbedeterminedwithoutcalculatingtimecomplexityofAlwaysstatement!(BecauseitdependsonhowoftenAlwaysstatementbeingstarted)Andtimecomplexwillbediffertocomputertocomputer,socan tbefixed.

Weneedtoaddtheconceptionof**fixedspeed**intothisproject.How?Lookat#1and#11.Thereis``pygame.time.Clock()``onInitialstatementand``tick(60)``onAlwaysstatement.60meansFPS(frameratepersecond).WeknowthatFPSmeanshowoftendisplayischangedin1second.Whatfunctionmeanschange(=update)displayinpygame?That sright.Pygame.display.update()function.So,FPSmeanshowoftenAlwaysstatementbeingexecutedin1second.Becausethereis1Pygame.display.update()functionin1alwaysstatements.(So,FPSmeans**selectivedelay**accordingtocurrentprogram sprocessspeed,**notselectiveacceleration**,soFPScannotworkifFPSistoohigh.)Ifweletfps(=**time**)tobefixedinthisproject,wecanchange**velocity**ofcertaingameobjectbyfindingappropriatevaluefor**displacement**.pygame.time.Clock()isneededtofixthespeedofprojectbeforegamestarted.NoticethattickfunctionhastobecalledwhenPygame.display.update()iscalled.Becausetickcountsthenumberofupdatefunction.ItisoneoftheexceptionoffunctionthatcanbeexecutedafterPygame.display.update().Okay,welearnthat

Fixingtime

isneededwhenscreenisupdated.Everyscreenofdynamicgameisfrequentlychangedunlessitisstaticgame.So,wehavetoknowthat.However,thisprojectisn tlooklikeagamebecauseitsresultcanbeanticipatedeasily(thereisnoinputtochangeresult)Now,inputlogicwillbeinserted.

<ReferenceCode>::importpygame,sysfrompygame.localsimport*white=(255,255,255)red=(255,0,0)green=(0,255,0)pygame.init()pygame.display.set_caption("MovingWorldProject")myScreen=pygame.display.set_mode((640,480))myTextFont=pygame.font.Font("HoonWhitecatR.ttf",32)myText=myTextFont.render("MovingWorld!",True,red,green)myTextArea=myText.get_rect()myTextArea.center=(320,240)fpsClock=pygame.time.Clock()#1x=0#2y=0#3moveRight=1#4moveUp=1#5whileTrue:if(moveRight==1):#6x=x+1if(x>=320-75):moveRight=0elif(moveRight==0):#7x=x-1if(x<=-320+75):moveRight=1if(moveUp==1):#8y=y+1if(y>=240-15):moveUp=0elif(moveUp==0):#9y=y-1if(y<=-240+15):moveUp=1myTextArea.center=(320+x,240+y)#10myScreen.fill(white)myScreen.blit(myText,myTextArea)foreventinpygame.event.get():ifevent.type==QUIT:pygame.quit()sys.exit()pygame.display.update()fpsClock.tick(60)#11




Edit on GitHub