导航:首页 > 编程语言 > c语言编程小游戏代码

c语言编程小游戏代码

发布时间:2022-11-19 00:45:06

‘壹’ C语言简易文字冒险游戏源代码

记忆游戏

#include<stdio.h>

#include<time.h>

#include<stdlib.h>

#include<windows.h>

#defineN10

intmain()

{inti,k,n,a[N],b[N],f=0;

srand(time(NULL));

printf("按1开始 按0退出:_");

scanf("%d",&n);

system("cls");

while(n!=0)

{for(k=0;k<N;k++)a[k]=rand()%N;

printf(" [请您牢记看到颜色的顺序] ");

for(k=0;k<N;k++)

{switch(a[k])

{case0:system("color90");printf("0:淡蓝色 ");break;//淡蓝色

case1:system("colorf0");printf("1:白色 ");break;//白色

case2:system("colorc0");printf("2:淡红色 ");break;//淡红色

case3:system("colord0");printf("3:淡紫色 ");break;//淡紫色

case4:system("color80");printf("4:灰色 ");break;//灰色

case5:system("colore0");printf("5:黄色 ");break;//黄色

case6:system("color10");printf("6:蓝色 ");break;//蓝色

case7:system("color20");printf("7:绿色 ");break;//绿色

case8:system("color30");printf("8:浅绿色 ");break;//浅绿色

case9:system("color40");printf("9:红色 ");break;//红色

}

Sleep(1500);

system("colorf");//单个控制文字颜色

Sleep(100);

}

system("cls");

printf("0:淡蓝色,1:白色,2:淡红色,3:淡紫色,4:灰色,5:黄色,6:蓝色7:绿色,8:浅绿色,9:红色 ");

printf(" 请输入颜色的顺序:");

for(k=0;k<N;k++)scanf("%d",&b[k]);

for(k=0;k<N;k++)if(a[k]==b[k])f++;

if(f==0)printf("你的记忆弱爆了0 ");

elseif(f==1)printf("你的记忆有点弱1 ");

elseif(f<5)printf("你的记忆一般<5 ");

elseprintf("你的记忆力很强! ");

Sleep(2000);

system("cls");

printf(" 按0退出 按任意键继续游戏: ");

scanf("%d",&n);

system("cls");

}

return0;

}

注:DEVc++运行通过,每输入一个数字要加入一个空格。

‘贰’ 教你如何使用C语言编写简单小游戏

