import pygame |
import sys |
import random |
import time |
# 颜色 |
redColor = pygame.Color( 255 , 0 , 0 ) # gameover |
whiteColor = pygame.Color( 255 , 255 , 255 ) # background |
blackColor = pygame.Color( 0 , 0 , 0 ) # score |
# Gameover函数 |
def gameover(play_surface, score): |
gameoverFont = pygame.font.SysFont( 'arial' , 54 ) |
gameoverSurf = gameoverFont.render( 'Game Over!' , True , redColor) |
gameoverRect = gameoverSurf.get_rect() |
gameoverRect.midtop = ( 320 , 10 ) |
play_surface.blit(gameoverSurf, gameoverRect) |
showScore(play_surface, score) |
pygame.display.flip() |
time.sleep( 4 ) |
pygame.quit() # 退出pygame |
sys.exit() # 退出系统 |
# 得分函数 |
def showScore(play_surface, score): |
scoreFont = pygame.font.SysFont( 'arial' , 34 ) |
scoreSurf = scoreFont.render( 'score : %s' % str (score), True , blackColor) |
scoreRect = scoreSurf.get_rect() |
scoreRect.midtop = ( 80 , 10 ) |
play_surface.blit(scoreSurf, scoreRect) |
# 主函数 |
def main(): |
# 初始化pygame |
pygame.init() |
fps_clock = pygame.time.Clock() |
# 设置窗口大小 |
play_surface = pygame.display.set_mode(( 640 , 480 )) |
pygame.display.set_caption( '贪吃蛇' ) |
# 初始化变量 |
snake_position = [ 100 , 100 ] |
snake_body = [[ 100 , 100 ], [ 80 , 100 ], [ 60 , 100 ]] |
target_position = [ 300 , 300 ] |
target_flag = 1 |
score = 0 |
# 确定方向 |
direction = 'right' |
change_direction = direction |
while True : |
for event in pygame.event.get(): |
if event. type = = pygame.QUIT: |
pygame.quit() |
sys.exit() |
elif event. type = = pygame.KEYDOWN: |
if event.key = = pygame.K_RIGHT: |
change_direction = 'right' |
if event.key = = pygame.K_LEFT: |
change_direction = 'left' |
if event.key = = pygame.K_UP: |
change_direction = 'up' |
if event.key = = pygame.K_DOWN: |
change_direction = 'down' |
# 判断是否输入的方向与当前方向冲突 |
if change_direction = = 'right' and not direction = = 'left' : |
direction = change_direction |
if change_direction = = 'left' and not direction = = 'right' : |
direction = change_direction |
if change_direction = = 'up' and not direction = = 'down' : |
direction = change_direction |
if change_direction = = 'down' and not direction = = 'up' : |
direction = change_direction |
if direction = = 'right' : |
snake_position[ 0 ] + = 20 |
if direction = = 'left' : |
snake_position[ 0 ] - = 20 |
if direction = = 'up' : |
snake_position[ 1 ] - = 20 |
if direction = = 'down' : |
snake_position[ 1 ] + = 20 |
# 增加蛇的长度 |
snake_body.insert( 0 , list (snake_position)) |
# 判断是否吃掉目标 |
if snake_position[ 0 ] = = target_position[ 0 ] and snake_position[ 1 ] = = target_position[ 1 ]: |
target_flag = 0 |
score + = 1 |
else : |
snake_body.pop() |
# 重新绘制目标 |
if target_flag = = 0 : |
x = random.randrange( 1 , 32 ) |
y = random.randrange( 1 , 24 ) |
target_position = [ int (x * 20 ), int (y * 20 )] |
target_flag = 1 |
# 绘制pygame窗口 |
play_surface.fill(whiteColor) |
for position in snake_body: |
pygame.draw.rect(play_surface, blackColor, pygame.Rect(position[ 0 ], position[ 1 ], 20 , 20 )) |
pygame.draw.rect(play_surface, redColor, pygame.Rect(target_position[ 0 ], target_position[ 1 ], 20 , 20 )) |
# 判断是否死亡 |
if snake_position[ 0 ] > 620 or snake_position[ 0 ] < 0 : |
gameover(play_surface, score) |
if snake_position[ 1 ] > 460 or snake_position[ 1 ] < 0 : |
gameover(play_surface, score) |
for snake_parts in snake_body[ 1 :]: |
if snake_position[ 0 ] = = snake_parts[ 0 ] and snake_position[ 1 ] = = snake_parts[ 1 ]: |
gameover(play_surface, score) |
showScore(play_surface, score) |
pygame.display.flip() |
fps_clock.tick( 5 ) |
if __name__ = = "__main__" : |
main() |