c语言穿墙
A. c语言中,按一个键让某个结构失效一个运行
你的意思是,按键之后让TouchWall()或者CrossWall()成为注释,永远都不可能走进去?
如果是这样,是不可能的,编译都过不去,就算有这种可能,也不建议这样去思维。
你可以用一个标志位,比如叫int FunFlag,当按一下是1,再按一下是0。
然后你在你的TouchWall()和CrossWall()加入if判断。
if(FunFlag != 1)
return;
和
if(FunFlag != 0)
return;
B. 用c语言中的栈的知识编写迷宫程序
用递归,每条路有不同个方向,每次递归判断,递归出口就是走出迷宫.
C. C语言:贪吃蛇在穿墙的时候怎么做到无视墙
我的见解是整个贪吃蛇的图案框格使用一个二维数组来代表,对二维数组作相应的填充可以作为障碍,如果做到无视墙,可以复制一个二维数组吗,将其中的障碍去掉即可。
D. 求C语言贪吃蛇代码能在DEV上运行通过的贪吃蛇,不要TC上的啊我们老师上课给在DEV我们运行了一次
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#defineU1
#defineD2
#defineL3
#defineR4//蛇的状态,U:上;D:下;L:左R:右
typedefstructSNAKE{//蛇身的一个节点
intx;
inty;
structSNAKE*next;
}snake;
//全局变量//
intscore=0,add=10;//总得分与每次吃食物得分。
intstatus,sleeptime=200;//每次运行的时间间隔
snake*head,*food;//蛇头指针,食物指针
snake*q;//遍历蛇的时候用到的指针
intendgamestatus=0;//游戏结束的情况,1:撞到墙;2:咬到自己;3:主动退出游戏。
//声明全部函数//
voidPos();
voidcreatMap();
voidinitsnake();
intbiteself();
voidcreatefood();
voidcantcrosswall();
voidsnakemove();
voidpause();
voidgamecircle();
voidwelcometogame();
voidendgame();
voidgamestart();
voidPos(intx,inty)//设置光标位置
{
COORDpos;
HANDLEhOutput;
pos.X=x;
pos.Y=y;
hOutput=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOutput,pos);
}
voidcreatMap()//创建地图
{
inti;
for(i=0;i<58;i+=2){//打印上下边框
Pos(i,0);
printf("■");
Pos(i,26);
printf("■");
}
for(i=1;i<26;i++){//打印左右边框
Pos(0,i);
printf("■");
Pos(56,i);
printf("■");
}
}
voidinitsnake()//初始化蛇身
{
snake*tail;
inti;
tail=(snake*)malloc(sizeof(snake));//从蛇尾开始,头插法,以x,y设定开始的位置//
tail->x=24;
tail->y=5;
tail->next=NULL;
for(i=1;i<=4;i++){
head=(snake*)malloc(sizeof(snake));
head->next=tail;
head->x=24+2*i;
head->y=5;
tail=head;
}
while(tail!=NULL){//从头到为,输出蛇身
Pos(tail->x,tail->y);
printf("■");
tail=tail->next;
}
}
intbiteself()//判断是否咬到了自己
{
snake*self;
self=head->next;
while(self!=NULL){
if(self->x==head->x&&self->y==head->y){
return1;
}
self=self->next;
}
return0;
}
voidcreatefood()//随机出现食物
{
snake*food_1;
srand((unsigned)time(NULL));
food_1=(snake*)malloc(sizeof(snake));
while((food_1->x%2)!=0){//保证其为偶数,使得食物能与蛇头对其
food_1->x=rand()%52+2;
}
food_1->y=rand()%24+1;
q=head;
while(q->next==NULL){
if(q->x==food_1->x&&q->y==food_1->y){//判断蛇身是否与食物重合
free(food_1);
createfood();
}
q=q->next;
}
Pos(food_1->x,food_1->y);
food=food_1;
printf("■");
}
voidcantcrosswall()//不能穿墙
{
if(head->x==0||head->x==56||head->y==0||head->y==26){
endgamestatus=1;
endgame();
}
}
voidsnakemove()//蛇前进,上U,下D,左L,右R
{
snake*nexthead;
cantcrosswall();
nexthead=(snake*)malloc(sizeof(snake));
if(status==U){
nexthead->x=head->x;
nexthead->y=head->y-1;
if(nexthead->x==food->x&&nexthead->y==food->y){//如果下一个有食物//
nexthead->next=head;
head=nexthead;
q=head;
while(q!=NULL){
Pos(q->x,q->y);
printf("■");
q=q->next;
}
score=score+add;
createfood();
}else{//如果没有食物//
nexthead->next=head;
head=nexthead;
q=head;
while(q->next->next!=NULL){
Pos(q->x,q->y);
printf("■");
q=q->next;
}
Pos(q->next->x,q->next->y);
printf("");
free(q->next);
q->next=NULL;
}
}
if(status==D){
nexthead->x=head->x;
nexthead->y=head->y+1;
if(nexthead->x==food->x&&nexthead->y==food->y){//有食物
nexthead->next=head;
head=nexthead;
q=head;
while(q!=NULL){
Pos(q->x,q->y);
printf("■");
q=q->next;
}
score=score+add;
createfood();
}else{//没有食物
nexthead->next=head;
head=nexthead;
q=head;
while(q->next->next!=NULL){
Pos(q->x,q->y);
printf("■");
q=q->next;
}
Pos(q->next->x,q->next->y);
printf("");
free(q->next);
q->next=NULL;
}
}
if(status==L){
nexthead->x=head->x-2;
nexthead->y=head->y;
if(nexthead->x==food->x&&nexthead->y==food->y){//有食物
nexthead->next=head;
head=nexthead;
q=head;
while(q!=NULL){
Pos(q->x,q->y);
printf("■");
q=q->next;
}
score=score+add;
createfood();
}else{//没有食物
nexthead->next=head;
head=nexthead;
q=head;
while(q->next->next!=NULL){
Pos(q->x,q->y);
printf("■");
q=q->next;
}
Pos(q->next->x,q->next->y);
printf("");
free(q->next);
q->next=NULL;
}
}
if(status==R){
nexthead->x=head->x+2;
nexthead->y=head->y;
if(nexthead->x==food->x&&nexthead->y==food->y){//有食物
nexthead->next=head;
head=nexthead;
q=head;
while(q!=NULL){
Pos(q->x,q->y);
printf("■");
q=q->next;
}
score=score+add;
createfood();
}else{//没有食物
nexthead->next=head;
head=nexthead;
q=head;
while(q->next->next!=NULL){
Pos(q->x,q->y);
printf("■");
q=q->next;
}
Pos(q->next->x,q->next->y);
printf("");
free(q->next);
q->next=NULL;
}
}
if(biteself()==1){//判断是否会咬到自己
endgamestatus=2;
endgame();
}
}
voidpause()//暂停
{
while(1){
Sleep(300);
if(GetAsyncKeyState(VK_SPACE)){
break;
}
}
}
voidgamecircle()//控制游戏
{
Pos(64,15);
printf("不能穿墙,不能咬到自己 ");
Pos(64,16);
printf("用↑.↓.←.→分别控制蛇的移动.");
Pos(64,17);
printf("F1为加速,F2为减速 ");
Pos(64,18);
printf("ESC:退出游戏.space:暂停游戏.");
Pos(64,20);
//
status=R;
while(1){
Pos(64,10);
printf("得分:%d",score);
Pos(64,11);
printf("每个食物得分:%d分",add);
if(GetAsyncKeyState(VK_UP)&&status!=D){
status=U;
}elseif(GetAsyncKeyState(VK_DOWN)&&status!=U){
status=D;
}elseif(GetAsyncKeyState(VK_LEFT)&&status!=R){
status=L;
}elseif(GetAsyncKeyState(VK_RIGHT)&&status!=L){
status=R;
}elseif(GetAsyncKeyState(VK_SPACE)){
pause();
}elseif(GetAsyncKeyState(VK_ESCAPE)){
endgamestatus=3;
break;
}elseif(GetAsyncKeyState(VK_F1)){
if(sleeptime>=50){
sleeptime=sleeptime-30;
add=add+2;
if(sleeptime==320){
add=2;//防止减到1之后再加回来有错
}
}
}elseif(GetAsyncKeyState(VK_F2)){
if(sleeptime<350){
sleeptime=sleeptime+30;
add=add-2;
if(sleeptime==350){
add=1;//保证最低分为1
}
}
}
Sleep(sleeptime);
snakemove();
}
}
voidwelcometogame()//开始界面
{
Pos(40,12);
//
printf("欢迎来到贪食蛇游戏!");
Pos(40,25);
//
system("pause");
system("cls");
Pos(25,12);
printf("用↑.↓.←.→分别控制蛇的移动,F1为加速,2为减速 ");
Pos(25,13);
printf("加速将能得到更高的分数。 ");
system("pause");
system("cls");
}
voidendgame()//结束游戏
{
system("cls");
Pos(24,12);
if(endgamestatus==1){
printf("对不起,您撞到墙了。游戏结束.");
}elseif(endgamestatus==2){
printf("对不起,您咬到自己了。游戏结束.");
}elseif(endgamestatus==3){
printf("您的已经结束了游戏。");
}
Pos(24,13);
printf("您的得分是%d ",score);
exit(0);
}
voidgamestart()//游戏初始化
{
system("modeconcols=100lines=30");
welcometogame();
creatMap();
initsnake();
createfood();
}
intmain()
{
gamestart();
gamecircle();
endgame();
return0;
}
E. 请高手指教:用c语言写了个贪吃蛇,但每按下方向键就必须按一下回车,不然就停在原地不走;
把读键函数getchar()换成getch()
F. 数据结构 c语言 课程设计 小鼠走迷宫问题
1、可以用“*”来代表老鼠,“|”来代表墙,空格来代表路。每走一步用system("cls")刷新一次屏幕。
2、墙不可穿过代表,墙与周围的格子没有边。
3、规定一个时间t,若在t步之内没有走到粮仓,则输出无解。
4、这个简单,无非就是修改条件,从而修改整个图。
5、所用路径可以用深搜(回朔)来解决,最短路就用广搜来解决。最短路也可以用Dijstra算法、floyd算法等,但广搜是最简单的。
具体的程序你自己实现吧,如果写不出来,就去请教一下你们学校的ACMer,他们应该会比较熟悉。加油吧。
G. dev-c++中的c语言游戏代码
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#define U 1
#define D 2
#define L 3
#define R 4 //蛇的状态,U:上 ;D:下;L:左 R:右
typedef struct SNAKE { //蛇身的一个节点
int x;
int y;
struct SNAKE *next;
} snake;
//全局变量//
int score=0,add=10;//总得分与每次吃食物得分。
int status,sleeptime=200;//每次运行的时间间隔
snake *head, *food;//蛇头指针,食物指针
snake *q;//遍历蛇的时候用到的指针
int endgamestatus=0; //游戏结束的情况,1:撞到墙;2:咬到自己;3:主动退出游戏。
//声明全部函数//
void Pos();
void creatMap();
void initsnake();
int biteself();
void createfood();
void cantcrosswall();
void snakemove();
void pause();
void gamecircle();
void welcometogame();
void endgame();
void gamestart();
void Pos(int x,int y)//设置光标位置
{
COORD pos;
HANDLE hOutput;
pos.X=x;
pos.Y=y;
hOutput=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOutput,pos);
}
void creatMap()//创建地图
{
int i;
for(i=0; i<58; i+=2) { //打印上下边框
Pos(i,0);
printf("■");
Pos(i,26);
printf("■");
}
for(i=1; i<26; i++) { //打印左右边框
Pos(0,i);
printf("■");
Pos(56,i);
printf("■");
}
}
void initsnake()//初始化蛇身
{
snake *tail;
int i;
tail=(snake*)malloc(sizeof(snake));//从蛇尾开始,头插法,以x,y设定开始的位置//
tail->x=24;
tail->y=5;
tail->next=NULL;
for(i=1; i<=4; i++) {
head=(snake*)malloc(sizeof(snake));
head->next=tail;
head->x=24+2*i;
head->y=5;
tail=head;
}
while(tail!=NULL) { //从头到为,输出蛇身
Pos(tail->x,tail->y);
printf("■");
tail=tail->next;
}
}
int biteself()//判断是否咬到了自己
{
snake *self;
self=head->next;
while(self!=NULL) {
if(self->x==head->x && self->y==head->y) {
return 1;
}
self=self->next;
}
return 0;
}
void createfood()//随机出现食物
{
snake *food_1;
srand((unsigned)time(NULL));
food_1=(snake*)malloc(sizeof(snake));
while((food_1->x%2)!=0) { //保证其为偶数,使得食物能与蛇头对其
food_1->x=rand()%52+2;
}
food_1->y=rand()%24+1;
q=head;
while(q->next==NULL) {
if(q->x==food_1->x && q->y==food_1->y) { //判断蛇身是否与食物重合
free(food_1);
createfood();
}
q=q->next;
}
Pos(food_1->x,food_1->y);
food=food_1;
printf("■");
}
void cantcrosswall()//不能穿墙
{
if(head->x==0 || head->x==56 ||head->y==0 || head->y==26) {
endgamestatus=1;
endgame();
}
}
void snakemove()//蛇前进,上U,下D,左L,右R
{
snake * nexthead;
cantcrosswall();
nexthead=(snake*)malloc(sizeof(snake));
if(status==U) {
nexthead->x=head->x;
nexthead->y=head->y-1;
if(nexthead->x==food->x && nexthead->y==food->y) { //如果下一个有食物//
nexthead->next=head;
head=nexthead;
q=head;
while(q!=NULL) {
Pos(q->x,q->y);
printf("■");
q=q->next;
}
score=score+add;
createfood();
} else { //如果没有食物//
nexthead->next=head;
head=nexthead;
q=head;
while(q->next->next!=NULL) {
Pos(q->x,q->y);
printf("■");
q=q->next;
}
Pos(q->next->x,q->next->y);
printf(" ");
free(q->next);
q->next=NULL;
}
}
if(status==D) {
nexthead->x=head->x;
nexthead->y=head->y+1;
if(nexthead->x==food->x && nexthead->y==food->y) { //有食物
nexthead->next=head;
head=nexthead;
q=head;
while(q!=NULL) {
Pos(q->x,q->y);
printf("■");
q=q->next;
}
score=score+add;
createfood();
} else { //没有食物
nexthead->next=head;
head=nexthead;
q=head;
while(q->next->next!=NULL) {
Pos(q->x,q->y);
printf("■");
q=q->next;
}
Pos(q->next->x,q->next->y);
printf(" ");
free(q->next);
q->next=NULL;
}
}
if(status==L) {
nexthead->x=head->x-2;
nexthead->y=head->y;
if(nexthead->x==food->x && nexthead->y==food->y) { //有食物
nexthead->next=head;
head=nexthead;
q=head;
while(q!=NULL) {
Pos(q->x,q->y);
printf("■");
q=q->next;
}
score=score+add;
createfood();
} else { //没有食物
nexthead->next=head;
head=nexthead;
q=head;
while(q->next->next!=NULL) {
Pos(q->x,q->y);
printf("■");
q=q->next;
}
Pos(q->next->x,q->next->y);
printf(" ");
free(q->next);
q->next=NULL;
}
}
if(status==R) {
nexthead->x=head->x+2;
nexthead->y=head->y;
if(nexthead->x==food->x && nexthead->y==food->y) { //有食物
nexthead->next=head;
head=nexthead;
q=head;
while(q!=NULL) {
Pos(q->x,q->y);
printf("■");
q=q->next;
}
score=score+add;
createfood();
} else { //没有食物
nexthead->next=head;
head=nexthead;
q=head;
while(q->next->next!=NULL) {
Pos(q->x,q->y);
printf("■");
q=q->next;
}
Pos(q->next->x,q->next->y);
printf(" ");
free(q->next);
q->next=NULL;
}
}
if(biteself()==1) { //判断是否会咬到自己
endgamestatus=2;
endgame();
}
}
void pause()//暂停
{
while(1) {
Sleep(300);
if(GetAsyncKeyState(VK_SPACE)) {
break;
}
}
}
void gamecircle()//控制游戏
{
Pos(64,15);
printf("不能穿墙,不能咬到自己\n");
Pos(64,16);
printf("用↑.↓.←.→分别控制蛇的移动.");
Pos(64,17);
printf("F1 为加速,F2 为减速\n");
Pos(64,18);
printf("ESC :退出游戏.space:暂停游戏.");
Pos(64,20);
//
status=R;
while(1) {
Pos(64,10);
printf("得分:%d ",score);
Pos(64,11);
printf("每个食物得分:%d分",add);
if(GetAsyncKeyState(VK_UP) && status!=D) {
status=U;
} else if(GetAsyncKeyState(VK_DOWN) && status!=U) {
status=D;
} else if(GetAsyncKeyState(VK_LEFT)&& status!=R) {
status=L;
} else if(GetAsyncKeyState(VK_RIGHT)&& status!=L) {
status=R;
} else if(GetAsyncKeyState(VK_SPACE)) {
pause();
} else if(GetAsyncKeyState(VK_ESCAPE)) {
endgamestatus=3;
break;
} else if(GetAsyncKeyState(VK_F1)) {
if(sleeptime>=50) {
sleeptime=sleeptime-30;
add=add+2;
if(sleeptime==320) {
add=2;//防止减到1之后再加回来有错
}
}
} else if(GetAsyncKeyState(VK_F2)) {
if(sleeptime<350) {
sleeptime=sleeptime+30;
add=add-2;
if(sleeptime==350) {
add=1; //保证最低分为1
}
}
}
Sleep(sleeptime);
snakemove();
}
}
void welcometogame()//开始界面
{
Pos(40,12);
//
printf("欢迎来到贪食蛇游戏!");
Pos(40,25);
//
system("pause");
system("cls");
Pos(25,12);
printf("用↑.↓.←.→分别控制蛇的移动, F1 为加速,2 为减速\n");
Pos(25,13);
printf("加速将能得到更高的分数。\n");
system("pause");
system("cls");
}
void endgame()//结束游戏
{
system("cls");
Pos(24,12);
if(endgamestatus==1) {
printf("对不起,您撞到墙了。游戏结束.");
} else if(endgamestatus==2) {
printf("对不起,您咬到自己了。游戏结束.");
} else if(endgamestatus==3) {
printf("您的已经结束了游戏。");
}
Pos(24,13);
printf("您的得分是%d\n",score);
exit(0);
}
void gamestart()//游戏初始化
{
system("mode con cols=100 lines=30");
welcometogame();
creatMap();
initsnake();
createfood();
}
int main()
{
gamestart();
gamecircle();
endgame();
return 0;
}