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

c語言小游戲大全免費

發布時間: 2022-07-02 00:19:35

c語言編寫小游戲

//相當精簡的貪吃蛇程序,在vc完全可以運行
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<Windows.h>
#include<conio.h>
#define width 25
#define height 60
#define w '-'
#define h '|'
#define SNAKE 's'
#define FOOD 'o'
#define UP 'w'
#define DOWN 's'
#define LEFT 'a'
#define RIGHT 'd'
char wall[width][height] = { 0 };
//定義位置結構體,變數僅兩個
typedef struct _spos
{
int x;
int y;
}Spos;
//定義節點,用於產生蛇身子節點變數。蛇一般有三個節點,每個節點可以指向前後身子,用於來調整蛇的前進與後退
typedef struct _node
{
Spos pos;
struct _node *next;
struct _node *head;
}Node;
//定義食物變數,在特定位置產生食物符號
typedef struct _food
{
int x;
int y;
}Food;
//定義初始化節點函數,用於動態創建初始節點所需的空間。
Node *CreateNode()
{
Node *phead = NULL;
phead = (Node *)malloc(sizeof(Node));
return phead;
}
//聲明全局變數,蛇頭;以供初始化及銷毀特定位置節點
extern Node *snake=NULL;
//定義動作函數吃食物,當蛇遇到食物時,食物消失。
BOOL EatFood(Spos *snake )
{
if (wall[snake->x][snake->y] == FOOD)
return TRUE;
else
return FALSE;
}
//定義貪吃蛇游戲結束函數,當違反游戲規則(即就是蛇頭撞牆)時退出。
BOOL GameOver(Spos *snake)
{
if (wall[snake->x][snake->y] == w || wall[snake->x][snake->y] == h)
return TRUE;
else
return FALSE;
}
//定義刪除蛇尾函數
void Delsnake()
{
Node *pNew = NULL;
Node *snake1 = snake;
Node *p;
p=pNew = snake1;/*->head*/
while (pNew->next != NULL)
{
p = pNew;
pNew = pNew->next;
}
wall[pNew->pos.x][pNew->pos.y] = ' ';
free(pNew);
p->next = NULL;
}
//定義食物產生(即就是舊的食物消失)動作函數
void Getfood()
{
Food foods;
int x, y;
do{
x = rand() % (width -1) + 1;
y = rand() % (height-1 ) + 1;
} while (wall[x][y] != ' ');
foods.x = x;
foods.y = y;
wall[x][y] = FOOD;
}
void initsanke()
{
Node *pNew = NULL;
Node *head = (Node *)malloc(sizeof(Node));
int snake_len = 3;
int x = 10;
int y = 20;
int i;
pNew =head;
pNew->pos.x = x;
pNew->pos.y = y;
wall[x][y] = SNAKE;
//關鍵的一句,頭結點賦值給蛇頭
snake = pNew;
//循環產生蛇身,蛇尾並列印圖像s
for (i = 1; i < snake_len; i++)
{
y = pNew->pos.y;
pNew->next = CreateNode();
pNew = pNew->next;
pNew->pos.x = x;
pNew->pos.y = y + 1;
pNew->next = NULL;
wall[x][y + 1] = SNAKE;
}

}
void showwall()
{
int i, j;
for (i=0; i<width; i++)
{
for (j = 0; j < height; j++)
{
printf("%c", wall[i][j]);
}
if(i!= width-1) printf("\n");
}

}
void initwall()
{
int i, j;
for (i = 0; i < width; i++)
{
wall[i][0] = h;
wall[i][height-1] =h;
for (j = 1; j < height - 1; j++)
wall[i][j] = ' ';
}
for (i = 0; i < height; i++)
{
wall[0][i] = w;
wall[width-1][i] =w;
}
}
BOOL MoveToup()
{
//Node *snake = NULL;
Node *pnew = NULL;
Food *foods = NULL;
BOOL eat;
Spos spos;

spos.x = snake->pos.x - 1;
spos.y = snake->pos.y;
eat = EatFood(&spos);
if (GameOver(&spos))
return FALSE;
pnew = CreateNode();
pnew->pos.x = spos.x;
pnew->pos.y = spos.y;
pnew->next = snake;
snake = pnew;
wall[spos.x][spos.y] = SNAKE;
if (eat)
{
Getfood();
}
else
{
Delsnake();
}
return TRUE;
}
BOOL MoveToright()
{
//Node *snake = NULL;
Node *pnew = NULL;
Food *foods = NULL;
BOOL eat;
Spos spos;

spos.x = snake->pos.x;
spos.y = snake->pos.y + 1;
eat = EatFood(&spos);
if (GameOver(&spos))
return FALSE;
pnew = CreateNode();
pnew->pos.x = spos.x;
pnew->pos.y = spos.y;
pnew->next = snake;
snake = pnew;
wall[spos.x][spos.y] = SNAKE;
if (eat)
{
Getfood();
}
else
{
Delsnake();
}

return TRUE;
}