爱玩是人的天性,而C语言是我们计算机专业都要学习的一门基础 学科.一般来说,是比较枯燥的.那么,我们能不能通过编一些小游戏来提高它的趣味性呢?这样学习程序设计,就不会是一件艰苦 ,枯燥的事,它变得象电脑游戏一样充满好奇,富有乐趣. 1, 总是从Hello,world开始 学习编程的第一个程序,一般就是打印一个亲切的词语——"Hell o,world!".让我们来看看这个最简单的C程序:#incolude /*把输入输出函数的头文件包含进来*/int main(){printf("Hello, world!");/*在屏幕上输出字符串"Hello,world!"*/return 0;/*退出main函数,并返回0*/} 下面我们发现几个值得改进的地方,1,程序的运行结果一闪而过 .2,每执行这个程序一次都能看见上次运行留下的字符.3,我们 还希望屏幕输出一个笑脸来欢迎我们. 让我们来改进一下这个程序吧!1,在return语句的前面加一句:getch ();,表示按任意键结束.2,在printf语句前用clrscr函数清屏,要使用这个函数和getch函数,需要在程序开头再包含头文件conio.h.3,ASCII码也有 许多非常好玩的字符,比如ASCII码值为2的就是一个笑脸,我们可 以用printf("%c", 2)来输出一个笑脸. 现在我们把Hello,world程序改成一个更好看的Hello,world了.下面让我们开始做游戏吧! 2, 心动的开始,一个运动中的笑脸 大家小时侯喜欢看动画片吗?哈哈,我猜你们都喜欢吧!下面就让我们来做一个小动画吧.在屏幕上显示一个运动的小笑脸,而且当它到达屏幕的边缘时会自动弹回来.先在程序定义一个在屏幕中运动的点的结构:struct move_point{ int x, y;/*该点的位置,包括x坐标和y坐标*/ int xv, yv;/*该点在x轴,y轴的速度*/};运动的原理是,先擦去物体先前的轨迹,让物体按其速度移动一段距离,再画出该物体.让我们看到以下代码:gotoxy(man.x, man.y);/*把光标移到指定的坐标*/printf(" ");/*输出一个空格,把先前的字符擦去*/然后我们让物体按其速度运动:man.x += man.xv;/*水平方向按x轴的速度运动*/man.y += man.yv;/*垂直方向按y轴的速度运动*/运动后还要判断物体是否出界,如果出了界,就令物体反弹,即让 它下一刻的速度等于现在的速度的相反数.最后打印出这个笑脸:gotoxy(man.x, man.y);printf("%c\b", 2); /*输出ASCII码值为2的"笑脸"字符*/怎么样?是不是很有趣呢?不过这个笑脸一直是自己运动,能不能 让我们来控制它运动呢? 不过这个程序没有什么目的,也没有什么判断胜负的条件.下面我们就利用这个能控制它移动的笑脸来做一个更有趣的游戏吧! 4, 在迷宫中探索 小时侯,我常在一些小人书和杂志上看见一些迷宫的游戏,非常喜欢玩,还常到一些书上找迷宫玩呢.好的,现在我们用C语言来编个迷宫的游戏,重温一下童年的乐趣. 首先,我们定义一个二维数组map,用它来保存迷宫的地图,其中map[x][y] == '#'表示在(x,y)坐标上的点是墙壁.DrawMap函数在屏幕上输出迷宫的地图和一些欢迎信息.在main函数里,我们定义了"小人"man的坐标和"目的地"des的 坐标.在游戏循环中,我们增加了一些用来判断胜负的语句:if (man.x == des.x && man.y == des.y) /*如果人的坐标等于目的地的坐标*/{ gotoxy(35, 3); printf("Ok! You win!"); /*输出胜利信息*/….}在判断按键时,如果玩家按的是方向键,我们还要先判断前面是不是有"墙壁",如果有的话,就不能往前移动了.好的,我们在判断按键的switch语句的各个分支加上了判断语句,如下:if (map[…][…] == '#') break;/*如果前面是墙壁,就不执行下去*/哇噻!真棒,我们做出了一个完整的游戏了.当然你还可以通过修改二维数组map来修改迷宫的地图,让它更有挑战性.不过,我们要设计一个更好玩的游戏—— 5, 聪明的搬运工 大家一定玩过"搬运工"的游戏吧!这是在电脑和电子字典上较流行的益智游戏,让我们动手做一个属于自己的"搬运工"吧!程序依然用数组map来保存地图,数组元素如果为空格则表示什么也没有,'b'表示箱子,'#'表示墙壁,'*'表示目的地,'i'表示箱子在目的地.我们以后每推一下箱子,不但要改变屏幕的显示,也要改变map相应元素的值.游戏的主循环依然是接受按键.当接收一个方向键,需要判断小人前面一格的状态,如果是空地或目的地,则人物可以直接移动;如果是墙壁,则不可移动;如果是箱子或目的地上的箱子,则需要继续判断箱子前面一格的状态:如果前一格是空地或目的地,则人推箱子前进,否则不可移动.好的,我们在switch中增加了这些判断语句.程序还有一个重要的功能就是判断胜利.数组Des用来记录全部目的地的坐标,我们每执行一步操作后,程序就要通过Des数组判断这些目的地上是否都有箱子了.真棒啊!我们可以做游戏了.而且是一个老少皆宜,趣味十足的游戏呢!当然,我们可以通过修改map数组来制作不同的游戏地图,我们还可以相互分享好的游戏地图呢. 尾声: 在C++等高级语言还没出来的时候,很多应用程序也是C语言开发的.C语言在与硬件联系紧密的编程中,也占有重要地位.其实我觉得学习编程,可以通过一些小游戏,实用的例子来学习.象学习音乐的人,不是要等到把全部乐理学完后才演奏一个完整的曲子.而是刚开始学时就有一些简单的曲子让你演奏,让你立刻就有成就感,让你很快就能卖弄出来在别人面前表现自己了.通过编游戏来学习编程,把学习变成游戏,不失为学习计算机的一种好方法. 好了,编游戏就这么简单,希望大家也尝试用C语言或其他的语言来做几个自己喜欢的小游戏.

‘叁’ 用C++编写的小游戏源代码

五子棋的代码:

#include<iostream>

#include<stdio.h>

#include<stdlib.h>

#include <time.h>

using namespace std;

const int N=15; //15*15的棋盘

const char ChessBoardflag = ' '; //棋盘标志

const char flag1='o'; //玩家1或电脑的棋子标志

const char flag2='X'; //玩家2的棋子标志

typedef struct Coordinate //坐标类

{

int x; //代表行

int y; //代表列

}Coordinate;

class GoBang //五子棋类

