Keys=pygame.key.get_pressed (), accept all keystrokes currently pressed in the game, and save them as a list, placed in the keys variable
Pygame. K_w tells Pygame you are checking the W key, by modifying the last letter, you can change it to any button you want to check.
Import Pygamepygame.init () size=[400,300]screen=pygame.display.set_mode (size) clock=pygame.time.clock () done= Falsewhile not done:keys=pygame.key.get_pressed () if Keys[pygame. K_w]: print "Hello" for event in Pygame.event.get (): If Event.type==pygame. Quit:done=true Clock.tick (+) Pygame.quit ()
Control the ball up and down move around
Import pygamepygame.init () size=[400,300]screen=pygame.display.set_mode (size) clock=pygame.time.clock () x= Size[0]/2y=size[1]/2red=pygame.color.color (' #FF8080 ') blue=pygame.color.color (' #8080FF ') white= Pygame.color.Color (' #FFFFFF ') black=pygame.color.color (' #000000 ') done=falsewhile not done: screen.fill (Black) keys=pygame.key.get_pressed () if keys[pygame. K_w]: y-=1 if keys[pygame. K_s]: y+=1 if keys[pygame. K_a]: x-=1 if keys[pygame. K_d]: x+=1 pygame.draw.circle (screen,red , [x,y],6) pygame.display.flip () for event in Pygame.event.get (): if event.type==pygame. quit: done=true Clock.tick (Pygame.quit) ()
Note: To prevent the ball out of the screen, add judgment
Import pygamepygame.init () size=[400,300]screen=pygame.display.set_mode (size) clock=pygame.time.clock () x= Size[0]/2y=size[1]/2red=pygame.color.color (' #FF8080 ') blue=pygame.color.color (' #8080FF ') white= Pygame.color.Color (' #FFFFFF ') black=pygame.color.color (' #000000 ') Def checkoffscreenx (x): if x>size[0]: x=0 elif x<0: x=size[0] return xdef checkoffscreeny (y): if y>size[1]: y=0 elif y<0: y=size [1] return ydone=falsewhile not done: screen.fill (black) keys=pygame.key.get_pressed () if keys[pygame. K_w]: &nBsp; y-=1 if keys[pygame. K_s]: y+=1 if keys[pygame. K_a]: x-=1 if keys[pygame. K_d]: x+=1 x=checkoffscreenx (x) y=checkoffscreeny (y) pygame.draw.circle (screen,red,[x,y],6) pygame.display.flip () for event in pygame.event.get (): if event.type==pygame. quit: done=true Clock.tick (Pygame.quit) ()
Full game code:
Import randomimport pygamepygame.init () size=[400,300]screen=pygame.display.set_mode (size) clock= Pygame.time.Clock () X=size[0]/2y=size[1]/2ballx=random.randrange (0,size[0]) Bally=random.randrange (0,size[1]) Goalx=size[0]/2-10goaly=size[1]/2-10goalw=20goalh=20points=0red=pygame.color.color (' #FF8080 ') blue= Pygame.color.Color (' #8080FF ') white=pygame.color.color (' #FFFFFF ') black=pygame.color.color (' #000000 ') def Checkoffscreenx (x): if x>size[0]: &NBSP;X=0&NBSP;&NBSP;&NBSP;&NBSP;ELIF&NBSP;X<0:&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;X=SIZE[0] return xdef checkoffscreeny (y): if y>size[1]: y=0 elif y<0: y=size[1] return ydef checktouching (): global x global ballx global y global bally if -10<y-ballY<10 and -10<x-ballX<10: pygame.draw.circle (screen,white,[x,y],15) xDiff=x-ballX yDiff=y-ballY if ballX==0: xDiff-=5 elif ballX==size[0]: xDiff+=5 if bally==0: ydiff-=5 elif ballY==size[1]: ydiff+=5 x+=xdiff*3 ballx-=xdiff*3 y+=ydiff*3 bally-=ydiff*3 done=falsewhile not done: screen.fill (Black) pygame.draw.rect (Screen,white, GoalX,goalY, GOALW,GOALH)) keys=pygame.key.get_pressed () if keys[pygame. K_w]: y-=1 if keys[pygame. K_s]: y+=1 if keys[pygame. K_a]: x-=1 if keys[pygame. K_d]: x+=1 x=checkoffscreenx (x) y=cHeckoffscreeny (y) ballx=checkoffscreenx (BALLX) bally= Checkoffscreeny (BallY) checktouching () For point in range (points): pointx=0+point*5 pygame.draw.rect (Screen,white, (pointX,3,4,10)) pygame.draw.circle (screen,red,[x,y],6) pygame.draw.circle (screen,blue,[ ballx,bally],6) if goalx<=ballx<=goalx+goalw and goaly<=bally <=goalY+goalH: points+=1 ballx=random.randrange (0,size[0]) bally= Random.randrange (0,size[1]) pygame.display.flip () for event in pygame.event.get (): &NBSP;&NBSP;&NBsp; if event.type==pygame. quit: done=true Clock.tick (pygame.quit) print "total points: " +str (points)
This article is from the "Small Stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1861648
Python Learning Gui (pygame keyboard)