BOOL MoveTodown()
{
//Node *snake = NULL;
Node *pnew = NULL;
Food *foods = NULL;
BOOL eat;
Spos spos;

spos.x = snake->pos.x + 1;
spos.y = snake->pos.y;
eat = EatFood(&spos);
if (GameOver(&spos))
return FALSE;
pnew = CreateNode();
pnew->pos.x = spos.x;
pnew->pos.y = spos.y;
pnew->next = snake;
snake = pnew;
wall[spos.x][spos.y] = SNAKE;
if (eat)
{
Getfood();
}
else
{
Delsnake();
}

return TRUE;
}
BOOL MoveToleft()
{
//Node *snake = NULL;
Node *pnew = NULL;
Food *foods = NULL;
BOOL eat;
Spos spos;

spos.x = snake->pos.x;
spos.y = snake->pos.y - 1;
eat = EatFood(&spos);
if (GameOver(&spos))
return FALSE;
pnew = CreateNode();
pnew->pos.x = spos.x;
pnew->pos.y = spos.y;
pnew->next = snake;
snake = pnew;
wall[spos.x][spos.y] = SNAKE;
if (eat)
{
Getfood();
}
else
{
Delsnake();
}
return TRUE;
}
void Menu()
{
BOOL move=FALSE;
char temp;
char ch=LEFT;
initwall();
initsanke();
srand((unsigned int)time(0));;
Getfood();
showwall();

while (1)
{
Sleep(100);
temp = ch;
if (_kbhit())//檢查是否有鍵盤輸入
ch = _getch();
if (ch != UP && ch != DOWN && ch != RIGHT && ch != LEFT)
ch = temp;
/*方向相反則無效化操作*/
if ((temp == UP && ch == DOWN) || (temp == DOWN && ch == UP))
ch = temp;
if ((temp == LEFT && ch == RIGHT) || (temp == RIGHT && ch == LEFT))
ch = temp;
switch (ch)
{
case 'a': move =MoveToleft(); break;
case 'w': move =MoveToup(); break;
case 's': move =MoveTodown(); break;
case 'd': move =MoveToright(); break;
default:
break;
}
if (move)
{
system("cls");
showwall();
}
else
break;
}
}
int main()
{
Menu();
printf("\nSorry!Game over!\n");
system("pause");
return 0;
}

㈡ 僅用c語言能編出哪些小游戲

可以編寫狼追兔子游戲,擲骰子游戲,24點游戲,井字棋游戲,農夫過河游戲,掃雷小游戲,人機猜數游戲,三色球游戲, 推箱子游戲,坦克大戰游戲,貪吃蛇游戲等。

㈢ C語言小游戲

貪吃蛇的源代碼

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>

