贪吃蛇c++程序代码
时间: 2023-12-11 12:04:10 浏览: 42
这是一个贪吃蛇的C++程序代码,包括游戏初始化、游戏循环和结束游戏等函数。游戏开始时会显示欢迎信息和游戏规则,玩家需要使用方向键控制蛇的移动,加速和减速可以得到更高的分数。当蛇撞到墙壁或咬到自己时,游戏结束并显示得分。程序中使用了一些系统命令和自定义函数,如Pos()函数用于设置光标位置,creatMap()函数用于创建游戏地图,initsnake()函数用于初始化蛇的位置和长度,createfood()函数用于生成食物。以下是程序中的一些函数和命令:
```c++
system("title c语言研究中心 www.dotcpp.com"); //设置窗口标题
printf("欢迎来到贪食蛇游戏!"); //显示欢迎信息
Pos(40,25); //设置光标位置
system("pause"); //暂停程序
system("cls"); //清屏
Pos(25,12); printf("用↑.↓.←.→分别控制蛇的移动, F1 为加速,2 为减速\n"); //显示游戏规则
Pos(25,13); printf("加速将能得到更高的分数。\n");
exit(0); //退出程序
system("mode con cols=100 lines=30"); //设置窗口大小
welcometogame(); //显示欢迎信息
creatMap(); //创建游戏地图
initsnake(); //初始化蛇的位置和长度
createfood(); //生成食物
gamecircle(); //游戏循环
```
相关问题
贪吃蛇c++语言程序代码
以下是一个简单的贪吃蛇C++语言程序代码:
```cpp
#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 || y > height || y < 0)
gameOver = true;
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;
}
```
c++编程贪吃蛇代码
贪吃蛇是一个经典的编程练习项目,下面是一个简单的C语言版贪吃蛇代码示例:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define WIDTH 20 // 地图宽度
#define HEIGHT 20 // 地图高度
int tailX[100]; // 蛇身坐标x
int tailY[100]; // 蛇身坐标y
int countTail = 0; // 蛇身长度
int fruitX, fruitY; // 食物坐标
int score; // 得分
int gameOver; // 游戏结束
int x, y; // 蛇头坐标
int dir; // 蛇头移动的方向
void Setup() {
gameOver = 0;
x = WIDTH / 2;
y = HEIGHT / 2;
dir = 1;
fruitX = rand() % WIDTH;
fruitY = rand() % HEIGHT;
score = 0;
}
void Draw() {
system("cls");
for (int i = 0; i < WIDTH; i++)
printf("#");
printf("\n");
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (j == 0 || j == WIDTH - 1)
printf("#");
else if (i == y && j == x)
printf("O");
else if (i == fruitY && j == fruitX)
printf("F");
else {
int isTail = 0;
for (int k = 0; k < countTail; k++) {
if (tailX[k] == j && tailY[k] == i) {
printf("o");
isTail = 1;
}
}
if (!isTail)
printf(" ");
}
}
printf("\n");
}
for (int i = 0; i < WIDTH; i++)
printf("#");
printf("\n");
printf("Score: %d\n", score);
}
void Input() {
if (_kbhit()) {
switch (_getch()) {
case 'w':
dir = 0;
break;
case 'a':
dir = 1;
break;
case 's':
dir = 2;
break;
case 'd':
dir = 3;
break;
case 'x':
gameOver = 1;
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 < countTail; i++) {
prev2X = tailX[i];
prev2Y = tailY[i];
tailX[i] = prevX;
tailY[i] = prevY;
prevX = prev2X;
prevY = prev2Y;
}
switch (dir) {
case 0:
y--;
break;
case 1:
x--;
break;
case 2:
y++;
break;
case 3:
x++;
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 < countTail; i++) {
if (tailX[i] == x && tailY[i] == y) {
gameOver = 1;
break;
}
}
if (x == fruitX && y == fruitY) {
score += 10;
countTail++;
fruitX = rand() % WIDTH;
fruitY = rand() % HEIGHT;
}
}
int main() {
Setup();
while (!gameOver) {
Draw();
Input();
Logic();
Sleep(10); // 控制游戏速度
}
return 0;
}
这是一个简单的贪吃蛇游戏代码,使用C语言编写。在运行程序后,玩家可以通过键盘上的"W"、"A"、"S"、"D"来控制蛇的移动方向,目标是吃掉地图上的食物。每次吃到食物,蛇的长度会增加,并且得分会增加。如果蛇头碰到地图边界或者自己的身体,游戏会结束。游戏中使用了Windows下的函数来实现定时器以及控制台UI的更新显示。希望这个简单的代码示例能够帮助到你。