當前位置:首頁 » 編程語言 » c語言編程游戲常用代碼
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言編程游戲常用代碼

發布時間: 2023-07-13 16:31:42

1. 用c語言編寫的小游戲代碼是什麼

/*也不知道你是什麼級別的,我是一個新手,剛接觸編程語言,以下是我自己變得一個小程序,在所有c語言的編譯器(vc++6.0、turbo????)上都能運行,你還可以進一步改進。這是一個類似貪吃蛇的小游戲。祝你好運*/x0dx0a/*貪吃蛇*/x0dx0a#includex0dx0a#includex0dx0a#includex0dx0a#includex0dx0aint head=3 ,tail=0;x0dx0aint main()x0dx0a{x0dx0aint i,j,k=0;x0dx0aint zuobiao[2][80];x0dx0along start;x0dx0aint direction=77;x0dx0aint gamespeed;x0dx0aint timeover;x0dx0aint change(char qipan[20][80],int zuobiao[2][80],char direction);x0dx0azuobiao[0][tail]=1;zuobiao[1][tail]=1;zuobiao[0][1]=1;zuobiao[1][1]=2;zuobiao[0][2]=1;zuobiao[1][2]=3;zuobiao[0][head]=1;zuobiao[1][head]=4;x0dx0a/*處理棋盤*/x0dx0achar qipan[20][80];//定義棋盤x0dx0afor(i=0;i<20;i++)x0dx0a for(j=0;j<80;j++)x0dx0aqipan[i][j]=' '//初始化棋盤x0dx0afor(i=0;i<80;i++)x0dx0aqipan[0][i]='_'x0dx0afor(i=0;i<20;i++)x0dx0aqipan[i][0]='|'x0dx0afor(i=0;i<20;i++)x0dx0aqipan[i][79]='|'x0dx0afor(i=0;i<80;i++)x0dx0aqipan[19][i]='_'x0dx0aqipan[1][1]=qipan[1][2]=qipan[1][3]='*'//初始化蛇的位置x0dx0aqipan[1][4]='#'x0dx0aprintf("This is a game of a SNAKE.\nGOOD LUCK TO YOU !\n");x0dx0aprintf("Input your game speed,please.(e.g.300)\n");x0dx0ascanf("%d",&gamespeed);x0dx0ax0dx0awhile(direction!='q')x0dx0a{x0dx0asystem("cls");x0dx0afor(i=0;i<20;i++)//列印出棋盤x0dx0afor(j=0;j<80;j++)x0dx0aprintf("%c",qipan[i][j]);x0dx0atimeover=1;x0dx0astart=clock();x0dx0awhile(!kbhit()&&(timeover=clock()-start<=gamespeed));x0dx0aif(timeover)x0dx0a{x0dx0agetch();x0dx0adirection=getch();x0dx0a}x0dx0aelsex0dx0adirection=direction;x0dx0aif(!(direction==72||direction==80||direction==75||direction==77))x0dx0a{x0dx0areturn 0;x0dx0asystem("cls");x0dx0aprintf("GAME OVER!\n");x0dx0a}x0dx0aif(!change(qipan,zuobiao,direction))x0dx0a{x0dx0adirection='q'x0dx0asystem("cls");x0dx0aprintf("GAME OVER!\n");x0dx0a}x0dx0a}x0dx0areturn 0;x0dx0a}x0dx0aint change(char qipan[20][80],int zuobiao[2][80],char direction)x0dx0a{x0dx0aint x,y;x0dx0aif(direction==72)x0dx0ax=zuobiao[0][head]-1;y=zuobiao[1][head];x0dx0aif(direction==80)x0dx0ax=zuobiao[0][head]+1;y=zuobiao[1][head];x0dx0aif(direction==75)x0dx0ax=zuobiao[0][head];y=zuobiao[0][head]-1;x0dx0aif(direction==77)x0dx0ax=zuobiao[0][head];y=zuobiao[1][head]+1;x0dx0aif(x==0||x==18||y==78||y==0)x0dx0areturn 0;x0dx0aif(qipan[x][y]!=' ')x0dx0areturn 0;x0dx0aqipan[zuobiao[0][tail]][zuobiao[1][tail]]=' 'x0dx0atail=(tail+1)%80;x0dx0aqipan[zuobiao[0][head]][zuobiao[1][head]]='*'x0dx0ahead=(head+1)%80;x0dx0azuobiao[0][head]=x;x0dx0azuobiao[1][head]=y;x0dx0aqipan[zuobiao[0][head]][zuobiao[1][head]]='#'x0dx0areturn 1;x0dx0a}

2. c語言游戲編程,下落的小鳥 求代碼