typedef struct snake
{
int a;
int b;
struct snake *u;
struct snake *n;
}snake,*snake1;
typedef struct food
{
int a;
int b;
}food;
void main()
{
char c,c0 = 'd';
int i,j,k,n=1,t,at;
snake p,q;
snake *dd,*dd0,*dd1,*dd2;
food f;
srand(time(NULL));
p.u = NULL;
p.n = &q;
p.a = 5;p.b = 6;q.a = 5;q.b = 5;
q.u = &p;q.n = NULL;
dd=dd2= &q;
f.a=(rand()%15+1);
f.b=(rand()%15+1);
while(1)
{
srand(time(NULL));
system("cls");
for(i = 0;i < 17;i ++)
{
for(j = 0; j < 17;j++)
{

if(i == 0 )
printf("▁");
else if(i == 16)
printf("▔");
else if(j == 0)
printf("▕");
else if(j == 16)
printf("▏");
else if(i == p.a && j == p.b)
printf("■");
else if(i == f.a && j == f.b)
printf("★");
else
{
t = 0;
dd = dd2;
for(k = 0; k < n ;k++)
{
if(i == dd->a && j == dd->b)
{
printf("□");
t = 1;
break;
}

dd = dd->u;
}
if(t == 0)
printf(" ");
}

}printf("\n");
}
at = 0;
dd =dd2;
for(i=0;i<n;i++)
{
if(p.a == dd->a && p.b == dd->b)
{
printf("game over!!\n");
exit(0);
}
dd = dd->u;
}
if(p.a == f.a && p.b == f.b)
{
dd = dd2;
at =1;
f.a = (rand()%15+1);
f.b = (rand()%15+1);
for(i=0;i<n;i++)
{
if(f.a == dd->a && f.b == dd->b)
{
f.a = dd2->a;
f.b = dd2->b;
break;
}
}
n++;
}
if(kbhit())
{
c = getch();
dd = dd2;
if(c == 'w' && c0 != 's')
{
if(at == 1)
{
dd0 =(snake1)malloc(sizeof(snake));
dd0->a = dd2->a;dd0->b = dd2->b;
dd0->n = NULL;dd0->u = dd2;
dd2=dd0;
}
dd = dd2;
for(i = 0; i<n ; i++)
{
dd1 = dd->u;
dd->b = dd1->b;
dd->a = dd1->a;
dd = dd->u;
}
if(p.a == 1)
p.a = 15;
else
p.a = (p.a-1)%15;
}
else if(c == 's' && c0 != 'w')
{
if(at == 1)
{
dd0 =(snake1)malloc(sizeof(snake));
dd0->a = dd2->a;dd0->b = dd2->b;
dd0->n = NULL;dd0->u = dd2;
dd2=dd0;
}
dd = dd2;
for(i = 0; i<n ; i++)
{
dd1 = dd->u;
dd->b = dd1->b;
dd->a = dd1->a;
dd = dd->u;
}
p.a = (p.a%15)+1;
}
else if(c == 'a' && c0 != 'd')
{
if(at == 1)
{
dd0 =(snake1)malloc(sizeof(snake));
dd0->a = dd2->a;dd0->b = dd2->b;
dd0->n = NULL;dd0->u = dd2;
dd2=dd0;
}
dd = dd2;
for(i = 0; i<n ; i++)
{
dd1 = dd->u;
dd->b = dd1->b;
dd->a = dd1->a;
dd = dd->u;
}
if(p.b == 1)
p.b = 15;
else
p.b = (p.b-1)%15;
}
else if(c == 'd' && c0 != 'a')
{
if(at == 1)
{
dd0 =(snake1)malloc(sizeof(snake));
dd0->a = dd2->a;dd0->b = dd2->b;
dd0->n = NULL;dd0->u = dd2;
dd2=dd0;
}
dd = dd2;
for(i = 0; i<n ; i++)
{
dd1 = dd->u;
dd->b = dd1->b;
dd->a = dd1->a;
dd = dd->u;
}
p.b = (p.b%15)+1;
}
else
{
goto qq;
}
c0 = c;
}
else
{
qq: if(c0 == 'w')
{
if(at == 1)
{
dd0 =(snake1)malloc(sizeof(snake));
dd0->a = dd2->a;dd0->b = dd2->b;
dd0->n = NULL;dd0->u = dd2;
dd2=dd0;
}
dd = dd2;
for(i = 0; i<n ; i++)
{
dd1 = dd->u;
dd->b = dd1->b;
dd->a = dd1->a;
dd = dd->u;
}
if(p.a == 1)
p.a = 15;
else
p.a=(p.a-1)%15;
}
else if(c0 == 's')
{
if(at == 1)
{
dd0 =(snake1)malloc(sizeof(snake));
dd0->a = dd2->a;dd0->b = dd2->b;
dd0->n = NULL;dd0->u = dd2;
dd2=dd0;
}
dd = dd2;
for(i = 0; i<n ; i++)
{
dd1 = dd->u;
dd->b = dd1->b;
dd->a = dd1->a;
dd = dd->u;
}
p.a=(p.a%15)+1;
}
else if(c0 == 'a')
{

if(at == 1)
{
dd0 =(snake1)malloc(sizeof(snake));
dd0->a = dd2->a;dd0->b = dd2->b;
dd0->n = NULL;dd0->u = dd2;
dd2=dd0;
}
dd = dd2;
for(i = 0; i<n ; i++)
{
dd1 = dd->u;
dd->b = dd1->b;
dd->a = dd1->a;
dd = dd->u;
}
if(p.b == 1)
p.b = 15;
else
p.b=(p.b-1)%15;
}
else if(c0 == 'd')
{
if(at == 1)
{
dd0 =(snake1)malloc(sizeof(snake));
dd0->a = dd2->a;dd0->b = dd2->b;
dd0->n = NULL;dd0->u = dd2;
dd2=dd0;
}
dd = dd2;
for(i = 0; i<n ; i++)
{
dd1 = dd->u;
dd->b = dd1->b;
dd->a = dd1->a;
dd = dd->u;
}
p.b=(p.b%15)+1;

}
}
fflush(stdin);
dd = &q;
_sleep(0);
}
}

㈣ 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語言編譯的小游戲(如掃雷),附帶源代碼和注釋。

小游戲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語言數字記憶小游戲,望採納

#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<time.h>
#defineTURE1
#defineFALSE0

voidmain()

{
charanother_name='Y';
charanother_game='Y';

intcorrect='TRUE';
intcounter=0;
intsequence_length=0;
inti=0;
longintseed=0;
intnumber=0;
longintnow=0;
longtime_taken=0;
intclock_per_sec;

printf(" ---------------------------------記憶小游戲-------------------------------------------- ");
printf("請牢記屏幕上出現的沒一個數字,並在規定時間內輸入您記下的數字。 ");
printf("回車開始游戲。 ");

scanf("%c",&another_game);

do
{
correct='TRUE';
counter=0;
sequence_length=2;
time_taken=clock();

while(correct)
{
sequence_length+=(counter++%3==0);
seed=time(NULL);
now=clock();
srand((int)seed);
for(i=0;i<=sequence_length;i++)
printf("%d",rand()%10);
for(;clock()-now<clock_per_sec;);
printf(" ");
for(i=0;i<=sequence_length;i++)
printf("");
if(counter==1)
printf(" 輸入您記住的數字,以空格隔開。 ");
else
printf(" ");
srand((int)seed);
for(i=0;i<=sequence_length;i++)
{
scanf("%d",&number);
if(number!=rand()%10)
{
correct=FALSE;
break;

}

}
printf("%s ",correct?"正確!":"錯誤!");

}
time_taken=(clock()-time_taken)/clock_per_sec;
printf(" 您的成績是:%d",--counter*100/time_taken);
fflush(stdin);
printf(" 是否繼續游戲?(Y/N)? ");
scanf("%c",&another_game);

}
while(another_game=='y'||another_game=='Y');
}

