##### 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========================================================================andplusalpha====================================Actually,everythingdoesn tseemlikeagame.Now,wewillinsertaruleintothisprogram.Then.Itwillbecomegame.Ruleissimple:countingredorblackfrom5x52Darrayandchoosethecolorwhichhasmuchmorenumber!Ifcorrect,HP++,otherwise,HP--.Thennewarraywillbesetfornextquiz!toosimplebutgamewhichcanbemadeinthistutorial.First,weneedtogenerate2Darrayandprintit.How?Welearnedhowtoprintintegerdata(whichequalssingledata(0Darray))andtwobuttons(whichequalssinglearray(1Darray).Caseof2Darrayjustneedsone-morestep...image::AdvancedOutputAlpha1.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("AdvancedOutputAlpha1.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::AdvancedOutputAlpha2.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("AdvancedOutputAlpha2.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::AdvancedOutputAlpha3.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("AdvancedOutputAlpha3.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()generateboardfunctionreturnsrandomlygenerated2Dboardwiththenumberofredblockandblackblock.Needlesstoexplain.Also,printboardfunctionprints2Dboardbysamemethodas1Darray.Outputcolorwillbechangedbydataofboard[i][j]is1ornot.Thisboardisforoutputonly.Processingaboutmarginseemstobeannoyingbecausewehavetoknowexactlocationbycalculating.Rememberprolog.Output(executingresult)ofPygameisGUIbutinput(coding)ofPygameisCUI.ThatisPygame.Actually,therearealotofideaforimprovingthisgame.Howaboutchangingbuttonintoimagefile?Howaboutaddingsoundeffectwhenpreviouschosewascorrectornot?Howaboutsettingtimelimit?Howaboutaddingvisualeffectwhenplayerwin(maxHP)ornot(minHP)?Howaboutmaketheboardbiggerwithanothercolors?HowaboutimplementFlood-itgamebygiveninterface?Therearestilllotsofselectionbecausethisgameissimple.<ReferenceCode>::importpygame,sys,randomfrompygame.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("RedorBlackProject")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=5board,b_red,b_black=generateBoard(5,5)#1whileTrue:myText=myTextFont.render((str(HP)+"/"+str(maxHP)),True,red,gray)myScreen.fill(gray)myScreen.blit(myText,myTextArea)drawHP(HP)drawButtons()drawBoard(board)#2foreventinpygame.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:x,y=event.posifpygame.Rect(270,425,45,45).collidepoint(x,y):#3ifb_red>=b_black:ifHP!=10:HP=HP+1board,b_red,b_black=generateBoard(5,5)elifb_red<b_black:ifHP!=0:HP=HP-1board,b_red,b_black=generateBoard(5,5)elifpygame.Rect(325,425,45,45).collidepoint(x,y):#4ifb_red<=b_black:ifHP!=10:HP=HP+1board,b_red,b_black=generateBoard(5,5)elifb_red>b_black:ifHP!=0:HP=HP-1board,b_red,b_black=generateBoard(5,5)pygame.display.update()fpsClock.tick(60)defdrawHP(HP):r=int((height-40)/maxHP)pygame.draw.rect(myScreen,gray,(20,20,20,20+((maxHP-0.5)*r)))foriinrange(maxHP):ifHP>=(maxHP-i):pygame.draw.rect(myScreen,blue,(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)defgenerateBoard(width,height):#5board=[]b_red=0b_black=0forxinrange(width):column=[]foryinrange(height):column.append(random.randint(0,1))board.append(column)forxinrange(width):foryinrange(height):if(board[x][y]==1):b_red=b_red+1elif(board[x][y]==0):b_black=b_black+1returnboard,b_red,b_blackdefdrawBoard(board):#6r=50b_width=5b_height=5l_margin=int((width-(b_width*r))/2)u_margin=int((height-(b_height*r))/2)forxinrange(5):foryinrange(5):left=x*r+l_marginup=y*r+u_marginifboard[x][y]==1:color=red;elifboard[x][y]==0:color=blackpygame.draw.rect(myScreen,color,(left,up,r,r))left=l_marginup=u_marginpygame.draw.rect(myScreen,white,(left-1,up-1,r*5+1,r*b_height+1),1)if__name__=='__main__':main()

Edit on GitHub