① 新手c語言代碼

#include<stdio.h>

intmain(void)
{
inta,b,sum;

printf("輸入兩個加數: a");
scanf("%d%d",&a,&b);//<----------------

sum=a+b;

printf("%d ",sum);

return0;
}

② c語言小代碼,if語句的使用

if復合語句需要加上{}來表明其控制的語句;特別如果其只控制單個語句,則可以不加{}
你這個if沒加{},所以只能控制其順序下的一個語句,即
if(x!=0)
if(x<0)

printf("");

剩下的if(x>0) printf();語句不在首個if的下轄范圍內
而else語句與其相鄰最近的if語句搭配,因為沒有表明{},所以else語句與if(x>0) printf();語句搭配成為一個完整的if。。。else語句
所以輸出不正常
所以要改為
if(x!=0)
{
if(x<0)printf("x<0\n");

else printf("x>0\n");

}
else printf("x=0\n");

③ 求一個用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.
GOOD
LUCK
TO
YOU
!
");
printf("Input
your
game
speed,please.(e.g.300)
");
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!
");
}
if(!change(qipan,zuobiao,direction))
{
direction='q';
system("cls");
printf("GAME
OVER!
");
}
}
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;
}

④ c語言源程序代碼

你可以到「如鵬網」
裡面就是講C語言的
應該對你有用把
裡面有視屏教學

⑤ 求C語言小游戲源程序

簡易「推箱子」游戲C代碼:

#include <stdio.h>

#include <conio.h>

#include<stdlib.h>

#include<windows.h>

int m =0; //m代表第幾關

struct maps{short a[9][11]; };

struct maps map[5]={ 0,0,0,0,0,0,0,0,0,0,0, //共5關,每關9行

0,1,1,1,1,1,1,1,0,0,0,

0,1,0,0,0,0,0,1,1,1,0,

1,1,4,1,1,1,0,0,0,1,0, //0空地,1牆

1,5,0,0,4,0,0,4,0,1,0, //4是箱子,5是人

1,0,3,3,1,0,4,0,1,1,0, //3是目的地

1,1,3,3,1,0,0,0,1,0,0, //7是箱子在目的地(4+3)

0,1,1,1,1,1,1,1,1,0,0, //8是人在目的地(5+3)

0,0,0,0,0,0,0,0,0,0,0,

0,0,0,0,0,0,0,0,0,0,0,

0,0,1,1,1,1,0,0,0,0,0,

0,0,1,5,0,1,1,1,0,0,0,

0,0,1,0,4,0,0,1,0,0,0,

0,1,1,1,0,1,0,1,1,0,0,

0,1,3,1,0,1,0,0,1,0,0,

0,1,3,4,0,0,1,0,1,0,0,

0,1,3,0,0,0,4,0,1,0,0,

0,1,1,1,1,1,1,1,1,0,0,

0,0,0,0,0,0,0,0,0,0,0,

0,0,0,1,1,1,1,1,1,1,0,

0,0,1,1,0,0,1,0,5,1,0,

0,0,1,0,0,0,1,0,0,1,0,

0,0,1,4,0,4,0,4,0,1,0,

0,0,1,0,4,1,1,0,0,1,0,

1,1,1,0,4,0,1,0,1,1,0,

1,3,3,3,3,3,0,0,1,0,0,

1,1,1,1,1,1,1,1,1,0,0,

0,1,1,1,1,1,1,1,1,1,0,

0,1,0,0,1,1,0,0,0,1,0,

0,1,0,0,0,4,0,0,0,1,0,

0,1,4,0,1,1,1,0,4,1,0,

0,1,0,1,3,3,3,1,0,1,0,

1,1,0,1,3,3,3,1,0,1,1,

1,0,4,0,0,4,0,0,4,0,1,

1,0,0,0,0,0,1,0,5,0,1,

1,1,1,1,1,1,1,1,1,1,1,

0,0,0,0,0,0,0,0,0,0,0,

0,0,0,1,1,1,1,1,1,0,0,

0,1,1,1,0,0,0,0,1,0,0,

1,1,3,0,4,1,1,0,1,1,0,

1,3,3,4,0,4,0,0,5,1,0,

1,3,3,0,4,0,4,0,1,1,0,

1,1,1,1,1,1,0,0,1,0,0,

0,0,0,0,0,1,1,1,1,0,0,

0,0,0,0,0,0,0,0,0,0,0 };

void DrMap( ) //繪制地圖