㈦ C語言可以寫哪些小游戲

C語言可以編手機游戲. 你叫他去死 不過我這有 貪吃蛇的代碼,你倒可以看看 (用TC 編譯一定過)

#include
#include
#include
#include
#include
#define Enter 7181
#define ESC 283
#define UP 18432
#define DOWN 20480
#define LEFT 19200
#define RIGHT 19712
#ifdef __cplusplus
#define __CPPARGS ...
#else
#define __CPPARGS
#endif
void interrupt (*oldhandler)(__CPPARGS);
void interrupt newhandler(__CPPARGS);
void SetTimer(void interrupt (*IntProc)(__CPPARGS));
void KillTimer(void);
void Initgra(void);
void TheFirstBlock(void);
void DrawMap(void);
void Initsnake(void);
void Initfood(void);
void Snake_Headmv(void);
void Flag(int,int,int,int);
void GameOver(void);
void Snake_Bodymv(void);
void Snake_Bodyadd(void);
void PrntScore(void);
void Timer(void);
void Win(void);
void TheSecondBlock(void);
void Food(void);
void Dsnkorfd(int,int,int);
void Delay(int);
struct Snake
{int x;int y;int color;}Snk[12];
struct Food
{int x;int y;int color;}Fd;
int flag1=1,flag2=0,flag3=0,flag4=0,flag5=0,flag6=0,
checkx,checky,num,key=0,Times,Score,Hscore,Snkspeed,TimerCounter,TureorFalse;
char Sco[2],Time[6];
void main()
{ Initgra();
SetTimer(newhandler);
TheFirstBlock();
while(1)
{DrawMap();
Snake_Headmv();
GameOver();
Snake_Bodymv();
Snake_Bodyadd();
PrntScore();
Timer();
Win();
if(key==ESC)
break;
if(key==Enter)
{cleardevice();
TheFirstBlock();
}
TheSecondBlock();
Food();
Delay(Snkspeed);
}
closegraph();
KillTimer();
}
void interrupt newhandler(__CPPARGS)
{
TimerCounter++;
oldhandler();
}
void SetTimer(void interrupt (*IntProc)(__CPPARGS))
{
oldhandler=getvect(0x1c);
disable();
setvect(0x1c,IntProc);
enable();
}

