给我写一个贪吃蛇的代码
时间: 2023-09-10 19:05:54 浏览: 124
### 回答1:
好的,以下是一个简单的贪吃蛇游戏代码:
```
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
bool gameOver;
const int width = 20;
const int height = 20;
int x, y, fruitX, fruitY, score;
int tailX[100], tailY[100];
int nTail;
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
eDirection dir;
void Setup()
{
gameOver = false;
dir = STOP;
x = width / 2;
y = height / 2;
fruitX = rand() % width;
fruitY = rand() % height;
score = 0;
}
void Draw()
{
system("cls");
for (int i = 0; i < width+2; i++)
cout << "#";
cout << endl;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (j == 0)
cout << "#";
if (i == y && j == x)
cout << "O";
else if (i == fruitY && j == fruitX)
cout << "F";
else
{
bool print = false;
for (int k = 0; k < nTail; k++)
{
if (tailX[k] == j && tailY[k] == i)
{
cout << "o";
print = true;
}
}
if (!print)
cout << " ";
}
if (j == width - 1)
cout << "#";
}
cout << endl;
}
for (int i = 0; i < width+2; i++)
cout << "#";
cout << endl;
cout << "Score:" << score << endl;
}
void Input()
{
if (_kbhit())
{
switch (_getch())
{
case 'a':
dir = LEFT;
break;
case 'd':
dir = RIGHT;
break;
case 'w':
dir = UP;
break;
case 's':
dir = DOWN;
break;
case 'x':
gameOver = true;
break;
}
}
}
void Logic()
{
int prevX = tailX[0];
int prevY = tailY[0];
int prev2X, prev2Y;
tailX[0] = x;
tailY[0] = y;
for (int i = 1; i < nTail; i++)
{
prev2X = tailX[i];
prev2Y = tailY[i];
tailX[i] = prevX;
tailY[i] = prevY;
prevX = prev2X;
prevY = prev2Y;
}
switch (dir)
{
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case UP:
y--;
break;
case DOWN:
y++;
break;
default:
break;
}
if (x >= width) x = 0; else if (x < 0) x = width - 1;
if (y >= height) y = 0; else if (y < 0) y = height - 1;
for (int i = 0; i < nTail; i++)
if (tailX[i] == x && tailY[i] == y)
gameOver = true;
if (x == fruitX && y == fruitY)
{
score += 10;
fruitX = rand() % width;
fruitY = rand() % height;
nTail++;
}
}
int main()
{
Setup();
while (!gameOver)
{
Draw();
Input();
Logic();
Sleep(50);
}
return 0;
}
```
该代码使用了Windows.h头文件中的Sleep函数,所以该代码仅能在Windows环境下运行。如果你在其他环境下使用该代码,可能会遇到一些问题,需要对代码进行修改。
### 回答2:
贪吃蛇是一种经典的游戏,下面是一个简单的贪吃蛇代码示例:
```python
import pygame
import random
# 初始化pygame库
pygame.init()
# 定义窗口的宽度和高度
window_width = 800
window_height = 600
# 创建窗口
window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption("贪吃蛇游戏")
# 定义颜色
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
# 定义贪吃蛇的初始位置和大小
snake_size = 20
snake_x = 300
snake_y = 300
# 定义贪吃蛇在x和y轴上的移动速度
snake_x_speed = 0
snake_y_speed = 0
# 定义食物的初始位置和大小
food_size = 20
food_x = random.randint(snake_size, window_width - snake_size)
food_y = random.randint(snake_size, window_height - snake_size)
# 用于游戏主循环的变量
running = True
# 游戏主循环
while running:
# 事件循环
for event in pygame.event.get():
# 检测到退出事件
if event.type == pygame.QUIT:
running = False
# 检测到键盘按下事件
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
# 左箭头按下,速度减小
snake_x_speed = -snake_size
snake_y_speed = 0
elif event.key == pygame.K_RIGHT:
# 右箭头按下,速度增大
snake_x_speed = snake_size
snake_y_speed = 0
elif event.key == pygame.K_UP:
# 上箭头按下,速度减小
snake_x_speed = 0
snake_y_speed = -snake_size
elif event.key == pygame.K_DOWN:
# 下箭头按下,速度增大
snake_x_speed = 0
snake_y_speed = snake_size
# 更新贪吃蛇的位置
snake_x += snake_x_speed
snake_y += snake_y_speed
# 绘制贪吃蛇和食物
window.fill(white)
pygame.draw.rect(window, green, (snake_x, snake_y, snake_size, snake_size))
pygame.draw.rect(window, red, (food_x, food_y, food_size, food_size))
pygame.display.update()
# 检测贪吃蛇是否碰到边界或食物
if (snake_x < 0 or snake_x >= window_width or snake_y < 0 or snake_y >= window_height) or \
(snake_x == food_x and snake_y == food_y):
# 重置食物位置
food_x = random.randint(snake_size, window_width - snake_size)
food_y = random.randint(snake_size, window_height - snake_size)
# 设置游戏的帧率
pygame.time.Clock().tick(10)
# 退出游戏
pygame.quit()
```
这个代码中,我们使用pygame库来创建窗口和处理用户输入。游戏主循环中,我们监听键盘事件,根据用户的输入来改变贪吃蛇的移动方向。游戏中使用矩形来表示贪吃蛇和食物,根据移动速度来更新贪吃蛇的位置,并在窗口中绘制贪吃蛇和食物。当贪吃蛇碰到边界或者吃到食物时,更新食物的位置并重绘窗口。通过设置帧率来控制游戏的速度。最后,pygame.quit()用于退出游戏。
### 回答3:
下面是一个简单的贪吃蛇游戏的代码:
```python
import pygame
import random
# 初始化游戏
pygame.init()
# 设置游戏窗口的大小
window_width, window_height = 800, 600
window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption("贪吃蛇游戏")
# 定义颜色
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
# 定义蛇的初始位置和大小
snake_x, snake_y = window_width // 2, window_height // 2
snake_size = 10
# 定义移动的步长
snake_speed_x = 0
snake_speed_y = 0
# 初始化食物的位置
food_x = random.randint(0, window_width - snake_size) // 10 * 10
food_y = random.randint(0, window_height - snake_size) // 10 * 10
# 计分系统
score = 0
score_font = pygame.font.Font(None, 36)
# 游戏主循环
running = True
while running:
# 将窗口设置为白色
window.fill(white)
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 监听按键事件
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
snake_speed_x = -snake_size
snake_speed_y = 0
elif event.key == pygame.K_RIGHT:
snake_speed_x = snake_size
snake_speed_y = 0
elif event.key == pygame.K_UP:
snake_speed_y = -snake_size
snake_speed_x = 0
elif event.key == pygame.K_DOWN:
snake_speed_y = snake_size
snake_speed_x = 0
# 更新蛇的位置
snake_x += snake_speed_x
snake_y += snake_speed_y
# 绘制食物
pygame.draw.rect(window, red, [food_x, food_y, snake_size, snake_size])
# 绘制蛇
pygame.draw.rect(window, green, [snake_x, snake_y, snake_size, snake_size])
# 判断是否吃到食物
if snake_x == food_x and snake_y == food_y:
score += 1
# 食物重新生成
food_x = random.randint(0, window_width - snake_size) // 10 * 10
food_y = random.randint(0, window_height - snake_size) // 10 * 10
# 绘制分数
score_text = score_font.render("得分:" + str(score), True, (0, 0, 0))
window.blit(score_text, (10, 10))
# 更新窗口
pygame.display.update()
# 退出游戏
pygame.quit()
```
这个代码使用 Pygame 库创建了一个简单的贪吃蛇游戏。在游戏中,蛇可以通过方向键控制移动,当蛇吃到食物时,得分增加并重新生成食物。游戏窗口会实时显示得分和蛇的位置。希望这个代码能满足你的需求!