{

public:

GoBang() //初始化

{

InitChessBoard();

}

void Play() //下棋

{

Coordinate Pos1; // 玩家1或电脑

Coordinate Pos2; //玩家2

int n = 0;

while (1)

{

int mode = ChoiceMode();

while (1)

{

if (mode == 1) //电脑vs玩家

{

ComputerChess(Pos1,flag1); // 电脑下棋

if (GetVictory(Pos1, 0, flag1) == 1) //0表示电脑,真表示获胜

break;

PlayChess(Pos2, 2, flag2); //玩家2下棋

if (GetVictory(Pos2, 2, flag2)) //2表示玩家2

break;

}

else //玩家1vs玩家2

{

PlayChess(Pos1, 1, flag1); // 玩家1下棋

if (GetVictory(Pos1, 1, flag1)) //1表示玩家1

break;

PlayChess(Pos2, 2, flag2); //玩家2下棋

if (GetVictory(Pos2, 2, flag2)) //2表示玩家2

break;

}

}

cout << "***再来一局***" << endl;

cout << "y or n :";

char c = 'y';

cin >> c;

if (c == 'n')

break;

}

}

protected:

int ChoiceMode() //选择模式

{

int i = 0;

system("cls"); //系统调用,清屏

InitChessBoard(); //重新初始化棋盘

cout << "***0、退出 1、电脑vs玩家 2、玩家vs玩家***" << endl;

while (1)

{

cout << "请选择:";

cin >> i;

if (i == 0) //选择0退出

exit(1);

if (i == 1 || i == 2)

return i;

cout << "输入不合法" << endl;

}

}

void InitChessBoard() //初始化棋盘

{

for (int i = 0; i < N + 1; ++i)

{

for (int j = 0; j < N + 1; ++j)

{

_ChessBoard[i][j] = ChessBoardflag;

}

}

}

void PrintChessBoard() //打印棋盘,这个函数可以自己调整

{

system("cls"); //系统调用,清空屏幕

for (int i = 0; i < N+1; ++i)

{

for (int j = 0; j < N+1; ++j)

{

if (i == 0) //打印列数字

{

if (j!=0)

printf("%d ", j);

else

printf(" ");

}

else if (j == 0) //打印行数字

printf("%2d ", i);

else

{

if (i < N+1)

{

printf("%c |",_ChessBoard[i][j]);

}

}

}

cout << endl;

cout << " ";

for (int m = 0; m < N; m++)

{

printf("--|");

}

cout << endl;

}

}

void PlayChess(Coordinate& pos, int player, int flag) //玩家下棋

{

PrintChessBoard(); //打印棋盘

while (1)

{

printf("玩家%d输入坐标:", player);

cin >> pos.x >> pos.y;

if (JudgeValue(pos) == 1) //坐标合法

break;

cout << "坐标不合法,重新输入" << endl;

}

_ChessBoard[pos.x][pos.y] = flag;

}

void ComputerChess(Coordinate& pos, char flag) //电脑下棋

{

PrintChessBoard(); //打印棋盘

int x = 0;

int y = 0;

while (1)

{

x = (rand() % N) + 1; //产生1~N的随机数

srand((unsigned int) time(NULL));

y = (rand() % N) + 1; //产生1~N的随机数

srand((unsigned int) time(NULL));

if (_ChessBoard[x][y] == ChessBoardflag) //如果这个位置是空的,也就是没有棋子

break;

}

pos.x = x;

pos.y = y;

_ChessBoard[pos.x][pos.y] = flag;

}

int JudgeValue(const Coordinate& pos) //判断输入坐标是不是合法

{

if (pos.x > 0 && pos.x <= N&&pos.y > 0 && pos.y <= N)

{

if (_ChessBoard[pos.x][pos.y] == ChessBoardflag)

{

return 1; //合法

}

}

return 0; //非法

}

int JudgeVictory(Coordinate pos, char flag) //判断有没有人胜负(底层判断)

{

int begin = 0;

int end = 0;

int begin1 = 0;

int end1 = 0;

//判断行是否满足条件

(pos.y - 4) > 0 ? begin = (pos.y - 4) : begin = 1;

(pos.y + 4) >N ? end = N : end = (pos.y + 4);

for (int i = pos.x, j = begin; j + 4 <= end; j++)

{

if (_ChessBoard[i][j] == flag&&_ChessBoard[i][j + 1] == flag&&

_ChessBoard[i][j + 2] == flag&&_ChessBoard[i][j + 3] == flag&&

_ChessBoard[i][j + 4] == flag)

return 1;

}

//判断列是否满足条件

(pos.x - 4) > 0 ? begin = (pos.x - 4) : begin = 1;

(pos.x + 4) > N ? end = N : end = (pos.x + 4);

for (int j = pos.y, i = begin; i + 4 <= end; i++)

{

if (_ChessBoard[i][j] == flag&&_ChessBoard[i + 1][j] == flag&&

_ChessBoard[i + 2][j] == flag&&_ChessBoard[i + 3][j] == flag&&

_ChessBoard[i + 4][j] == flag)

return 1;

}

int len = 0;

//判断主对角线是否满足条件

pos.x > pos.y ? len = pos.y - 1 : len = pos.x - 1;

if (len > 4)

len = 4;

begin = pos.x - len; //横坐标的起始位置

begin1 = pos.y - len; //纵坐标的起始位置

pos.x > pos.y ? len = (N - pos.x) : len = (N - pos.y);

if (len>4)

len = 4;

end = pos.x + len; //横坐标的结束位置

end1 = pos.y + len; //纵坐标的结束位置

for (int i = begin, j = begin1; (i + 4 <= end) && (j + 4 <= end1); ++i, ++j)

{

if (_ChessBoard[i][j] == flag&&_ChessBoard[i + 1][j + 1] == flag&&

_ChessBoard[i + 2][j + 2] == flag&&_ChessBoard[i + 3][j + 3] == flag&&

_ChessBoard[i + 4][j + 4] == flag)

return 1;

}

//判断副对角线是否满足条件

(pos.x - 1) >(N - pos.y) ? len = (N - pos.y) : len = pos.x - 1;

if (len > 4)

len = 4;

begin = pos.x - len; //横坐标的起始位置

begin1 = pos.y + len; //纵坐标的起始位置

(N - pos.x) > (pos.y - 1) ? len = (pos.y - 1) : len = (N - pos.x);

if (len>4)

len = 4;

end = pos.x + len; //横坐标的结束位置

end1 = pos.y - len; //纵坐标的结束位置

for (int i = begin, j = begin1; (i + 4 <= end) && (j - 4 >= end1); ++i, --j)

{

if (_ChessBoard[i][j] == flag&&_ChessBoard[i + 1][j - 1] == flag&&

_ChessBoard[i + 2][j - 2] == flag&&_ChessBoard[i + 3][j - 3] == flag&&

_ChessBoard[i + 4][j - 4] == flag)

return 1;

}

for (int i = 1; i < N + 1; ++i) //棋盘有没有下满

{

for (int j =1; j < N + 1; ++j)

{

if (_ChessBoard[i][j] == ChessBoardflag)

return 0; //0表示棋盘没满

}

}

return -1; //和棋

}

bool GetVictory(Coordinate& pos, int player, int flag) //对JudgeVictory的一层封装,得到具体那个玩家获胜

{

int n = JudgeVictory(pos, flag); //判断有没有人获胜

if (n != 0) //有人获胜,0表示没有人获胜

{

PrintChessBoard();

if (n == 1) //有玩家赢棋

{

if (player == 0) //0表示电脑获胜,1表示玩家1,2表示玩家2

printf("***电脑获胜*** ");

else

printf("***恭喜玩家%d获胜*** ", player);

}

else

printf("***双方和棋*** ");

return true; //已经有人获胜

}

return false; //没有人获胜

}

private:

char _ChessBoard[N+1][N+1];

};

