当前位置:首页 » 编程语言 » c语言手游代码
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言手游代码

发布时间: 2023-02-16 22:46:51

⑴ 怎样用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 语言代码

2L你给的什么呀,明明是加密算法调用我给你一个贪吃蛇的 C# #define N 200/*定义全局常量*/
#define m 25
#include <graphics.h>
#include <math.h>
#include <stdlib.h>
#include <dos.h>
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define Esc 0x011b
int i,j,key,k;
struct Food/*构造食物结构体*/
{
int x;
int y;
int yes;
}food;
struct Goods/*构造宝贝结构体*/
{
int x;
int y;
int yes;
}goods;
struct Block/*构造障碍物结构体*/
{
int x[m];
int y[m];
int yes;
}block;
struct Snake{/*构造蛇结构体*/
int x[N];
int y[N];
int node;
int direction;
int life;
}snake;
struct Game/*构建游戏级别参数体*/
{
int score;
int level;
int speed;
}game;
/*定义函数*/
void init(void);/*定义图形驱动*/
void close(void);/*定义关闭函数*/
void drawk(void);/*定义界面函数*/
void gameover(void);/*定义游戏结束函数*/
void gameplay(void);/*定义游戏主函数*/
void prscore(void);/*定义得分函数*/

void main(void){/*主函数体,调用以下四个函数*/
init();
setbkcolor(7);
drawk();
gameplay();
close();
}

void init(void){/*构建图形驱动函数*/
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
cleardevice();
}

void drawk(void){/*构建游戏界面函数*/
/*setbkcolor(LIGHTGREEN);*/
char str3[50];
setfillstyle(SOLID_FILL,BLUE);/*条型边框,显示版本信息*/
bar3d(48,9,610,38,1,45);
setcolor(YELLOW);/*版本信息*/
sprintf(str3,"Version:5.01,Powerwing Studio");
outtextxy(330,20,str3);
setfillstyle(LTSLASH_FILL,YELLOW);/*设定墙边的填充形式*/
bar3d(48,48,58,462,0,0);/*设定墙边*/
bar3d(48,39,611,48,0,0);
bar3d(48,452,611,462,0,0);
bar3d(602,39,611,462,0,0);

}

