❶ 谁能提供几个比较好的c语言游戏源码下载网站呢pudn和csdn的就不要说了下写小游戏代码研究下!
成都聚人科技有限公司旗下 聚游戏网 西南最大的游戏门户网 强烈推荐
❷ C语言扫雷游戏源代码
"扫雷"小游戏C代码
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
main( )
{char a[102][102],b[102][102],c[102][102],w;
int i,j; /*循环变量*/
int x,y,z[999]; /*雷的位置*/
int t,s; /*标记*/
int m,n,lei; /*计数*/
int u,v; /*输入*/
int hang,lie,ge,mo; /*自定义变量*/
srand((int)time(NULL)); /*启动随机数发生器*/
leb1: /*选择模式*/
printf("
请选择模式:
1.标准 2.自定义
");
scanf("%d",&mo);
if(mo==2) /*若选择自定义模式,要输入三个参数*/
{do
{t=0; printf("请输入
行数 列数 雷的个数
");
scanf("%d%d%d",&hang,&lie,&ge);
if(hang<2){printf("行数太少
"); t=1;}
if(hang>100){printf("行数太多
");t=1;}
if(lie<2){printf("列数太少
");t=1;}
if(lie>100){printf("列数太多
");t=1;}
if(ge<1){printf("至少要有一个雷
");t=1;}
if(ge>=(hang*lie)){printf("雷太多了
");t=1;}
}while(t==1);
}
else{hang=10,lie=10,ge=10;} /*否则就是选择了标准模式(默认参数)*/
for(i=1;i<=ge;i=i+1) /*确定雷的位置*/
{do
{t=0; z[i]=rand( )%(hang*lie);
for(j=1;j<i;j=j+1){if(z[i]==z[j]) t=1;}
}while(t==1);
}
for(i=0;i<=hang+1;i=i+1) /*初始化a,b,c*/
{for(j=0;j<=lie+1;j=j+1) {a[i][j]='1'; b[i][j]='1'; c[i][j]='0';} }
for(i=1;i<=hang;i=i+1)
{for(j=1;j<=lie;j=j+1) {a[i][j]='+';} }
for(i=1;i<=ge;i=i+1) /*把雷放入c*/
{x=z[i]/lie+1; y=z[i]%lie+1; c[x][y]='#';}
for(i=1;i<=hang;i=i+1) /*计算b中数字*/
{for(j=1;j<=lie;j=j+1)
{m=48;
if(c[i-1][j-1]=='#')m=m+1; if(c[i][j-1]=='#')m=m+1;
if(c[i-1][j]=='#')m=m+1; if(c[i+1][j+1]=='#')m=m+1;
if(c[i][j+1]=='#')m=m+1; if(c[i+1][j]=='#')m=m+1;
if(c[i+1][j-1]=='#')m=m+1; if(c[i-1][j+1]=='#')m=m+1;
b[i][j]=m;
}
}
for(i=1;i<=ge;i=i+1) /*把雷放入b中*/
{x=z[i]/lie+1; y=z[i]%lie+1; b[x][y]='#';}
lei=ge; /*以下是游戏设计*/
do
{leb2: /*输出*/
system("cls");printf("
");
printf(" ");
for(i=1;i<=lie;i=i+1)
{w=(i-1)/10+48; printf("%c",w);
w=(i-1)%10+48; printf("%c ",w);
}
printf("
|");
for(i=1;i<=lie;i=i+1){printf("---|");}
printf("
");
for(i=1;i<=hang;i=i+1)
{w=(i-1)/10+48; printf("%c",w);
w=(i-1)%10+48; printf("%c |",w);
for(j=1;j<=lie;j=j+1)
{if(a[i][j]=='0')printf(" |");
else printf(" %c |",a[i][j]);
}
if(i==2)printf(" 剩余雷个数");
if(i==3)printf(" %d",lei);
printf("
|");
for(j=1;j<=lie;j=j+1){printf("---|");}
printf("
");
}
scanf("%d%c%d",&u,&w,&v); /*输入*/
u=u+1,v=v+1;
if(w!='#'&&a[u][v]=='@')
goto leb2;
if(w=='#')
{if(a[u][v]=='+'){a[u][v]='@'; lei=lei-1;}
else if(a[u][v]=='@'){a[u][v]='?'; lei=lei+1;}
else if(a[u][v]=='?'){a[u][v]='+';}
goto leb2;
}
a[u][v]=b[u][v];
leb3: /*打开0区*/
t=0;
if(a[u][v]=='0')
{for(i=1;i<=hang;i=i+1)
{for(j=1;j<=lie;j=j+1)
{s=0;
if(a[i-1][j-1]=='0')s=1; if(a[i-1][j+1]=='0')s=1;
if(a[i-1][j]=='0')s=1; if(a[i+1][j-1]=='0')s=1;
if(a[i+1][j+1]=='0')s=1; if(a[i+1][j]=='0')s=1;
if(a[i][j-1]=='0')s=1; if(a[i][j+1]=='0')s=1;
if(s==1)a[i][j]=b[i][j];
}
}
for(i=1;i<=hang;i=i+1)
{for(j=lie;j>=1;j=j-1)
{s=0;
if(a[i-1][j-1]=='0')s=1; if(a[i-1][j+1]=='0')s=1;
if(a[i-1][j]=='0')s=1; if(a[i+1][j-1]=='0')s=1;
if(a[i+1][j+1]=='0')s=1; if(a[i+1][j]=='0')s=1;
if(a[i][j-1]=='0')s=1; if(a[i][j+1]=='0')s=1;
if(s==1)a[i][j]=b[i][j];
}
}
for(i=hang;i>=1;i=i-1)
{for(j=1;j<=lie;j=j+1)
{s=0;
if(a[i-1][j-1]=='0')s=1; if(a[i-1][j+1]=='0')s=1;
if(a[i-1][j]=='0')s=1; if(a[i+1][j-1]=='0')s=1;
if(a[i+1][j+1]=='0')s=1; if(a[i+1][j]=='0')s=1;
if(a[i][j-1]=='0')s=1; if(a[i][j+1]=='0')s=1;
if(s==1)a[i][j]=b[i][j];
}
}
for(i=hang;i>=1;i=i-1)
{for(j=lie;j>=1;j=j-1)
{s=0;
if(a[i-1][j-1]=='0')s=1; if(a[i-1][j+1]=='0')s=1;
if(a[i-1][j]=='0')s=1; if(a[i+1][j-1]=='0')s=1;
if(a[i+1][j+1]=='0')s=1;if(a[i+1][j]=='0')s=1;
if(a[i][j-1]=='0')s=1; if(a[i][j+1]=='0')s=1;
if(s==1)a[i][j]=b[i][j];
}
}
for(i=1;i<=hang;i=i+1) /*检测0区*/
{for(j=1;j<=lie;j=j+1)
{if(a[i][j]=='0')
{if(a[i-1][j-1]=='+'||a[i-1][j-1]=='@'||a[i-1][j-1]=='?')t=1;
if(a[i-1][j+1]=='+'||a[i-1][j+1]=='@'||a[i-1][j+1]=='?')t=1;
if(a[i+1][j-1]=='+'||a[i+1][j-1]=='@'||a[i+1][j-1]=='?')t=1;
if(a[i+1][j+1]=='+'||a[i+1][j+1]=='@'||a[i+1][j+1]=='?')t=1;
if(a[i+1][j]=='+'||a[i+1][j]=='@'||a[i+1][j]=='?')t=1;
if(a[i][j+1]=='+'||a[i][j+1]=='@'||a[i][j+1]=='?')t=1;
if(a[i][j-1]=='+'||a[i][j-1]=='@'||a[i][j-1]=='?')t=1;
if(a[i-1][j]=='+'||a[i-1][j]=='@'||a[i-1][j]=='?')t=1;
}
}
}
if(t==1)goto leb3;
}
n=0; /*检查结束*/
for(i=1;i<=hang;i=i+1)
{for(j=1;j<=lie;j=j+1)
{if(a[i][j]!='+'&&a[i][j]!='@'&&a[i][j]!='?')n=n+1;}
}
}
while(a[u][v]!='#'&&n!=(hang*lie-ge));
for(i=1;i<=ge;i=i+1) /*游戏结束*/
{x=z[i]/lie+1; y=z[i]%lie+1; a[x][y]='#'; }
printf(" ");
for(i=1;i<=lie;i=i+1)
{w=(i-1)/10+48; printf("%c",w);
w=(i-1)%10+48; printf("%c ",w);
}
printf("
|");
for(i=1;i<=lie;i=i+1){printf("---|");}
printf("
");
for(i=1;i<=hang;i=i+1)
{w=(i-1)/10+48; printf("%c",w);
w=(i-1)%10+48; printf("%c |",w);
for(j=1;j<=lie;j=j+1)
{if(a[i][j]=='0')printf(" |");
else printf(" %c |",a[i][j]);
}
if(i==2)printf(" 剩余雷个数");
if(i==3)printf(" %d",lei); printf("
|");
for(j=1;j<=lie;j=j+1) {printf("---|");}
printf("
");
}
if(n==(hang*lie-ge)) printf("你成功了!
");
else printf(" 游戏结束!
");
printf(" 重玩请输入1
");
t=0;
scanf("%d",&t);
if(t==1)goto leb1;
}
/*注:在DEV c++上运行通过。行号和列号都从0开始,比如要确定第0行第9列不是“雷”,就在0和9中间加入一个字母,可以输入【0a9】三个字符再按回车键。3行7列不是雷,则输入【3a7】回车;第8行第5列是雷,就输入【8#5】回车,9行0列是雷则输入【9#0】并回车*/
❸ 求C语言小游戏源程序
新手要方便写代码,可以收藏下面几个自编函数:
gtxy (6, 3) //光标定位于窗口的第6列,第3行处(准备输出,行与列都是从0算起)
Color (4, 0) //设置为红字配黑底 如 Color (10, 0)则是淡绿字配黑底
yinc (1,0) //隐藏光标(第二个参数设为0就隐藏,没有光标闪烁,yinc代表隐藏)
kou(80,25) //设定窗口缓冲区大小为80列,25行
下面几个是库函数,不需自己编写,只要用#include包含就可以使用。
SetConsoleTitle("俄罗斯方块"); //设置窗口左上角标题栏处出现"俄罗斯方块"5个字
srand( (unsigned) time(NULL) ); //初始化随机数发生器
n= rand( ) % 20; //产生随机数0-19中的一个. 如 rand( )%5 就产生0-4中的一个数
SetConsoleTitle( )函数在<windows.h>里,srand( )函数与rand( )函数要配合用,
就是同时要用,在<stdlib.h>里。如果 rand( )%10+1 就产生1-10之中的一个数。
Sleep(300); //延时300毫秒(就是程序暂停300毫秒后继续运行)
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 <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
using namespace std;
void gotoxy(int x,int y) {COORD pos={x,y}; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);}//光标定位
class Food {//食物类
private: int m_x; int m_y;
public:
void randfood() {//随机产生一个食物
srand((int)time(NULL));//利用时间添加随机数种子,需要ctime头文件
L1:{m_x=rand()%(85)+2;//2~86
m_y=rand()%(25)+2;//2~26
if(m_x%2) goto L1;//如果食物的x坐标不是偶数则重新确定食物的坐标
gotoxy(m_x,m_y);//在确认好的位置输出食物
cout << "★";}}
int getFoodm_x() {return m_x;}//返回食物的x坐标
int getFoodm_y() {return m_y;}};//返回食物的y坐标
class Snake {
private:
struct Snakecoor {int x; int y;};//定义一个蛇的坐标机构
vector<Snakecoor> snakecoor;//将坐标存入vector容器中
//判断并改变前进方向的函数
void degdir(Snakecoor&nexthead) {//定义新的蛇头变量
static char key='d';//静态变量防止改变移动方向后重新改回来
if(_kbhit()) {
char temp=_getch();//定义一个临时变量储存键盘输入的值
switch(temp) {//如果临时变量的值为wasd中的一个,则赋值给key
default: break;//default是缺省情况,只有任何条件都不匹配的情况下才会执行 必须写在前面!不然蛇无法转向
case'w': case'a': case's': case'd':
//如果temp的方向和key的方向不相反则赋值 因为两次移动方向不能相反 将蛇设置为初始向右走
if(key=='w' && temp!='s' || key=='s' && temp!='w' || key=='a' && temp!='d' || key=='d' && temp!='a') key=temp;}}
switch (key) {//根据key的值来确定蛇的移动方向
case'd': nexthead.x=snakecoor.front().x+2; nexthead.y=snakecoor.front().y; break;
//新的蛇头的头部等于容器内第一个数据(旧蛇头)x坐标+2 因为蛇头占两个坐标,移动一次加2
case'a': nexthead.x=snakecoor.front().x-2; nexthead.y=snakecoor.front().y; break;
case'w': nexthead.x=snakecoor.front().x; nexthead.y=snakecoor.front().y-1; break;
//因为控制台的x长度是y的一半,所以用两个x做蛇头,需要的坐标是二倍
case's': nexthead.x=snakecoor.front().x; nexthead.y=snakecoor.front().y+1;}}
//游戏结束时设计一个界面输出“游戏结束”以及分数
void finmatt(const int score) {
system("cls"); gotoxy(40, 14);//清屏然后输出
cout << "游戏结束"; gotoxy(40, 16);
cout << "得分:" << score; gotoxy(0, 26);
exit(0);}//exit为C++的退出函数 exit(0)表示程序正常退出,非0表示非正常退出
void finishgame(const int score) {//游戏结束
if(snakecoor[0].x>=88 || snakecoor[0].x<0 || snakecoor[0].y>=28 || snakecoor[0].y<0) finmatt(score);//撞墙
for(int i=1;i<snakecoor.size();i++) if(snakecoor[0].x==snakecoor[i].x && snakecoor[0].y==snakecoor[i].y) finmatt(score);}//撞到自身
public://构造初始化蛇的位置
Snake() { Snakecoor temp;//临时结构变量用于创建蛇
for(int i=5;i>=0;i--) {//反向创建初始蛇身,初始蛇头朝右
temp.x=16+(i<<1); temp.y=8;//偶数 在蛇头左移生成蛇身
snakecoor.push_back(temp);}}//在蛇尾尾插入临时变量
void move(Food&food, int& score) {//蛇运动的函数
Snakecoor nexthead;//新蛇头变量
degdir(nexthead);//判断和改变蛇前进的方向
snakecoor.insert(snakecoor.begin(), nexthead);//将蛇头插入容器的头部
gotoxy(0, 0); cout << "得分:" << score;//每次移动都在左上角刷新得分
gotoxy(0, 2); cout << "蛇的长度为:" << snakecoor.size();//长度用来测试
finishgame(score);//判断游戏结束,输出分数
//吃到食物蛇的变化
if(snakecoor[0].x==food.getFoodm_x() && snakecoor[0].y==food.getFoodm_y()) {//蛇头与食物重合
gotoxy(snakecoor[0].x, snakecoor[0].y); cout << "●";//吃到食物时这次蛇没有移动,所以蛇会卡顿一下
gotoxy(snakecoor[1].x, snakecoor[1].y); cout << "■";//重新输出一下蛇头和第一节蛇身让蛇不卡顿
score++; food.randfood(); return;}//吃到食物得分+1,如果蛇头坐标和食物坐标重合则重新产生一个食物并直接结束本次移动
for(int i=0;i<snakecoor.size();i++) {//遍历容器,判断食物与蛇身是否重合并输出整条蛇
gotoxy(snakecoor[i].x, snakecoor[i].y);
if (!i) cout << "●"; else cout << "■";//头部输出圆形否则输出方块
if (snakecoor[i].x==food.getFoodm_x() && snakecoor[i].y==food.getFoodm_y())food.randfood();}//如果食物刷新到了蛇身上,则重新产生一个
gotoxy(snakecoor.back().x,snakecoor.back().y);cout << " ";snakecoor.pop_back();}};
//数据与画面是分开,的在容器尾部的地方输出空格 清除画面上的蛇尾,删除容器中最后一个数据 清除数据上的蛇尾
void HideCursor() {CONSOLE_CURSOR_INFO cursor_info={1,0};SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);}//隐藏光标
int main() {system("mode con cols=88 lines=28"); system("title 贪吃蛇"); HideCursor();//光标隐藏,设置控制台窗口大小、标题
int score=0; Food food; food.randfood(); Snake snake;//得分变量,食物对象,开局随机产生一个食物,蛇对象
while(true) {snake.move(food, score);Sleep(150);}return 0;}//蛇移动,游戏速度
❺ 求一些C语言小游戏的源代码,谢谢
学习一下数字版“拼图”代码写法:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include<windows.h>
inti,j,r,k; //i、j、r用于循环,k存放随机数值
intm,n; //m、n是当前空位的下标
inta[4][4]; //存储4×4共16个数字的数组
voidshow(void); //输出界面
voidcsh(void); //初始化界面
intyes(void); //判断排序是否成功(1是0否)
voip(void);//数字向上移动到空位(空位则下移)
voiddown(void); //数字向下移
voidleft(void); //数字向左移
voidrght(void); //数字向右移
voidinkey(void); //按键操作
voidgtxy(intx,inty); //控制光标位置的函数
intmain(void)
{while(1)
{csh();
while(1)
{inkey();
show();
if(yes())
{gtxy(6,12);printf("你成功了!再来一局y/n?");break;}
}
if(getch()=='n')break;
}
return0;
}
voidcsh(void) //初始化
{r=0;
CONSOLE_CURSOR_INFOcursor_info={1,0};//以下两行是隐藏光标的设置
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
for(i=0;i<4;i++)//给数组a依序赋值
for(j=0;j<4;j++)
{if(i==3&&j==3)a[i][j]=0;
elsea[i][j]=1+r++;
}
m=3; n=3; //记下空格(值为0)的下标
down( ); rght( ); rght( ); down( ); //预演4步
srand((unsigned)time(0)); //初始化随机数发生器
for(r=0;r<500;r++)//将数组各值打乱
{k=rand()%(4);
switch(k)
{case0:{up();break;}
case1:{down();break;}
case2:{left();break;}
case3:{rght();break;}
}
}
system("cls");
printf(" 数字拼图");
printf(" ┌──────┬──────┬──────┬──────┐");
printf(" │││││");
printf(" ├──────┼──────┼──────┼──────┤");
printf(" │││││");
printf(" ├──────┼──────┼──────┼──────┤");
printf(" │││││");
printf(" ├──────┼──────┼──────┼──────┤");
printf(" │││││");
printf(" └──────┴──────┴──────┴──────┘");
show();
}
voidshow(void)//输出界面
{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("│");
elseif(a[i][j]>9)printf("%d│",a[i][j]);
elseprintf("%d│",a[i][j]);
}
}
voidinkey(void) //按键操作
{intkey;
key=getch();
switch(key)
{case72:{up();break;}
case80:{down();break;}
case75:{left();break;}
case77:{rght();break;}
}
}
voip(void) //向上移动
{if(m!=3)//空位不得在下边界
{a[m][n]=a[m+1][n];m++;a[m][n]=0;}
}
voiddown(void) //向下移动
{if(m!=0)//空位不得在上边界
{a[m][n]=a[m-1][n];m--;a[m][n]=0;}
}
voidleft(void) //向左移动
{if(n!=3)//空位不得在右边界
{a[m][n]=a[m][n+1];n++;a[m][n]=0;}
}
voidrght(void) //向右移动
{if(n!=0)//空位不得在左边界
{a[m][n]=a[m][n-1];n--;a[m][n]=0;}
}
intyes(void)//判断是否成功
{r=0;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{if(a[i][j]!=1+r++)return(r==16)?1:0;}
}
voidgtxy(intx,inty)//控制光标位置的函数
{COORDcoord;
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
❻ 跪求c语言游戏源码(完全版)
大一编的扫雷(完整),一百多行,Turbo C下编译成功 #include<stdio.h> #include<conio.h> #include<time.h> void adjust(int*,int*); int a[23][23],c[22][22],mm,nn; int roundmine(int,int); void spread(); main() { int i,j,b[22][22],minenum,mine; int x,y,yy,s; char g,h,z; int*x1; int*y1; time_t t; again:srand((unsigned) time(&t)); textcolor(LIGHTGREEN); for(i=2;i<=21;i++) for(j=2;j<=21;j++) { gotoxy(i,j); putch(219); b[i][j]=1; c[i][j]=1; } minenum=0; for(i=1;i<=22;i++) for(j=1;j<=22;j++) { a[i][j]=0; if(i>1&&i<22&j>1&&j<22) {if((s=rand()%10)>0) a[i][j]=0; else if(s==0) a[i][j]=1; } if(a[i][j]) minenum++; } gotoxy(50,2); puts("dirction"); gotoxy(50,3); puts("SPACE:mark"); gotoxy(50,4); puts("ENTER:dig"); gotoxy(50,8); printf("mine num:%4d",minenum); gotoxy(50,5); printf("ESC:exit"); gotoxy(30,15); puts(" "); gotoxy(30,20); puts(" "); mine=minenum; gotoxy(2,2); x=2;y=2; while(mine) { h=getch(); if(h=='H') gotoxy(x,--y); else if(h=='P') gotoxy(x,++y); else if(h=='K') gotoxy(--x,y); else if(h=='M') gotoxy(++x,y); x1=&x;y1=&y; adjust(x1,y1); if(h==' ') { if(c[wherex()][wherey()]) if(b[wherex()][wherey()]) { textcolor(LIGHTRED); putch(16); mine--; gotoxy(59,8); printf("%4d",mine); gotoxy(x,y); b[wherex()][wherey()]=0; } else { textcolor(LIGHTGREEN); putch(219); mine++; gotoxy(59,8); printf("%4d",mine); gotoxy(x,y); b[wherex()][wherey()]=1; } } if(h=='\015') { mm=wherex();nn=wherey(); c[wherex()][wherey()]=0; if(a[wherex()][wherey()]) { for(i=2;i<=21;i++) for(j=2;j<=21;j++) {gotoxy(i,j); if(a[i][j]) {textcolor(LIGHTRED); putch(017); } } break; } else {spread();gotoxy(mm,nn);} } if(h=='\033') exit(0); } yy=1; for(i=2;i<=21;i++) for(j=2;j<=21;j++) if (a[i][j]==1&&b[i][j]==1) {gotoxy(30,15); cprintf("You lose!!"); yy=0; } if(yy) {gotoxy(30,15); cprintf("You win!!"); } gotoxy(30,20); cprintf("Once again(y/n)?"); z=getch(); if(z=='y') goto again; } void adjust(int* x,int* y) { if(*x<2) gotoxy(*x+=20,*y); else if(*x>21) gotoxy(*x-=20,*y); else if(*y<2) gotoxy(*x,*y+=20); else if(*y>21) gotoxy(*x,*y-=20); } int roundmine(int x,int y) { int i,j,r=0; for(i=x-1;i<=x+1;i++) for(j=y-1;j<=y+1;j++) if(a[i][j]) r++; return r; } void spread() { int r,i,j,x,y; x=wherex(); y=wherey(); r=roundmine(x,y); if(r) {printf("%d",r);c[x][y]=0;} else if(r==0&&x>1&&x<22&&y>1&&y<22) { putchar(' ');c[x][y]=0; for(i=x-1;i<=x+1;i++) for(j=y-1;j<=y+1;j++) if(x>1&&x<22&&y>1&&y<22&&c[i][j]) {gotoxy(i,j); spread(); } } }
❼ 谁有能在vc++6.0上运行的c语言小游戏代码
最基础的贪吃蛇的代码
#include<stdio.h>
#include<windows.h>//基本型态定义。支援型态定义函数。使用者界面函数 图形装置界面函数。
#include<conio.h> //用户通过按键盘产生的对应操作 (控制台)
#include<stdlib.h>
#include<time.h> //日期和时间头文件
#define LEN 30
#define WID 25
int Snake[LEN][WID] = {0}; //数组的元素代表蛇的各个部位
char Sna_Hea_Dir = 'a';//记录蛇头的移动方向
int Sna_Hea_X, Sna_Hea_Y;//记录蛇头的位置
int Snake_Len = 3;//记录蛇的长度
clock_t Now_Time;//记录当前时间,以便自动移动
int Wait_Time ;//记录自动移动的时间间隔
int Eat_Apple = 1;//吃到苹果表示为1
int Level ;
int All_Score = -1;
int Apple_Num = -1;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); //获取标准输出的句柄 <windows.h>
//句柄 :标志应用程序中的不同对象和同类对象中的不同的实例 方便操控,
void gotoxy(int x, int y)//设置光标位置
{
COORD pos = {x,y}; //定义一个字符在控制台屏幕上的坐标POS
SetConsoleCursorPosition(hConsole, pos); //定位光标位置的函数<windows.h>
}
void Hide_Cursor()//隐藏光标 固定函数
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(hConsole, &cursor_info);
}
void SetColor(int color)//设置颜色
{
SetConsoleTextAttribute(hConsole, color);
//是API设置字体颜色和背景色的函数 格式:SetConsoleTextAttribute(句柄,颜色);
}
void Print_Snake()//打印蛇头和蛇的脖子和蛇尾
{
int iy, ix, color;
for(iy = 0; iy < WID; ++iy)
for(ix = 0; ix < LEN; ++ix)
{
if(Snake[ix][iy] == 1)//蛇头
{
SetColor(0xf); //oxf代表分配的内存地址 setcolor:34行自定义设置颜色的函数
gotoxy(ix*2, iy);
printf("※");
}
if(Snake[ix][iy] == 2)//蛇的脖子
{
color = rand()%15 + 1; //rand()函数是产生随机数的一个随机函数。C语言里还有 srand()函数等。
//头文件:stdlib.h
if(color == 14)
color -= rand() % 13 + 1; //变色
SetColor(color);
gotoxy(ix*2, iy);
printf("■");
}
if(Snake[ix][iy] == Snake_Len)
{
gotoxy(ix*2, iy);
SetColor(0xe);
printf("≈");
}
}
}
void Clear_Snake()//擦除贪吃蛇
{
int iy, ix;
for(iy = 0; iy < WID; ++iy)
for(ix = 0; ix < LEN; ++ix)
{
gotoxy(ix*2, iy);
if(Snake[ix][iy] == Snake_Len)
printf(" ");
}
}
void Rand_Apple()//随机产生苹果
{
int ix, iy;
do
{
ix = rand() % LEN;
iy = rand() % WID;
}while(Snake[ix][iy]);
Snake[ix][iy] = -1;
gotoxy(ix*2, iy);
printf("⊙");
Eat_Apple = 0;
}
void Game_Over()//蛇死掉了
{
gotoxy(30, 10);
printf("Game Over");
Sleep(3000);
system("pause > nul");
exit(0);
}
void Move_Snake()//让蛇动起来
{
int ix, iy;
for(ix = 0; ix < LEN; ++ix)//先标记蛇头
for(iy = 0; iy < WID; ++iy)
if(Snake[ix][iy] == 1)
{
switch(Sna_Hea_Dir)//根据新的蛇头方向标志蛇头
{
case 'w':
if(iy == 0)
Game_Over();
else
Sna_Hea_Y = iy - 1;
Sna_Hea_X = ix;
break;
case 's':
if(iy == (WID -1))
Game_Over();
else
Sna_Hea_Y = iy + 1;
Sna_Hea_X = ix;
break;
case 'a':
if(ix == 0)
Game_Over();
else
Sna_Hea_X = ix - 1;
Sna_Hea_Y = iy;
break;
case 'd':
if(ix == (LEN - 1))
Game_Over();
else
Sna_Hea_X = ix + 1;
Sna_Hea_Y = iy;
break;
default:
break;
}
}
if(Snake[Sna_Hea_X][Sna_Hea_Y]!=1&&Snake[Sna_Hea_X][Sna_Hea_Y]!=0&&Snake[Sna_Hea_X][Sna_Hea_Y]!=-1)
Game_Over();
if(Snake[Sna_Hea_X][Sna_Hea_Y] < 0)//吃到苹果
{
++Snake_Len;
Eat_Apple = 1;
}
for(ix = 0; ix < LEN; ++ix)//处理蛇尾
for(iy = 0; iy < WID; ++iy)
{
if(Snake[ix][iy] > 0)
{
if(Snake[ix][iy] != Snake_Len)
Snake[ix][iy] += 1;
else
Snake[ix][iy] = 0;
}
}
Snake[Sna_Hea_X][Sna_Hea_Y] = 1;//处理蛇头
}
void Get_Input()//控制蛇的移动方向
{
if(kbhit())
{
switch(getch())
{
case 87:
Sna_Hea_Dir = 'w';
break;
case 83:
Sna_Hea_Dir = 's';
break;
case 65:
Sna_Hea_Dir = 'a';
break;
case 68:
Sna_Hea_Dir = 'd';
break;
default:
break;
}
}
if(clock() - Now_Time >= Wait_Time)//蛇到时间自动行走
{
Clear_Snake();
Move_Snake();
Print_Snake();
Now_Time = clock();
}
}
void Init()//初始化
{
system("title 贪吃毛毛蛇");
system("mode con: cols=80 lines=25");
Hide_Cursor();
gotoxy(61, 4);
printf("You Score:");
gotoxy(61, 6);
printf("You Level:");
gotoxy(61, 8);
printf("The Lenght:");
gotoxy(61, 10);
printf("The Speed:");
gotoxy(61, 12);
printf("Apple Num:");
int i;
for(i = 0; i < Snake_Len; ++i)//生成蛇
Snake[10+i][15] = i+1;
int iy, ix;//打印蛇
for(iy = 0; iy < WID; ++iy)
for(ix = 0; ix < LEN; ++ix)
{
if(Snake[ix][iy])
{
SetColor(Snake[ix][iy]);
gotoxy(ix*2, iy);
printf("■");
}
}
}
void Pri_News()//打印信息
{
SetColor(0xe);
gotoxy(73,4);
All_Score += Level;
printf("%3d", All_Score);
gotoxy(73, 6);
printf("%3d", Level);
gotoxy(73, 8);
printf("%3d",Snake_Len);
gotoxy(73, 10);
printf("0.%3ds", Wait_Time/10);
gotoxy(73, 12);
printf("%d", Apple_Num);
}
void Lev_Sys()//等级系统
{
if(((Apple_Num-1) / 10) == Level)
{
++Level;
if(Wait_Time > 50)
Wait_Time -= 50;
else
if(Wait_Time > 10)
Wait_Time -= 10;
else
Wait_Time -= 1;
}
}
int main(void)
{
Init();
srand((unsigned)time(NULL));//设置随机数的种子
Now_Time = clock();
int speed1=1000,speed2,a;
printf("\n");
printf("请输入你想要的速度\n");
scanf("%d",&speed2);
Level=1;
Wait_Time=speed1-speed2;
printf("请输入你想要的苹果数\n");
scanf("%d",&a);
while(a--)
Rand_Apple();
while(1)
{
if(Eat_Apple)
{
++Apple_Num;
Rand_Apple();
Lev_Sys();
Pri_News();
}
Get_Input();
Sleep(10);
}
return 0;
}
❽ 求c语言游戏编程源代码
不知道楼主学没学window api等等;如果没,只懂C语言是编不出来游戏程序,如果基于windows游戏,你可以学widow api,再编吧。如果想要,这里有http://www.easy-share.com/1911030739/经典小游戏的c语言源代码.rar
❾ 急需一个C语言 猜拳游戏的源代码!!!!
分类: 电脑/网络 >> 程序设计 >> 其他编程语言
问题描述:
急需一个C语言 猜拳游戏的源代码!!!!急需一个C语言 猜拳游戏的源代码!!!!急需一个C语言 猜拳游戏的源代码!!!!急需一个C语言 猜拳游戏的源代码!!!!急需一个C语言 猜拳游戏的源代码!!!!急需一个C语言 猜拳游戏的源代码!!!!急需一个C语言 猜拳游戏的源代码!!!!
解析:
enum p_r_s{
paper,rock,scissors,game,help,instructions,quit
};
#include <stdio.h>
main()
{
enum p_r_s player,machine;
enum p_r_s selection_by_player(),selection_by_machine();
int win,lose,tie;
win=lose=tie=0;
instructions_for_the_player();
while((player=selection_by_player())!=quit)
switch(player){
case paper:
case rock:
case scissors:
machine=selection_by_machine();
if(player==machine){
++tie;
printf("\n a tie");
}
else if(you_won(player,machine)){
++win;
printf("\n you won");
}
else{
++lose;
printf("\n i won");
}
break;
case game:
game_status(win,lose,tie);
break;
case instructions:
instructions_for_the_player();
break;
case help:
help_for_the_player();
break;
}
game_status(win,lose,tie);
printf("\n\nBYE\n\n");
}
instructions_for_the_player()
{
printf("\n%s\n\n%s\n\n%s\n%s\n%s\n\n%s\n%s\n%s\n\n%s\n%s\n%s",
"PAPER,ROCK,SCISSORS",
"In this game",
"p is for paper,",
"r is for rock,",
"s is for scissors.",
"Both the player and the machine will choose one",
"of p,r,or s. If the o choices are the same,",
"then the game is a tie. Otherwise:",
"\"paper covers the rock\" (a win for paper),",
"\"rock breaks the scissors\" (a win for rock),",
"\"scissors cut the paper\" (a win for scissors).");
printf("\n\n%s\n\n%s\n%s\n%s\n%s\n\n%s\n\n%s",
"There are other allowable inputs:",
"g for game status (the number of wins so far),",
"h for help,",
"i for instructions (reprin these instructions),",
"q for quit (to quit the game).",
"This game is played repeatedly until q is entered.",
"Good luck!");
}
enum p_r_s selection_by_player()
{
char c;
enum p_r_s player;
printf("\n\ninput p,r,or s:");
while((c=getchar())==''||c=='\n'||c=='t');
;
switch(c){
case 'p':
player=paper;
break;
case 'r':
player=rock;
break;
case 's':
player=scissors;
break;
case 'g':
player=game;
break;
case 'i':
player=instructions;
break;
case 'q':
player=quit;
break;
default:
player=help;
}
return(player);
}
enum p_r_s selection_by_machine()
{
static int i;
i=++i%3;
return((i==0)? paper:((i==1)? rock:scissors));
}
you_won(player,machine)
enum p_r_s player,machine;
{
int victory;
if(player==paper)
victory=machine==rock;
else if(player==rock)
victory=machine==scissors;
else/*player==scissors*/
victory=machine==paper;
return(victory);
}
game_status(win,lose,tie)
{
printf("\nGAME STATUS");
printf("\n\n%7d%s\n%7d%s\n%7d%s\n%7d%s",
win,"games won by you",
lose,"games won by me",
tie,"game tied",
win+lose+tie,"games played:");
}
help_for_the_player()
{
printf("\n%s\n\n%s\n%s\n%s\n%s\n%s\n%s\n%s",
"the following characters can be used for input:",
" p for paper",
" r for rock",
" s for scissors",
" g to find out the game status",
" h to print this list",
" i to reprint the instructions for this game",
" q to quit this game");
}
❿ 急求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);
}