//沒玩過,你看這個是嗎
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include<Windows.h>
/********函數變數聲明********/
#define PR_Box printf("■")
#define PR_Gold printf("★")
#define PR_Ag printf("☆")
#define PR_FBird printf("Ю")
#define PR_DBird printf("Ф")
#define PR_Land printf("┳┳┯")
#define PR_Bg_TL printf("╔")
#define PR_Bg_TR printf("╗")
#define PR_Bg_DL printf("╚")
#define PR_Bg_DR printf("╝")
#define PR_Bg_X printf("═")
#define PR_Bg_Y printf("║")
#define PR_Blank printf(" ");
int Grade = 1, C_Gold = 0, C_Ag = 0, Score = 0, Delay_time = 1000, Max_blank = 9, Distance = 18;
struct Birds
{
int x, y;
int condition;
};
Birds *Bird = (Birds*)malloc(sizeof(Birds));
struct Bg
{
int x, y;
int l_blank;
int reward[9];
Bg *pri;
Bg *next;
};
Bg *Bg1 = new Bg[sizeof(Bg)];
void Position(int x, int y)
{
COORD pos = { x - 1, y - 1 };
HANDLE Out = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(Out, pos);
}
void CreatBird()
{
Bird->x = 41;
Bird->y = 10;
Bird->condition = 0;
}
void CreatBg()
{
Bg *Bg2 = (Bg*)malloc(sizeof(Bg));
Bg1->x = 90; Bg1->y = 8;
Bg2->x = Bg1->x + Distance; Bg2->y = 9;
Bg1->l_blank = Max_blank - Grade;
Bg2->l_blank = Max_blank - Grade;
Bg1->next = Bg2;
Bg1->pri = Bg2;
Bg2->next = Bg1;
Bg2->pri = Bg1;
}
void InsertBg(Bg *p)
{
int temp;
Bg *Bgs = (Bg*)malloc(sizeof(Bg));
Bgs->x = p->pri->x + Distance;
Bgs->l_blank = Max_blank - Grade;
srand((int)time(0));
temp = rand();
if (temp % 2 == 0)//++
{
if ((temp % 4 + p->pri->y + Max_blank - Grade)<21)
Bgs->y = p->pri->y + temp % 4;
else
Bgs->y = p->pri->y;
}
else
{
if ((p->pri->y - temp % 4)>2)
Bgs->y = p->pri->y - temp % 4;
else
Bgs->y = p->pri->y;
}
Bgs->pri = p->pri;
Bgs->next = p;
p->pri->next = Bgs;
p->pri = Bgs;
}
void Check_Bg(Bg *q)
{
Bg *p = q; int i = 0, temp;
while (++i <= 5)
{
if (p->x>-4)
p = p->next;
else
{
srand((int)time(0));
temp = rand();
if (temp % 2 == 0)//++
{
if ((temp % 4 + p->y + Max_blank - Grade)<21)
p->y = p->y + temp % 4;
else
p->y = p->y;
p->x = p->pri->x + Distance;
p->l_blank = Max_blank - Grade;
}
else
{
if ((p->y - temp % 4)>2)
p->y = p->y - temp % 4;
else
p->y = p->y;
p->x = p->pri->x + Distance;
p->l_blank = Max_blank - Grade;
}
}
}
}
void Loop_Bg(Bg *q)
{
Bg *p = q; int i = 0;
while (++i <= 5)
{
p->x = p->x - 1;
p = p->next;
if (Bird->x == p->x)
{
Score += 1;
if (Score % 4 == 0 && Grade<4)
Grade++;
}
}
}
void Prt_Bg(Bg *q)
{
Bg *p = q; int i = 0, k, j;
while (++i <= 5)
{
if (p->x>0 && p->x <= 78)
{
for (k = 2; k<p->y; k++)
{
Position(p->x + 1, k);
PR_Box; PR_Box; PR_Blank
}
Position(p->x, p->y);
PR_Box; PR_Box; PR_Box; PR_Blank;
Position(p->x, p->y + p->l_blank);
PR_Box; PR_Box; PR_Box; PR_Blank;
k = k + p->l_blank + 1;
for (k; k <= 22; k++)
{
Position(p->x + 1, k);
PR_Box; PR_Box; PR_Blank;
}
Position(p->x, 23);
for (k = 1; k<Distance / 3 - 2; k++)
PR_Land;
}
p = p->next;
if (p->x == 0)
{
for (j = 2; j<p->y; j++)
{
Position(p->x + 1, j);
PR_Blank; PR_Blank;
}
Position(p->x + 1, p->y);
PR_Blank; PR_Blank; PR_Blank;
Position(p->x + 1, p->y + Max_blank - Grade);
PR_Blank; PR_Blank; PR_Blank;
j = j + Max_blank - Grade + 1;
for (j; j <= 22; j++)
{
Position(p->x + 1, j);
PR_Blank; PR_Blank;
}
}
}
}
void PrtBg()
{
int i;
Position(1, 1); PR_Bg_TL;
Position(79, 1); PR_Bg_TR;
Position(1, 24); PR_Bg_DL;
Position(79, 24); PR_Bg_DR;
for (i = 3; i <= 78; i += 2)
{
Position(i, 1); PR_Bg_X;
Position(i, 24); PR_Bg_X;
}
/*for(i=2;i<=23;i++)
{ Position(1,i);PR_Bg_Y;printf("%d",i-1);
Position(79,i);PR_Bg_Y;
}*/
}
void PrtBird()
{
Position(Bird->x, Bird->y - 1);
PR_Blank;
Position(Bird->x, Bird->y);
PR_FBird;
Position(38, 2);
printf("Score:%d", Score);
}
int CheckYN(Bg *q)
{
Bg *p = q; int i = 0;
while (++i <= 5)
{
if (Bird->y>23)
return 0;
if (Bird->x == p->x&&Bird->y <= p->y)
return 0;
if ((Bird->x == p->x || Bird->x == p->x + 1 || Bird->x == p->x + 2) && Bird->y == p->y)
return 0;
if (Bird->x == p->x&&Bird->y>p->y + p->l_blank)
return 0;
if ((Bird->x == p->x || Bird->x == p->x + 1 || Bird->x == p->x + 2) && Bird->y == p->y + p->l_blank)
return 0;
p = p->next;
}
return 1;
}
void Prtfirst()
{
printf("══════════════════════════════════════\n");
printf(" ■■ ■■\n");
printf(" ■■ ■■\n");
printf(" ■■ ■■ C語言版 Flappy Bird\n");
printf(" ■■ ■■ 瞎搞人:yyposs\n");
printf(" ■■ ■■ 瞎搞日期:2014.2\n");
printf(" ■■ ■■ 耗時:4小時\n");
printf(" ■■■ ■■ 游戲說明:\n");
printf(" ■■ 1-按上箭頭使鳥起飛\n");
printf(" ■■ 2-等級越高,難度越大!\n");
printf(" Ю123 ■■■\n");
printf("\n");
printf(" ■■■ 歡迎各路大神與我探討C、\nC++、VB、PHP、C#\n");
printf(" ■■\n");
printf(" ■■\n");
printf(" ■■ ■■■ \n");
printf(" ■■ ■■\n");
printf(" ■■ Ф ■■\n");
printf(" ■■ ■■\n");
printf(" ■■ ■■\n");
printf(" ■■ ■■\n");
printf(" ■■ ■■\n");
printf(" ┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳\n");
system("pause");
Position(1, 1);
int i = 0;
while (i++<40 * 25)
PR_Blank;
}
int main()
{
int i = 0; char ch;
Prtfirst();
PrtBg();
CreatBg();
InsertBg(Bg1);
InsertBg(Bg1);
InsertBg(Bg1);
CreatBird();
while (1)
{
if (!CheckYN(Bg1))
break;
Check_Bg(Bg1);
Prt_Bg(Bg1);
PrtBird();
Loop_Bg(Bg1);
Bird->y = Bird->y + 1;
if (GetAsyncKeyState(VK_UP))
{
Position(Bird->x, Bird->y - 1);
PR_Blank;
Bird->y = Bird->y - 4;
}
while (i++<500);
{
Sleep(100);
}
i = 0;
}
Position(38, 10);
printf("You Lost!");
Position(1, 25);
system("pause");
return 0;
}