void gameplay(void){/*构建游戏主函数*/
/*初始化游戏角色*/
randomize();/*随机数发生器*/
goods.yes=1;
block.yes=1;
food.yes=1;/*场景中需建立新的食物*/
snake.life=1;/*初始化蛇生命值*/
snake.direction=1;/*蛇起始的移动方向定义为向右*/
snake.x[0]=100;snake.y[0]=100;/*蛇头的位置坐标初始化*/
snake.x[1]=110;snake.y[1]=100;
snake.node=2;/*蛇初始化节数,共两节只有蛇头*/
/*初始化障碍物的数组*/
block.x[0]=170;block.y[0]=270;/*level 1*/
block.x[1]=410;block.y[1]=310;
block.x[2]=300;block.y[2]=200;
block.x[3]=320;block.y[3]=420;
block.x[4]=250;block.y[4]=350;
block.x[5]=220;block.y[5]=320;/*level 2*/
block.x[6]=310;block.y[6]=410;
block.x[7]=400;block.y[7]=500;
block.x[8]=230;block.y[8]=230;
block.x[9]=280;block.y[9]=280;
block.x[10]=170;block.y[10]=280;/*level 3*/
block.x[11]=420;block.y[11]=310;
block.x[12]=310;block.y[12]=200;
block.x[13]=320;block.y[13]=400;
block.x[14]=250;block.y[14]=260;/*level 4*/
block.x[15]=220;block.y[15]=330;
block.x[16]=130;block.y[16]=410;
block.x[17]=310;block.y[17]=510;
block.x[18]=230;block.y[18]=340;
block.x[19]=280;block.y[19]=380;
block.x[20]=270;block.y[20]=170;/*level 5*/
block.x[21]=410;block.y[21]=450;
block.x[22]=190;block.y[22]=200;
block.x[23]=150;block.y[23]=320;
block.x[24]=270;block.y[24]=350;
block.x[25]=340;block.y[25]=320;

game.score=0;
game.speed=50000;
game.level=1;
prscore();/*得分初始化*/
while(1){/*判断为真可以按Esc退出循环结束游戏*/
while(!kbhit()){/*无按键按下时,蛇自己移动身体*/

if(game.level==1){/*画出障碍物*/
for(j=0;j<5;j++){
setcolor(5);/**/
rectangle(block.x[j],block.y[j],block.x[j]+10,block.y[j]-10);
block.yes=0;
}
}
if(game.level==2){/*画出障碍物*/
for(j=0;j<9;j++){
setcolor(5);/**/
rectangle(block.x[j],block.y[j],block.x[j]+10,block.y[j]-10);
block.yes=0;
}
}
if(game.level==3){/*画出障碍物*/
for(j=0;j<14;j++){
setcolor(5);/**/
rectangle(block.x[j],block.y[j],block.x[j]+10,block.y[j]-10);
block.yes=0;
}
}
if(game.level==4){/*画出障碍物*/
for(j=0;j<19;j++){
setcolor(5);/**/
rectangle(block.x[j],block.y[j],block.x[j]+10,block.y[j]-10);
block.yes=0;
}
}
if(game.level==5){/*画出障碍物*/
for(j=0;j<25;j++){
setcolor(5);/**/
rectangle(block.x[j],block.y[j],block.x[j]+10,block.y[j]-10);
block.yes=0;
}
}
if(food.yes==1){/*需要画出新的食物*/
food.x=rand()%400+60;/*获得间隔60的随机数食物坐标值*/
food.y=rand()%350+60;
while(food.x%10!=0)/*判断坐标值是否满足被10整除,否,自动增加*/
food.x++;
while(food.y%10!=0)
food.y++;
food.yes=0;/*新的食物已经产生*/
}

if(goods.yes==1){/*需要画出新的宝物*/
goods.x=rand()%380+60;/*获得间隔60的随机数宝贝坐标值*/
goods.y=rand()%320+80;
while(goods.x%10!=0)/*判断坐标值是否满足被10整除,否,自动增加*/
goods.x++;
while(goods.y%10!=0)
goods.y++;
goods.yes=0;/*新的宝贝已经产生*/
}
if(goods.yes==0){/*新宝贝产生,应显示出来*/

setcolor(0);/*擦除*/
rectangle(goods.x,goods.y,goods.x+10,goods.y-10);
delay(50);/*延时*/
setcolor(YELLOW);
goods.x=goods.x+random(10)-random(20);/*随机数增量*/
goods.y=goods.y+random(10)-random(20);
while(goods.x%10!=0)/*判断变化后的坐标值是否满足被10整除,否,自动增加*/
goods.x++;
while(goods.y%10!=0)
goods.y++;
rectangle(goods.x,goods.y,goods.x+10,goods.y-10);/*重画出宝贝*/
if(goods.x<65||goods.x>585||goods.y<65|goods.y>445){/*判定宝贝是否越界*/
setcolor(0);/*擦除越界的宝贝*/
rectangle(goods.x,goods.y,goods.x+10,goods.y-10);
goods.yes=1;/*越界后重新生成宝贝*/
}

}

if(food.yes==0){/*新食物产生,应显示出来*/
setcolor(GREEN);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*设定当前线型*/
rectangle(food.x,food.y,food.x+10,food.y-10);
}
for(i=snake.node-1;i>0;i--){/*取得需重画的蛇的节数*/
snake.x[i]=snake.x[i-1];/*最后一节的坐标值等于倒数第二节的坐标值*/
snake.y[i]=snake.y[i-1];
}
switch(snake.direction){/*判断蛇头的移动方向*/
case 1:snake.x[0]+=10;break;/*向右*/
case 2:snake.x[0]-=10;break;/*向左*/
case 3:snake.y[0]-=10;break;/*向上*/
case 4:snake.y[0]+=10;break;/*向下*/
}
for(i=3;i<snake.node;i++){/*超过4节后,判断蛇自身碰撞*/
if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0]){/*即自身的任一节坐标值与蛇头坐标相等*/
for(i=1;i<snake.node-1;i++){/*擦除自己碰撞后位置蛇的身子*/
setcolor(0);
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],
snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);
}
snake.life-=1;/*生命值减少一*/
snake.node-=5;
prscore();/*输出结果*/
if(snake.life==0){/*判断生命值是否为0*/
gameover();/*游戏结束*/
break;/*退出内循环*/
}
}
}
if(snake.x[0]<55||snake.x[0]>595||snake.y[0]<55||snake.y[0]>455){/*判断蛇是否与墙体碰撞*/
for(i=1;i<snake.node-1;i++){/*擦除撞墙后位置蛇的身子*/
setcolor(0);
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],
snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);
}
snake.x[0]=100;snake.y[0]=100;/*蛇头的位置坐标重新初始化*/
snake.x[1]=110;snake.y[1]=100;
snake.direction=1;/*蛇起始的移动方向定义为向右*/
snake.life-=1;/*生命值减少一*/
snake.node-=5;/*相应节数减少5节*/
prscore();
if(snake.life==0){
gameover();
break;
}
}
/*判断蛇与障碍物碰撞,食物是否与障碍物重叠*/
if(game.level==1){/*判断级别,并设定相应的障碍物数量,即数组个数*/
k=5;
}
else if(game.level==2){
k=9;
}
else if(game.level==3){
k=14;
}
else if(game.level==4){
k=19;
}
else if(game.level==5){
k=25;
}
for(j=0;j<k;j++){
if(snake.x[0]==block.x[j]&&snake.y[0]==block.y[j]){
for(i=1;i<snake.node-1;i++){/*擦除撞墙后位置蛇的身子*/
setcolor(0);
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],
snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);
}
if(food.x==block.x[j]&&block.y[j]==food.y){/*防止障碍物与食物重叠*/
setcolor(0);/*设定食物的颜色为背景色,即擦除*/
rectangle(food.x,food.y,food.x+10,food.y-10);
food.yes=1;/*食物重新生成*/
}
snake.x[0]=100;snake.y[0]=100;/*蛇头的位置坐标重新初始化*/
snake.x[1]=110;snake.y[1]=100;
snake.direction=1;/*蛇起始的移动方向定义为向右*/
snake.life-=1;
snake.node-=5;
prscore();
if(snake.life==0){
gameover();
break;
}
}
}