void KillTimer()
{
disable();
setvect(0x1c,oldhandler);
enable();
}
void Initgra()
{int gd=DETECT,gm;
initgraph(&gd,&gm,"d:\\tc");
}
void TheFirstBlock()
{setcolor(11);
settextstyle(0,0,4);
outtextxy(100,220,"The First Block");
loop:key=bioskey(0);
if(key==Enter)
{cleardevice();
Initsnake();
Initfood();
Score=0;
Hscore=1;
Snkspeed=10;
num=2;
Times=0;
key=0;
TureorFalse=1;
TimerCounter=0;
Time[0]='0';Time[1]='0';Time[2]=':';Time[3]='1';Time[4]='0';Time[5]='\0';
}
else if(key==ESC) cleardevice();
else goto loop;
}
void DrawMap()
{line(10,10,470,10);
line(470,10,470,470);
line(470,470,10,470);
line(10,470,10,10);
line(480,20,620,20);
line(620,20,620,460);
line(620,460,480,460);
line(480,460,480,20);
}
void Initsnake()
{randomize();
num=2;
Snk[0].x=random(440);
Snk[0].x=Snk[0].x-Snk[0].x%20+50;
Snk[0].y=random(440);
Snk[0].y=Snk[0].y-Snk[0].y%20+50;
Snk[0].color=4;
Snk[1].x=Snk[0].x;
Snk[1].y=Snk[0].y+20;
Snk[1].color=4;
}
void Initfood()
{randomize();
Fd.x=random(440);
Fd.x=Fd.x-Fd.x%20+30;
Fd.y=random(440);
Fd.y=Fd.y-Fd.y%20+30;
Fd.color=random(14)+1;
}
void Snake_Headmv()
{if(bioskey(1))
{key=bioskey(0);
switch(key)
{case UP:Flag(1,0,0,0);break;
case DOWN:Flag(0,1,0,0);break;
case LEFT:Flag(0,0,1,0);break;
case RIGHT:Flag(0,0,0,1);break;

default:break;
}
}
if(flag1)
{checkx=Snk[0].x;
checky=Snk[0].y;
Dsnkorfd(Snk[0].x,Snk[0].y,0);
Snk[0].y-=20;
Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color);
}
if(flag2)
{checkx=Snk[0].x;
checky=Snk[0].y;
Dsnkorfd(Snk[0].x,Snk[0].y,0);
Snk[0].y+=20;
Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color);
}
if(flag3)
{checkx=Snk[0].x;
checky=Snk[0].y;
Dsnkorfd(Snk[0].x,Snk[0].y,0);
Snk[0].x-=20;
Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color);
}
if(flag4)
{checkx=Snk[0].x;
checky=Snk[0].y;
Dsnkorfd(Snk[0].x,Snk[0].y,0);
Snk[0].x+=20;
Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color);
}
}
void Flag(int a,int b,int c,int d)
{flag1=a;flag2=b;flag3=c;flag4=d;}
void GameOver()
{int i;
if(Snk[0].x460||Snk[0].y460)
{cleardevice();
setcolor(11);
settextstyle(0,0,4);
outtextxy(160,220,"Game Over");
loop1:key=bioskey(0);
if(key==Enter)
{cleardevice();
TheFirstBlock();
}
else
if(key==ESC)
cleardevice();
else
goto loop1;
}
for(i=3;i<num;i++)
{if(Snk[0].x==Snk[i].x&&Snk[0].y==Snk[i].y)
{cleardevice();
setcolor(11);
settextstyle(0,0,4);
outtextxy(160,220,"Game Over");
loop2:key=bioskey(0);
if(key==Enter)
{cleardevice();
TheFirstBlock();
}
else
if(key==ESC)
cleardevice();
else goto loop2;
}
}
}
void Snake_Bodymv()
{int i,s,t;
for(i=1;i<num;i++)
{Dsnkorfd(checkx,checky,Snk[i].color);
Dsnkorfd(Snk[i].x,Snk[i].y,0);
s=Snk[i].x;
t=Snk[i].y;
Snk[i].x=checkx;
Snk[i].y=checky;
checkx=s;
checky=t;
}
}
void Food()
{if(flag5)
{randomize();
Fd.x=random(440);
Fd.x=Fd.x-Fd.x%20+30;
Fd.y=random(440);
Fd.y=Fd.y-Fd.y%20+30;
Fd.color=random(14)+1;
flag5=0;
}
Dsnkorfd(Fd.x,Fd.y,Fd.color);
}
void Snake_Bodyadd()
{if(Snk[0].x==Fd.x&&Snk[0].y==Fd.y)
{if(Snk[num-1].x>Snk[num-2].x)
{num++;
Snk[num-1].x=Snk[num-2].x+20;
Snk[num-1].y=Snk[num-2].y;
Snk[num-1].color=Fd.color;
}
else
if(Snk[num-1].x<Snk[num-2].x)
{num++;
Snk[num-1].x=Snk[num-2].x-20;
Snk[num-1].y=Snk[num-2].y;
Snk[num-1].color=Fd.color;
}
else
if(Snk[num-1].y>Snk[num-2].y)
{num++;
Snk[num-1].x=Snk[num-2].x;
Snk[num-1].y=Snk[num-2].y+20;
Snk[num-1].color=Fd.color;
}
else
if(Snk[num-1].y<Snk[num-2].y)
{num++;
Snk[num-1].x=Snk[num-2].x;
Snk[num-1].y=Snk[num-2].y-20;
Snk[num-1].color=Fd.color;
}
flag5=1;
Score++;
}
}
void PrntScore()
{if(Hscore!=Score)
{setcolor(11);
settextstyle(0,0,3);
outtextxy(490,100,"SCORE");
setcolor(2);
setfillstyle(1,0);
rectangle(520,140,580,180);
floodfill(530,145,2);
Sco[0]=(char)(Score+48);
Sco[1]='\0';
Hscore=Score;
setcolor(4);
settextstyle(0,0,3);
outtextxy(540,150,Sco);
}
}
void Timer()
{if(TimerCounter>18)
{Time[4]=(char)(Time[4]-1);
if(Time[4]<'0')
{Time[4]='9';
Time[3]=(char)(Time[3]-1);
}
if(Time[3]<'0')
{Time[3]='5';
Time[1]=(char)(Time[1]-1);
}
if(TureorFalse)
{setcolor(11);
settextstyle(0,0,3);
outtextxy(490,240,"TIMER");
setcolor(2);
setfillstyle(1,0);
rectangle(490,280,610,320);
floodfill(530,300,2);
setcolor(11);
settextstyle(0,0,3);
outtextxy(495,290,Time);
TureorFalse=0;
}
if(Time[1]=='0'&&Time[3]=='0'&&Time[4]=='0')
{setcolor(11);
settextstyle(0,0,4);
outtextxy(160,220,"Game Over");
loop:key=bioskey(0);
if(key==Enter)
{cleardevice();
TheFirstBlock();
}
else if(key==ESC) cleardevice();
else goto loop;
}
TimerCounter=0;
TureorFalse=1;
}
}
void Win()
{if(Score==3)
Times++;
if(Times==2)
{cleardevice();
setcolor(11);
settextstyle(0,0,4);
outtextxy(160,220,"You Win");
loop:key=bioskey(0);
if(key==Enter)
{cleardevice();
TheFirstBlock();
key=0;
}
else if(key==ESC) cleardevice();
else goto loop;
}
}
void TheSecondBlock()
{if(Score==3)
{cleardevice();
setcolor(11);
settextstyle(0,0,4);
outtextxy(100,220,"The Second Block");
loop:key=bioskey(0);
if(key==Enter)
{cleardevice();
Initsnake();
Initfood();
Score=0;
Hscore=1;
Snkspeed=8;
num=2;
key=0;
}
else if(key==ESC) cleardevice();
else goto loop;
}
}
void Dsnkorfd(int x,int y,int color)
{setcolor(color);
setfillstyle(1,color);
circle(x,y,10);
floodfill(x,y,color);
}
void Delay(int times)
{int i;
for(i=1;i<=times;i++)
delay(15000);
}