3. 就C語言中 猜拳游戲的代碼

這是一個簡單的猜拳游戲(剪子包子錘),讓你與電腦對決。你出的拳頭由你自己決定,電腦則隨機出拳,最後判斷勝負。
下面的代碼會實現一個猜拳游戲,讓你與電腦對決。你出的拳頭由你自己決定,電腦則隨機出拳,最後判斷勝負。
啟動程序後,讓用戶出拳,截圖:

用戶出拳,顯示對決結果:截圖:

代碼實現:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
char gamer; // 玩家出拳
int computer; // 電腦出拳
int result; // 比賽結果
// 為了避免玩一次游戲就退出程序,可以將代碼放在循環中
while (1){
printf("這是一個猜拳的小游戲,請輸入你要出的拳頭:\n");
printf("A:剪刀\nB:石頭\nC:布\nD:不玩了\n");
scanf("%c%*c",&gamer);
switch (gamer){
case 65: //A
case 97: //a
gamer=4;
break;
case 66: //B
case 98: //b
gamer=7;
break;
case 67: //C
case 99: //c
gamer=10;
break;
case 68: //D
case 100: //d
return 0;

default:
printf("你的選擇為 %c 選擇錯誤,退出...\n",gamer);
getchar();
system("cls"); // 清屏
return 0;
break;
}

srand((unsigned)time(NULL)); // 隨機數種子
computer=rand()%3; // 產生隨機數並取余,得到電腦出拳
result=(int)gamer+computer; // gamer 為 char 類型,數學運算時要強制轉換類型
printf("電腦出了");
switch (computer)
{
case 0:printf("剪刀\n");break; //4 1
case 1:printf("石頭\n");break; //7 2
case 2:printf("布\n");break; //10 3
}
printf("你出了");
switch (gamer)
{
case 4:printf("剪刀\n");break;
case 7:printf("石頭\n");break;
case 10:printf("布\n");break;
}
if (result==6||result==7||result==11) printf("你贏了!");
else if (result==5||result==9||result==10) printf("電腦贏了!");
else printf("平手");
system("pause>nul&&cls"); // 暫停並清屏
}
return 0;
}
代碼分析
1) 首先,我們需要定義3個變數來儲存玩家出的拳頭(gamer)、電腦出的拳頭(computer)和最後的結果(result),然後給出文字提示,讓玩家出拳。
接下來接收玩家輸入:
scanf("%c%*c",&gamer);

