##### 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========================================================================BasicINPUT========================================================================-NewInputisnewEvent====================================Usually,welearnhowtooutputsomethingfirst(ThinkaboutHelloWorld!),learninghowtoinputsomethingisalwayssecond.Why?Becauseinputisnottherequirementforsomeprogramincontrasttooutputistherequirementforeveryprogram.(That sdefinitionofprogram.morethanorsameas0input,morethanorsameas1output.)However,everygameneedsinput.That swhywesaid

> Ilikeplayinggames.Playingmeansmovingyourpartofbody(maybeyourfinger).Anyway,let saddinputlogictomakethisprojectintorealgame.

..image::Bagic-INPUT-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-INPUT-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-INPUT-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-INPUT-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()(SourceCodeforControllingWorldProjectanditsresultscreen)(NottheentiresourcecodeofControllingWorldProject,butpart)(ControllingWorld!moveswhenplayerpressoneoffourdirectionarrowofkeyboard)Thereare2bigdifferenceincomparisontobeforeproject.Firstbigdifferenceisline#5,whichaddschecking``KEYDOWN``**event**istriggeredornot.Otherlinesarejustchangingpreviousalgorithmtoactdifferently.WeknowthatsamecommandcanmakebigdifferenceinentireprogramwhenitisexecutedbeforeEventstatementofafterEventstatement.PayattentionthatprocessaboutchanginglocationappearafterEventstatement.(**Updateafterset**.Thatissecondbigdifference).Variable``event.key``meanslatestpressedkeyonkeyboard.Lookatthespecifickeyname.K_UP,K_LEFT,K_DOWN,K_RIGHT.Veryintuitive**K_series**.(Givenbypygame.localswhichweaddedattheHeader)Furthermore,thereareotherkeynamedK_8,K_a,K_L,K_LCTRL,K_DELETE,orK_F4.Wecanunderstandmeaningofthesekeyswithoutextraexplanation.Fullkeylistcanbefoundin`https://www.pygame.org/docs/ref/key.html#pygame.key.name.`NoticethatKEYDOWNmeans

> thiskeywasnotpressedbefore,but**nowispressed**

>   andmeaningof**

> hold

>   isnotincluded**here.Inthecaseofhold,newevent-handlingaboutchecking``KEYUP``(itmeans

> thiskeywaspressedbefore,butnowisnotpressed)isneededwithsomeprocessing(whichneedsextravariableandalgorithm).Thiswillbementionedatadvancedpart.

Addinginputwaseasybecauseit sjustaddingifphasewithcertaineventparameter.Nowgameprojectisdonebecauseprojecthasoutput,processandinputstep-by-step!Really?No.Thisprojectcan tbecalledasagamebecausethereisno**interaction**betweenatleasttwo**gameobjects**,no**rule**forplayingthis(neitherconstrains(ex.HP,time)norscore).Mostly,not**enjoyable**(nomotivation,novarietyofinputandoutput,noattractivecontents)Firstly,wehavetolearnmoreadvancedinput(forexample,handlingformousestatus),process(forexample,ideaforfunctionalization)andoutput(forexample,printingimage/soundorvisualizinginternaldata)toflourishgameinterface/system.Don tstayinthelevelofprintingconsttextorinputtingsinglepressedkey.Ofcourse,experiencesofthislevelaresurelyhelpfulforadvancedlevel.So,let sgotoadvancedlevel!<ReferenceCode>::importpygame,sysfrompygame.localsimport*white=(255,255,255)red=(255,0,0)green=(0,255,0)pygame.init()pygame.display.set_caption("ControllingWorldProject")myScreen=pygame.display.set_mode((640,480))myTextFont=pygame.font.Font("HoonWhitecatR.ttf",32)myText=myTextFont.render("ControllingWorld!",True,red,green)myTextArea=myText.get_rect()myTextArea.center=(320,240)fpsClock=pygame.time.Clock()x=0y=0moveRight=0#1moveDown=0#2whileTrue:moveRight=0#3moveDown=0#4myTextArea.center=(320+x,240+y)myScreen.fill(white)myScreen.blit(myText,myTextArea)foreventinpygame.event.get():ifevent.type==QUIT:pygame.quit()sys.exit()elifevent.type==KEYDOWN:#5ifevent.key==K_UP:moveDown=-1moveRight=0elifevent.key==K_LEFT:moveDown=0moveRight=-1elifevent.key==K_DOWN:moveDown=1moveRight=0elifevent.key==K_RIGHT:moveDown=0moveRight=1if(moveRight==1):#6x=x+10elif(moveRight==-1):#7x=x-10if(moveDown==1):#8y=y+10elif(moveDown==-1):#9y=y-10pygame.display.update()

Edit on GitHub