Snake Game:
To install Pygame First, you can install Pygame using PIP:
Pip Install Pygame
You can run the following code:
#!/usr/bin/env pythonImportPygame,sys,time,random fromPygame.localsImport*#Defining color VariablesRedcolour = Pygame. Color (255, 0,0) Blackcolour=Pygame. Color (0,0,0) Whitecolour= Pygame. Color (255,255,255) Greycolour= Pygame. Color (150,150,150)#defining the Gameover functiondefGameover (playsurface): Gameoverfont= Pygame.font.Font ('Arial.ttf', 72) Gameoversurf= Gameoverfont.render ('Game over', True, greycolour) Gameoverrect=gameoversurf.get_rect () gameoverrect.midtop= (320, 10) Playsurface.blit (Gameoversurf, Gameoverrect) Pygame.display.flip () Time.sleep (5) Pygame.quit () sys.exit ()#define the main functiondefMain ():#Initialize Pygamepygame.init () Fpsclock=Pygame.time.Clock ()#Creating a pygame display layerPlaysurface = Pygame.display.set_mode ((640,480)) Pygame.display.set_caption ('Raspberry Snake') #Initialize VariablesSnakeposition = [100,100] Snakesegments= [[100,100],[80,100],[60,100]] Raspberryposition= [300,300] raspberryspawned= 1direction=' Right'changedirection=direction whileTrue:#detection of Pygame events such as keystrokes forEventinchpygame.event.get ():ifEvent.type = =QUIT:pygame.quit () sys.exit ( )elifEvent.type = =KEYDOWN:#Judging keyboard Events ifEvent.key = = K_rightorEvent.key = = Ord ('D'): Changedirection=' Right' ifEvent.key = = K_leftorEvent.key = = Ord ('a'): Changedirection=' Left' ifEvent.key = = K_uporEvent.key = = Ord ('W'): Changedirection=' up' ifEvent.key = = K_downorEvent.key = = Ord ('s'): Changedirection=' Down' ifEvent.key = =K_ESCAPE:pygame.event.post (Pygame.event.Event (QUIT))#determine if the opposite direction is entered ifChangedirection = =' Right' and notDirection = =' Left': Direction=changedirectionifChangedirection = =' Left' and notDirection = =' Right': Direction=changedirectionifChangedirection = =' up' and notDirection = =' Down': Direction=changedirectionifChangedirection = =' Down' and notDirection = =' up': Direction=changedirection#move the coordinates of a snake head in the direction ifDirection = =' Right': snakeposition[0]+ = 20ifDirection = =' Left': snakeposition[0]-= 20ifDirection = =' up': snakeposition[1]-= 20ifDirection = =' Down': snakeposition[1] + = 20#increase the length of the snakeSnakesegments.insert (0,list (snakeposition))#To determine if the raspberry was eaten. ifSnakeposition[0] = = Raspberryposition[0] andSNAKEPOSITION[1] = = raspberryposition[1]: raspberryspawned=0Else: Snakesegments.pop ()#If you eat raspberries, regenerate the raspberry ifraspberryspawned = =0:x= Random.randrange (1,32) y= Random.randrange (1,24) Raspberryposition= [Int (x*20), int (y*20)] raspberryspawned= 1#drawing the Pygame display layerPlaysurface.fill (Blackcolour) forPositioninchSnakeSegments:pygame.draw.rect (Playsurface,whitecolour,rect (position[0],position[1],20,20)) Pygame.draw.rect (Playsurface,redcolour,rect (raspberryposition[0], raspberryposition[1],20,20)) #Refresh pygame Display layerPygame.display.flip ()#Judging if it's dead. ifSNAKEPOSITION[0] > 620orSnakeposition[0] <0:gameover (playsurface)ifSNAKEPOSITION[1] > 460orSNAKEPOSITION[1] <0: forSnakebodyinchSnakesegments[1:]: ifSnakeposition[0] = = Snakebody[0] andSNAKEPOSITION[1] = = snakebody[1]: Gameover (playsurface)#Control Game SpeedFpsclock.tick (5)if __name__=="__main__": Main ()
Operation Method:
Up or down key or Wsad key control
ESC key to exit the game
Download code: Http://files.cnblogs.com/files/qiu2013/snake.zip
The game code is derived from the Raspberry Pi User Guide, for informational purposes only.
Python learning Note 05: Snake Game Code