4. dev-c++中的c語言游戲代碼是什麼

#include x0dx0a#include x0dx0a#include x0dx0a#include x0dx0ax0dx0a#define U 1x0dx0a#define D 2x0dx0a#define L 3x0dx0a#define R 4 //蛇的狀態,U:上 ;D:下;L:左 R:右x0dx0ax0dx0atypedef struct SNAKE { //蛇身的一個節點x0dx0aint x;x0dx0aint y;x0dx0astruct SNAKE *next;x0dx0a} snake;x0dx0ax0dx0a/源啟中/全局變數//x0dx0aint score=0,add=10;//總得分與每次吃食物得分。x0dx0aint status,sleeptime=200;//每次運行的時間間隔x0dx0asnake *head, *food;//蛇頭指針,食物指針x0dx0asnake *q;//遍歷蛇的時候用到的指針x0dx0aint endgamestatus=0; //游戲結束的情況,1:撞到牆;2:咬到自己;3:主動退旁笑出遊戲。x0dx0ax0dx0a//聲明全部函數//x0dx0avoid Pos();x0dx0avoid creatMap();x0dx0avoid initsnake();x0dx0aint biteself();x0dx0avoid createfood();x0dx0avoid cantcrosswall();x0dx0avoid snakemove();x0dx0avoid pause();x0dx0avoid gamecircle();x0dx0avoid welcometogame();x0dx0avoid endgame();x0dx0avoid gamestart();x0dx0ax0dx0avoid Pos(int x,int y)//設置游標位置x0dx0a{x0dx0aCOORD pos;x0dx0aHANDLE hOutput;x0dx0apos.X=x;x0dx0apos.Y=y;x0dx0ahOutput=GetStdHandle(STD_OUTPUT_HANDLE);(hOutput,pos);x0dx0a}x0dx0ax0dx0avoid creatMap()//創建地圖x0dx0a{x0dx0aint i;x0dx0afor(i=0; i<58; i+=2) { //列印上下邊框x0dx0aPos(i,0);x0dx0aprintf("■");x0dx0aPos(i,26);x0dx0aprintf("■");x0dx0a}x0dx0afor(i=1; i<26; i++) { //列印左右邊框x0dx0aPos(0,i);x0dx0aprintf("■");x0dx0aPos(56,i);x0dx0aprintf("■");x0dx0a}x0dx0a}x0dx0ax0dx0avoid initsnake()//初始化蛇身x0dx0a{x0dx0asnake *tail;x0dx0aint i;x0dx0atail=(snake*)malloc(sizeof(snake));//從蛇尾開始,頭插法,以x,y設定開始的位置//x0dx0atail->x=24;x0dx0atail->y=5;x0dx0atail->next=NULL;x0dx0afor(i=1; i<=4; i++) {x0dx0ahead=(snake*)malloc(sizeof(snake));x0dx0ahead->next=tail;x0dx0ahead->x=24+2*i;x0dx0ahead->y=5;x0dx0atail=head;x0dx0a}x0dx0awhile(tail!=NULL) { //從頭到為,輸出蛇身雹山x0dx0aPos(tail->x,tail->y);x0dx0aprintf("■");x0dx0atail=tail->next;x0dx0a}x0dx0a}x0dx0ax0dx0aint biteself()//判斷是否咬到了自己x0dx0a{x0dx0asnake *self;x0dx0aself=head->next;x0dx0awhile(self!=NULL) {x0dx0aif(self->x==head->x && self->y==head->y) {x0dx0areturn 1;x0dx0a}x0dx0aself=self->next;x0dx0a}x0dx0areturn 0;x0dx0a}x0dx0ax0dx0avoid createfood()//隨機出現食物x0dx0a{x0dx0asnake *food_1;x0dx0asrand((unsigned)time(NULL));x0dx0afood_1=(snake*)malloc(sizeof(snake));x0dx0awhile((food_1->x%2)!=0) { //保證其為偶數,使得食物能與蛇頭對其x0dx0afood_1->x=rand()%52+2;x0dx0a}x0dx0afood_1->y=rand()%24+1;x0dx0aq=head;x0dx0awhile(q->next==NULL) {x0dx0aif(q->x==food_1->x && q->y==food_1->y) { //判斷蛇身是否與食物重合x0dx0afree(food_1);x0dx0acreatefood();x0dx0a}x0dx0aq=q->next;x0dx0a}x0dx0aPos(food_1->x,food_1->y);x0dx0afood=food_1;x0dx0aprintf("■");x0dx0a}x0dx0ax0dx0avoid cantcrosswall()//不能穿牆x0dx0a{x0dx0aif(head->x==0 || head->x==56 ||head->y==0 || head->y==26) {x0dx0aendgamestatus=1;x0dx0aendgame();x0dx0a}x0dx0a}x0dx0ax0dx0avoid snakemove()//蛇前進,上U,下D,左L,右Rx0dx0a{x0dx0asnake * nexthead;x0dx0acantcrosswall();x0dx0ax0dx0anexthead=(snake*)malloc(sizeof(snake));x0dx0aif(status==U) {x0dx0anexthead->x=head->x;x0dx0anexthead->y=head->y-1;x0dx0aif(nexthead->x==food->x && nexthead->y==food->y) { //如果下一個有食物//x0dx0anexthead->next=head;x0dx0ahead=nexthead;x0dx0aq=head;x0dx0awhile(q!=NULL) {x0dx0aPos(q->x,q->y);x0dx0aprintf("■");x0dx0aq=q->next;x0dx0a}x0dx0ascore=score+add;x0dx0acreatefood();x0dx0a} else { //如果沒有食物//x0dx0anexthead->next=head;x0dx0ahead=nexthead;x0dx0aq=head;x0dx0awhile(q->next->next!=NULL) {x0dx0aPos(q->x,q->y);x0dx0aprintf("■");x0dx0aq=q->next;x0dx0a}x0dx0aPos(q->next->x,q->next->y);x0dx0aprintf(" ");x0dx0afree(q->next);x0dx0aq->next=NULL;x0dx0a}x0dx0a}x0dx0aif(status==D) {x0dx0anexthead->x=head->x;x0dx0anexthead->y=head->y+1;x0dx0aif(nexthead->x==food->x && nexthead->y==food->y) { //有食物x0dx0anexthead->next=head;x0dx0ahead=nexthead;x0dx0aq=head;x0dx0awhile(q!=NULL) {x0dx0aPos(q->x,q->y);x0dx0aprintf("■");x0dx0aq=q->next;x0dx0a}x0dx0ascore=score+add;x0dx0acreatefood();x0dx0a} else { //沒有食物x0dx0anexthead->next=head;x0dx0ahead=nexthead;x0dx0aq=head;x0dx0awhile(q->next->next!=NULL) {x0dx0aPos(q->x,q->y);x0dx0aprintf("■");x0dx0aq=q->next;x0dx0a}x0dx0aPos(q->next->x,q->next->y);x0dx0aprintf(" ");x0dx0afree(q->next);x0dx0aq->next=NULL;x0dx0a}x0dx0a}x0dx0aif(status==L) {x0dx0anexthead->x=head->x-2;x0dx0anexthead->y=head->y;x0dx0aif(nexthead->x==food->x && nexthead->y==food->y) { //有食物x0dx0anexthead->next=head;x0dx0ahead=nexthead;x0dx0aq=head;x0dx0awhile(q!=NULL) {x0dx0aPos(q->x,q->y);x0dx0aprintf("■");x0dx0aq=q->next;x0dx0a}x0dx0ascore=score+add;x0dx0acreatefood();x0dx0a} else { //沒有食物x0dx0anexthead->next=head;x0dx0ahead=nexthead;x0dx0aq=head;x0dx0awhile(q->next->next!=NULL) {x0dx0aPos(q->x,q->y);x0dx0aprintf("■");x0dx0aq=q->next;x0dx0a}x0dx0aPos(q->next->x,q->next->y);x0dx0aprintf(" ");x0dx0afree(q->next);x0dx0aq->next=NULL;x0dx0a}x0dx0a}x0dx0aif(status==R) {x0dx0anexthead->x=head->x+2;x0dx0anexthead->y=head->y;x0dx0aif(nexthead->x==food->x && nexthead->y==food->y) { //有食物x0dx0anexthead->next=head;x0dx0ahead=nexthead;x0dx0aq=head;x0dx0awhile(q!=NULL) {x0dx0aPos(q->x,q->y);x0dx0aprintf("■");x0dx0aq=q->next;x0dx0a}x0dx0ascore=score+add;x0dx0acreatefood();x0dx0a} else { //沒有食物x0dx0anexthead->next=head;x0dx0ahead=nexthead;x0dx0aq=head;x0dx0awhile(q->next->next!=NULL) {x0dx0aPos(q->x,q->y);x0dx0aprintf("■");x0dx0aq=q->next;x0dx0a}x0dx0aPos(q->next->x,q->next->y);x0dx0aprintf(" ");x0dx0afree(q->next);x0dx0aq->next=NULL;x0dx0a}x0dx0a}x0dx0aif(biteself()==1) { //判斷是否會咬到自己x0dx0aendgamestatus=2;x0dx0aendgame();x0dx0a}x0dx0a}x0dx0ax0dx0avoid pause()//暫停x0dx0a{x0dx0awhile(1) {x0dx0aSleep(300);x0dx0aif(GetAsyncKeyState(VK_SPACE)) {x0dx0abreak;x0dx0a}x0dx0ax0dx0a}x0dx0a}x0dx0ax0dx0avoid gamecircle()//控制游戲x0dx0a{x0dx0ax0dx0aPos(64,15);x0dx0aprintf("不能穿牆,不能咬到自己\n");x0dx0aPos(64,16);x0dx0aprintf("用↑.↓.←.→分別控制蛇的移動.");x0dx0aPos(64,17);x0dx0aprintf("F1 為加速,F2 為減速\n");x0dx0aPos(64,18);x0dx0aprintf("ESC :退出遊戲.space:暫停游戲.");x0dx0aPos(64,20);x0dx0a//x0dx0astatus=R;x0dx0awhile(1) {x0dx0aPos(64,10);x0dx0aprintf("得分:%d ",score);x0dx0aPos(64,11);x0dx0aprintf("每個食物得分:%d分",add);x0dx0aif(GetAsyncKeyState(VK_UP) && status!=D) {x0dx0astatus=U;x0dx0a} else if(GetAsyncKeyState(VK_DOWN) && status!=U) {x0dx0astatus=D;x0dx0a} else if(GetAsyncKeyState(VK_LEFT)&& status!=R) {x0dx0astatus=L;x0dx0a} else if(GetAsyncKeyState(VK_RIGHT)&& status!=L) {x0dx0astatus=R;x0dx0a} else if(GetAsyncKeyState(VK_SPACE)) {x0dx0apause();x0dx0a} else if(GetAsyncKeyState(VK_ESCAPE)) {x0dx0aendgamestatus=3;x0dx0abreak;x0dx0a} else if(GetAsyncKeyState(VK_F1)) {x0dx0aif(sleeptime>=50) {x0dx0asleeptime=sleeptime-30;x0dx0aadd=add+2;x0dx0aif(sleeptime==320) {x0dx0aadd=2;//防止減到1之後再加回來有錯x0dx0a}x0dx0a}x0dx0a} else if(GetAsyncKeyState(VK_F2)) {x0dx0aif(sleeptime<350) {x0dx0asleeptime=sleeptime+30;x0dx0aadd=add-2;x0dx0aif(sleeptime==350) {x0dx0aadd=1; //保證最低分為1x0dx0a}x0dx0a}x0dx0a}x0dx0aSleep(sleeptime);x0dx0asnakemove();x0dx0a}x0dx0a}x0dx0ax0dx0avoid welcometogame()//開始界面x0dx0a{x0dx0aPos(40,12);x0dx0ax0dx0a//x0dx0aprintf("歡迎來到貪食蛇游戲!");x0dx0aPos(40,25);x0dx0a//x0dx0asystem("pause");x0dx0asystem("cls");x0dx0aPos(25,12);x0dx0aprintf("用↑.↓.←.→分別控制蛇的移動, F1 為加速,2 為減速\n");x0dx0aPos(25,13);x0dx0aprintf("加速將能得到更高的分數。\n");x0dx0asystem("pause");x0dx0asystem("cls");x0dx0a}x0dx0ax0dx0avoid endgame()//結束游戲x0dx0a{x0dx0ax0dx0asystem("cls");x0dx0aPos(24,12);x0dx0aif(endgamestatus==1) {x0dx0aprintf("對不起,您撞到牆了。游戲結束.");x0dx0a} else if(endgamestatus==2) {x0dx0aprintf("對不起,您咬到自己了。游戲結束.");x0dx0a} else if(endgamestatus==3) {x0dx0aprintf("您的已經結束了游戲。");x0dx0a}x0dx0aPos(24,13);x0dx0aprintf("您的得分是%d\n",score);x0dx0aexit(0);x0dx0a}x0dx0ax0dx0avoid gamestart()//游戲初始化x0dx0a{x0dx0asystem("mode con cols=100 lines=30");x0dx0awelcometogame();x0dx0acreatMap();x0dx0ainitsnake();x0dx0acreatefood();x0dx0a}x0dx0ax0dx0aint main()x0dx0a{x0dx0agamestart();x0dx0agamecircle();x0dx0aendgame();x0dx0areturn 0;x0dx0a}

