① 給我提供個小游戲的C 語言代碼
2L你給的什麼呀,明明是加密演算法調用我給你一個貪吃蛇的 C# #define N 200/*定義全局常量*/
#define m 25
#include <graphics.h>
#include <math.h>
#include <stdlib.h>
#include <dos.h>
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define Esc 0x011b
int i,j,key,k;
struct Food/*構造食物結構體*/
{
int x;
int y;
int yes;
}food;
struct Goods/*構造寶貝結構體*/
{
int x;
int y;
int yes;
}goods;
struct Block/*構造障礙物結構體*/
{
int x[m];
int y[m];
int yes;
}block;
struct Snake{/*構造蛇結構體*/
int x[N];
int y[N];
int node;
int direction;
int life;
}snake;
struct Game/*構建游戲級別參數體*/
{
int score;
int level;
int speed;
}game;
/*定義函數*/
void init(void);/*定義圖形驅動*/
void close(void);/*定義關閉函數*/
void drawk(void);/*定義界面函數*/
void gameover(void);/*定義游戲結束函數*/
void gameplay(void);/*定義游戲主函數*/
void prscore(void);/*定義得分函數*/
void main(void){/*主函數體,調用以下四個函數*/
init();
setbkcolor(7);
drawk();
gameplay();
close();
}
void init(void){/*構建圖形驅動函數*/
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
cleardevice();
}
void drawk(void){/*構建游戲界面函數*/
/*setbkcolor(LIGHTGREEN);*/
char str3[50];
setfillstyle(SOLID_FILL,BLUE);/*條型邊框,顯示版本信息*/
bar3d(48,9,610,38,1,45);
setcolor(YELLOW);/*版本信息*/
sprintf(str3,"Version:5.01,Powerwing Studio");
outtextxy(330,20,str3);
setfillstyle(LTSLASH_FILL,YELLOW);/*設定牆邊的填充形式*/
bar3d(48,48,58,462,0,0);/*設定牆邊*/
bar3d(48,39,611,48,0,0);
bar3d(48,452,611,462,0,0);
bar3d(602,39,611,462,0,0);
}
void gameplay(void){/*構建游戲主函數*/
/*初始化游戲角色*/
randomize();/*隨機數發生器*/
goods.yes=1;
block.yes=1;
food.yes=1;/*場景中需建立新的食物*/
snake.life=1;/*初始化蛇生命值*/
snake.direction=1;/*蛇起始的移動方向定義為向右*/
snake.x[0]=100;snake.y[0]=100;/*蛇頭的位置坐標初始化*/
snake.x[1]=110;snake.y[1]=100;
snake.node=2;/*蛇初始化節數,共兩節只有蛇頭*/
/*初始化障礙物的數組*/
block.x[0]=170;block.y[0]=270;/*level 1*/
block.x[1]=410;block.y[1]=310;
block.x[2]=300;block.y[2]=200;
block.x[3]=320;block.y[3]=420;
block.x[4]=250;block.y[4]=350;
block.x[5]=220;block.y[5]=320;/*level 2*/
block.x[6]=310;block.y[6]=410;
block.x[7]=400;block.y[7]=500;
block.x[8]=230;block.y[8]=230;
block.x[9]=280;block.y[9]=280;
block.x[10]=170;block.y[10]=280;/*level 3*/
block.x[11]=420;block.y[11]=310;
block.x[12]=310;block.y[12]=200;
block.x[13]=320;block.y[13]=400;
block.x[14]=250;block.y[14]=260;/*level 4*/
block.x[15]=220;block.y[15]=330;
block.x[16]=130;block.y[16]=410;
block.x[17]=310;block.y[17]=510;
block.x[18]=230;block.y[18]=340;
block.x[19]=280;block.y[19]=380;
block.x[20]=270;block.y[20]=170;/*level 5*/
block.x[21]=410;block.y[21]=450;
block.x[22]=190;block.y[22]=200;
block.x[23]=150;block.y[23]=320;
block.x[24]=270;block.y[24]=350;
block.x[25]=340;block.y[25]=320;
game.score=0;
game.speed=50000;
game.level=1;
prscore();/*得分初始化*/
while(1){/*判斷為真可以按Esc退出循環結束游戲*/
while(!kbhit()){/*無按鍵按下時,蛇自己移動身體*/
if(game.level==1){/*畫出障礙物*/
for(j=0;j<5;j++){
setcolor(5);/**/
rectangle(block.x[j],block.y[j],block.x[j]+10,block.y[j]-10);
block.yes=0;
}
}
if(game.level==2){/*畫出障礙物*/
for(j=0;j<9;j++){
setcolor(5);/**/
rectangle(block.x[j],block.y[j],block.x[j]+10,block.y[j]-10);
block.yes=0;
}
}
if(game.level==3){/*畫出障礙物*/
for(j=0;j<14;j++){
setcolor(5);/**/
rectangle(block.x[j],block.y[j],block.x[j]+10,block.y[j]-10);
block.yes=0;
}
}
if(game.level==4){/*畫出障礙物*/
for(j=0;j<19;j++){
setcolor(5);/**/
rectangle(block.x[j],block.y[j],block.x[j]+10,block.y[j]-10);
block.yes=0;
}
}
if(game.level==5){/*畫出障礙物*/
for(j=0;j<25;j++){
setcolor(5);/**/
rectangle(block.x[j],block.y[j],block.x[j]+10,block.y[j]-10);
block.yes=0;
}
}
if(food.yes==1){/*需要畫出新的食物*/
food.x=rand()%400+60;/*獲得間隔60的隨機數食物坐標值*/
food.y=rand()%350+60;
while(food.x%10!=0)/*判斷坐標值是否滿足被10整除,否,自動增加*/
food.x++;
while(food.y%10!=0)
food.y++;
food.yes=0;/*新的食物已經產生*/
}
if(goods.yes==1){/*需要畫出新的寶物*/
goods.x=rand()%380+60;/*獲得間隔60的隨機數寶貝坐標值*/
goods.y=rand()%320+80;
while(goods.x%10!=0)/*判斷坐標值是否滿足被10整除,否,自動增加*/
goods.x++;
while(goods.y%10!=0)
goods.y++;
goods.yes=0;/*新的寶貝已經產生*/
}
if(goods.yes==0){/*新寶貝產生,應顯示出來*/
setcolor(0);/*擦除*/
rectangle(goods.x,goods.y,goods.x+10,goods.y-10);
delay(50);/*延時*/
setcolor(YELLOW);
goods.x=goods.x+random(10)-random(20);/*隨機數增量*/
goods.y=goods.y+random(10)-random(20);
while(goods.x%10!=0)/*判斷變化後的坐標值是否滿足被10整除,否,自動增加*/
goods.x++;
while(goods.y%10!=0)
goods.y++;
rectangle(goods.x,goods.y,goods.x+10,goods.y-10);/*重畫出寶貝*/
if(goods.x<65||goods.x>585||goods.y<65|goods.y>445){/*判定寶貝是否越界*/
setcolor(0);/*擦除越界的寶貝*/
rectangle(goods.x,goods.y,goods.x+10,goods.y-10);
goods.yes=1;/*越界後重新生成寶貝*/
}
}
if(food.yes==0){/*新食物產生,應顯示出來*/
setcolor(GREEN);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*設定當前線型*/
rectangle(food.x,food.y,food.x+10,food.y-10);
}
for(i=snake.node-1;i>0;i--){/*取得需重畫的蛇的節數*/
snake.x[i]=snake.x[i-1];/*最後一節的坐標值等於倒數第二節的坐標值*/
snake.y[i]=snake.y[i-1];
}
switch(snake.direction){/*判斷蛇頭的移動方向*/
case 1:snake.x[0]+=10;break;/*向右*/
case 2:snake.x[0]-=10;break;/*向左*/
case 3:snake.y[0]-=10;break;/*向上*/
case 4:snake.y[0]+=10;break;/*向下*/
}
for(i=3;i<snake.node;i++){/*超過4節後,判斷蛇自身碰撞*/
if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0]){/*即自身的任一節坐標值與蛇頭坐標相等*/
for(i=1;i<snake.node-1;i++){/*擦除自己碰撞後位置蛇的身子*/
setcolor(0);
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],
snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);
}
snake.life-=1;/*生命值減少一*/
snake.node-=5;
prscore();/*輸出結果*/
if(snake.life==0){/*判斷生命值是否為0*/
gameover();/*游戲結束*/
break;/*退出內循環*/
}
}
}
if(snake.x[0]<55||snake.x[0]>595||snake.y[0]<55||snake.y[0]>455){/*判斷蛇是否與牆體碰撞*/
for(i=1;i<snake.node-1;i++){/*擦除撞牆後位置蛇的身子*/
setcolor(0);
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],
snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);
}
snake.x[0]=100;snake.y[0]=100;/*蛇頭的位置坐標重新初始化*/
snake.x[1]=110;snake.y[1]=100;
snake.direction=1;/*蛇起始的移動方向定義為向右*/
snake.life-=1;/*生命值減少一*/
snake.node-=5;/*相應節數減少5節*/
prscore();
if(snake.life==0){
gameover();
break;
}
}
/*判斷蛇與障礙物碰撞,食物是否與障礙物重疊*/
if(game.level==1){/*判斷級別,並設定相應的障礙物數量,即數組個數*/
k=5;
}
else if(game.level==2){
k=9;
}
else if(game.level==3){
k=14;
}
else if(game.level==4){
k=19;
}
else if(game.level==5){
k=25;
}
for(j=0;j<k;j++){
if(snake.x[0]==block.x[j]&&snake.y[0]==block.y[j]){
for(i=1;i<snake.node-1;i++){/*擦除撞牆後位置蛇的身子*/
setcolor(0);
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],
snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);
}
if(food.x==block.x[j]&&block.y[j]==food.y){/*防止障礙物與食物重疊*/
setcolor(0);/*設定食物的顏色為背景色,即擦除*/
rectangle(food.x,food.y,food.x+10,food.y-10);
food.yes=1;/*食物重新生成*/
}
snake.x[0]=100;snake.y[0]=100;/*蛇頭的位置坐標重新初始化*/
snake.x[1]=110;snake.y[1]=100;
snake.direction=1;/*蛇起始的移動方向定義為向右*/
snake.life-=1;
snake.node-=5;
prscore();
if(snake.life==0){
gameover();
break;
}
}
}
if(snake.x[0]==food.x&&snake.y[0]==food.y){/*判斷蛇是否吃到食物*/
setcolor(0);/*設定食物的顏色為背景色,即擦除*/
rectangle(food.x,food.y,food.x+10,food.y-10);
snake.x[snake.node]=-20;/*新的一節放在不可見的位置*/
snake.y[snake.node]=-20;
snake.node++;/*蛇身增加一節*/
if(snake.node>2){/*當節數每增加5節生命值增加一*/
snake.life=1+fabs((snake.node-2)/5);
}
food.yes=1;/*場景需要增加食物*/
game.score+=20;/*加分*/
prscore();/*輸出得分*/
}
if(snake.x[0]==goods.x&&snake.y[0]==goods.y){/*判定蛇是否得到寶貝*/
setcolor(0);/*設定寶貝的顏色為背景色,即擦除*/
rectangle(goods.x,goods.y,goods.x+10,goods.y-10);
goods.yes=1;/*場景需要增加新的寶貝*/
game.score+=100;/*得到寶貝後加100分*/
prscore();/*輸出得分*/
}
if(game.score<500){/*設定游戲速度和難度級別*/
game.speed=50000;
game.level=1;}
else if(game.score>=500&&game.score<1000){
game.level=2;
game.speed=40000;}
else if(game.score>=1000&&game.score<1500){
game.level=3;
game.speed=30000;}
else if(game.score>=1500&&game.score<2000){
game.level=4;
game.speed=20000; }
else if(game.score>=5000){
game.level=5;
game.speed=10000;}
setcolor(4);/*畫出移動的蛇*/
setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*設定當前線型*/
for(i=0;i<snake.node;i++)
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);
delay(game.speed);
setcolor(0);/*用背景色擦去最後一節*/
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],
snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);
} /*endwhile(! kbhit) */
if(snake.life==0)/*判斷循環結束條件:蛇死或者檢測到Esc按鍵*/
break;
key=bioskey(0);/*判斷按鍵*/
if(key==Esc)
break;
/*判斷蛇頭接收到的用戶按鍵響應的移動方向*/
else if(key==UP&&snake.direction!=4)
snake.direction=3;
else if(key==RIGHT&&snake.direction!=2)
snake.direction=1;
else if(key==LEFT&&snake.direction!=1)
snake.direction=2;
else if(key==DOWN&&snake.direction!=3)
snake.direction=4;
}/*endwhile(1)*/
}
void gameover(void){/*游戲結束處理*/
cleardevice();/*清屏*/
prscore();/*輸出得分*/
setcolor(RED);/*列印出「Game Over」字樣*/
settextstyle(0,0,4);
outtextxy(200,200,"Game Over!");
getch();
}
void prscore(void){/*定義分數輸出函數*/
char str1[10];
char str2[10];
char str4[20];
setfillstyle(SOLID_FILL,BLUE);/*用於清除舊的顯示信息*/
bar(49,10,320,37);
setcolor(WHITE);
settextstyle(0,0,1);
sprintf(str1,"score:%d",game.score);/*輸出得分*/
outtextxy(55,20,str1);
sprintf(str2,"level:%d",game.level);/*輸出級別*/
outtextxy(250,20,str2);
sprintf(str4,"life:%d",snake.life);/*輸出級別*/
outtextxy(150,20,str4);
}
void close(void){/*定義關閉函數,退出圖形模式*/
getch();
closegraph();
}
② 誰有一些簡單小游戲的c語言程序
可以學寫「貪吃蛇」代碼:
#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];
int y[100];
}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 setColor(unsigned short p, unsigned short q); //設定顯示顏色
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個位置
{ setColor(2, 0); //設定列印顏色為綠字黑底
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的倍數(為偶數)
}
setColor(12, 0); //設定列印顏色為淡紅字黑底
gtxy(fod.x,fod.y); printf("●"); //到食物坐標處列印初試食物
snk.len=3; //蛇身長
snk.speed=350; //刷新蛇的時間,即是移動速度
snk.x[0]=W/2+1; //蛇頭橫坐標要為偶數(因為W/2=39)
snk.y[0]=H/2; //蛇頭縱坐標
setColor(9, 0); //設定列印顏色為淡藍字黑底
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("■"); //列印蛇身
}
setColor(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要向右移動
}
setColor(9, 0);
gtxy(snk.x[0], snk.y[0]); printf("■"); //列印蛇頭
if (snk.x[0] == fod.x && snk.y[0] == fod.y) //如吃到食物則執行以下操作
{ printf("