1.要安装的库
按住win+r输入cmd 回车确定
再输入 pip install pygame 下载pygame
2.源码
# 玩家一(左边的)用w s a d控制
# 玩家二(右边的)用↑ ↓ ← →控制
# 导入模块
import pygame
import sys
import random
# 主程序
# 颜色变量
LIGHT_BLUE = [30, 144, 255]
BLUE = [0, 0, 255]
DARK_BLUE=[25,25,112]
GREEN = [0, 255, 0]
LIGHT_GREEN = [189, 252, 210]
DARK_GREEN=[34,139,34]
RED = [255, 0, 0]
LIGHT_RED = [250, 128, 114]
WHITE = [255, 255, 255]
BLACK = [0, 0, 0]
# 速度变量
time_clock = pygame.time.Clock()
# 创建窗口
screen = pygame.display.set_mode((800, 800))
pygame.display.set_caption('贪吃蛇大作战')
# 初始化两条蛇
# 坐标
snake_head = [40, 0]
snake_body = [[40, 0], [20, 0], [0, 0]]
# 方向
direction = 'right'
new_direction = direction
# 复活
resurrection = False
resurrection_time = 25
resurrection_num = 0
# 坐标
snake_head1 = [740, 0]
snake_body1 = [[740, 0], [760, 0], [780, 0]]
# 方向
direction1 = 'left'
new_direction1 = direction1
# 复活
resurrection1 = False
resurrection_time1 = 25
resurrection_num1 = 0
# 初始化食物
# 坐标
food_position = [[400, 400]]
x = 400
y = 400
# 生产食物的时间
food_time = 25
# 要删除的项目
del_food_position = []
# 食物是否要生成
food_num = 1
# 得分变量
score = 0
score1 = 0
pygame.init()
# 函数
# 显示字
def draw_font(type, size, font, Anti_Aliasing, RGB, x, y, screen):
my_font = pygame.font.SysFont(type, size)
author = my_font.render(font, Anti_Aliasing, RGB)
screen.blit(author, (x, y))
# 循环
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_UP:
direction1 = 'up'
if event.key == pygame.K_DOWN:
direction1 = 'down'
if event.key == pygame.K_LEFT:
direction1 = 'left'
if event.key == pygame.K_RIGHT:
direction1 = 'right'
if event.key == pygame.K_w: