c++游戏编程代码大全(c语言程序游戏代码大全)

c++游戏编程代码大全(c语言程序游戏代码大全)

百科常识打吡咯2022-06-28 9:27:0256A+A-

序言

很多学过C语言的朋友都说已经学完了C语言入门,但是实际能力还处于很低的水平。基本上就是他们设置几个for循环,暴力解决排列组合问题的关卡。很多人基本都无法独立编写一个小程序。今天给大家举一个我很久以前做过的简单的吃蛇案例。

这一次编写控制台蛇程序,对于学过C语言没有做过项目的小伙伴来说,可能是一个不小的挑战。

本文中蛇案用到的东西不多,游戏的实现主要是按照一定的逻辑对一个二维数组进行修改和变换(实际操作中,我使用了字符串来减少闪烁)。这里就不重复编写过程了,主要说一下最基本的函数的逻辑,还有一些以前很少用到的函数。

一、 基本功能逻辑

1、游戏的背景、打印

定义一个二维字符串,用“”和空格表示边界,蛇形体,空格等。打印是用for循环遍历整个字符串,并以一定的频率刷新,可以达到游戏效果。

2、建立蛇数组

考虑到我没做过链表,也不是很熟练,所以用array做蛇。数组有一些缺点,比如容量有限,需要先定义最长长度(只要我设置的足够长hhhh),很多地方需要获取地址(敲n次)。存储阵列蛇的结数、XY坐标、移动方向等参数。需要注意的是,“”占用了两个字节,很多地方写坐标都要乘以二。

3、生成蛇的随机坐标

首先,种植随机种子,使用系统时间作为种子。两个变量x和y被定义为坐标值,rand()函数用于获得所需的坐标值范围。那么最初将生成两个或三个部分。

4、把蛇画到地图上

构建一个for循环遍历整条蛇,使用strncpy()函数将空白部分复制到" "。

5、蛇的运动

我被困在这里很久了。期间去玩过饕餮蛇,发现蛇的移动方式并不是很复杂。可以说是蛇尾去了一个,蛇头加了一个。我采用了整个蛇身前移,蛇头分开处理的方法,也便于以后的方向控制。

6、擦除运动轨迹

写到最后一步你会发现蛇越来越长。就像崩溃后的鼠标光标一样。因为虽然前一个节点的属性赋给了后一个节点,但是这个节点并没有改变。所以每次锻炼前都要把蛇擦掉。方法与第四步相同,只是“”用两个空格代替。

7、蛇改变方向

由于蛇运动的特殊性,只需要对蛇头进行处理。用GetAsyncKeyState()函数读取键盘输入,注意防止snake被附加条件调头。

8、生成食物

随机坐标,复印打印。

9、蛇吃食物长长

蛇移动到食物的地方会把食物盖住,吃到食物的效果就不用写了。只需要判断蛇头坐标和食物坐标的重合,然后判断移动的方向就可以确定在哪里加一段了。然后用一个布尔值来判断场上是否有食物,从而生成新的食物。分数也可以写在这里。

网络效果图

代码如下:

# DEFINE _ CRT _ SECURE _ NO _ WARNINGS 1 # include stdio . h # include stdlib . h # include math . h # include conio . h # include time . h # include windows . h # DEFINE MAXWIDTH 30 # DEFINE MAX HEIGHT 30 # DEFINIT l EN3//snake结构的初始长度{

char * chint颜色;字符类型;

}

Charborder={ " ",4,1},//border charbg={ " ",2,2},//background charsnake={ " ",0xe,3},//饕餮蛇节点charfood={ " ",0xc,4 };//Food//用数组结构保存地图中的每个点结构{

字符类型;int索引;

} global map[MAXWIDTH][max height];结构{

int x;int y;

} snake map[(max width2)*(max height2)],scoresPostionint分数=0;//score int snake maplen=(maxwidth2)*(max height2);int headerIndex,tailIndex

处理hStdin

//设置光标位置,其中x是行,y是列空集。

Position(int x, int y){

COORD coord;

coord.X = 2 * y;

coord.Y = x;

SetConsoleCursorPosition(hStdin, coord);

}// 设置颜色void setColor(int color){

SetConsoleTextAttribute(hStdin, color);

}//创建食物void createFood(){ int index, rang, x, y;

srand((unsigned)time(NULL)); if (tailIndex<headerIndex){

rang = headerIndex – tailIndex – 1;

index = rand() % rang + tailIndex + 1;

} else{

rang = snakeMapLen – (tailIndex – headerIndex + 1);

index = rand() % rang; if (index >= headerIndex){

index += (tailIndex – headerIndex + 1);

}

}

x = snakeMap[index].x;

y = snakeMap[index].y;

setPosition(x, y);

setColor(charFood.color); printf(“%s”, charFood.ch);

globalMap[x][y].type = charFood.type;

}//死了void die(){ int xCenter = MAXHEIGHT % 2 == 0 ? MAXHEIGHT / 2 : MAXHEIGHT / 2 + 1; int yCenter = MAXWIDTH % 2 == 0 ? MAXWIDTH / 2 : MAXWIDTH / 2 + 1;

setPosition(xCenter, yCenter – 5);

setColor(0xC); exit(1);

_getch(); exit(0);

}// 蛇移动void move(char direction){ int newHeaderX, newHeaderY; //新蛇头的坐标

int newHeaderPreIndex; //新蛇头坐标以前对应的索引

int newHeaderPreX, newHeaderPreY; //新蛇头的索引以前对应的坐标

int newHeaderPreType; //新蛇头以前的类型

int oldTailX, oldTailY; //老蛇尾坐标

