當前位置:首頁 » 編程語言 » c語言穿牆

c語言穿牆

發布時間: 2022-12-23 01:33:50

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;
}

熱點內容
超級訪問陳小春應采兒 發布:2025-05-16 09:43:29 瀏覽:478
緩存視頻合並工具最新版 發布:2025-05-16 09:35:03 瀏覽:194
花雨庭伺服器ip地址和埠 發布:2025-05-16 09:34:58 瀏覽:239
同時修改多台伺服器管理地址工具 發布:2025-05-16 09:20:36 瀏覽:421
什麼配置就能玩地平線 發布:2025-05-16 09:13:46 瀏覽:82
python旋轉圖片 發布:2025-05-16 09:13:40 瀏覽:638
少女前線防檢測腳本 發布:2025-05-16 08:59:07 瀏覽:728
編譯器對系統的依賴 發布:2025-05-16 08:37:29 瀏覽:711
javamap數組 發布:2025-05-16 08:37:28 瀏覽:451
移動光貓如何自行修改密碼 發布:2025-05-16 08:20:15 瀏覽:125