python pygame do little game (Snake)

# Pygame game library, sys controlled environment python running 
Import pygame, sys, Random
 # This module contains all pygame Always used 
from pygame.locals Import * # 1, custom color variable 
# 0-2550 black 255 white 
redColor = pygame.Color (255 , 0, 0)
 # black background 
blackColor = pygame.Color (0, 0, 0)
 # Snake as a white 
whiteColor = pygame.Color (255, 255, 255 ) # function definition game is over DEF GameOver (): 
    pygame.quit () 
    sys.exit () # define the main function - "to define our entry function DEF main ():
     # initialize pygame










    pygame.init ()
     # define a variable to control the speed 
    fpsClock = pygame.time.Clock ()
     # Create pygame display layer, to create an interface 
    playsurface with pygame.display.set_mode = ((640, 480))   # generates the main screen creation screen size 
    pygame.display.set_caption ( ' snake ' )
     # initializes the variable 
    # Snake initial coordinate position (100, 100 in the first reference) 
    snakePosition = [100, 100 ]
     # length list initialization Snake elements have to representatives paragraphs body 
    snakeBody = [[100, 100], [80, 100], [60, 100 ]]
     # initialize the target position direction amount 
    targetPosition = [300, 300 ]
     # marks the object target block: determining whether eaten the goal box 1 is do not eat is eaten 0
    =. 1 targetflag # initialization direction - "right 
    direction = ' right ' # define a variable direction (artificially control keys) 
    changeDirection = direction
     the while True: for Event in pygame.event.get ():   # get events from the queue if == Event.type the QUIT: 
                pygame.quit () 
                the sys.exit () elif Event.type == KEYDOWN:   # when the button is pressed, the event will trigger IF event.key == K_RIGHT: 
                    changeDirection = ' right ' IF
    
    

        
            
            
                
                == event.key K_LEFT: 
                    changeDirection = ' left ' 
                IF event.key == K UP: 
                    changeDirection = ' up ' 
                IF event.key == K_DOWN: 
                    changeDirection = ' Down ' 
                    # corresponding to the document on the keyboard esc 
                IF event.key = = K_ESCAPE: 
                    pygame.event.post (pygame.event.Event (the QUIT)) 

        # orient 
        IF changeDirection == ' left '  and  Not direction == 'right':
            direction = changeDirection
        if changeDirection == 'right' and not direction == 'left':
            direction = changeDirection
        if changeDirection == 'up' and not direction == 'down':
            direction = changeDirection
        if changeDirection == 'down' and not direction == 'up':
            direction = changeDirection

        # 根据方向移动蛇头
        if direction == 'right':
            snakePosition[0] += 20
        if direction == 'left':
            snakePosition[0] -= 20
        if direction == 'up':
            snakePosition[1] -= 20
        if direction == 'down':
            snakePosition [ . 1] + = 20 is
         # increase the length of the snake 
        snakeBody.insert (0, List (snakePosition))
         # if the position of the target block and Snake coincide 
        IF snakePosition [0] == targetPosition [0] and snakePosition [. 1] targetPosition == [. 1 ]: 
            targetflag = 0
         the else : 
            snakeBody.pop () 
        IF targetflag == 0: 
            X = random.randrange (. 1, 32 ) 
            Y = random.randrange (. 1, 24 ) 
            targetPosition = [int (X * 20 is), int (* 20 is Y )] 
            targetflag= 1
         # fill the background color 
        playsurface.fill (blackColor)
         for position in snakeBody:
             # The first argument specifies a serface serface editing area drawn in this area 
            # second parameter color: Color 
            # third parameter: rect: returns a rectangle (XY), (width, height) 
            # fourth parameter: width: filling lines indicates the thickness of the solid width0 
            # snake 
            pygame.draw.rect (playsurface, redColor, Rect ( position [0], position [1 ], 20 is, 20 is )) 
            pygame.draw.rect (playsurface, whiteColor, Rect (targetPosition [0], targetPosition [ . 1], 20 is, 20 is )) 

        # update to the screen surface 
        pygame.display.flip ()
         # determines whether game over
        IF snakePosition [0]> 620. or snakePosition [0] < 0: 
            GameOver () 
        elif snakePosition [. 1]> 460 or snakePosition [. 1] < 0: 
            GameOver () 
        # Control game speed 
        fpsClock.tick (2 ) 


#    start entry function 
IF  the __name__ == ' __main__ ' : 
    main ()
  • effect

Guess you like

Origin www.cnblogs.com/u-damowang1/p/12159092.html