(3)c语言编程小游戏代码扩展阅读:

设计思路

1、进行问题分析与设计,计划实现的功能为,开局选择人机或双人对战,确定之后比赛开始。

2、比赛结束后初始化棋盘,询问是否继续比赛或退出,后续可加入复盘、悔棋等功能。

3、整个过程中,涉及到了棋子和棋盘两种对象,同时要加上人机对弈时的AI对象,即涉及到三个对象。

‘肆’ c语言游戏编程,下落的小鸟 求代码

下落的小鸟

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include<Windows.h>
int Grade = 1, Score = 0, Max_blank = 9, Distance = 18;
struct Birds{int x; int y;}; //定义一种Birds数据类型(含3个成员)
Birds *Bird = (Birds*)malloc(sizeof(Birds)); //定义Birds类型 指针变量Bird并赋初值
struct Bg{int x, y; int l_blank; Bg *pri; Bg *next;}; //定义一种Bg数据类型(含5个成员)
Bg *Bg1 = (Bg*)malloc(sizeof(Bg)); //定义Bg类型 指针变量Bg1并赋初值

void Position(int x, int y) //光标定位函数(用于指定位置输出)
{COORD pos = { x - 1, y - 1 };
HANDLE Out = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(Out, pos);
}

void Csh( ) //初始化界面

{

printf("══════════════════════════════════════ ");

printf(" ■■ ■■ C语言版 Flappy Bird ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ 瞎搞人:yyposs原创 ");

printf(" ■■ ■■ 瞎搞日期:2014.2 ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ 改编:鸣蝉百2021.7 ");

printf(" ■■ ■■ 操作:按向上方向键让小鸟起飞 ");

printf(" ■■ ");

printf(" ■■ ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ DEVc++运行通过 ");

printf("══════════════════════════════════════ ");

printf(" 按键继续…");

getch( );

system("cls");

}

void PrFK( ) //输出方框(游戏范围区)

{int i;

Position(1, 1); printf("╔"); Position(79, 1); printf("╗");

Position(1, 24); printf("╚"); Position(79, 24); printf("╝");

for (i = 3; i <= 78; i += 2){Position(i, 1); printf("═"); Position(i, 24); printf("═");}

for(i=2;i<=23;i++)

{ Position(1,i); printf("║");if(i<11)printf("0%d",i-1);else printf("%d",i-1);

Position(79,i); printf("║");

}

Position(4, 25); printf("小鸟即将出现,请准备按键起飞… ");

getch( );

Position(4, 25); printf(" ");

}

