程序员大佬直播C语言写个贪吃蛇游戏练手,如此简单,你会做了吗
用C语言完成简单经典小游戏——贪吃蛇。出格适合新手熟谙CC语言。
首要触及C语言常识点以下:
*布局体,函数的定义及调用,指针变量,指针和数组,逻辑表达式,根基的选择和轮回语句,头文件的编写等*。
可以说是麻雀虽小,五脏俱全,是新手演习C措辞的绝佳小项目!
游戏界面以下:
根基思路:
蛇每吃一个食品蛇身子就增加一格,用WASD节制蛇头的勾当,而蛇身子跟着蛇头走,每后一格蛇身子下一步走到上一格蛇身子的位置,以此类推。
法度典型首要变量以下:
```
#define M 20//全数图形界面的长和宽
#define N 60
struct snake a[(M - 2)*(N - 2)];//蛇身数组
int snake_x =4;//蛇身的XY坐标
int snake_y =4;
int X = 1;//节制蛇头的偏向量
int Y = 0;
int food_x , food_y ;//食品的XY坐标
int score = 0;//分数
```
食品的随机位置产生是经过过程两个随机数辨别代表X,Y坐标完成的。
```
void food(int *x, int *y,int *fx,int *fy,int *s, struct snake *snake)
{
int ffx, ffy;//上一次食品的XY坐标
ffx = *fx;
ffy = *fy;
if (*x == *fx && *y == *fy)//假定吃到了食品,产生下一个食品
{
do {
*fx = 1 + rand() % (N - 3);
*fy = 1 + rand() % (M - 3);
} while (ffx == *fx && ffy == *fy);//包管与前次食品位置不合
for (int i= (*s); i >= 0; i--)
{
if ((snake + *s)->snake_x == *fx && (snake + *s)->snake_y == *fy) {
*fx = 1 + rand() % (N - 3);
*fy = 1 + rand() % (M - 3);
}
}//大年夜大年夜概率包管食品与蛇身子的位置不合(不克不及完全包管)
(*s)++ ; //分数加一!!!!!!!
}
}
```
用WASD节制蛇头的勾当是经过过程改动蛇头的X,Y坐标完成的。
```
void control(int *x,int *y,int *X,int *Y)//xy是蛇头的坐标,XY是节制勾当偏向的量
{
if (_kbhit())
{
switch (_getch())
{
case 'w' :
case 'W' :
if(interf[*y - 1][*x]!='@'){ //if语句包管蛇不克不及倒着走
*X = 0; *Y = -1;
}
break;
case 'd':
case 'D':
if(interf[*y][*x + 1] != '@'){
*X = 1; *Y = 0;
}
break;
case 's':
case 'S':
if (interf[*y + 1][*x] != '@') {
*X = 0; *Y = 1;
}
break;
case 'a':
case 'A':
if (interf[*y][*x - 1] != '@') {
*X = -1; *Y = 0;
}
break;
default:
break;
}
}
*x = *x + *X;//改动一次位置
*y = *y + *Y;
}
```
用定义蛇身子的布局体储藏身安身子的XY坐标,然后定义蛇身子布局体数组存储每节蛇身子。
```
struct snake
{
int number;
int snake_x;
int snake_y;
//struct snake *next;
};
struct snake a[(M - 2)*(N - 2)];
void build_snake(struct snake *snake,int s,int *sx,int *sy)//sx,sy蛇头的位置
{
for (int i = s; i >= 0; i--)
{
if (i == 0) {
(snake + i)->number = i;
(snake + i)->snake_x = *sx;
(snake + i)->snake_y = *sy;
}else{
(snake + i)->number = i;
(snake + i)->snake_x = (snake + i - 1)->snake_x;
(snake + i)->snake_y = (snake + i - 1)->snake_y;
}
}
}
```
有了蛇身子和食品的坐标位置,将它们一路存储在一个暗示全数界面的二位数组,然后轮回打印全数数组构成动画成果!
```
char interf[M][N];
void newinterface(struct snake *snake, int fx, int fy,int s)
{
int x, y;
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
if (i == 0 || i == M - 1)
interf[i][j] = '-';
//printf("-");
else if (j == 0)
interf[i][j] = '|';
//printf("|");
else if (j == N - 1)
interf[i][j] = '|';
//printf("| ");
else if (i == fy && j == fx)
interf[i][j] = '$';
//printf("$");
else
interf[i][j] = ' ';
//printf(" ");
}
}
for (; s >= 0; s--)
{
x = (snake + s)->snake_x;
y = (snake + s)->snake_y;
interf[y][x] = '@';
}
}
```
完全法度典型代码!!!
```
文件 snakemain.cpp
#include
#include
#include
#include "interface.h"
#include "snake.h"
int main(void)
{
struct snake a[(M - 2)*(N - 2)];
int snake_x =4;
int snake_y =4;
int X = 1;
int Y = 0;
int food_x , food_y ;
int score = 0;
do {
food_x = 1 + rand() % (N - 3);
food_y = 1 + rand() % (M - 3);
} while (food_x == 4 && food_y == 4);
for (;;)
{
system("cls");
printf(" 贪吃蛇小游戏");
printf(" 作者:xhyang 博客地址:http://blog.csdn.net/weixin_39449570 ");
printf("按W节制向上勾当,按D节制向右勾当,按S节制向下勾当,按A节制向左勾当。 ");
printf("得分:%d",score);
printf(" ");
control(&snake_x, &snake_y,&X,&Y);
build_snake(a, score, &snake_x, &snake_y);
death(snake_x, snake_y,score);
newinterface(a, food_x, food_y, score);
food(&snake_x, &snake_y, &food_x, &food_y,&score,a);
draw();
Sleep(140);
}
system("pause");
return 0;
}
```
```
文件 snake.h
#ifndef SNAKE_H
#define SNAKE_H
struct snake
{
int number;
int snake_x;
int snake_y;
//struct snake *next;
};
void build_snake(struct snake *snake, int s, int *sx, int *sy);
#endif
```
```
文件 snake.cpp
#include
#include
#include "snake.h"
void build_snake(struct snake *snake,int s,int *sx,int *sy)
{
for (int i = s; i >= 0; i--)
{
if (i == 0) {
(snake + i)->number = i;
(snake + i)->snake_x = *sx;
(snake + i)->snake_y = *sy;
}else{
(snake + i)->number = i;
(snake + i)->snake_x = (snake + i - 1)->snake_x;
(snake + i)->snake_y = (snake + i - 1)->snake_y;
}
}
}
```
```
文件 interface.h
#ifndef INTERFACE_H
#define INTERFACE_H
#define M 20
#define N 60
void control(int *x, int *y, int *X, int *Y);
void snake(int x, int y);
void newinterface(struct snake *snake, int fx, int fy, int s);
void food(int *x, int *y, int *fx, int *fy, int *s, struct snake *snake);
void draw(void);
void death(int x, int y, int s);
#endif
```
```
文件 interface.cpp
#include
#include
#include
#include
#include "interface.h"
#include "snake.h"
char interf[M][N];
void newinterface(struct snake *snake, int fx, int fy,int s)
{
int x, y;
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
if (i == 0 || i == M - 1)
interf[i][j] = '-';
//printf("-");
else if (j == 0)
interf[i][j] = '|';
//printf("|");
else if (j == N - 1)
interf[i][j] = '|';
//printf("| ");
else if (i == fy && j == fx)
interf[i][j] = '$';
//printf("$");
else
interf[i][j] = ' ';
//printf(" ");
}
}
for (; s >= 0; s--)
{
x = (snake + s)->snake_x;
y = (snake + s)->snake_y;
interf[y][x] = '@';
}
}
void food(int *x, int *y,int *fx,int *fy,int *s, struct snake *snake)
{
int ffx, ffy;//上一次食品的XY坐标
ffx = *fx;
ffy = *fy;
if (*x == *fx && *y == *fy)//假定吃到了食品,产生下一个食品
{
do {
*fx = 1 + rand() % (N - 3);
*fy = 1 + rand() % (M - 3);
} while (ffx == *fx && ffy == *fy);//包管与前次食品位置不合
for (int i= (*s); i >= 0; i--)
{
if ((snake + *s)->snake_x == *fx && (snake + *s)->snake_y == *fy) {
*fx = 1 + rand() % (N - 3);
*fy = 1 + rand() % (M - 3);
}
}//大年夜大年夜概率包管食品与蛇身子的位置不合(不克不及完全包管)
(*s)++ ; //分数加一!!!!!!!
}
}
void control(int *x,int *y,int *X,int *Y)//xy是蛇头的坐标,XY是节制勾当偏向的量
{
if (_kbhit())
{
switch (_getch())
{
case 'w' :
case 'W' :
if(interf[*y - 1][*x]!='@'){ //if语句包管蛇不克不及倒着走
*X = 0; *Y = -1;
}
break;
case 'd':
case 'D':
if(interf[*y][*x + 1] != '@'){
*X = 1; *Y = 0;
}
break;
case 's':
case 'S':
if (interf[*y + 1][*x] != '@') {
*X = 0; *Y = 1;
}
break;
case 'a':
case 'A':
if (interf[*y][*x - 1] != '@') {
*X = -1; *Y = 0;
}
break;
default:
break;
}
}
*x = *x + *X;//改动一次位置
*y = *y + *Y;
}
void draw(void)
{
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
printf("%c", interf[i][j]);
}
printf(" ");
}
}
void death(int x,int y,int s)
{
if (x == 0 || x == N-1 || y == 0 || y == M-1 || interf[y][x] == '@') {
for (;;)
{
system("cls");
printf(" 游戏中断! ");
printf(" 终究得分:%d ", s);
Sleep(140);
//system("pause");
}
}
}