switch (direction){ case ‘w’:

newHeaderX = snakeMap[headerIndex].x – 1;

newHeaderY = snakeMap[headerIndex].y; break; case ‘s’:

newHeaderX = snakeMap[headerIndex].x + 1;

newHeaderY = snakeMap[headerIndex].y; break; case ‘a’:

newHeaderX = snakeMap[headerIndex].x;

newHeaderY = snakeMap[headerIndex].y – 1; break; case ‘d’:

newHeaderX = snakeMap[headerIndex].x;

newHeaderY = snakeMap[headerIndex].y + 1; break;

}

headerIndex = headerIndex == 0 ? snakeMapLen – 1 : headerIndex – 1;

newHeaderPreIndex = globalMap[newHeaderX][newHeaderY].index;

newHeaderPreX = snakeMap[headerIndex].x;

newHeaderPreY = snakeMap[headerIndex].y;

snakeMap[headerIndex].x = newHeaderX;

snakeMap[headerIndex].y = newHeaderY;

globalMap[newHeaderX][newHeaderY].index = headerIndex;

snakeMap[newHeaderPreIndex].x = newHeaderPreX;

snakeMap[newHeaderPreIndex].y = newHeaderPreY;

globalMap[newHeaderPreX][newHeaderPreY].index = newHeaderPreIndex; //新蛇头以前的类型

newHeaderPreType = globalMap[newHeaderX][newHeaderY].type; //设置新蛇头类型

globalMap[newHeaderX][newHeaderY].type = charSnake.type; // 判断是否出界或撞到自己

if (newHeaderPreType == charBorder.type || newHeaderPreType == charSnake.type){

die();

} //输出新蛇头

setPosition(newHeaderX, newHeaderY);

setColor(charSnake.color); printf(“%s”, charSnake.ch); //判断是否吃到食物

if (newHeaderPreType == charFood.type){ //吃到食物

createFood(); //更改分数

setPosition(scoresPostion.x, scoresPostion.y); printf(“%d”, ++scores);

} else{ //老蛇尾坐标

oldTailX = snakeMap[tailIndex].x;

oldTailY = snakeMap[tailIndex].y; //删除蛇尾

setPosition(oldTailX, oldTailY);

setColor(charBg.color); printf(“%s”, charBg.ch);

globalMap[oldTailX][oldTailY].type = charBg.type;

tailIndex = (tailIndex == 0) ? snakeMapLen – 1 : tailIndex – 1;

}

}//下次移动的方向char nextDirection(char ch, char directionOld){ int sum = ch + directionOld;

ch = tolower(ch); if ((ch == ‘w’ || ch == ‘a’ || ch == ‘s’ || ch == ‘d’) && sum != 197 && sum != 234){ return ch;

} else{ return directionOld;

}

}//暂停char pause(){ return _getch();

}// 初始化void init(){ // 设置相关变量

int x, y, index; int xCenter = MAXHEIGHT % 2 == 0 ? MAXHEIGHT / 2 : MAXHEIGHT / 2 + 1; int yCenter = MAXWIDTH % 2 == 0 ? MAXWIDTH / 2 : MAXWIDTH / 2 + 1;

CONSOLE_CURSOR_INFO cci; //控制台光标信息

//判断相关设置是否合理

if (MAXWIDTH<16){ printf(“‘MAXWIDTH’ is too small!”);

_getch(); exit(0);

} //设置窗口大小

system(“mode con: cols=96 lines=32”); //隐藏光标

hStdin = GetStdHandle(STD_OUTPUT_HANDLE);

GetConsoleCursorInfo(hStdin, &cci);

cci.bVisible = 0;

SetConsoleCursorInfo(hStdin, &cci); //打印背景

for (x = 0; x<MAXHEIGHT; x++){ for (y = 0; y<MAXWIDTH; y++){ if (y == 0 || y == MAXWIDTH – 1 || x == 0 || x == MAXHEIGHT – 1){

globalMap[x][y].type = charBorder.type;

setColor(charBorder.color); printf(“%s”, charBorder.ch);

} else{

index = (x – 1)*(MAXWIDTH – 2) + (y – 1);

snakeMap[index].x = x;

snakeMap[index].y = y;

globalMap[x][y].type = charBg.type;

globalMap[x][y].index = index;

setColor(charBg.color); printf(“%s”, charBg.ch);

}

} printf(“n”);

} //初始化贪吃蛇

globalMap[xCenter][yCenter – 1].type = globalMap[xCenter][yCenter].type = globalMap[xCenter][yCenter + 1].type = charSnake.type;

headerIndex = (xCenter – 1)*(MAXWIDTH – 2) + (yCenter – 1) – 1;

tailIndex = headerIndex + 2;

setPosition(xCenter, yCenter – 1);

setColor(charSnake.color); for (y = yCenter – 1; y <= yCenter + 1; y++){ printf(“%s”, charSnake.ch);

} //生成食物

createFood(); //设置程序信息

setPosition(xCenter – 1, MAXWIDTH + 2); printf(” 得分 : 0″);

setPosition(xCenter, MAXWIDTH + 2); printf(” 姓名班级 :33班杨超”);

scoresPostion.x = xCenter – 1;

scoresPostion.y = MAXWIDTH + 8;

}int main(){ char charInput, direction = ‘a’;

init();

charInput = tolower(_getch());

direction = nextDirection(charInput, direction); while (1){ if (_kbhit()){

charInput = tolower(_getch()); if (charInput == ‘ ‘){

charInput = pause();

}

direction = nextDirection(charInput, direction);

}

move(direction);

Sleep(500);

}

_getch(); return 0;

}

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

点击这里复制本文地址 版权声明:本文内容由网友提供,该文观点仅代表作者本人。本站(https://www.angyang.net.cn)仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

昂扬百科 © All Rights Reserved.  渝ICP备2023000803号-3网赚杂谈