void CreatBg( ) //创建障碍物坐标(便于打印输出)
{Bg *Bg2 = (Bg*)malloc(sizeof(Bg));
Bg1->x = 90; Bg1->y = 8; //确定障碍物的一对基本坐标(此时值是在游戏框之外)
Bg2->x = Bg1->x + Distance; Bg2->y = 9; //下一障碍物的基本坐标x、y
Bg1->l_blank = Max_blank - Grade; //障碍物上下两部分之间的空白距离l_blank
Bg2->l_blank = Max_blank - Grade;
Bg1->next = Bg2; Bg1->pri = Bg2;
Bg2->next = Bg1; Bg2->pri = Bg1;
}


void InsertBg(Bg *p) //随机改变障碍物的y坐标,让空白通道有上下变化
{int temp;
Bg *Bgs = (Bg*)malloc(sizeof(Bg));
Bgs->x = p->pri->x + Distance;
Bgs->l_blank = Max_blank - Grade;
srand((int)time(0)); //启动随机数发生器
temp = rand( ); //产生一个随机数并赋值给temp
if (temp % 2 == 0)
{if ((temp % 4 + p->pri->y + Max_blank - Grade)<21)
Bgs->y = p->pri->y + temp % 4;
else Bgs->y = p->pri->y;
}
else
{if ((p->pri->y - temp % 4)>2)Bgs->y = p->pri->y - temp % 4;
else Bgs->y = p->pri->y;
}
Bgs->pri = p->pri; Bgs->next = p;
p->pri->next = Bgs; p->pri = Bgs;
}

void CreatBird( ) //建立小鸟的坐标(初始打印输出小鸟的位置)
{Bird->x = 41; Bird->y = 10;}

int CheckYN(Bg *q) //判断游戏结束与否(值为0是要结束,为1没有要结束)
{Bg *p = q; int i = 0;
while (++i <= 5)
{if (Bird->y>23)return 0;
if (Bird->x == p->x&&Bird->y <= p->y)return 0;
if ((Bird->x == p->x || Bird->x == p->x + 1 || Bird->x == p->x + 2) && Bird->y == p->y)return 0;
if (Bird->x == p->x&&Bird->y>p->y + p->l_blank)return 0;
if ((Bird->x == p->x || Bird->x == p->x + 1 || Bird->x == p->x + 2) && Bird->y == p->y + p->l_blank)
return 0;
p = p->next;
}
return 1;
}


void Check_Bg(Bg *q) //核查开头的障碍物坐标是否在游戏区内
{Bg *p = q; int i = 0, temp;
while (++i <= 5)
{if (p->x>-4)p = p->next;
else
{srand((int)time(0)); temp = rand();
if (temp % 2 == 0)
{if ((temp % 4 + p->y + Max_blank - Grade)<21)p->y = p->y + temp % 4;
else p->y = p->y; p->x = p->pri->x + Distance;
p->l_blank = Max_blank - Grade;
}
else
{if ((p->y - temp % 4)>2)p->y = p->y - temp % 4;
else p->y = p->y; p->x = p->pri->x + Distance;
p->l_blank = Max_blank - Grade;
}
}
}
}

void Prt_Bg(Bg *q) //打印输出障碍物(依据其x、y坐标进行相应输出)
{Bg *p = q; int i = 0, k, j;
while (++i <= 5)
{if (p->x>0 && p->x <= 78)
{for (k = 2; k<p->y; k++){Position(p->x + 1, k); printf("■"); printf("■"); printf(" ");}
Position(p->x, p->y);
printf("■"); printf("■"); printf("■"); printf(" ");
Position(p->x, p->y + p->l_blank);
printf("■"); printf("■"); printf("■"); printf(" ");
k = k + p->l_blank + 1;
for (k; k <= 23; k++){Position(p->x + 1, k); printf("■"); printf("■"); printf(" ");}
}
p = p->next;
if (p->x == 0)
{for (j = 2; j<p->y; j++){Position(p->x + 1, j); printf(" "); printf(" ");}
Position(p->x + 1, p->y);
printf(" "); printf(" "); printf(" ");
Position(p->x + 1, p->y + Max_blank - Grade);
printf(" "); printf(" "); printf(" ");
j = j + Max_blank - Grade + 1;
for (j; j <= 22; j++){Position(p->x + 1, j); printf(" "); printf(" ");}
}
}
}

void PrtBird( ) //打印输出小鸟
{Position(Bird->x, Bird->y - 1); printf(" ");
Position(Bird->x, Bird->y); printf("Ю");
Position(38, 2); printf("Score:%d", Score);
}
void Loop_Bg(Bg *q) //障碍物x坐标左移,并记录成绩
{Bg *p = q; int i = 0;
while (++i <= 5)
{p->x = p->x - 1; p = p->next;
if (Bird->x == p->x)
{Score += 1;
if (Score % 4 == 0 && Grade<4)Grade++;
}
}
}