{ CONSOLE_CURSOR_INFO cursor_info={1,0}; //隱藏游標的設置

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);

printf(" 推箱子");

printf(" ");

for (int i = 0; i < 9; i++)

{for (int j = 0; j < 11; j++)

{switch (map[m].a[i][j])

{case 0: printf(" "); break;

case 1: printf("■"); break;

case 3: printf("◎");break;

case 4: printf("□"); break;

case 5: printf("♀"); break; //5是人

case 7: printf("□"); break; //4 + 3箱子在目的地中

case 8: printf("♀");break; // 5 + 3人在目的地中

}

}

printf(" ");

}

}

void gtxy(int x, int y) //控制游標位置的函數

{ COORD coord;

coord.X = x;

coord.Y = y;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);

}

void start( ) //開始游戲

{ int r, c; //存儲人的下標

for (int i = 0; i < 9; i++)

{ for (int j = 0; j < 11; j++)

{if (map[m].a[i][j] == 5||map[m].a[i][j]==8) { r = i; c = j; } } //i j 人的下標

}

char key;

key = getch( );

switch (key)

{case 'W':

case 'w':

case 72:

if (map[m]. a[r - 1][c] == 0|| map[m]. a [r - 1][c] == 3)

{ gtxy(2*c+8,r-1+3); printf("♀"); // gtxy(2*c+8,r-1+3)是到指定位置輸出字元

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r - 1][c] += 5; map[m]. a [r][c] -= 5; }

else if (map[m]. a [r - 1][c] == 4 || map[m]. a [r - 1][c] == 7)

{ if (map[m]. a [r - 2][c] == 0 || map[m]. a [r - 2][c] == 3)

{ gtxy(2*c+8,r-2+3); printf("□"); gtxy(2*c+8,r-1+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r - 2][c] += 4; map[m]. a [r - 1][c] += 1;

map[m]. a [r][c] -= 5; }

} break;

case 'S':

case 's':

case 80:

if (map[m]. a [r + 1][c] == 0 || map[m]. a [r + 1][c] == 3)

{ gtxy(2*c+8,r+1+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r + 1][c] += 5; map[m]. a [r][c] -= 5; }

else if (map[m]. a [r + 1][c] == 4 || map[m]. a [r+ 1][c] == 7)

{ if (map[m]. a [r + 2][c] == 0 || map[m]. a [r + 2][c] == 3)

{ gtxy(2*c+8,r+2+3); printf("□"); gtxy(2*c+8,r+1+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r + 2][c] += 4; map[m]. a [r + 1][c] += 1;

map[m]. a [r][c] -= 5; }

}break;

case 'A':

case 'a':

case 75:

if (map[m]. a [r ][c - 1] == 0 || map[m]. a [r ][c - 1] == 3)

{ gtxy(2*(c-1)+8,r+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r ][c - 1] += 5; map[m]. a [r][c] -= 5; }

else if (map[m]. a [r][c - 1] == 4 || map[m]. a [r][c - 1] == 7)

{if (map[m]. a [r ][c - 2] == 0 || map[m]. a [r ][c - 2] == 3)

{ gtxy(2*(c-2)+8,r+3); printf("□"); gtxy(2*(c-1)+8,r+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r ][c - 2] += 4; map[m]. a [r ][c - 1] += 1;

map[m]. a [r][c] -= 5; }

}break;

case 'D':

case 'd':

case 77:

if (map[m]. a [r][c + 1] == 0 || map[m]. a [r][c + 1] == 3)

{ gtxy(2*(c+1)+8,r+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8) {gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r][c + 1] += 5; map[m]. a [r][c] -= 5; }

else if (map[m]. a [r][c + 1] == 4 || map[m]. a [r][c + 1] == 7)

{ if (map[m]. a [r][c + 2] == 0 || map[m]. a [r][c + 2] == 3)

{ gtxy(2*(c+2)+8,r+3); printf("□"); gtxy(2*(c+1)+8,r+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r][c + 2] += 4; map[m]. a [r][c + 1] += 1;

map[m]. a [r][c] -= 5; }

}break;

}

}

int ifwan( ) //是否完成(1是0否)

