Python learning notes 05: Snake game code, python learning notes

Source: Internet
Author: User

Python learning notes 05: Snake game code, python learning notes

Snake games:

Install pygame first. You can use pip to install pygame:

Pip install pygame

Run the following code:

#! /Usr/bin/env pythonimport pygame, sys, time, randomfrom pygame. locals import * # define the color variable redColour = pygame. color (255, 0, 0) blackColour = pygame. color (0, 0) whiteColour = pygame. color (255,255,255) greyColour = pygame. color (150,150,150) # define the gameOver function def gameOver (playSurface): gameOverFont = pygame. font. font ('arial. ttf', 72) gameOverSurf = gameOverFont. render ('game over', True, greyColour) gameOverRect = gameOverSurf. get_rect () gameOverRect. midtop = (320, 10) playSurface. BITs (gameOverSurf, gameOverRect) pygame. display. flip () time. sleep (5) pygame. quit () sys. exit () # define the main function def main (): # initialize pygame. init () fpsClock = pygame. time. clock () # create a pygame display layer playSurface = pygame. display. set_mode (640,480) pygame. display. set_caption ('Raspberry Snake ') # initialize the variable "Snail keposition" = [100,100] snail kesegments = [[100,100], [80,100], [60,100] raspberryPosition = [300,300] raspberrySpawned = 1 ction = 'right' changeDirection = direction while True: # detect pygame events such as buttons in pygame. event. get (): if event. type = QUIT: pygame. quit () sys. exit () elif event. type = KEYDOWN: # determine if event for a keyboard event. key = K_RIGHT or event. key = ord ('D'): changeDirection = 'right' if event. key = K_LEFT or event. key = ord ('A'): changeDirection = 'left' if event. key = K_UP or event. key = ord ('W'): changeDirection = 'up' if event. key = K_DOWN or event. key = ord ('s '): changeDirection = 'low' if event. key = K_ESCAPE: pygame. event. post (pygame. event. event (QUIT) # determine whether the inverse direction is input. if changeDirection = 'right' and not direction = 'left ': direction = changeDirection if changeDirection = 'left' and not direction = 'right': ction = changeDirection if changeDirection = 'up' and not direction = 'drop ': direction = changeDirection if changeDirection = 'low' and not direction = 'up': ction = changeDirection # if direction = 'right ': snail keposition [0] + = 20 if direction = 'left': snail keposition [0]-= 20 if direction = 'up ': snail keposition [1]-= 20 if direction = 'lowdown ': snail keposition [1] + = 20 # increase the length of the snake. insert (0, list (snkeposition) # determine whether Raspberry is eaten if the snakkeposition [0] = raspberryPosition [0] and snakkeposition [1] = raspberryPosition [1]: raspberrySpawned = 0 else: snkesegments. pop () # if you eat raspberry, regenerate raspberry if raspberrySpawned = 0: x = random. randrange (1, 32) y = random. randrange () raspberryPosition = [int (x * 20), int (y * 20)] raspberrySpawned = 1 # Draw the playSurface of the pygame display layer. fill (blackColour) for position in snail kesegments: 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 the pygame display layer pygame. display. flip () # determine whether the result is dead if snail keposition [0]> 620 or snail keposition [0] <0: gameOver (playSurface) if snail keposition [1]> 460 or snail keposition [1] <0: for snail body in snail kesegments [1:]: if snail keposition [0] = snail kebody [0] and snail keposition [1] = snail kebody [1]: gameOver (playSurface) # control the speed of the game fpsClock. tick (5) if _ name _ = "_ main _": main ()


Operation Method:

Upper/lower left key or wsad Key Control

Exit the game with the ESC key

 

Download: http://files.cnblogs.com/files/qiu2013/snake.zip

The game code comes from the Raspberry Pi user guide for your reference only.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.