加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Snake.py 4.35 KB
一键复制 编辑 原始数据 按行查看 历史
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020/8/22 16:43
# @USER : Shengji He
# @File : Snake.py
# @Software: PyCharm
# @Version : Python-
# @TASK:
import pygame
from pygame.locals import *
import time
import random
import sys
RED_COLOR = pygame.Color(255, 0, 0)
BLACK_COLOR = pygame.Color(0, 0, 0)
WHITE_COLOR = pygame.Color(255, 255, 255)
GREY_COLOR = pygame.Color(150, 150, 150)
LIGHT_COLOR = pygame.Color(220, 220, 220)
def game_over(playSurface, score):
gameOverFont = pygame.font.Font('./algorithms/images/Arial.ttf', 72)
gameOverSurf = gameOverFont.render('Game Over', True, GREY_COLOR)
gameOverRect = gameOverSurf.get_rect()
playSurface.blit(gameOverSurf, gameOverRect)
scoreFont = pygame.font.Font('./algorithms/images/Arial.ttf', 48)
# scoreFont = pygame.font.Font('arial.ttf', 48)
scoreSurf = scoreFont.render('SCORE: ' + str(score), True, GREY_COLOR)
scoreRect = scoreSurf.get_rect()
scoreRect.midtop = (320, 225)
playSurface.blit(scoreSurf, scoreRect)
pygame.display.flip()
time.sleep(5)
pygame.quit()
sys.exit()
def App():
pygame.init()
fpsClock = pygame.time.Clock()
playSurface = pygame.display.set_mode((640, 480))
pygame.display.set_caption('Snake Game')
# img = pygame.image.load('game.ico')
# pygame.display.set_icon(img)
snakePosition = [100, 100]
snakeSegments = [[100, 100], [80, 100], [60, 100]]
raspberryPosition = [300, 300]
raspberrySpawned = 1
direction = 'right'
changeDirection = direction
score = 0
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
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 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] += 20
if direction == 'left':
snakePosition[0] -= 20
if direction == 'up':
snakePosition[1] -= 20
if direction == 'down':
snakePosition[1] += 20
snakeSegments.insert(0, list(snakePosition))
if snakePosition[0] == raspberryPosition[0] and snakePosition[1] == raspberryPosition[1]:
raspberrySpawned = 0
else:
snakeSegments.pop()
if raspberrySpawned == 0:
x = random.randrange(1, 32)
y = random.randrange(1, 24)
raspberryPosition = [int(x * 20), int(y * 20)]
raspberrySpawned = 1
score += 1
playSurface.fill(BLACK_COLOR)
for position in snakeSegments[1:]:
pygame.draw.rect(playSurface, WHITE_COLOR, Rect(position[0], position[1], 20, 20))
pygame.draw.rect(playSurface, LIGHT_COLOR, Rect(snakePosition[0], snakePosition[1], 20, 20))
pygame.draw.rect(playSurface, RED_COLOR, Rect(raspberryPosition[0], raspberryPosition[1], 20, 20))
pygame.display.flip()
if snakePosition[0] > 620 or snakePosition[0] < 0:
game_over(playSurface, score)
if snakePosition[1] > 460 or snakePosition[1] < 0:
game_over(playSurface, score)
for snakeBody in snakeSegments[1:]:
if snakePosition[0] == snakeBody[0] and snakePosition[1] == snakeBody[1]:
game_over(playSurface, score)
if len(snakeSegments) < 40:
speed = 6 + len(snakeSegments) // 4
else:
speed = 16
fpsClock.tick(speed)
if __name__ == '__main__':
App()
print('done')
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化