㈧ C語言寫的小游戲

這就是經典游戲-掃雷 的代碼#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
#define LEFTPRESS 0xff01
#define LEFTCLICK 0xff10
#define LEFTDRAG 0xff19
#define MOUSEMOVE 0xff08
struct
{
int num;
int roundnum;
int flag;
}Mine[10][10];
int gameAGAIN=0;
int gamePLAY=0;
int mineNUM;
char randmineNUM[3];
int Keystate;
int MouseExist;
int MouseButton;
int MouseX;
int MouseY;
void Init(void);
void MouseOn(void);
void MouseOff(void);
void MouseSetXY(int,int);
int LeftPress(void);
int RightPress(void);
void MouseGetXY(void);
void Control(void);
void GameBegain(void);
void DrawSmile(void);
void DrawRedflag(int,int);
void DrawEmpty(int,int,int,int);
void GameOver(void);
void GameWin(void);
int MineStatistics(int,int);
int ShowWhite(int,int);
void GamePlay(void);
void Close(void);
void main(void)
{
Init();
Control();
Close();
}
void Init(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"D:\\tc20\\BGI");
}
void Close(void)
{
closegraph();
}
void MouseOn(void)
{
_AX=0x01;
geninterrupt(0x33);
}
void MouseOff(void)
{
_AX=0x02;
geninterrupt(0x33);
}
void MouseSetXY(int x,int y)
{
_CX=x;
_DX=y;
_AX=0x04;
geninterrupt(0x33);
}
int LeftPress(void)
{
_AX=0x03;
geninterrupt(0x33);
return(_BX&1);
}
int RightPress(void)
{
_AX=0x03;
geninterrupt(0x33);
return(_BX&2);
}
void MouseGetXY(void)
{
_AX=0x03;
geninterrupt(0x33);
MouseX=_CX;
MouseY=_DX;
}
void Control(void)
{
int gameFLAG=1;
while(1)
{
if(gameFLAG)
{
GameBegain();
GamePlay();
if(gameAGAIN==1)
{
gameAGAIN=0;
continue;
}
}
MouseOn();
gameFLAG=0;
if(LeftPress())
{
MouseGetXY();
if(MouseX>280&&MouseX<300&&MouseY>65&&MouseY<85)
{
gameFLAG=1;
continue;
}
}
if(kbhit())
break;
}
MouseOff();
}
void DrawSmile(void)
{
setfillstyle(SOLID_FILL,YELLOW);
fillellipse(290,75,10,10);
setcolor(YELLOW);
setfillstyle(SOLID_FILL,BLACK);
fillellipse(285,75,2,2);
fillellipse(295,75,2,2);
setcolor(BLACK);
bar(287,80,293,81);
}
void DrawRedflag(int i,int j)
{
setcolor(7);
setfillstyle(SOLID_FILL,RED);
bar(198+j*20,95+i*20,198+j*20+5,95+i*20+5);
setcolor(BLACK);
line(198+j*20,95+i*20,198+j*20,95+i*20+10);
}
void DrawEmpty(int i,int j,int mode,int color)
{
setcolor(color);
setfillstyle(SOLID_FILL,color);
if(mode==0)
bar(200+j*20-8,100+i*20-8,200+j*20+8,100+i*20+8);
else
if(mode==1)
bar(200+j*20-7,100+i*20-7,200+j*20+7,100+i*20+7);
}
void GameBegain(void)
{
int i,j;
cleardevice();
if(gamePLAY!=1)
{
MouseSetXY(290,70);
MouseX=290;
MouseY=70;
}
gamePLAY=1;
mineNUM=0;
setfillstyle(SOLID_FILL,7);
bar(190,60,390,290);
for(i=0;i<10;i++)
for(j=0;j<10;j++)
DrawEmpty(i,j,0,8);
setcolor(7);
DrawSmile();
randomize();//__page_break__
for(i=0;i<10;i++)
for(j=0;j<10;j++)
{
Mine[i][j].num=random(8);
if(Mine[i][j].num==1)
mineNUM++;
else
Mine[i][j].num=2;
Mine[i][j].flag=0;
}
sprintf(randmineNUM,"%d",mineNUM);
setcolor(1);
settextstyle(0,0,2);
outtextxy(210,70,randmineNUM);
mineNUM=100-mineNUM;
MouseOn();
}
void GameOver(void)
{
int i,j;
setcolor(0);
for(i=0;i<10;i++)
for(j=0;j<10;j++)
if(Mine[i][j].num==1)
{
DrawEmpty(i,j,0,RED);
setfillstyle(SOLID_FILL,BLACK);
fillellipse(200+j*20,100+i*20,7,7);
}
}
void GameWin(void)
{
setcolor(11);
settextstyle(0,0,2);
outtextxy(230,30,"YOU WIN!");
}
int MineStatistics(int i,int j)
{
int nNUM=0;
if(i==0&&j==0)
{
if(Mine[0][1].num==1)
nNUM++;
if(Mine[1][0].num==1)
nNUM++;
if(Mine[1][1].num==1)
nNUM++;
}
else
if(i==0&&j==9)
{
if(Mine[0][8].num==1)
nNUM++;
if(Mine[1][9].num==1)
nNUM++;
if(Mine[1][8].num==1)
nNUM++;
}
else
if(i==9&&j==0)
{
if(Mine[8][0].num==1)
nNUM++;
if(Mine[9][1].num==1)
nNUM++;
if(Mine[8][1].num==1)
nNUM++;
}
else
if(i==9&&j==9)
{
if(Mine[9][8].num==1)
nNUM++;
if(Mine[8][9].num==1)
nNUM++;
if(Mine[8][8].num==1)
nNUM++;
}
else if(j==0)
{
if(Mine[i][j+1].num==1)
nNUM++;
if(Mine[i+1][j].num==1)
nNUM++;
if(Mine[i-1][j].num==1)
nNUM++;
if(Mine[i-1][j+1].num==1)
nNUM++;
if(Mine[i+1][j+1].num==1)
nNUM++;
}
else if(j==9)
{
if(Mine[i][j-1].num==1)
nNUM++;
if(Mine[i+1][j].num==1)
nNUM++;
if(Mine[i-1][j].num==1)
nNUM++;
if(Mine[i-1][j-1].num==1)
nNUM++;
if(Mine[i+1][j-1].num==1)
nNUM++;
}
else if(i==0)
{
if(Mine[i+1][j].num==1)
nNUM++;
if(Mine[i][j-1].num==1)
nNUM++;
if(Mine[i][j+1].num==1)
nNUM++;
if(Mine[i+1][j-1].num==1)
nNUM++;
if(Mine[i+1][j+1].num==1)
nNUM++;
}
else if(i==9)
{
if(Mine[i-1][j].num==1)
nNUM++;
if(Mine[i][j-1].num==1)
nNUM++;
if(Mine[i][j+1].num==1)
nNUM++;
if(Mine[i-1][j-1].num==1)
nNUM++;
if(Mine[i-1][j+1].num==1)
nNUM++;
}
else
{
if(Mine[i-1][j].num==1)
nNUM++;
if(Mine[i-1][j+1].num==1)
nNUM++;
if(Mine[i][j+1].num==1)
nNUM++;
if(Mine[i+1][j+1].num==1)
nNUM++;
if(Mine[i+1][j].num==1)
nNUM++;
if(Mine[i+1][j-1].num==1)
nNUM++;
if(Mine[i][j-1].num==1)
nNUM++;
if(Mine[i-1][j-1].num==1)
nNUM++;
}//__page_break__
return (nNUM);
}
int ShowWhite(int i,int j)
{
if(Mine[i][j].flag==1||Mine[i][j].num==0)
return;
mineNUM--;
if(Mine[i][j].roundnum==0&&Mine[i][j].num!=1)
{
DrawEmpty(i,j,1,7);
Mine[i][j].num=0;
}
else
if(Mine[i][j].roundnum!=0)
{
DrawEmpty(i,j,0,8);
sprintf(randmineNUM,"%d",Mine[i][j].roundnum);
setcolor(RED);
outtextxy(195+j*20,95+i*20,randmineNUM);
Mine[i][j].num=0;
return ;
}

if(i!=0&&Mine[i-1][j].num!=1)
ShowWhite(i-1,j);
if(i!=0&&j!=9&&Mine[i-1][j+1].num!=1)
ShowWhite(i-1,j+1);
if(j!=9&&Mine[i][j+1].num!=1)
ShowWhite(i,j+1);
if(j!=9&&i!=9&&Mine[i+1][j+1].num!=1)
ShowWhite(i+1,j+1);
if(i!=9&&Mine[i+1][j].num!=1)
ShowWhite(i+1,j);
if(i!=9&&j!=0&&Mine[i+1][j-1].num!=1)
ShowWhite(i+1,j-1);
if(j!=0&&Mine[i][j-1].num!=1)
ShowWhite(i,j-1);
if(i!=0&&j!=0&&Mine[i-1][j-1].num!=1)
ShowWhite(i-1,j-1);
}
void GamePlay(void)
{
int i,j,Num;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
Mine[i][j].roundnum=MineStatistics(i,j);
while(!kbhit())
{
if(LeftPress())
{
MouseGetXY();
if(MouseX>280&&MouseX<300&&MouseY>65&&MouseY<85)
{
MouseOff();
gameAGAIN=1;
break;
}
if(MouseX>190&&MouseX<390&&MouseY>90&&MouseY<290)
{
j=(MouseX-190)/20;
i=(MouseY-90)/20;
if(Mine[i][j].flag==1)
continue;
if(Mine[i][j].num!=0)
{
if(Mine[i][j].num==1)
{
MouseOff();
GameOver();
break;
}
else
{
MouseOff();
Num=MineStatistics(i,j);
if(Num==0)
ShowWhite(i,j);
else
{
sprintf(randmineNUM,"%d",Num);
setcolor(RED);
outtextxy(195+j*20,95+i*20,randmineNUM);
mineNUM--;
}
MouseOn();
Mine[i][j].num=0;
if(mineNUM<1)
{
GameWin();
break;
}
}
}
}
}
if(RightPress())
{
MouseGetXY();
if(MouseX>190&&MouseX<390&&MouseY>90&&MouseY<290)
{
j=(MouseX-190)/20;
i=(MouseY-90)/20;
MouseOff();
if(Mine[i][j].flag==0&&Mine[i][j].num!=0)
{
DrawRedflag(i,j);
Mine[i][j].flag=1;
}
else
if(Mine[i][j].flag==1)
{
DrawEmpty(i,j,0,8);
Mine[i][j].flag=0;
}
}
MouseOn();
sleep(1);
}
}
}