{ if(m==0){if(map[m].a[5][2]==7&& map[m].a[5][3]==7&&

map[m].a[6][2]==7&& map[m].a[6][3]==7) return 1;}

if(m==1){if(map[m].a[5][2]==7&& map[m].a[6][2]==7&&

map[m].a[7][2]==7) return 1;}

if(m==2){if(map[m].a[7][1]==7&& map[m].a[7][2]==7&& map[m].a[7][3]==7&&

map[m].a[7][4]==7&& map[m].a[7][5]==7) return 1;}

if(m==3){if(map[m].a[4][4]==7&& map[m].a[4][5]==7&& map[m].a[4][6]==7&&

map[m].a[5][4]==7&& map[m].a[5][5]==7&& map[m].a[5][6]==7) return 1;}

if(m==4){if(map[m].a[3][2]==7&& map[m].a[4][1]==7&& map[m].a[4][2]==7&&

map[m].a[5][1]==7&& map[m].a[5][2]==7) return 1;}

return 0;

}

int main( ) //主函數

{ while (1)

{ system("cls");

DrMap( );

while (1)

{ start( );

if(ifwan()){printf("07");break;} //完成後響鈴

}

m+=1;

}

return 0;

}

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

「猜數字小游戲」,每個數字後按空格,最後按回車確認

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

int a[4],b[4];

int count=0; //計算猜測次數

void csh( ); //初始化

void start( ); //開始游戲

int main( )

{ csh( );

start( );

}

void csh( ) //初始化

{ printf(" 猜 數 字 小 游 戲 ");

printf(「 猜四個數字,如數字與順序都正確記為A,數字正確位置不對記為B. 」);

}

void start( ) //開始游戲

{int m,n; //m是完全猜對的個數,n是順序不對的個數

while(1)

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

while(1) { for(int i=0;i<4;i++) a[i]=rand( )%10; //rand( )函數每次隨機產生一個0-9的數

if( (a[3]!=a[2]&&a[3]!=a[1]&&a[3]!=a[0])&&

(a[2]!=a[1]&&a[2]!=a[0])&&a[1]!=a[0] ) break; } //4個隨機數各自不相等

printf(" 請依次輸入4個一位整數: ");

while(1)

{for(int i=0;i<4;i++) scanf(「%d」,&b[i]);

printf(" 你輸入的是:%d %d %d %d ",b[0],b[1],b[2],b[3]);

m=0;n=0;

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

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

{ if(b[i]==a[j]&&i==j)m=m+1; if(b[i]==a[j]&&i!=j)n=n+1; }

}

count=count+1;

printf(" %dA %dB 你試了%d次 ",m,n,count);

if(m==4)break;

if(count==8){ count=0; break; }

}

printf(" ");

if(m==4)printf(" 你猜對了(^-^)! 就是:%d %d %d %d ",a[0],a[1],a[2],a[3]);

else printf(" 你輸了(T-T)!哈哈!應該是:%d %d %d %d ",a[0],a[1],a[2],a[3]);

int z;

printf(" (要繼續嗎?1或0) ");

scanf(「%d」,&z);

if(z==0) break;

}

}

⑦ 求簡單C語言程序代碼!

小游戲2048源碼:

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

#include<time.h>

#include<windows.h>

int jsk( ); //計算空格數

void rsgm( ); //重置游戲

void inkey( ); //按鍵輸入

void left( ); //向左移動

void right( ); //向右移動

void up( ); //向上移動

void down( ); //向下移動

void show( ); //輸出界面

void adnum( ); //添加隨機數

void yes( ); //游戲是否結束(1是0否)

void gtxy(int x, int y); //控制游標位置的函數

int a[4][4]; //存儲16個格子中的數字

int score = 0; //每局得分

int best = 0; //最高得分

int ifnum; //是否需要添加數字(1是0否)

int over; //游戲結束標志(1是0否)

int i,j,k;

int main( )

{ rsgm( ); //重置游戲

inkey( ); //按鍵輸入

return 0;

}

void setColor(unsigned short ForeColor = 7, unsigned short BackGroundColor = 0)

{ HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleTextAttribute(handle, ForeColor + BackGroundColor * 0x10);

} //用於控制字元顏色的函數

void rsgm( ) //重置游戲

{ score = 0; ifnum = 1; over = 0; srand((unsigned)time(0)); //啟動隨機數發生器

int n = rand( ) % 16; //隨機函數產生0-15的數字

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

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

{ if (n == 0) { int k = rand( ) % 3; if (k == 0 || k == 1) { a[i][j] = 2; }

else { a[i][j] = 4; } n--; }

else { a[i][j] = 0; n--; }

}

}

adnum( );

system("cls");

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

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);

setColor(14, 0); //設置字體淡紅色,背景為黑色

