源代码:
代码语言:javascript复制import pygame
import sys
import random
# 初始化pygame
pygame.init()
# 设置窗口的大小,单位为像素
WIDTH,HEIGHT = 640, 480
screen = pygame.display.set_mode((WIDTH,HEIGHT))
# 设置窗口标题
pygame.display.set_caption('标题')
# 字体
my_font = pygame.font.SysFont("arial", 20)
my_font2 = pygame.font.SysFont("arial", 80)
CELLSIZE = 20
darkblue = (0,0,155)
blue = (0,0,255)
darkgreen = (0,155,0)
green = (0 ,255, 0)
darkred = (155,0,0)
red = (255,0,0)
darkyellow = (155,155,0)
yellow = (255,255,0)
darkgray = (40,40,40)
gray = (155,155,155)
white = (255, 255, 255)
food_x = random.randint(1,640//CELLSIZE-2)
food_y = random.randint(1,480//CELLSIZE-2)
startx = random.randint(5,640//CELLSIZE-6)
starty = random.randint(5,480//CELLSIZE-6)
snake_coords = [
{'x': startx, 'y': starty},
{'x': startx-1, 'y': starty},
{'x': startx-2, 'y': starty},
]
direction = 'right'
up = 'up'
down = 'down'
left = 'left'
right = 'right'
score = 0
is_over = False
# 时钟
clock = pygame.time.Clock()
# 程序主循环
while True:
# ---- 事件处理开始 ----
for event in pygame.event.get():
# 判断事件是否为退出事件
if event.type == pygame.QUIT:
# 退出pygame
pygame.quit()
# 退出系统
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
if direction != right:
direction = left
if event.key == pygame.K_RIGHT:
if direction != left:
direction = right
if event.key == pygame.K_UP:
if direction != down:
direction = up
if event.key == pygame.K_DOWN:
if direction != down:
direction = down
# ---- 事件处理结束 ----
#----- 逻辑处理开始-----
if direction == up:
new_head = {'x':snake_coords[0]['x'],'y':snake_coords[0]['y'] - 1 }
if direction == down:
new_head = {'x': snake_coords[0]['x'], 'y': snake_coords[0]['y'] 1}
if direction == left:
new_head = {'x':snake_coords[0]['x']-1,'y':snake_coords[0]['y'] }
if direction == right:
new_head = {'x': snake_coords[0]['x'] 1, 'y': snake_coords[0]['y']}
# 增加蛇头
snake_coords.insert(0,new_head)
# 删除蛇尾
snake_coords.pop()
# 检测是否吃到食物
if snake_coords[0]['x'] == food_x and snake_coords[0]['y'] == food_y:
food_x = random.randint(1, 640 // CELLSIZE - 2)
food_y = random.randint(1, 480 // CELLSIZE - 2)
score = score 1
snake_coords.insert(0, new_head)
# 设置穿墙
if snake_coords[0]['y'] < 0:
snake_coords[0]['y'] = HEIGHT//CELLSIZE
elif snake_coords[0]['y'] >= HEIGHT//CELLSIZE:
snake_coords[0]['y'] = 0
elif snake_coords[0]['x'] < 0:
snake_coords[0]['x'] = WIDTH//CELLSIZE
elif snake_coords[0]['x'] >= WIDTH//CELLSIZE:
snake_coords[0]['x'] = 0
# 检测是否头有碰到身体
if snake_coords[0] in snake_coords[3:]:
is_over = True
# ----逻辑处理结束------
# ----- 绘制处理开始-----
# 设置屏幕背景
screen.fill((0,0,0))
# 画网格
for x in range(0,WIDTH,CELLSIZE):
pygame.draw.line(screen,darkgray,(x,0),(x,HEIGHT))
for y in range(0,HEIGHT,CELLSIZE):
pygame.draw.line(screen,darkgray,(0,y),(WIDTH,y))
# 画食物
pygame.draw.rect(screen,red,(food_x*CELLSIZE,food_y*CELLSIZE,CELLSIZE,CELLSIZE))
# 画蛇
for coord in snake_coords:
pygame.draw.rect(screen, darkyellow, (coord['x'] * CELLSIZE, coord['y'] * CELLSIZE, CELLSIZE, CELLSIZE))
pygame.draw.rect(screen,yellow, (coord['x'] * CELLSIZE 4, coord['y'] * CELLSIZE 4, CELLSIZE-8, CELLSIZE-8))
# 设置蛇头不同的颜色
pygame.draw.rect(screen, darkgreen, (snake_coords[0]['x'] * CELLSIZE, snake_coords[0]['y'] * CELLSIZE, CELLSIZE, CELLSIZE))
pygame.draw.rect(screen, green, (snake_coords[0]['x'] * CELLSIZE 4, snake_coords[0]['y'] * CELLSIZE 4, CELLSIZE - 8, CELLSIZE - 8))
# 画分数
textImage = my_font.render("Score:" str(score), True, red)
screen.blit(textImage, (540, 30))
# 画失败界面
if is_over:
while True:
textImage = my_font2.render("Game Over", True, red)
screen.blit(textImage, (150, 200))
pygame.display.update()
# ----绘制处理结束------
# 刷新屏幕
pygame.display.update()
# 游戏帧率
clock.tick(10)