python贪吃蛇双人版(有问题放在评论区)

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:
    
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值