printf(" 2048小游戲"); setColor(7, 0); //恢復白字黑底

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

printf(" │ │ │ │ │");

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

printf(" │ │ │ │ │");

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

printf(" │ │ │ │ │");

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

printf(" │ │ │ │ │");

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

show( );

}

void show( ) //輸出界面

{ 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(" "); setColor(7, 0); printf("│");}

else if(a[i][j]<10){ if (a[i][j] == 2) {setColor(14, 0); }

else if (a[i][j] == 4) {setColor(13, 0); }

else if (a[i][j] == 8) {setColor(12, 0); }

printf(" %d ", a[i][j]); setColor(7, 0); printf("│");

}

else if (a[i][j] < 100){if (a[i][j] == 16) {setColor(12, 0); }

else if (a[i][j] == 32) {setColor(10, 0); }

else if (a[i][j] == 64) {setColor(2, 0); }

printf(" %d ", a[i][j]); setColor(7, 0); printf("│");

}

else if (a[i][j] < 1000) {if (a[i][j] == 128) {setColor(9, 0); }

else if (a[i][j] == 256) {setColor(1, 0); }

else if (a[i][j] == 512) {setColor(13, 0); }

printf(" %d ", a[i][j]); setColor(7, 0); printf("│");

}

else if (a[i][j] < 10000) {if (a[i][j] == 1024) {setColor(5, 0); }

else {setColor(15, 0); }

printf(" %d ", a[i][j]); setColor(7, 0); printf("│");

}

}

if (jsk( ) == 0)

{ yes( ); if (over) { gtxy(9,12); setColor(10, 0);

printf(" 游戲結束!是否繼續? [ Y/N ]:"); }

}

}

void inkey( ) //按鍵輸入

{ int key;

while (1)

{ key = getch( );

if (over) { if (key == 89|| key == 121) {rsgm( ); continue; }

else if (key == 78|| key == 110) { return; }

else continue; }

ifnum = 0;

if(key==224)key=getch( );

switch (key)

{ case 75: left( ); break;

case 77: right( ); break;

case 72: up( ); break;

case 80: down( );break;

}

if (score > best) { best = score; }

if (ifnum) { adnum( ); show( ); }

}

}

int jsk( ) //計算空格數

{ int n = 0;

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

{ for (j = 0; j < 4; j++) { if ( a[i][j] == 0) {n++;} } }

return n;

}

void left( ) //向左移動

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

{for (j = 1, k = 0; j < 4; j++)

{ if (a[i][j] > 0)

{ if ( a[i][k] == a[i][j])

{ a[i][k] *= 2; k++;

score = score + 2 * a[i][j];

a[i][j] = 0; ifnum = 1; }

else if ( a[i][k] == 0) { a[i][k] = a[i][j]; a[i][j] = 0; ifnum = 1; }

else { a[i][k + 1] = a[i][j]; if ((k + 1) != j) { a[i][j] = 0; ifnum = 1; }

k++; }

}

}

}

}

void right( ) //向右移動

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

{for (j = 2, k = 3; j >= 0; j--)

{if (a[i][j] > 0)

{ if (a[i][k] == a[i][j])

{a[i][k] *= 2; k--; score = score + 2 * a[i][j]; a[i][j] = 0; ifnum = 1; }

else if ( a[i][k] == 0) {a[i][k] = a[i][j]; a[i][j] = 0; ifnum = 1; }

else { a[i][k - 1] = a[i][j]; if ((k - 1) != j) { a[i][j] = 0; ifnum = 1; } k--; }

}

}

}

}

void up( ) //向上移動

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

{for (j = 1, k = 0; j < 4; j++)

{if (a[j][i] > 0)

{if ( a[k][i] == a[j][i]) { a[k][i] *= 2; k++;score = score + 2 * a[j][i];

a[j][i] = 0; ifnum = 1; }

else if ( a[k][i] == 0) { a[k][i] = a[j][i]; a[j][i] = 0; ifnum = 1; }

else { a[k + 1][i] = a[j][i]; if ((k + 1) != j) { a[j][i] = 0; ifnum = 1; }

k++; }

}

}

}

}

void down( ) //向下移動

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

{for (j = 2, k = 3; j >= 0; j--)

{if (a[j][i] > 0)

{if (a[k][i] == a[j][i])

{a[k][i] *= 2; k--;score = score + 2 * a[j][i]; a[j][i] = 0; ifnum = 1; }

else if (a[k][i] == 0) {a[k][i] = a[j][i]; a[j][i] = 0; ifnum = 1; }

else {a[k - 1][i] = a[j][i];

if ((k - 1) != j) {a[j][i] = 0; ifnum = 1; } k--; }

}

}

}

}

