python pigame 貪食蛇 @ 071's blog :: 隨意窩 Xuite日誌
    1. 沒有新回應!
  • 201805301615python pigame 貪食蛇

    #!/usr/bin/env python

    #reference from : https://media.wiley.com/product_ancillary/6X/11184644/DOWNLOAD/raspberrysnake.py

    # Raspberry Snake
    # Written by Gareth Halfacree for the Raspberry Pi User Guide

    import pygame, sys, time, random
    from pygame.locals import *

    pygame.init()

    fpsClock = pygame.time.Clock()

    X_BOUNDARY = 640
    Y_BOUNDARY = 480
    BLOCK_W = 5
    BLOCK_H = 5
    SpeedTick = 12
    MaxSpeedTick = 360
    EatCount = 0
    playSurface = pygame.display.set_mode((X_BOUNDARY, Y_BOUNDARY))
    pygame.display.set_caption('Raspberry Snake')

    redColour = pygame.Color(255, 0, 0)
    blackColour = pygame.Color(0, 0, 0)
    whiteColour = pygame.Color(255, 255, 255)
    greyColour = pygame.Color(150, 150, 150)
    snakePosition = [100,100]
    snakeSegments = [[100,100],[100-BLOCK_W,100],[100-2*BLOCK_W,100]]
    raspberryPosition = [300,300]
    raspberrySpawned = 1
    direction = 'right'
    changeDirection = direction

    def gameOver():
    gameOverFont = pygame.font.Font('freesansbold.ttf', 72)
    gameOverSurf = gameOverFont.render('Game Over', True, greyColour)
    gameOverRect = gameOverSurf.get_rect()
    gameOverRect.midtop = (X_BOUNDARY/2, 10)
    playSurface.blit(gameOverSurf, gameOverRect)
    pygame.display.flip()
    time.sleep(7)
    pygame.quit()
    sys.exit()

    time.sleep(2)
    while True:
    for event in pygame.event.get():
    if event.type == QUIT:
    pygame.quit()
    elif event.type == KEYDOWN:
    if 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 = 'down'
    if event.key == K_ESCAPE:
    pygame.event.post(pygame.event.Event(QUIT))

    if snakePosition[0] == 0 and snakePosition[1] == raspberryPosition[1]:
    if changeDirection == direction:
    changeDirection = 'right'
    if snakePosition[0] == X_BOUNDARY-BLOCK_W and snakePosition[1] == raspberryPosition[1]:
    if changeDirection == direction:
    changeDirection = 'left'
    if snakePosition[1] == 0 and snakePosition[0] == raspberryPosition[0]:
    if changeDirection == direction:
    changeDirection = 'down'
    if snakePosition[1] == Y_BOUNDARY-BLOCK_H and snakePosition[0] == raspberryPosition[0]:
    if changeDirection == direction:
    changeDirection = 'up'

    if changeDirection == 'right' and not direction == 'left':
    direction = changeDirection
    if changeDirection == 'left' and not direction == 'right':
    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] += BLOCK_W
    if direction == 'left':
    snakePosition[0] -= BLOCK_W
    if direction == 'up':
    snakePosition[1] -= BLOCK_H
    if direction == 'down':
    snakePosition[1] += BLOCK_H
    snakeSegments.insert(0,list(snakePosition))

    #let it turn around the boundary
    if direction == 'up' and snakePosition[1] == 0:
    if snakePosition[0] < raspberryPosition[0]:
    changeDirection = 'right'
    else:
    changeDirection = 'left'
    if direction == 'right' and snakePosition[0] == X_BOUNDARY-BLOCK_W:
    if snakePosition[1] < raspberryPosition[1]:
    changeDirection = 'down'
    else:
    changeDirection = 'up'
    if direction == 'down' and snakePosition[1] == Y_BOUNDARY-BLOCK_H:
    if snakePosition[0] < raspberryPosition[0]:
    changeDirection = 'right'
    else:
    changeDirection = 'left'
    if direction == 'left' and snakePosition[0] == 0:
    if snakePosition[1] < raspberryPosition[1]:
    changeDirection = 'down'
    else:
    changeDirection = 'up'

    if (raspberryPosition[0]-BLOCK_W <= snakePosition[0] <= raspberryPosition[0]+BLOCK_W) and (raspberryPosition[1]-BLOCK_H <= snakePosition[1] <= raspberryPosition[1]+BLOCK_H):
    raspberrySpawned = 0
    else:
    snakeSegments.pop()
    if raspberrySpawned == 0:
    x = random.randrange(1,X_BOUNDARY/BLOCK_W)
    y = random.randrange(1,Y_BOUNDARY/BLOCK_H)
    raspberryPosition = [int(x*BLOCK_W),int(y*BLOCK_H)]
    EatCount += 1
    if SpeedTick< MaxSpeedTick:
    if (EatCount<=20):
    SpeedTick += 1
    print '1. EatCount:%d , SpeedTick:%d' %(EatCount,SpeedTick)
    elif EatCount>20 and EatCount<=60 and EatCount%2 == 0:
    SpeedTick += 1
    print '2. EatCount:%d , SpeedTick:%d' %(EatCount,SpeedTick)
    elif EatCount>60 and EatCount<=120 and EatCount%3 == 0:
    SpeedTick += 1
    print '3. EatCount:%d , SpeedTick:%d' %(EatCount,SpeedTick)
    elif EatCount>120 and EatCount<=300 and EatCount%5 == 0:
    SpeedTick += 1
    print '4. EatCount:%d , SpeedTick:%d' %(EatCount,SpeedTick)
    elif EatCount>300 and EatCount%10 == 0:
    SpeedTick += 1
    print '5. EatCount:%d , SpeedTick:%d' %(EatCount,SpeedTick)

    raspberrySpawned = 1
    playSurface.fill(blackColour)
    for position in snakeSegments:
    pygame.draw.rect(playSurface,whiteColour,Rect(position[0], position[1], BLOCK_W, BLOCK_H))
    pygame.draw.rect(playSurface,redColour,Rect(raspberryPosition[0], raspberryPosition[1], BLOCK_W, BLOCK_H))
    pygame.display.flip()

    #if snakePosition[0] > 620 or snakePosition[0] < 0:
    # gameOver()
    #if snakePosition[1] > 460 or snakePosition[1] < 0:
    # gameOver()
    for snakeBody in snakeSegments[1:]:
    if snakePosition[0] == snakeBody[0] and snakePosition[1] == snakeBody[1]:
    gameOver()
    fpsClock.tick(SpeedTick)

    recvfrom with timeout|日誌首頁|websocket上一篇recvfrom with timeout下一篇websocket
    回應