int main( )
{int i = 0; int t;

while (1)

{
Csh( );PrFK( );CreatBg( );
InsertBg(Bg1);InsertBg(Bg1);InsertBg(Bg1);
CreatBird( );
while (1)
{if (!CheckYN(Bg1))break;
Check_Bg(Bg1);Prt_Bg(Bg1);
PrtBird( );Loop_Bg(Bg1);
Bird->y = Bird->y + 1;
if (GetAsyncKeyState(VK_UP))
//按下了向上方向键
{Position(Bird->x, Bird->y - 1);printf(" ");
Bird->y = Bird->y - 4;
}
Sleep(200);
//程序延时200毫秒(数值大小决定游戏速度快慢)
i = 0;
}
Position(6, 25);
printf("游戏结束! 请输入:0.退出 1.重玩");

scanf("%d",&t);

if (t==0)break;

system("cls"); Score = 0;

}
return 0;
}

‘伍’ 怎样用C语言编写一个小游戏

“贪吃蛇”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("07"); snk.len++; score += 100; snk.speed -= 5; Flag = 1; } //007是响铃

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且横坐标为偶数,则食物坐标取值成功

}

setColor(12, 0);

gtxy(fod.x, fod.y); printf("●"); //光标到取得的坐标处打印食物

}

return;

}

int Over( ) //判断游戏是否结束的函数

{ int i;

setColor(7, 0);

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 setColor(unsigned short ForeColor = 7, unsigned short BackGroundColor = 0)

{ HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);

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

} //用来设定颜色的函数

‘陆’ 求C语言小游戏源程序

新手要方便写代码,可以收藏下面几个自编函数:

  1. gtxy (6, 3) //光标定位于窗口的第6列,第3行处(准备输出,行与列都是从0算起)

  2. Color (4, 0) //设置为红字配黑底 如 Color (10, 0)则是淡绿字配黑底

  3. yinc (1,0) //隐藏光标(第二个参数设为0就隐藏,没有光标闪烁,yinc代表隐藏)

  4. kou(80,25) //设定窗口缓冲区大小为80列,25行

    下面几个是库函数,不需自己编写,只要用#include包含就可以使用。

  5. SetConsoleTitle("俄罗斯方块"); //设置窗口左上角标题栏处出现"俄罗斯方块"5个字

  6. srand( (unsigned) time(NULL) ); //初始化随机数发生器

  7. n= rand( ) % 20; //产生随机数0-19中的一个. 如 rand( )%5 就产生0-4中的一个数

    SetConsoleTitle( )函数在<windows.h>里,srand( )函数与rand( )函数要配合用,

    就是同时要用,在<stdlib.h>里。如果 rand( )%10+1 就产生1-10之中的一个数。

  8. Sleep(300); //延时300毫秒(就是程序暂停300毫秒后继续运行)

  9. system("cls"); //清屏(把窗口里的内容全部清除,光标定于(0,0)位置处)

    这两个函数都在<windows.h>里。开头4个自编函数 编写如下:

void gtxy (int x, int y) //控制光标位置的函数

{ COORD pos;

pos.X = x;

pos.Y = y;

SetConsoleCursorPosition ( GetStdHandle (STD_OUTPUT_HANDLE), pos );

}

void Color (short ForeColor= 7, short BackGroundColor= 0) //设定颜色的函数

{ HANDLE hl = GetStdHandle ( STD_OUTPUT_HANDLE );

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

}

声明时原型可写 void Color (short x, short y);

void yinc (int x,int y) //隐藏光标的函数

{ CONSOLE_CURSOR_INFO gb={ x , y }; //gb代表光标

SetConsoleCursorInfo ( GetStdHandle(STD_OUTPUT_HANDLE), &gb );

}

void kou(int w,int h) //设置窗口大小的函数

{HANDLE hl=GetStdHandle ( STD_OUTPUT_HANDLE ) ;

COORD size={ w , h };

SetConsoleScreenBufferSize( hl , size );

SMALL_RECT rc={ 0, 0, w, h };

SetConsoleWindowInfo( hl, 1, &rc );

}

最后这个函数,参数w是宽h是高。里边5行中第一行定义了句柄型变量hl,并给它赋值。

第二行定义了坐标型结构体变量size,它的取值决定了缓冲区的大小。第三行就是使用

size的值设置好缓冲区大小。第四行定义了变量rc,它的值决定当前窗口显示的位置与

大小(不得超过缓冲区的大小)。前两个0,0是从缓冲区左上角0列0行位置处开始,后两

个参数可以小于w和h.比如rc={0,0,w-10,h-5}; 最后一行使用rc的值设置好窗口,中间

那个参数要为" 1 "或写“ true ”才有效。

‘柒’ 就C语言中 猜拳游戏的代码

这是一个简单的猜拳游戏(剪子包子锤),让你与电脑对决。你出的拳头由你自己决定,电脑则随机出拳,最后判断胜负。
下面的代码会实现一个猜拳游戏,让你与电脑对决。你出的拳头由你自己决定,电脑则随机出拳,最后判断胜负。
启动程序后,让用户出拳,截图:

用户出拳,显示对决结果:截图:

代码实现:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
char gamer; // 玩家出拳
int computer; // 电脑出拳
int result; // 比赛结果
// 为了避免玩一次游戏就退出程序,可以将代码放在循环中
while (1){
printf("这是一个猜拳的小游戏,请输入你要出的拳头:\n");
printf("A:剪刀\nB:石头\nC:布\nD:不玩了\n");
scanf("%c%*c",&gamer);
switch (gamer){
case 65: //A
case 97: //a
gamer=4;
break;
case 66: //B
case 98: //b
gamer=7;
break;
case 67: //C
case 99: //c
gamer=10;
break;
case 68: //D
case 100: //d
return 0;

default:
printf("你的选择为 %c 选择错误,退出...\n",gamer);
getchar();
system("cls"); // 清屏
return 0;
break;
}

srand((unsigned)time(NULL)); // 随机数种子
computer=rand()%3; // 产生随机数并取余,得到电脑出拳
result=(int)gamer+computer; // gamer 为 char 类型,数学运算时要强制转换类型
printf("电脑出了");
switch (computer)
{
case 0:printf("剪刀\n");break; //4 1
case 1:printf("石头\n");break; //7 2
case 2:printf("布\n");break; //10 3
}
printf("你出了");
switch (gamer)
{
case 4:printf("剪刀\n");break;
case 7:printf("石头\n");break;
case 10:printf("布\n");break;
}
if (result==6||result==7||result==11) printf("你赢了!");
else if (result==5||result==9||result==10) printf("电脑赢了!");
else printf("平手");
system("pause>nul&&cls"); // 暂停并清屏
}
return 0;
}
代码分析
1) 首先,我们需要定义3个变量来储存玩家出的拳头(gamer)、电脑出的拳头(computer)和最后的结果(result),然后给出文字提示,让玩家出拳。
接下来接收玩家输入:
scanf("%c%*c",&gamer);

‘捌’ 求几C语言个小游戏代码,简单的,要注释、、谢谢了、

