当前位置:首页 » 游戏种类 » 用c编写的小游戏

用c编写的小游戏

发布时间: 2022-05-31 11:31:15

Ⅰ 仅用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);
}

热点内容
绝地求生未来之役比赛为什么进不去 发布:2023-08-31 22:07:08 浏览:1281
dota2位置什么意思 发布:2023-08-31 22:00:04 浏览:722
lol电竞是什么样子 发布:2023-08-31 21:58:40 浏览:1179
绝地求生八倍镜的那个圆圈怎么弄 发布:2023-08-31 21:58:31 浏览:1245
lol龙龟一个多少金币 发布:2023-08-31 21:55:07 浏览:630
王者如何改游戏内名称 发布:2023-08-31 21:55:06 浏览:921
游戏主播打广告是什么意思 发布:2023-08-31 21:55:06 浏览:1576
绝地求生如何免费拿到ss7赛季手册 发布:2023-08-31 21:52:13 浏览:792
pgg是哪个国家的战队lol 发布:2023-08-31 21:52:07 浏览:675
一个人的时候才发现游戏很没意思 发布:2023-08-31 21:49:24 浏览:1278