void adnum( ) //添加隨機數

{ srand(time(0)); //啟動隨機數發生器

int n = rand( ) % jsk( );

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

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

{ if (a[i][j] == 0) { if (n != 0) { n--; }

else {int k = rand() % 3;

if (k == 0 || k == 1) {a[i][j] = 2; return; }

else {a[i][j] = 4; return; } }

}

}

}

}

void yes( ) //游戲是否結束

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

{for (int j = 0; j < 3; j++)

{if (a[i][j] == a[i][j + 1] || a[j][i] == a[j + 1][i]) {over = 0; return; }}

}

over = 1;

}

void gtxy(int x, int y) //控制游標位置的函數

{ COORD coord;

coord.X = x;

coord.Y = y;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);

}

⑧ c語言小游戲代碼

「貪吃蛇」C代碼,在dev C++試驗通過(用4個方向鍵控制)

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

#include <time.h>

#include <Windows.h>

#define W 78 //游戲框的寬,x軸

#define H 26 //游戲框的高,y軸

int dir=3; //方向變數,初值3表示向「左」

int Flag=0; //吃了食物的標志(1是0否)

int score=0; //玩家得分

struct food{ int x; //食物的x坐標

int y; //食物的y坐標

}fod; //結構體fod有2個成員

struct snake{ int len; //蛇身長

int speed; //移動速度

int x[100]; //蛇身某節x坐標

int y[100]; //蛇身某節y坐標

}snk; //結構體snk有4個成員

void gtxy( int x,int y) //控制游標移動的函數

{ COORD coord;

coord.X=x;

coord.Y=y;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);

}

void gtxy( int x,int y); //以下聲明要用到的幾個自編函數

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

void keymove( ); //按鍵操作移動蛇

void putFod( ); //投放食物

int Over( ); //游戲結束(1是0否)

void Color(int a); //設定顯示顏色的函數

int main( ) //主函數

{ csh( );

while(1)

{ Sleep(snk.speed);

keymove( );

putFod( );

if(Over( ))

{ system(「cls」);

gtxy(W/2+1,H/2); printf(「游戲結束!T__T」);

gtxy(W/2+1,H/2+2); printf(「玩家總分:%d分」,score);

getch( );

break;

}

}

return 0;

}

void csh( ) //初始化界面

{ int i;

gtxy(0,0);

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

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);

for(i=0;i<=W;i=i+2) //橫坐標要為偶數,因為這個要列印的字元佔2個位置

{Color(2); //設定列印顏色為綠色

gtxy(i,0); printf("■"); //列印上邊框

gtxy(i,H); printf("■"); //列印下邊框

}

for(i=1;i<H;i++)

{ gtxy(0,i); printf("■"); //列印左邊框

gtxy(W,i); printf("■"); //列印右邊框

}

while(1)

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

fod.x=rand()%(W-4)+2; //隨機函數rand( )產生一個從0到比」(W-4)」小1的數再加2

fod.y=rand()%(H-2)+1; //隨機函數rand( )產生一個從0到比」(H-2)」小1的數再加1

if (fod.x%2==0) break; //fod.x是食物的橫坐標,要是2的倍數(為偶數)

}

Color(12); //設定列印顏色為淡紅

gtxy(fod.x,fod.y); printf("●"); //到食物坐標處列印初試食物

snk.len=3; //蛇身長初值為3節

snk.speed=350; //刷新蛇的時間,即移動速度初值為350毫秒

snk.x[0]=W/2+1; //蛇頭橫坐標要為偶數(因為W/2=39)

snk.y[0]=H/2; //蛇頭縱坐標

Color(9); //設定列印顏色為淡藍

gtxy(snk.x[0], snk.y[0]); printf("■"); //列印蛇頭

for(i=1;i<snk.len;i++)

{ snk.x[i]=snk.x[i-1]+2; snk.y[i]=snk.y[i-1];

gtxy(snk.x[i],snk.y[i]); printf("■"); //列印蛇身

}

Color(7, 0); //恢復默認的白字黑底

return;

}

void keymove( ) //按鍵操作移動蛇