// Calcu24.cpp : Defines the entry point for the console application.
//
/*
6-6
24点游戏
*/
#include "conio.h"
#include "stdlib.h"
#include "time.h"
#include "math.h"
#include "string.h"/*
从一副扑克牌中,任取4张。
2-10 按其点数计算(为了表示方便10用T表示),J,Q,K,A 统一按 1 计算
要求通过加减乘除四则运算得到数字 24。
本程序可以随机抽取纸牌,并用试探法求解。
*/void GivePuzzle(char* buf)
{
char card[] = {'A','2','3','4','5','6','7','8','9','T','J','Q','K'}; for(int i=0; i<4; i++){
buf[i] = card[rand() % 13];
}
}
void shuffle(char * buf)
{
for(int i=0; i<5; i++){
int k = rand() % 4;
char t = buf[k];
buf[k] = buf[0];
buf[0] = t;
}
}
int GetCardValue(int c)
{
if(c=='T') return 10;
if(c>='0' && c<='9') return c - '0';
return 1;
}
char GetOper(int n)
{
switch(n)
{
case 0:
return '+';
case 1:
return '-';
case 2:
return '*';
case 3:
return '/';
} return ' ';
}double MyCalcu(double op1, double op2, int oper)
{
switch(oper)
{
case 0:
return op1 + op2;
case 1:
return op1 - op2;
case 2:
return op1 * op2;
case 3:
if(fabs(op2)>0.0001)
return op1 / op2;
else
return 100000;
} return 0;
}
void MakeAnswer(char* answer, int type, char* question, int* oper)
{
char p[4][3];
for(int i=0; i<4; i++)
{
if( question[i] == 'T' )
strcpy(p[i], "10");
else
sprintf(p[i], "%c", question[i]);
}

switch(type)
{
case 0:
sprintf(answer, "%s %c (%s %c (%s %c %s))",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
case 1:
sprintf(answer, "%s %c ((%s %c %s) %c %s)",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
case 2:
sprintf(answer, "(%s %c %s) %c (%s %c %s)",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
case 3:
sprintf(answer, "((%s %c %s) %c %s) %c %s",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
case 4:
sprintf(answer, "(%s %c (%s %c %s)) %c %s",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
}
}
bool TestResolve(char* question, int* oper, char* answer)
{
// 等待考生完成
int type[5]={0,1,2,3,4};//计算类型
double p[4];
double sum=0;
//
for(int i=0; i<4; i++) //循环取得点数
{
p[i]=GetCardValue(int(question[i]));
} for(i=0;i<5;i++)
{
MakeAnswer(answer,type[i],question,oper); //获取可能的答案
switch(type[i])
{
case 0:
sum=MyCalcu(p[0],MyCalcu( p[1],MyCalcu(p[2], p[3], oper[2]),oper[1]),oper[0]); //A*(B*(c*D))
break;
case 1:
sum=MyCalcu(p[0],MyCalcu(MyCalcu(p[1], p[2], oper[1]),p[3],oper[2]),oper[0]); //A*((B*C)*D)
break;
case 2:
sum=MyCalcu(MyCalcu(p[0], p[1], oper[0]),MyCalcu(p[2], p[3], oper[2]),oper[1]); // (A*B)*(C*D)
break;
case 3:
sum=MyCalcu(MyCalcu(MyCalcu(p[0], p[1], oper[0]),p[2],oper[1]),p[3],oper[2]); //((A*B)*C)*D
break;
case 4:
sum=MyCalcu(MyCalcu(p[0],MyCalcu(p[1], p[2], oper[1]),oper[0]),p[3],oper[2]); //(A*(B*C))*D
break;
}
if(sum==24) return true;
}
return false;
}
/*
采用随机试探法:就是通过随机数字产生 加减乘除的 组合,通过大量的测试来命中的解法
提示:
1. 需要考虑用括号控制计算次序的问题 比如:( 10 - 4 ) * ( 3 + A ), 实际上计算次序的数目是有限的:
A*(B*(c*D))
A*((B*C)*D)
(A*B)*(C*D)
((A*B)*C)*D
(A*(B*C))*D
2. 需要考虑计算结果为分数的情况:( 3 + (3 / 7) ) * 7
3. 题目中牌的位置可以任意交换
*/
bool TryResolve(char* question, char* answer)
{
int oper[3]; // 存储运算符,0:加法 1:减法 2:乘法 3:除法
for(int i=0; i<1000 * 1000; i++)
{
// 打乱纸牌顺序
shuffle(question);

// 随机产生运算符
for(int j=0; j<3; j++)
oper[j] = rand() % 4; if( TestResolve(question, oper, answer) ) return true;
} return false;
}
int main(int argc, char* argv[])
{
// 初始化随机种子
srand( (unsigned)time( NULL ) ); char buf1[4]; // 题目
char buf2[30]; // 解答
printf("***************************\n");
printf("计算24\n");
printf("A J Q K 均按1计算,其它按牌点计算\n");
printf("目标是:通过四则运算组合出结果:24\n");
printf("***************************\n\n");
for(;;)
{
GivePuzzle(buf1); // 出题
printf("题目:");
for(int j=0; j<4; j++){
if( buf1[j] == 'T' )
printf("10 ");
else
printf("%c ", buf1[j]);
} printf("\n按任意键参考答案...\n");
getch(); if( TryResolve(buf1, buf2) ) // 解题
printf("参考:%s\n", buf2);
else
printf("可能是无解...\n"); printf("按任意键出下一题目,x 键退出...\n");
if( getch() == 'x' ) break;
} return 0;
}

‘玖’ 用C语言编写的小游戏代码是什么

“猜数字小游戏”,每个数字后按空格,最后按回车确认

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

int a[4],b[4];

int count=0; //计算猜测次数

void csh( ); //初始化

void start( ); //开始游戏

int main( )

{ csh( );

start( );

}

void csh( ) //初始化

{ printf(" 猜 数 字 小 游 戏 ");

printf(“ 猜四个数字,如数字与顺序都正确记为A,数字正确位置不对记为B. ”);

}

void start( ) //开始游戏

{int m,n; //m是完全猜对的个数,n是顺序不对的个数

while(1)

{srand((unsigned)time(NULL)); //初始化随机数发生器srand( )

while(1) { for(int i=0;i<4;i++) a[i]=rand( )%10; //rand( )函数每次随机产生一个0-9的数

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

(a[2]!=a[1]&&a[2]!=a[0])&&a[1]!=a[0] ) break; } //4个随机数各自不相等

printf(" 请依次输入4个一位整数: ");

while(1)

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

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

m=0;n=0;

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

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

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

}

count=count+1;

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

if(m==4)break;

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

}

printf(" ");

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

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

int z;

printf(" (要继续吗?1或0) ");

scanf(“%d”,&z);

if(z==0) break;

}

}

阅读全文

与c语言编程小游戏代码相关的资料

热点内容
网盘忘记解压码怎么办 浏览:850
文件加密看不到里面的内容 浏览:651
程序员脑子里都想什么 浏览:430
oppp手机信任app在哪里设置 浏览:185
java地址重定向 浏览:268
一年级下册摘苹果的算法是怎样的 浏览:448
程序员出轨电视剧 浏览:88
服务器系统地址怎么查 浏览:54
解压游戏发行官 浏览:601
国外小伙解压实验 浏览:336
顶级大学开设加密货币 浏览:437
java重载与多态 浏览:528
腾讯应届程序员 浏览:942
一键编译程序 浏览:129
语音加密包哪个好 浏览:339
有什么学习高中语文的app 浏览:282
安卓手机的表格里怎么打勾 浏览:409
阿里云服务器有网络安全服务吗 浏览:969
超解压兔子视频 浏览:24
单片机怎么测负脉冲 浏览:174