if(snake.x[0]==food.x&&snake.y[0]==food.y){/*判断蛇是否吃到食物*/

setcolor(0);/*设定食物的颜色为背景色,即擦除*/
rectangle(food.x,food.y,food.x+10,food.y-10);
snake.x[snake.node]=-20;/*新的一节放在不可见的位置*/
snake.y[snake.node]=-20;
snake.node++;/*蛇身增加一节*/
if(snake.node>2){/*当节数每增加5节生命值增加一*/
snake.life=1+fabs((snake.node-2)/5);
}
food.yes=1;/*场景需要增加食物*/
game.score+=20;/*加分*/
prscore();/*输出得分*/
}
if(snake.x[0]==goods.x&&snake.y[0]==goods.y){/*判定蛇是否得到宝贝*/
setcolor(0);/*设定宝贝的颜色为背景色,即擦除*/
rectangle(goods.x,goods.y,goods.x+10,goods.y-10);
goods.yes=1;/*场景需要增加新的宝贝*/
game.score+=100;/*得到宝贝后加100分*/
prscore();/*输出得分*/
}

if(game.score<500){/*设定游戏速度和难度级别*/
game.speed=50000;
game.level=1;}
else if(game.score>=500&&game.score<1000){
game.level=2;
game.speed=40000;}
else if(game.score>=1000&&game.score<1500){
game.level=3;
game.speed=30000;}
else if(game.score>=1500&&game.score<2000){
game.level=4;
game.speed=20000; }
else if(game.score>=5000){
game.level=5;
game.speed=10000;}

setcolor(4);/*画出移动的蛇*/
setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*设定当前线型*/
for(i=0;i<snake.node;i++)
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);
delay(game.speed);
setcolor(0);/*用背景色擦去最后一节*/
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],
snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);

} /*endwhile(! kbhit) */

if(snake.life==0)/*判断循环结束条件:蛇死或者检测到Esc按键*/
break;

key=bioskey(0);/*判断按键*/
if(key==Esc)
break;
/*判断蛇头接收到的用户按键响应的移动方向*/
else if(key==UP&&snake.direction!=4)
snake.direction=3;
else if(key==RIGHT&&snake.direction!=2)
snake.direction=1;
else if(key==LEFT&&snake.direction!=1)
snake.direction=2;
else if(key==DOWN&&snake.direction!=3)
snake.direction=4;

}/*endwhile(1)*/
}
void gameover(void){/*游戏结束处理*/
cleardevice();/*清屏*/
prscore();/*输出得分*/
setcolor(RED);/*打印出“Game Over”字样*/
settextstyle(0,0,4);
outtextxy(200,200,"Game Over!");
getch();
}

void prscore(void){/*定义分数输出函数*/
char str1[10];
char str2[10];
char str4[20];
setfillstyle(SOLID_FILL,BLUE);/*用于清除旧的显示信息*/
bar(49,10,320,37);
setcolor(WHITE);
settextstyle(0,0,1);
sprintf(str1,"score:%d",game.score);/*输出得分*/
outtextxy(55,20,str1);
sprintf(str2,"level:%d",game.level);/*输出级别*/
outtextxy(250,20,str2);
sprintf(str4,"life:%d",snake.life);/*输出级别*/
outtextxy(150,20,str4);
}

void close(void){/*定义关闭函数,退出图形模式*/
getch();
closegraph();
}

⑶ 用C语言编写的小游戏代码是什么

/*贪吃蛇*/

#include<stdio.h>

#include<time.h>

#include<conio.h>

#include<stdlib.h>

inthead=3,tail=0;

intmain()