5. 如何用c語言編寫一個小游戲

一、數學知識:

長方形的面積S=a*b

長方形周長L=2*(a+b)

其中a b分別為長方形的寬和高。

二、演算法分析:

長方形面積及周長巧棚均依賴於寬和高,所以先要輸入寬悶團高值,然後根據公式計算,輸出結果即可。

三、參考代碼:

#include<stdio.h>
voidmain()
{
螞寬橘doublea,b;
doubleL,S;
scanf("%lf%lf",&a,&b);//輸入寬和高。
L=2*(a+b);//計算周長。
S=a*b;//計算面積。
printf("面積=%lf,周長=%lf ",S,L);//輸出結果。
}

四、注意事項:

因為沒有限制輸入為整型,所以使用浮點型用來存儲各項值。輸入輸出要用%lf。

6. 求一些C語言小游戲的源代碼,謝謝

學習一下數字版「拼圖」代碼寫法:

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

#include<time.h>

#include<windows.h>

inti,j,r,k; //i、j、r用於循環,k存放隨機數值

intm,n; //m、n是當前空位的下標

inta[4][4]; //存儲4×4共16個數字的數組

voidshow(void); //輸出界面

voidcsh(void); //初始化界面

intyes(void); //判斷排序是否成功(1是0否)

voip(void);//數字向上移動到空位(空位則下移)

