提交 6929d029 编写于 作者: U u010663691

Wed Sep 6 19:07:00 CST 2023 inscode

上级 450a45da
run = "pip install -r requirements.txt;python3 main.py" run = "pip install -r requirements.txt;python3 main.py"
language = "python"
[env] [env]
VIRTUAL_ENV = "/root/${PROJECT_DIR}/venv" VIRTUAL_ENV = "/root/${PROJECT_DIR}/venv"
...@@ -7,3 +8,6 @@ PYTHONPATH = "$PYTHONHOME/lib/python3.10:${VIRTUAL_ENV}/lib/python3.10/site-pack ...@@ -7,3 +8,6 @@ PYTHONPATH = "$PYTHONHOME/lib/python3.10:${VIRTUAL_ENV}/lib/python3.10/site-pack
REPLIT_POETRY_PYPI_REPOSITORY = "http://mirrors.csdn.net.cn/repository/csdn-pypi-mirrors/simple" REPLIT_POETRY_PYPI_REPOSITORY = "http://mirrors.csdn.net.cn/repository/csdn-pypi-mirrors/simple"
MPLBACKEND = "TkAgg" MPLBACKEND = "TkAgg"
POETRY_CACHE_DIR = "/root/${PROJECT_DIR}/.cache/pypoetry" POETRY_CACHE_DIR = "/root/${PROJECT_DIR}/.cache/pypoetry"
[debugger]
program = "main.py"
import pygame #!/usr/bin/python
import sys # -*- coding: UTF-8 -*-
import time
import random
color_red = pygame.Color(255, 0, 0) a = 21
color_white = pygame.Color(255, 255, 255) b = 10
color_green = pygame.Color(0, 255, 0) c = 0
pygame.init()
screen = pygame.display.set_mode((600, 400))
screen.fill(color_white)
pygame.display.set_caption("贪吃蛇小游戏")
arr = [([0] * 41) for i in range(61)] # 创建一个二维数组
x = 10 # 蛇的初始x坐标
y = 10 # 蛇的初始y坐标
foodx = random.randint(1, 60) # 食物随机生成的x坐标
foody = random.randint(1, 40) # 食物随机生成的y坐标
arr[foodx][foody] = -1
snake_lon = 3 # 蛇的长度
way = 1 # 蛇的运动方向
c = a + b
print("1 - c 的值为:", c)
while True: c = a - b
screen.fill(color_white) print("2 - c 的值为:", c)
time.sleep(0.1)
for event in pygame.event.get(): # 监听器 c = a * b
if event.type == pygame.QUIT: print("3 - c 的值为:", c)
sys.exit()
elif event.type == pygame.KEYDOWN: c = a // b
if (event.key == pygame.K_RIGHT) and (way != 2): # 向右移动且避免反向移动 print("4 - c 的值为:", c)
way = 1
if (event.key == pygame.K_LEFT) and (way != 1): # 向左移动且避免反向移动 c = a % b
way = 2 print("5 - c 的值为:", c)
if (event.key == pygame.K_UP) and (way != 4): # 向上移动且避免反向移动
way = 3 # 修改变量 a 、b 、c
if (event.key == pygame.K_DOWN) and (way != 3): # 向下移动且避免反向移动 a = 2
way = 4 b = 3
if way == 1: c = a**b
x += 1 print("6 - c 的值为:", c)
if way == 2:
x -= 1 a = 10
if way == 3: b = 5
y -= 1 c = a // b
if way == 4: print("7 - c 的值为:", c)
y += 1
if (x > 60) or (y > 40) or (x < 1) or (y < 1) or (arr[x][y] > 0): # 判断死亡(撞墙或自食)
sys.exit()
arr[x][y] = snake_lon
for a, b in enumerate(arr, 1):
for c, d in enumerate(b, 1):
# 在二维数组中,食物为-1,空地为0,蛇的位置为正数
if (d > 0):
# print(a,c) #输出蛇的当前坐标
arr[a - 1][c - 1] = arr[a - 1][c - 1] - 1
pygame.draw.rect(screen, color_green, ((a - 1) * 10, (c - 1) * 10, 10, 10))
if (d < 0):
pygame.draw.rect(screen, color_red, ((a - 1) * 10, (c - 1) * 10, 10, 10))
if (x == foodx) and (y == foody): # 蛇吃到食物
snake_lon += 1 # 长度+1
while (arr[foodx][foody] != 0): # 刷新食物
foodx = random.randint(1, 60)
foody = random.randint(1, 40)
arr[foodx][foody] = -1
pygame.display.update()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册