{

inti,j,k=0;

intzuobiao[2][80];

longstart;

intdirection=77;

intgamespeed;

inttimeover;

intchange(charqipan[20][80],

intzuobiao[2][80],

chardirection);

zuobiao[0][tail]=1;

zuobiao[1][tail]=1;

zuobiao[0][1]=1;

zuobiao[1][1]=2;zuobiao[0

[2]=1;

zuobiao[1][2]=3;

zuobiao[0][head]=1;

zuobiao[1][head]=4;

/*处理棋盘*/

charqipan[20][80];

//定义棋盘

for(i=0;i<20;i++)

for(j=0;j<80;j++)

qipan[i][j]='';//初始化棋盘

for(i=0;i<80;i++)

qipan[0][i]='_';

for(i=0;i<20;i++)

qipan[i][0]='|';

for(i=0;i<20;i++)

qipan[i][79]='|';

for(i=0;i<80;i++)

C语言是一门通用计算机编程语言,应用广泛。C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。

⑷ 求一些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语言编写的小游戏代码是什么

/*也不知道你是什么级别的,我是一个新手,刚接触编程语言,以下是我自己变得一个小程序,在所有c语言的编译器(vc++6.0、turbo…………)上都能运行,你还可以进一步改进。这是一个类似贪吃蛇的小游戏。祝你好运*/
/*贪吃蛇*/
#include<stdio.h>
#include<time.h>
#include<conio.h>
#include<stdlib.h>
int head=3 ,tail=0;
int main()
{
int i,j,k=0;
int zuobiao[2][80];
long start;
int direction=77;
int gamespeed;
int timeover;
int change(char qipan[20][80],int zuobiao[2][80],char direction);
zuobiao[0][tail]=1;zuobiao[1][tail]=1;zuobiao[0][1]=1;zuobiao[1][1]=2;zuobiao[0][2]=1;zuobiao[1][2]=3;zuobiao[0][head]=1;zuobiao[1][head]=4;
/*处理棋盘*/
char qipan[20][80];//定义棋盘
for(i=0;i<20;i++)
for(j=0;j<80;j++)
qipan[i][j]=' ';//初始化棋盘
for(i=0;i<80;i++)
qipan[0][i]='_';
for(i=0;i<20;i++)
qipan[i][0]='|';
for(i=0;i<20;i++)
qipan[i][79]='|';
for(i=0;i<80;i++)
qipan[19][i]='_';
qipan[1][1]=qipan[1][2]=qipan[1][3]='*';//初始化蛇的位置
qipan[1][4]='#';
printf("This is a game of a SNAKE.\nGOOD LUCK TO YOU !\n");
printf("Input your game speed,please.(e.g.300)\n");
scanf("%d",&gamespeed);

while(direction!='q')
{
system("cls");
for(i=0;i<20;i++)//打印出棋盘
for(j=0;j<80;j++)
printf("%c",qipan[i][j]);
timeover=1;
start=clock();
while(!kbhit()&&(timeover=clock()-start<=gamespeed));
if(timeover)
{
getch();
direction=getch();
}
else
direction=direction;
if(!(direction==72||direction==80||direction==75||direction==77))
{
return 0;
system("cls");
printf("GAME OVER!\n");
}
if(!change(qipan,zuobiao,direction))
{
direction='q';
system("cls");
printf("GAME OVER!\n");
}
}
return 0;
}
int change(char qipan[20][80],int zuobiao[2][80],char direction)
{
int x,y;
if(direction==72)
x=zuobiao[0][head]-1;y=zuobiao[1][head];
if(direction==80)
x=zuobiao[0][head]+1;y=zuobiao[1][head];
if(direction==75)
x=zuobiao[0][head];y=zuobiao[0][head]-1;
if(direction==77)
x=zuobiao[0][head];y=zuobiao[1][head]+1;
if(x==0||x==18||y==78||y==0)
return 0;
if(qipan[x][y]!=' ')
return 0;
qipan[zuobiao[0][tail]][zuobiao[1][tail]]=' ';
tail=(tail+1)%80;
qipan[zuobiao[0][head]][zuobiao[1][head]]='*';
head=(head+1)%80;
zuobiao[0][head]=x;
zuobiao[1][head]=y;
qipan[zuobiao[0][head]][zuobiao[1][head]]='#';
return 1;
}

⑹ 用C语言编写的小游戏代码是什么

/*也不知道你是什么级别的,我是一个新手,刚接触编程语言,以下是我自己变得一个小程序,在所有c语言的编译器(vc++6.0、turbo????)上都能运行,你还可以进一步改进。这是一个类似贪吃蛇的小游戏。祝你好运*/x0dx0a/*贪吃蛇*/x0dx0a#includex0dx0a#includex0dx0a#includex0dx0a#includex0dx0aint head=3 ,tail=0;x0dx0aint main()x0dx0a{x0dx0aint i,j,k=0;x0dx0aint zuobiao[2][80];x0dx0along start;x0dx0aint direction=77;x0dx0aint gamespeed;x0dx0aint timeover;x0dx0aint change(char qipan[20][80],int zuobiao[2][80],char direction);x0dx0azuobiao[0][tail]=1;zuobiao[1][tail]=1;zuobiao[0][1]=1;zuobiao[1][1]=2;zuobiao[0][2]=1;zuobiao[1][2]=3;zuobiao[0][head]=1;zuobiao[1][head]=4;x0dx0a/*处理棋盘*/x0dx0achar qipan[20][80];//定义棋盘x0dx0afor(i=0;i<20;i++)x0dx0a for(j=0;j<80;j++)x0dx0aqipan[i][j]=' '//初始化棋盘x0dx0afor(i=0;i<80;i++)x0dx0aqipan[0][i]='_'x0dx0afor(i=0;i<20;i++)x0dx0aqipan[i][0]='|'x0dx0afor(i=0;i<20;i++)x0dx0aqipan[i][79]='|'x0dx0afor(i=0;i<80;i++)x0dx0aqipan[19][i]='_'x0dx0aqipan[1][1]=qipan[1][2]=qipan[1][3]='*'//初始化蛇的位置x0dx0aqipan[1][4]='#'x0dx0aprintf("This is a game of a SNAKE.\nGOOD LUCK TO YOU !\n");x0dx0aprintf("Input your game speed,please.(e.g.300)\n");x0dx0ascanf("%d",&gamespeed);x0dx0ax0dx0awhile(direction!='q')x0dx0a{x0dx0asystem("cls");x0dx0afor(i=0;i<20;i++)//打印出棋盘x0dx0afor(j=0;j<80;j++)x0dx0aprintf("%c",qipan[i][j]);x0dx0atimeover=1;x0dx0astart=clock();x0dx0awhile(!kbhit()&&(timeover=clock()-start<=gamespeed));x0dx0aif(timeover)x0dx0a{x0dx0agetch();x0dx0adirection=getch();x0dx0a}x0dx0aelsex0dx0adirection=direction;x0dx0aif(!(direction==72||direction==80||direction==75||direction==77))x0dx0a{x0dx0areturn 0;x0dx0asystem("cls");x0dx0aprintf("GAME OVER!\n");x0dx0a}x0dx0aif(!change(qipan,zuobiao,direction))x0dx0a{x0dx0adirection='q'x0dx0asystem("cls");x0dx0aprintf("GAME OVER!\n");x0dx0a}x0dx0a}x0dx0areturn 0;x0dx0a}x0dx0aint change(char qipan[20][80],int zuobiao[2][80],char direction)x0dx0a{x0dx0aint x,y;x0dx0aif(direction==72)x0dx0ax=zuobiao[0][head]-1;y=zuobiao[1][head];x0dx0aif(direction==80)x0dx0ax=zuobiao[0][head]+1;y=zuobiao[1][head];x0dx0aif(direction==75)x0dx0ax=zuobiao[0][head];y=zuobiao[0][head]-1;x0dx0aif(direction==77)x0dx0ax=zuobiao[0][head];y=zuobiao[1][head]+1;x0dx0aif(x==0||x==18||y==78||y==0)x0dx0areturn 0;x0dx0aif(qipan[x][y]!=' ')x0dx0areturn 0;x0dx0aqipan[zuobiao[0][tail]][zuobiao[1][tail]]=' 'x0dx0atail=(tail+1)%80;x0dx0aqipan[zuobiao[0][head]][zuobiao[1][head]]='*'x0dx0ahead=(head+1)%80;x0dx0azuobiao[0][head]=x;x0dx0azuobiao[1][head]=y;x0dx0aqipan[zuobiao[0][head]][zuobiao[1][head]]='#'x0dx0areturn 1;x0dx0a}

⑺ 求几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语言编写的小游戏的游戏代码,如黑白棋贪吃蛇等

我这儿有c语言的自写俄罗斯方块,请指教:#include
#include
#include
#include
#include
#include
#include

#define ESC 0x011b
#define UP 0x4800
#define DOWN 0x5000
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define SPACE 0x3920
#define Y 0x1579
#define N 0x316e
#define clearkbd(); while(bioskey(1)) bioskey(0); /*清空键盘缓冲队列*/

void update();
void messagebox();
void process();
void initremove();
void initinfo();
void initbox();
void initposition();
void next_shape();

typedef struct shape /*形状单一状态的记录*/
{ int attr;
int co[8];
}shape;

typedef struct RE_AB /*相对,绝对坐标记录*/
{ int Rx,Ry;
int x1,x2,y1,y2;
}RE_AB;

RE_AB RA;

shape p[19]={ { RED,0,1,1,0,1,1,2,1 }, /*数组中保证y最大的在最后,以便initposition使用*/
{ RED,0,1,1,0,1,1,1,2 },
{ RED,0,0,1,0,2,0,1,1 },
{ RED,0,0,0,1,1,1,0,2 },
{ GREEN,0,0,1,0,2,0,3,0 },
{ GREEN,0,0,0,1,0,2,0,3 },
{ CYAN,0,0,0,1,1,0,1,1 },
{ BROWN,0,0,1,0,1,1,2,1 },
{ BROWN,1,0,0,1,1,1,0,2 },
{ BLUE,1,0,2,0,1,1,0,1 },
{ BLUE,0,0,0,1,1,1,1,2 },
{ MAGENTA,0,0,0,1,0,2,1,2 },
{ MAGENTA,2,0,0,1,1,1,2,1},
{ MAGENTA,0,0,1,0,1,1,1,2 },
{ MAGENTA,0,0,0,1,1,0,2,0 },
{ YELLOW,0,2,1,0,1,1,1,2 },
{ YELLOW,0,0,1,0,2,0,2,1 },
{ YELLOW,1,0,0,0,0,1,0,2},
{ YELLOW,0,0,0,1,1,1,2,1 },
};
int nback,nleft,nright,r_f[12][22],rs1,rs2,xcors,xcorb,ycors,ycorb;
/*检查方快有没有左,右,下接触,游戏区内所有格子有无颜色记录数组,rs1形状记录,rs2为提示框用,记录小格子在游戏区中的位置,按键存储*/

void interrupt (*oldint)(); /*系统定时中断*/
int count_down=0,count_other=0; /*中断记时*/

void interrupt newint() /*设置新的中断程序*/
{ count_down++;
count_other++;
oldint();
}

void intenable() /*设置中断向量表,启动新的中断程序*/
{ oldint=getvect(0x1c);
disable();
setvect(0x1c,newint);
enable();
}

void intrestore() /*恢复中断向量*/
{ disable();
setvect(0x1c,oldint);
enable();
}

void HZ12(int x0,int y0,int w,int color,char *s) /*根据字模,在dos下显示汉字*/
/*横坐标,纵坐标,字间隔,汉字颜色,汉字字符串*/
{ FILE *fp;
register char buffer[24];
register char str[2];
unsigned long fpos;/*fpos为最终偏移动量*/
register int i,j,k;
fp=fopen(hzk12,r);/*打开12*12汉字苦*/
while(*s)/*一直到字符串结束为止*/
{
if(*s<0)/*汉字输出*/
{ str[0]=(*s)-0xa0;
str[1]=*(s+1)-0xa0;
fpos=((str[0]-1)*94+(str[1]-1))*24L;/*计算汉字在hzk12的偏移量*/
fseek(fp,fpos,SEEK_SET);/*指针移动到当前位置*/
fread(buffer,24,1,fp);/*读取一个汉字到数组中*/
for(i=0;i<12;i++)/*12行*/
for(j=0;j<2;j++)/*两个字节*/
for(k=0;k<8;k++)/*8位*/
if (((buffer[i*2+j]>>(7-k))&0x1)!=NULL)/*是一就画点*/
putpixel(x0+8*j+k,y0+i,color);
s+=2;/*一个汉字占两个字节,现在将指针移动两个字节*/
x0+=w;/*显示坐标也按照间隔移动*/
}
else/*显示非汉字字符*/
{ settextstyle(0,0,1);
setcolor(color);
str[0]=*s;str[1]=0;
outtextxy(x0,y0+3,str);/*显示单个字符*/
x0+=w-7;/*显示单个字符后的x坐标变化*/
s++;/*指针移动到下一个字节*/
}
}
fclose(fp);
}

void translation() /*把相对坐标解释为绝对坐标*/
{ if(RA.Rx==1)
{ RA.x1=1; RA.x2=16; }
else
{ RA.x1=16*(RA.Rx-1); RA.x2=16*RA.Rx; }
if(RA.Ry==1)
{ RA.y1=1; RA.y2=16; }
else
{ RA.y1=16*(RA.Ry-1); RA.y2=16*RA.Ry; }
}

int check_b() /*检查是否到达低部*/
{ int x,y,i,zf=0; /*zf为是否有颜色填充记录*/
for(i=0;i<7;i++,i++)
{ x=RA.Rx+p[rs1].co[i];
y=RA.Ry+p[rs1].co[i+1];
if(y>=6)
zf+=r_f[x-15][y-6+1];
}
if(zf==0)
return 1;
else
return 0;
}

int finish()
{ int tfull=0,i; /*判断顶层空间是否有填充*/
for(i=1;i<11;i++)
tfull+=r_f[i][1];
if(tfull!=0)
return 1; /*告诉judge()可以结束了*/
}

int check_l() /*检查形状是否与左接触*/
{ int x,y,i,zf=0;
for(i=0;i<7;i++,i++)
{ x=RA.Rx+p[rs1].co[i];
y=RA.Ry+p[rs1].co[i+1];
if(y>6)
zf+=r_f[x-15-1][y-6];
if(y<=6&&x==16)
zf+=1;
}
if(zf==0)
return 1;
else
return 0;
}

int check_r() /*检查形状是否与右接触*/
{ /*zf为是否有颜色填充记录*/
int x,y,i,zf=0; /*zf为是否有颜色填充记录*/
for(i=0;i<7;i++,i++)
{
x=RA.Rx+p[rs1].co[i];
y=RA.Ry+p[rs1].co[i+1];
if(y>6)
zf+=r_f[x-15+1][y-6];
if(y<=6&&x==25)
zf+=1;
}
if(zf==0)
return 1;
else
return 0;
}

void check_touch()
{ nback=check_b();
nleft=check_l();
nright=check_r();
}

void draw(int cb) /*画形状,cb=1以填充色画形状,cb=2以背景色画形状,cb=3以白色画形状*/
{ int i,recordx=RA.Rx,recordy=RA.Ry;
for(i=0;i<7;i++,i++)
{ RA.Rx+=p[rs1].co[i];
RA.Ry+=p[rs1].co[i+1];
if(RA.Ry<=6)
{ RA.Rx=recordx;
RA.Ry=recordy;
continue;
}
translation();
if(cb==1)
setfillstyle(1,p[rs1].attr);
else
if(cb==2)
setfillstyle(1,BLACK);
else
if(cb==3)
{ setfillstyle(1,WHITE);
r_f[RA.Rx-15][RA.Ry-6]=1; /*置对应数组标记元素*/
}

bar(RA.x1+1,RA.y1+1,RA.x2-1,RA.y2-1);
RA.Rx=recordx;
RA.Ry=recordy;
}
}

void mov(int key) /*向下,左,右移动方块*/
{ draw(2);
if(key==LEFT&&nleft)
RA.Rx--;
else
if(key==RIGHT&&nright)
RA.Rx++;
else
RA.Ry++;
nback=check_b();
if(nback) /*判断形状有没有到达底部,有就将其颜色变为白色*/
draw(1);
else
draw(3);
}

void change() /*变换形状*/
{ int status=rs1,buffer,i,x,y,zf=0;
if(p[rs1].attr==p[rs1+1].attr)
rs1++;
else
while(p[rs1].attr==p[rs1-1].attr)
rs1--;

for(i=0;i<7;i++,i++) /*检查变化形状后是否与已存形状发生冲突*/
{ x=RA.Rx+p[rs1].co[i];
y=RA.Ry+p[rs1].co[i+1];
if(y>6)
zf+=r_f[x-15][y-6];
}
if(zf!=0)
rs1=status;

buffer=rs1;
rs1=status;
status=buffer;
draw(2);

buffer=rs1;
rs1=status;
status=buffer;

nback=check_b(); /*判断变化后的形状是不是到达了低部,这个检查是十分必要的*/
if(nback)
draw(1);
else
draw(3);
}

void accelerate()
{ if(count_down>=1)
{ check_touch(); /*消除上一步动作对方块状态的影响*/
count_down=0;
if(nback) /*0表示到达底部,1表示没有到达*/
mov(DOWN);
}
}

void drawbox() /*画方块所在方框*/
{ int xcor,ycor;
for(xcor=xcors;xcor<=xcorb;xcor++)
for(ycor=ycors;ycor<=ycorb;ycor++)
{ if(xcor==xcors||xcor==xcorb||ycor==ycors||ycor==ycorb)
{ RA.Rx=xcor;
RA.Ry=ycor;
translation();
setfillstyle(1,DARKGRAY);
bar(RA.x1+1,RA.y1+1,RA.x2-1,RA.y2-1);
}
}
}

void erasure(int k)
{ int i,j,recordx=RA.Rx,recordy=RA.Ry;
{ j=k-1;
for(;j>0;j--)
{ for(i=1;i<11;i++)
{ r_f[i][j+1]=r_f[i][j];
RA.Rx=i+15;
RA.Ry=j+1+6;
translation();
if(r_f[i][j+1]==1)
setfillstyle(1,WHITE);
else
setfillstyle(1,BLACK);
bar(RA.x1+1,RA.y1+1,RA.x2-1,RA.y2-1);
RA.Rx=recordx;
RA.Ry=recordy;
}
}
}
}

void pause()
{ HZ12(450,400,15,BLACK,正常);
HZ12(450,400,15,GREEN,暂停);
for(;;)
if(bioskey(1)&&bioskey(0)==SPACE)
{ clearkbd();
HZ12(450,400,15,BLACK,暂停);
HZ12(450,400,15,RED,正常);
return;
}
}

void judge()
{ int i,j,full=0; /*full等于10说明某一行满,该消除了*/
if(finish()) /*判断游戏是否该结束了*/
messagebox(); /*win编程里有这个函数*/
for(j=1;j<21;j++) /*判断某一行是否满了*/
{ for(i=1;i<11;i++)
full+=r_f[i][j];
if(full==10)
erasure(j); /*消除这行*/
full=0;
}
}

void update() /*使程序可以重新运行*/
{ cleardevice();
setbkcolor(BLACK);
initinfo(); /*提示信息初始化*/
initbox(); /*游戏框架初始化*/
srand((unsigned)time(NULL)); /*随机器函数的初始化*/
rs1=random(19);
rs2=random(19);
next_shape();
initposition(); /*方块最开始的出现位置*/
initremove(); /*记录每个方格有无颜色填充数组初始化*/
HZ12(450,400,15,RED,正常);
process();
}

void EXIT()
{ closegraph();
intrestore(); /*恢复中断向量*/
exit(0);
}

void initremove()
{ int i,j;
for(i=0;i<12;i++)
for(j=0;j<22;j++)
if(i==0||i==11||j==0||j==21)
r_f[i][j]=1;
else
r_f[i][j]=0;
}

void initinfo()
{ char aStr[2];
setcolor(RED);
outtextxy(450,100,This game's writer is:);
HZ12(450,140,15,RED,该程序作者:NULL);
outtextxy(525,110,NULL);
outtextxy(450,180,FUNCTION FOR KEYS:);
outtextxy(450,200,UP:change the shape);
outtextxy(450,210,DOWN:accelerate);
outtextxy(450,220,LEFT:move left);
outtextxy(450,230,RIGHT:move right);
outtextxy(450,240,ESC:exit this game);
outtextxy(450,250,SPACE:pause);
HZ12(450,260,20,RED,上:);
HZ12(450,280,20,RED,下:);
HZ12(450,300,20,RED,左:);
HZ12(450,320,20,RED,右:);
HZ12(450,340,20,RED,ESC:退出);
HZ12(450,360,15,RED,空格: 暂停/开始);
HZ12(450,380,15,RED,目前状态:);
HZ12(20,200,15,RED,下一个形状);

aStr[0]=24;
aStr[1]=0;
aStr[6]=0;
HZ12(480,260,12,GREEN,aStr);
HZ12(500,260,12,GREEN,( 变形 ));
aStr[0]=25;
aStr[1]=0;
HZ12(480,280,12,GREEN,aStr);
HZ12(500,280,12,GREEN,( 加速 ));
aStr[0]=27;
aStr[1]=0;
HZ12(480,300,12,GREEN,aStr);
HZ12(500,300,12,GREEN,向左);
aStr[0]=26;
aStr[1]=0;
HZ12(480,320,12,GREEN,aStr);
HZ12(500,320,12,GREEN,向右);
}

void messagebox()
{ int key;
setcolor(GREEN);
setfillstyle(1,DARKGRAY);
rectangle(220,200,420,300);
bar(221,201,419,299);
HZ12(280,210,15,GREEN,GAME OVER);
HZ12(275,230,15,GREEN,重新游戏: Y);
HZ12(275,270,15,GREEN,退出游戏: N);
HZ12(450,400,15,BLACK,正常);
HZ12(450,400,15,GREEN,GAME OVER);
for(;;)
if(bioskey(1))
{ key=bioskey(0);
if(key==Y)
{ clearkbd();
update();
}
else
if(key==N)
{ clearkbd();
EXIT();
}
else
clearkbd();
}
}

void initbox()
{ xcors=15; /*画游戏框*/
xcorb=26;
ycors=6;
ycorb=27;
drawbox();

xcors=2; /*画提示框*/
xcorb=7;
ycors=6;
ycorb=11;
drawbox();
}

void initposition()
{ RA.Rx=18;
RA.Ry=6-p[rs1].co[7];;
RA.x1=0;
RA.x2=0;
RA.y1=0;
RA.y2=0;
}

void next_shape() /*画下一形状提示框*/
{ int recordx=RA.Rx,recordy=RA.Ry,buffer;
RA.Rx=3;
RA.Ry=7;
draw(2);

buffer=rs1;
rs1=rs2;
rs2=buffer;

draw(1);
RA.Rx=recordx;
RA.Ry=recordy;

buffer=rs1;
rs1=rs2;
rs2=buffer;
}

void process() /*游戏过程*/
{ for(;;)
{ check_touch();
if(!nback)
{ rs1=rs2;
rs2=random(19); /*产生另一种方块的码数*/
initposition();
judge(); /*判断某一行是否满了和这个游戏是否可以结束了*/
draw(1);
next_shape();
}

if(count_other>=1)
{ count_other=0;
if(bioskey(1)) /*对按键的处理*/
{ int key=bioskey(0);
clearkbd(); /*清除键盘缓冲队列*/
if(key==ESC)
EXIT();
if(key==LEFT&&nleft&&nback)
mov(LEFT);
if(key==RIGHT&&nright&&nback)
mov(RIGHT);
if(key==UP&&nback)
change();
if(key==SPACE)
pause();
if(key==DOWN)
accelerate();
}
}

if(count_down>=4)
{ check_touch(); /*消除上一步动作对方块状态的影响*/
count_down=0;
if(nback) /*0表示到达底部,1表示没有到达*/
mov(DOWN);
}
}/*for*/
}

main()
{ int gdriver=DETECT,gmode=0;
initgraph(&gdriver,&gmode,d:turboc); /*启动图形与中断部分*/
intenable();
update();
}

⑼ C语言 游戏 代码

“坑人的无限”(一):

#include<iostream>
#include<windows.h>
#include<ctime>
#include<cstdlib>
#include<conio.h>
using namespace std;
int a;
class Screen
{
private:
int n;
public:
Screen()
{
n=5;
}
void move1()//注意只是循环输出各个数字,不能对循环输出再进行循环(如果对循环输出0123456789再进行循环,则move1就变成一个无限循环的函数,则下面的screen循环就进行不下去了)
{
for(int i=0;i<10;++i)
{
cout<<char(1)<<" ";
}
}
void move2()
{
char i;
for(i='a';i<='z';++i)
{
cout<<char(1)<<" ";
}
}
void screen()
{
int t;
while(!kbhit())
{
t=time(0)%(2*n);//如果是放在循环外面的话,time(0)的值就一直不变,放在循环里面,一秒钟进行一次判断,一秒钟进行一次循环
if(t<n)
move1();
else
move2();
}
}
};
int main(){
cout<<"欢迎来到“无限 ”游戏"<<char(1)<<endl;
cout<<"下面会输出无限个笑脸"<<char(1)<<endl;
cout<<"按'enter'取消"<<endl;
Sleep(4000);
Screen s;
s.screen();
cout<<endl<<"哈哈!!控制不住了吧!"<<char(1)<<endl;
cout<<"接下来会更让你丧心病狂的!"<<char(1)<<endl;
cout<<"但是坚持过后必有彩蛋!!!!!!加油!!";
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
Sleep(10000);
for(int as=0;as<=50;as++){
for(int i=0;i<=100;i++){
for(int j=0;j<=10;j++){
cout<<char(2)<<" ";
}
cout<<endl;
}
for(int i=0;i<=100;i++){
for(int j=0;j<=10;j++){
cout<<char(1)<<" ";
}
cout<<endl;
}
}
cout<<"你居然坚持下来了!"<<char(1)<<" "<<char(2)<<endl<<"不可思议呀!"<<endl;
for(int i=0;i<=10;i++){
cout<<"-----------------------------------------------"<<endl;
}
cout<<"敬请期待!等待无限游戏(二)!";
return 0;
}

⑽ c语言能编写手机游戏吗

C语言可以编手机游戏. 你叫他去死 不过我这有 贪吃蛇的代码,你倒可以看看 (用TC 编译一定过)

#include <stdlib.h>
#include <graphics.h>
#include <bios.h>
#include <dos.h>
#include <conio.h>
#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].x<20||Snk[0].x>460||Snk[0].y<20||Snk[0].y>460)
{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);
}