voiddown(void); //數字向下移

voidleft(void); //數字向左移

voidrght(void); //數字向右移

voidinkey(void); //按鍵操作

voidgtxy(intx,inty); //控制游標位置的函數

intmain(void)

{while(1)

{csh();

while(1)

{inkey();

show();

if(yes())

{gtxy(6,12);printf("你成功了!再來一局y/n?");break;}

}

if(getch()=='n')break;

}

return0;

}

voidcsh(void) //初始化

{r=0;

CONSOLE_CURSOR_INFOcursor_info={1,0};//以下兩行是隱藏游標的設置

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);

for(i=0;i<4;i++)//給數組a依序賦值

for(j=0;j<4;j++)

{if(i==3&&j==3)a[i][j]=0;

elsea[i][j]=1+r++;

}

m=3; n=3; //記下空格(值為0)的下標

down( ); rght( ); rght( ); down( ); //預演4步

srand((unsigned)time(0)); //初始化隨機數發生器

for(r=0;r<500;r++)//將數組各值打亂

{k=rand()%(4);

switch(k)

{case0:{up();break;}

case1:{down();break;}

case2:{left();break;}

case3:{rght();break;}

}

}

system("cls");

printf(" 數字拼圖");

printf(" ┌──────┬──────┬──────┬──────┐");