{ int key;

if( kbhit( ) ) //如有按鍵輸入才執行下面操作

{ key=getch( );

if (key==224) //值為224表示按下了方向鍵,下面要再次獲取鍵值

{ key=getch( );

if(key==72&&dir!=2)dir=1; //72表示按下了向上方向鍵

if(key==80&&dir!=1)dir=2; //80為向下

if(key==75&&dir!=4)dir=3; //75為向左

if(key==77&&dir!=3)dir=4; //77為向右

}

if (key==32)

{ while(1) if((key=getch( ))==32) break; } //32為空格鍵,這兒用來暫停

}

if (Flag==0) //如沒吃食物,才執行下面操作擦掉蛇尾

{ gtxy(snk.x[snk.len-1],snk.y[snk.len-1]); printf(" "); }

int i;

for (i = snk.len - 1; i > 0; i--) //從蛇尾起每節存儲前一節坐標值(蛇頭除外)

{ snk.x[i]=snk.x[i-1]; snk.y[i]=snk.y[i-1]; }

switch (dir) //判斷蛇頭該往哪個方向移動,並獲取最新坐標值

{ case 1: snk.y[0]--; break; //dir=1要向上移動

case 2: snk.y[0]++; break; //dir=2要向下移動

case 3: snk.x[0]-=2; break; //dir=3要向左移動

case 4: snk.x[0]+=2; break; //dir=4要向右移動

}

Color(9);

gtxy(snk.x[0], snk.y[0]); printf("■"); //列印蛇頭

if (snk.x[0] == fod.x && snk.y[0] == fod.y) //如吃到食物則執行以下操作

{ printf("7"); snk.len++; score += 100; snk.speed -= 5; Flag = 1; } //7是響鈴

else Flag = 0; //沒吃到食物Flag的值為0

if(snk.speed<150) snk.speed= snk.speed+5; //作弊碼,不讓速度無限加快

}

void putFod( ) //投放食物

{ if (Flag == 1) //如吃到食物才執行以下操作,生成另一個食物

{ while (1)

{ int i,n= 1;

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

fod.x = rand( ) % (W - 4) + 2; //產生在游戲框范圍內的一個x坐標值

fod.y = rand( ) % (H - 2) + 1; //產生在游戲框范圍內的一個y坐標值

for (i = 0; i < snk.len; i++) //隨機生成的食物不能在蛇的身體上

{ if (fod.x == snk.x[i] &&fod.y == snk.y[i]) { n= 0; break;} }

if (n && fod.x % 2 == 0) break; //n不為0且橫坐標為偶數,則食物坐標取值成功

}

Color(12); //設定字元為紅色

gtxy(fod.x, fod.y); printf("●"); //游標到取得的坐標處列印食物

}

return;

}

int Over( ) //判斷游戲是否結束的函數

{ int i;

Color(7);

gtxy(2,H+1); printf(「暫停鍵:space.」); //以下列印一些其它信息

gtxy(2,H+2); printf(「游戲得分:%d」,score);

if (snk.x[0] == 0 || snk.x[0] == W) return 1; //蛇頭觸碰左右邊界

if (snk.y[0] == 0 || snk.y[0] == H) return 1; //蛇頭觸碰上下邊界

for (i = 1; i < snk.len; i++)

{ if (snk.x[0] == snk.x[i] && snk.y[0] == snk.y[i]) return 1; } //蛇頭觸碰自身

return 0; //沒碰到邊界及自身時就返回0

}

void Color(int a) //設定顏色的函數

{ SetConsoleTextAttribute(GetStdHandle( STD_OUTPUT_HANDLE ),a ); }

⑨ 最簡單的C語言代碼

最簡單的C語言代就是輸出「helloWord」,通常是作為初學編程語言時的第一個程序代碼。具體代碼如下:

#include <stdio.h>

int main(){

printf("Hello, World! ");

return 0;

}

(9)c語言小代碼擴展閱讀:

1、程序的第一行#include <stdio.h>是預處理器指令,告訴 C 編譯器在實際編譯之前要包含 stdio.h 文件。

2、下一行intmain()是主函數,程序從這里開始執行。

3、下一行printf(...)是C中另一個可用的函數,會在屏幕上顯示消息"Hello,World!"。

4、下一行return0;終止main()函數,並返回值0。

⑩ C語言編寫一個小程序

#include<stdio.h>
#include<time.h>
intmain()
{
inta,b;
srand(time(NULL));
a=rand();
scanf("%d",&b);
a==b?printf("true"):printf("false");
return0;
}

//運行抄示例: