用c編寫的小游戲
Ⅰ 僅用c語言能編出哪些小游戲
可以編寫狼追兔子游戲,擲骰子游戲,24點游戲,井字棋游戲,農夫過河游戲,掃雷小游戲,人機猜數游戲,三色球游戲, 推箱子游戲,坦克大戰游戲,貪吃蛇游戲等。
Ⅱ 用C語言編一個小游戲,註明編碼,(簡單易懂的游戲,不要復雜)
我這有許多C的小游戲。給你一個基礎的簡單的漢諾塔程序。你看看:
這是個漢諾塔程序,在調試的時候,輸入的數字最好不要大於15,因為每大一個數
所得的結果的步驟都會多一倍。如果你有耐心等待結果的話除外。漢諾塔是在歐洲
流行的一種游戲,有a,b,c三個竿。a竿上有若干個由大到小的圓盤,大的在下面,
小的在上面,b,c都是空桿,請你把a桿上的圓盤都倒到別的桿上,或b或c,在倒盤
的過程中不可以大的壓小的,實常式序如下:
#include <stdio.h>
int i=0;
main()
{
unsigned n;
printf("Please enter the number of discs: ");
scanf("%d",&n);
printf("\tneedle:\ta\t b\t c\n");
movedisc(n,'a','c','b');
printf("\t Total: %d\n",i);
getch();
}
movedisc(n,fromneedle,toneedle,usingneedle)
unsigned n;
char fromneedle,toneedle,usingneedle;
{
if(n>0)
{
movedisc(n-1,fromneedle,usingneedle,toneedle);
i++;
switch(fromneedle)
{
case 'a':switch(toneedle)
{
case 'b':printf("\t[%d]:\t%2d------>%2d\n",i,n,n);
break;
case 'c':printf("\t[%d]:\t%2d------------->%2d\n",i,n,n);
break;
}
break;
case 'b':switch(toneedle)
{
case 'a':printf("\t[%d]:\t%2d<----------%2d\n",i,n,n);
break;
case 'c':printf("\t[%d]:\t\t%2d------>%2d\n",i,n,n);
break;
}
break;
case 'c':switch(toneedle)
{
case 'a':printf("\t[%d]:\t%2d<--------------%2d\n",i,n,n);
break;
case 'b':printf("\t[%d]:\t\t%2d<--------%2d\n",i,n,n);
break;
}
break;
}
movedisc(n-1,usingneedle,toneedle,fromneedle);
}
}
Ⅲ 如何使用C語言編寫簡單小游戲
C語言是計算機專業都要學習的一門基礎學科。一般來說,是比較枯燥的.那麼,我們能不能通過編一些小游戲來提高它的趣味性呢?這樣學習程序設計,就不會是一件艱苦 ,枯燥的事,它變得象電腦游戲一樣充滿好奇,富有樂趣。
例如2048這款游戲:
方法/步驟:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#include<windows.h>
#define SIZE 4
static int score=0;
void putn(int n[][SIZE]);
void getn(int n[][SIZE]);
int isempty(int n[][SIZE]);
int isfull(int n[][SIZE]);
void math(int n[][SIZE],char c);
void tow(int n[][SIZE]);
void toa(int n[][SIZE]);
void tos(int n[][SIZE]);
void tod(int n[][SIZE]);
//主函數
int main()
{
int i,j;
int n[SIZE][SIZE];
char c=' ';
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE;j++)
{
n[i][j]=0;
}
}
printf( "*********************** "
" 2048(%dX%d) "
" control:W/A/S/D "
"press any key to begin "
"*********************** ",SIZE,SIZE);
getch();
system("cls");
//n[0][1]=2048;
//n[0][3]=2048;
while(1)
{
if(isempty(n))
getn(n);
putn(n);
if(!isempty(n)&&isfull(n))
break;
sleep(200);
c=getch();
while(c!='w'&&c!='a'&&c!='s'&&c!='d')
c=getch();
math(n,c);
system("cls");
}
printf(" Game Over! ",score);
return 0;
}
//函數
void putn(int n[][SIZE])
{
int i,j;
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE;j++)
printf("| ");
printf("| ");
for(j=0;j<SIZE;j++)
{
if(n[i][j]==0)
printf("| ");
else
printf("|%4d ",n[i][j]);
}
printf("| ");
for(j=0;j<SIZE;j++)
printf("|_____");
printf("| ");
}
printf("score: %d",score);
}
void getn(int n[][SIZE])
{
int a,b;
a=rand()%SIZE;
b=rand()%SIZE;
while(n[a][b]!=0)
{
a=rand()%SIZE;
b=rand()%SIZE;
}
n[a][b]=2;
}
int isempty(int n[][SIZE])
{
int i,j,count=0;
for(i=0;i<SIZE;i++)
for(j=0;j<SIZE;j++)
if(n[i][j]==0)
count++;
return count;
}
int isfull(int n[][SIZE])
{
int i,j,count=0;
for(i=0;i<SIZE;i++)
{
for(j=1;j<SIZE-1;j++)
{
if(n[i][j]==n[i][j+1]||n[i][j]==n[i][j-1])
count++;
}
}
for(j=0;j<SIZE;j++)
{
for(i=1;i<SIZE-1;i++)
{
if(n[i][j]==n[i+1][j]||n[i][j]==n[i-1][j])
count++;
}
}
return count>0?0:1;
}
void math(int n[][SIZE],char c)
{
switch(c)
{
case 'w':tow(n);break;
case 'a':toa(n);break;
case 's':tos(n);break;
case 'd':tod(n);break;
default :;
}
}
void tow(int n[][SIZE])
{
int i,j,a;
int m[SIZE];
for(a=0;a<SIZE;a++)
m[a]=0;
for(j=0;j<SIZE;j++)
{
for(a=0;a<SIZE;a++)
{
for(i=0;i<SIZE-1;i++)
{
if(n[i][j]==0)
{
n[i][j]=n[i+1][j];
n[i+1][j]=0;
}
}
}
}
for(j=0;j<SIZE;j++)
{
for(a=0,i=0;i<SIZE;i++)
{
if(n[i][j]!=n[i+1][j]&&n[i][j]!=0||n[i][j]==2048)
{
m[a++]=n[i][j];
n[i][j]=0;
}
else if(n[i][j]==n[i+1][j])
{
m[a++]=n[i][j]+n[i+1][j];
score+=m[a-1];
n[i][j]=0,n[i+1][j]=0;
}
}
for(i=0;i<SIZE;i++)
{
n[i][j]=m[i];
m[i]=0;
}
}
}
void toa(int n[][SIZE])
{
int i,j,a;
int m[SIZE];
for(a=0;a<SIZE;a++)
m[a]=0;
for(i=0;i<SIZE;i++)
{
for(a=0;a<SIZE;a++)
{
for(j=0;j<SIZE-1;j++)
{
if(n[i][j]==0)
{
n[i][j]=n[i][j+1];
n[i][j+1]=0;
}
}
}
}
for(i=0;i<SIZE;i++)
{
for(a=0,j=0;j<SIZE;j++)
{
if(n[i][j]!=n[i][j+1]&&n[i][j]!=0||n[i][j]==2048)
{
m[a++]=n[i][j];
n[i][j]=0;
}
else if(n[i][j]==n[i][j+1])
{
m[a++]=n[i][j]+n[i][j+1];
score+=m[a-1];
n[i][j]=0,n[i][j+1]=0;
}
}
for(j=0;j<SIZE;j++)
{
n[i][j]=m[j];
m[j]=0;
}
}
}
void tos(int n[][SIZE])
{
int i,j,a;
int m[SIZE];
for(a=0;a<SIZE;a++)
m[a]=0;
for(j=SIZE-1;j>=0;j--)
{
for(a=SIZE-1;a>=0;a--)
{
for(i=SIZE-1;i>0;i--)
{
if(n[i][j]==0)
{
n[i][j]=n[i-1][j];
n[i-1][j]=0;
}
}
}
}
for(j=SIZE-1;j>=0;j--)
{
for(a=SIZE-1,i=SIZE-1;i>=0;i--)
{
if(n[i][j]!=n[i-1][j]&&n[i][j]!=0||n[i][j]==2048)
{
m[a--]=n[i][j];
n[i][j]=0;
}
else if(n[i][j]==n[i-1][j])
{
m[a--]=n[i][j]+n[i-1][j];
score+=m[a+1];
n[i][j]=0,n[i-1][j]=0;
}
}
for(i=SIZE-1;i>=0;i--)
{
n[i][j]=m[i];
m[i]=0;
}
}
}
void tod(int n[][SIZE])
{
int i,j,a;
int m[SIZE];
for(a=0;a<SIZE;a++)
m[a]=0;
for(i=SIZE-1;i>=0;i--)
{
for(a=SIZE-1;a>=0;a--)
{
for(j=SIZE-1;j>0;j--)
{
if(n[i][j]==0)
{
n[i][j]=n[i][j-1];
n[i][j-1]=0;
}
}
}
}
for(i=SIZE-1;i>=0;i--)
{
for(a=SIZE-1,j=SIZE-1;j>=0;j--)
{
if(n[i][j]!=n[i][j-1]&&n[i][j]!=0||n[i][j]==2048)
{
m[a--]=n[i][j];
n[i][j]=0;
}
else if(n[i][j]==n[i][j-1])
{
m[a--]=n[i][j]+n[i][j-1];
score+=m[a+1];
n[i][j]=0,n[i][j-1]=0;
}
}
for(j=SIZE-1;j>=0;j--)
{
n[i][j]=m[j];
m[j]=0;
}
}
}
Ⅳ 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語言編寫簡單小游戲
編寫程序,實現如下表所示的5-魔方陣。
17
24
1
8
15
23
5
7
14
16
4
6
13
20
22
10
12
19
21
3
11
18
25
2
9
5-魔方陣
問題分析
所謂「n-魔方陣」,指的是使用1〜n2共n2個自然數排列成一個n×n的方陣,其中n為奇數;該方陣的每行、每列及對角線元素之和都相等,並為一個只與n有關的常數,該常數為n×(n2+1)/2。
例如5-魔方陣,其第一行、第一列及主對角線上各元素之和如下:
第一行元素之和:17+24+1+8+15=65
第一列元素之和:17+23+4+10+11=65
主對角線上元素之和:17+5+13+21+9=65
而
n×(n2+1)/2=5×(52+1)/2=65
可以驗證,5-魔方陣中其餘各行、各列及副對角線上的元素之和也都為65。
假定陣列的行列下標都從0開始,則魔方陣的生成方法為:在第0行中間置1,對從2開始的其餘n2-1個數依次按下列規則存放:
(1)
假定當前數的下標為(i,j),則下一個數的放置位置為當前位置的右上方,即下標為(i-1,j+1)的位置。
(2)
如果當前數在第0行,即i-1小於0,則將下一個數放在最後一行的下一列上,即下標為(n-1,j+1)的位置。
(3)
如果當前數在最後一列上,即j+1大於n-1,則將下一個數放在上一行的第一列上,即下標為(i-1,0)的位置。
(4)
如果當前數是n的倍數,則將下一個數直接放在當前位置的正下方,即下標為(i+1,j)的位置。
演算法設計
在設計演算法時釆用了下面一些方法:
定義array()函數,array()函數的根據輸入的n值,生成並顯示一個魔方陣,當發現n不是奇數時,就加1使之成為奇數。
使用動態內存分配與釋放函數malloc()與free(),在程序執行過程中動態分配與釋放內存,這樣做的好處是使代碼具有通用性,同時提高內存的使用率。
在分配內存時還要注意,由於一個整型數要佔用兩個內存,因此,如果魔方陣中要存放的數有max個,則分配內存時要分配2*max個單元,從而有malloc(max+max)。在malloc()函數中使用max+max而不是2*max是考慮了程序運行的性能。
顯然應該使用二維數組來表示魔方陣,但雖然數組是二維形式的,而由於內存是一維線性的,因此在存取數組元素時,要將雙下標轉換為單個索引編號。在程序中直接定義了指針變數來指向數組空間,即使用malloc()函數分配的內存。
Ⅵ 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);}}
Ⅷ 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);
}