printf(" │││││");

printf(" ├──────┼──────┼──────┼──────┤");

printf(" │││││");

printf(" ├──────┼──────┼──────┼──────┤");

printf(" │││││");

printf(" ├──────┼──────┼──────┼──────┤");

printf(" │││││");

printf(" └──────┴──────┴──────┴──────┘");

show();

}

voidshow(void)//輸出界面

{for(i=0;i<4;i++)

for(j=0;j<4;j++) //gtxy(7*j+9,2*i+4)是游標到指定位置輸出數字

{gtxy(7*j+9,2*i+4); if(a[i][j]==0)printf("│");

elseif(a[i][j]>9)printf("%d│",a[i][j]);

elseprintf("%d│",a[i][j]);

}

}

voidinkey(void) //按鍵操作

{intkey;

key=getch();

switch(key)

{case72:{up();break;}

case80:{down();break;}

case75:{left();break;}

case77:{rght();break;}

}

}

voip(void) //向上移動

{if(m!=3)//空位不得在下邊界

{a[m][n]=a[m+1][n];m++;a[m][n]=0;}

}

voiddown(void) //向下移動

{if(m!=0)//空位不得在上邊界

{a[m][n]=a[m-1][n];m--;a[m][n]=0;}

}

voidleft(void) //向左移動

