分享一个自己写的C++贪吃蛇
之前学C++时写的,希望大家喜欢!
部分核心代码:
void initiBoard() {
for(int i = 1; i<=15; i++) {
strcpy(Board[i],Boardiniti[i]); //把Boardiniti复制到Board中
}
}
void printBoard() {
system("cls");//清屏
for(int i = 1; i <= 15; i++) {
cout<<Board[i];
}
}
void changeBoard(int x,int y,int changeFlag) {
if(changeFlag==1) {
Board[y][x] = ' ';
} else if(changeFlag==3) {
Board[y][x] = 'X'; //■█□ 嘿嘿嘿
} else if(changeFlag==4) {
Board[y][x] = '*';
}
}
void changeiniBoard(int x,int y,int changeFlag) {
if(changeFlag == 1)
Boardiniti[y][x] = '*';
if(changeFlag == 2)
Boardiniti[y][x] = ' ';
}
void changeSnakeBody() {
for(int i = 0; i <=1; i++) {
for(int j = snakeEnd; j >=1; j--) {
snake[i][j] = snake[i][j-1];
}
}
}
void move() {
if(towards == 2) { //上
changeSnakeBody();
snake[1][0]++;
} else if(towards == 1) { //下
changeSnakeBody();
snake[1][0]--;
} else if(towards == 3) { //左
changeSnakeBody();
snake[0][0]--;
} else if(towards == 4) { //右
changeSnakeBody();
snake[0][0]++;
}
}
void getTowards() {
int twds=towards;
timeNow = GetTickCount();
while(GetTickCount()<timeNow+hard) {
if(kbhit()) {
char tows = getch();
if((tows == 'w'||tows == 'W')&&twds!=2) towards = 1;
else if((tows == 's'||tows == 'S')&&twds!=1) towards = 2;
else if((tows == 'a'||tows == 'A')&&twds!=4) towards = 3;
else if((tows == 'd'||tows == 'D')&&twds!=3) towards = 4;
}
}
}
void updataBoard() {
initiBoard();
for(int i = snakeEnd; i >=0; i--) {
changeBoard(snake[0][i],snake[1][i],3);
}
}
另外,此软件由 Dev-C++ 编写,源代码在压缩文件中的新建文件夹 project1.dev 文件中打开,没有Dev的同学也可使用压缩文件中的 贪吃蛇.cpp 文件编译(只是没有图标),感谢大家的支持! |