㈨ 用C語言編寫小游戲(除了貪吃蛇)

#include#include#include#includetypedef struct snake{int a;int b;struct snake *u;struct snake *n;}snake,*snake1;typedef struct food{int a;int b;}food;void main(){ char c,c0 = 'd';int i,j,k,n=1,t,at;snake p,q;snake *dd,*dd0,*dd1,*dd2;food f;srand(time(NULL));p.u = NULL;p.n = &q;p.a = 5;p.b = 6;q.a = 5;q.b = 5;q.u = &p;q.n = NULL;dd=dd2= &q;f.a=(rand()%15+1);f.b=(rand()%15+1);while(1){srand(time(NULL));system("cls");for(i = 0;i a && j == dd->b){printf("㊣");t = 1;break;}dd = dd->u;}if(t == 0)printf(" ");}}printf("\n");}at = 0;dd =dd2;for(i=0;ia && p.b == dd->b){printf("game over,感謝試玩!!本游戲由付宇璠與胡群陽共同製作,如有雷同,純屬巧合。\n");exit(0);}dd = dd->u;}if(p.a == f.a && p.b == f.b){dd = dd2;at =1;f.a = (rand()%15+1);f.b = (rand()%15+1);for(i=0;ia && f.b == dd->b){f.a = dd2->a;f.b = dd2->b;break;}}n++;}if(kbhit()){c = getch();dd = dd2;if(c == 'w' && c0 != 's'){if(at == 1){dd0 =(snake1)malloc(sizeof(snake));dd0->a = dd2->a;dd0->b = dd2->b;dd0->n = NULL;dd0->u = dd2;dd2=dd0;}dd = dd2;for(i = 0; iu;dd->b = dd1->b;dd->a = dd1->a;dd = dd->u;}if(p.a == 1)p.a = 15;elsep.a = (p.a-1)%15;}else if(c == 's' && c0 != 'w'){if(at == 1){dd0 =(snake1)malloc(sizeof(snake));dd0->a = dd2->a;dd0->b = dd2->b;dd0->n = NULL;dd0->u = dd2;dd2=dd0;}dd = dd2;for(i = 0; iu;dd->b = dd1->b;dd->a = dd1->a;dd = dd->u;}p.a = (p.a%15)+1;}else if(c == 'a' && c0 != 'd'){if(at == 1){dd0 =(snake1)malloc(sizeof(snake));dd0->a = dd2->a;dd0->b = dd2->b;dd0->n = NULL;dd0->u = dd2;dd2=dd0;}dd = dd2;for(i = 0; iu;dd->b = dd1->b;dd->a = dd1->a;dd = dd->u;}if(p.b == 1)p.b = 15;elsep.b = (p.b-1)%15;}else if(c == 'd' && c0 != 'a'){if(at == 1){dd0 =(snake1)malloc(sizeof(snake));dd0->a = dd2->a;dd0->b = dd2->b;dd0->n = NULL;dd0->u = dd2;dd2=dd0;}dd = dd2;for(i = 0; iu;dd->b = dd1->b;dd->a = dd1->a;dd = dd->u;}p.b = (p.b%15)+1;}else{goto qq;}c0 = c;}else{qq:if(c0 == 'w'){if(at == 1){dd0 =(snake1)malloc(sizeof(snake));dd0->a = dd2->a;dd0->b = dd2->b;dd0->n = NULL;dd0->u = dd2;dd2=dd0;}dd = dd2;for(i = 0; iu;dd->b = dd1->b;dd->a = dd1->a;dd = dd->u;}if(p.a == 1)p.a = 15;elsep.a=(p.a-1)%15;}else if(c0 == 's'){if(at == 1){dd0 =(snake1)malloc(sizeof(snake));dd0->a = dd2->a;dd0->b = dd2->b;dd0->n = NULL;dd0->u = dd2;dd2=dd0;}dd = dd2;for(i = 0; iu;dd->b = dd1->b;dd->a = dd1->a;dd = dd->u;}p.a=(p.a%15)+1;}else if(c0 == 'a'){if(at == 1){dd0 =(snake1)malloc(sizeof(snake));dd0->a = dd2->a;dd0->b = dd2->b;dd0->n = NULL;dd0->u = dd2;dd2=dd0;}dd = dd2;for(i = 0; iu;dd->b = dd1->b;dd->a = dd1->a;dd = dd->u;}if(p.b == 1)p.b = 15;elsep.b=(p.b-1)%15;}else if(c0 == 'd'){if(at == 1){dd0 =(snake1)malloc(sizeof(snake));dd0->a = dd2->a;dd0->b = dd2->b;dd0->n = NULL;dd0->u = dd2;dd2=dd0;}dd = dd2;for(i = 0; iu;dd->b = dd1->b;dd->a = dd1->a;dd = dd->u;}p.b=(p.b%15)+1;}}fflush(stdin);dd = &q;_sleep(200);}}