{if(n!=3)//空位不得在右邊界

{a[m][n]=a[m][n+1];n++;a[m][n]=0;}

}

voidrght(void) //向右移動

{if(n!=0)//空位不得在左邊界

{a[m][n]=a[m][n-1];n--;a[m][n]=0;}

}

intyes(void)//判斷是否成功

{r=0;

for(i=0;i<4;i++)

for(j=0;j<4;j++)

{if(a[i][j]!=1+r++)return(r==16)?1:0;}

}

voidgtxy(intx,inty)//控制游標位置的函數

{COORDcoord;

coord.X=x;

coord.Y=y;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);

}

7. 用C語言編寫的小游戲代碼是什麼

/*也不知道你是什麼級別的,我是一個新手,剛接觸編程語言,以下是我自己變得一個小程序,在所有c語言的編譯器(vc++6.0、turbo…………)上都能運行,你還可以進一步改進。這是一個類似貪吃蛇的小游戲。祝你好運*/
/*貪吃蛇*/
#include<stdio.h>
#include<time.h>
#include<conio.h>
#include<stdlib.h>
int head=3 ,tail=0;
int main()
{
int i,j,k=0;
int zuobiao[2][80];
long start;
int direction=77;
int gamespeed;
int timeover;
int change(char qipan[20][80],int zuobiao[2][80],char direction);
zuobiao[0][tail]=1;zuobiao[1][tail]=1;zuobiao[0][1]=1;zuobiao[1][1]=2;zuobiao[0][2]=1;zuobiao[1][2]=3;zuobiao[0][head]=1;zuobiao[1][head]=4;
/*處理棋盤*/
char qipan[20][80];//定義棋盤
for(i=0;i<20;i++)
for(j=0;j<80;j++)
qipan[i][j]=' ';//初始化棋盤
for(i=0;i<80;i++)
qipan[0][i]='_';
for(i=0;i<20;i++)
qipan[i][0]='|';
for(i=0;i<20;i++)
qipan[i][79]='|';
for(i=0;i<80;i++)
qipan[19][i]='_';
qipan[1][1]=qipan[1][2]=qipan[1][3]='*';//初始化蛇的位置
qipan[1][4]='#';
printf("This is a game of a SNAKE.\nGOOD LUCK TO YOU !\n");
printf("Input your game speed,please.(e.g.300)\n");
scanf("%d",&gamespeed);

while(direction!='q')
{
system("cls");
for(i=0;i<20;i++)//列印出棋盤
for(j=0;j<80;j++)
printf("%c",qipan[i][j]);
timeover=1;
start=clock();
while(!kbhit()&&(timeover=clock()-start<=gamespeed));
if(timeover)
{
getch();
direction=getch();
}
else
direction=direction;
if(!(direction==72||direction==80||direction==75||direction==77))
{
return 0;
system("cls");
printf("GAME OVER!\n");
}
if(!change(qipan,zuobiao,direction))
{
direction='q';
system("cls");
printf("GAME OVER!\n");
}
}
return 0;
}
int change(char qipan[20][80],int zuobiao[2][80],char direction)
{
int x,y;
if(direction==72)
x=zuobiao[0][head]-1;y=zuobiao[1][head];
if(direction==80)
x=zuobiao[0][head]+1;y=zuobiao[1][head];
if(direction==75)
x=zuobiao[0][head];y=zuobiao[0][head]-1;
if(direction==77)
x=zuobiao[0][head];y=zuobiao[1][head]+1;
if(x==0||x==18||y==78||y==0)
return 0;
if(qipan[x][y]!=' ')
return 0;
qipan[zuobiao[0][tail]][zuobiao[1][tail]]=' ';
tail=(tail+1)%80;
qipan[zuobiao[0][head]][zuobiao[1][head]]='*';
head=(head+1)%80;
zuobiao[0][head]=x;
zuobiao[1][head]=y;
qipan[zuobiao[0][head]][zuobiao[1][head]]='#';
return 1;
}