C语言设计一个简单的猜数游戏:先由计算机“想”一个数请用户猜,如果用户猜对了,则计算机给出提示信息

C语言设计一个简单的猜数游戏:先由计算机“想”一个数请用户猜,如果用户猜对了,则计算机给出提示信息,第1张

//希望我的回答对你的学习有帮助

#include <stdioh>

#include <stdlibh>

#include <timeh>

int main()

{

int x, magic, a = 0, c = 1;

char b;

do

{

srand(unsigned(time(NULL))); //为函数rand()设置随机数种子

magic = rand() % 100 + 1; //magic为1~100间的一个数

printf("您每次游戏中,只有十次机会\n");

while (a <= 10)

{

scanf("%d", &x);

if (x>magic)

printf("Wrong,比magic大\n");

else if (x<magic)

printf("Wrong,比magic小\n");

//else if (x = magic) //这里的逻辑运算应该使用“==”

else if (x == magic) //你应该是写错了

{

printf("Right\n");

a = a + 1;

printf("你猜了%d次了\n", a);

a = 0; //猜对之后,应该对猜测的次数初始化

break;

}

a++;

printf("你猜了%d次了\n", a);

if (a == 10)

{

a = 0;

printf("Do you want to continue(回答Y/N或者y/n)\n");

//scanf_s("%c", &b); //这么使用,会将你在输入字符之前的

// 空行(enter键)赋值给 b,而不是你输入的Y/N/y/n

scanf(" %c", &b); //而这种写法,%c 前面的 空格会使 scanf 忽略前面的 enter键

//等待第一个非 enter键 元素读入

if (b == 'y' || b == 'Y')

c = 1;

else if (b == 'n' || b == 'N')

c = 0;

break; //当猜测次数为 10 时,应当跳出

//这里不会自动跳出,因为 a 被初始化为 0

}

}

} while (c == 1);

return 0;

}

scanf 里面没有空格的情况,程序会自动执行 10 次

修改后的程序,猜对的情况

修改后的程序,猜错 10 次,Y的情况

修改后的程序,猜错 10 次,N的情况

#include<graphicsh>

#include<stdlibh>

#define N 200

#define up 0x4800

#define down 0x5000

#define left 0x4b00

#define right 0x4d00

#define esc 0x011b

#define Y 0x1579

#define n 0x316e

int gamespeed; / 游戏速度 /

int i,key,color;

int score=0; / 游戏分数 /

char cai48H[]={

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x04,0x00,0x18,0x00,0x00,0x00,0x0E,0x00,

0x1C,0x00,0x00,0x00,0x1C,0x00,0x1C,0x00,

0x00,0x00,0x20,0x00,0x38,0x00,0x00,0x00,

0x40,0x00,0x78,0x00,0x00,0x01,0x80,0x40,

0x70,0x00,0x00,0x03,0x80,0xC0,0xE0,0x00,

0x00,0x07,0x80,0x80,0xC0,0x00,0x00,0x0E,

0x11,0x81,0xC0,0x00,0x00,0x08,0x61,0x01,

0x80,0x00,0x00,0x00,0x23,0x03,0x04,0x00,

0x00,0x02,0x02,0x00,0x06,0x00,0x00,0x1E,

0x04,0x00,0x0F,0x00,0x00,0x1C,0x1F,0x80,

0x1E,0x00,0x00,0x08,0x3F,0x80,0x3C,0x00,

0x00,0x00,0xFF,0x80,0x38,0x00,0x00,0x03,

0xFF,0x80,0x78,0x00,0x00,0x0F,0xF8,0x00,

0xF0,0x00,0x00,0x7F,0xF0,0x00,0xE0,0x00,

0x03,0xFF,0xFC,0x01,0x80,0x00,0x03,0xC0,

0xFF,0x01,0x03,0x80,0x01,0x01,0xFF,0x00,

0x03,0x80,0x00,0x01,0x3F,0x00,0x07,0x80,

0x00,0x02,0x11,0x00,0x07,0x00,0x00,0x00,

0x10,0x00,0x07,0x00,0x00,0x00,0x10,0x00,

0x0E,0x00,0x00,0x08,0x10,0x00,0x1C,0x00,

0x00,0x30,0x10,0x00,0x18,0x00,0x00,0x70,

0x10,0x00,0x30,0x00,0x01,0xE0,0x10,0x00,

0x70,0x00,0x03,0x80,0x10,0x00,0x60,0x00,

0x00,0x00,0x30,0x00,0xE0,0x00,0x00,0x00,

0xF0,0x01,0xC0,0x00,0x00,0x00,0x70,0x03,

0xC0,0x00,0x00,0x00,0x10,0x07,0x80,0x00,

0x00,0x00,0x00,0x0F,0x00,0x00,0x00,0x00,

0x00,0x1E,0x00,0x00,0x00,0x00,0x00,0x3C,

0x00,0x00,0x00,0x00,0x00,0x70,0x00,0x00,

0x00,0x00,0x01,0xC0,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

};

char she48H[]={

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,

0x00,0x00,0x00,0x00,0x00,0x0C,0x00,0x00,

0x00,0x00,0x00,0x0E,0x00,0x00,0x00,0x00,

0x00,0x0E,0x00,0x00,0x00,0x03,0x00,0x07,

0x00,0x00,0x00,0x02,0x00,0x03,0x00,0x00,

0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x02,

0x00,0x00,0xF8,0x00,0x00,0x02,0x00,0x07,

0x86,0x00,0x00,0x02,0x00,0x18,0x03,0x00,

0x00,0x02,0x00,0x00,0x07,0x80,0x00,0x03,

0xF0,0x00,0x07,0x80,0x00,0x0F,0xFC,0x00,

0x0C,0x00,0x00,0x7E,0x3F,0x80,0x00,0x00,

0x01,0xFE,0x1F,0x80,0x00,0x00,0x01,0xE2,

0x39,0x8C,0x00,0x00,0x00,0xC2,0x30,0x08,

0x00,0x00,0x00,0xC2,0x60,0x08,0x00,0x00,

0x00,0xC3,0xE0,0x08,0x60,0x00,0x00,0x7F,

0xE0,0x01,0xE0,0x00,0x00,0x3F,0x80,0x1F,

0xE0,0x00,0x00,0x1E,0x00,0x1F,0x80,0x00,

0x00,0x1E,0x00,0x1F,0x00,0x00,0x00,0x02,

0x38,0x1E,0x00,0x00,0x00,0x07,0xFC,0x1C,

0x00,0x20,0x00,0x07,0xFC,0x18,0x00,0x20,

0x00,0x1F,0x0C,0x10,0x00,0x20,0x00,0x7C,

0x04,0x10,0x00,0x60,0x01,0xF0,0x00,0x10,

0x00,0x60,0x01,0xE0,0x00,0x08,0x00,0xF0,

0x00,0x80,0x00,0x08,0x03,0xF0,0x00,0x00,

0x00,0x07,0xFF,0xF0,0x00,0x00,0x00,0x07,

0xFF,0xF0,0x00,0x00,0x00,0x03,0xFF,0xE0,

0x00,0x00,0x00,0x01,0xFF,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

};

char tun48H[]={

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x0E,0x00,0x00,0x00,0x00,0x00,0x3E,

0x00,0x00,0x00,0x00,0x00,0x7F,0x00,0x00,

0x00,0x00,0x00,0xE0,0x00,0x00,0x00,0x00,

0x03,0xC0,0x00,0x00,0x00,0x00,0x1F,0x00,

0x00,0x00,0x00,0x00,0x7C,0x00,0x00,0x00,

0x00,0x01,0xF8,0x00,0x00,0x00,0x00,0x03,

0xF8,0x00,0x40,0x00,0x00,0x00,0x06,0x07,

0xC0,0x00,0x00,0x00,0x07,0xFF,0xE0,0x00,

0x00,0x00,0x07,0xFF,0xE0,0x00,0x00,0x00,

0x0F,0xFF,0x80,0x00,0x00,0x00,0x7F,0xF8,

0x00,0x00,0x00,0x1F,0xFF,0xF8,0x00,0x00,

0x00,0x1F,0xFF,0xF8,0x00,0x00,0x00,0x1F,

0xFC,0x3C,0x00,0x00,0x00,0x0F,0xF8,0x0E,

0x00,0x00,0x00,0x04,0x70,0x07,0x00,0x00,

0x00,0x00,0x60,0x03,0x80,0x00,0x00,0x00,

0xC0,0x00,0xC0,0x00,0x00,0x01,0x80,0x00,

0x30,0x00,0x00,0x01,0x00,0x3C,0x18,0x00,

0x00,0x02,0x03,0xFF,0x0C,0x00,0x00,0x0C,

0x7F,0xFF,0x8E,0x00,0x00,0x18,0xFF,0xFF,

0xC7,0x80,0x00,0x78,0xFE,0x07,0x87,0xE0,

0x01,0xF0,0x70,0x07,0x03,0xF8,0x07,0xE0,

0x70,0x0E,0x03,0xFE,0x00,0x00,0x38,0x1E,

0x01,0xFE,0x00,0x00,0x3F,0xFE,0x00,0x0C,

0x00,0x00,0x1F,0xFE,0x00,0x00,0x00,0x00,

0x1F,0xFE,0x00,0x00,0x00,0x00,0x0F,0xFE,

0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

};

char dan48H[]={

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0xFC,0x00,0x00,0x00,0x00,0x07,0xFF,

0x00,0x00,0x00,0x00,0x7F,0xC0,0x80,0x00,

0x00,0x03,0xFF,0x80,0x40,0x00,0x00,0x01,

0xF1,0x80,0x40,0x00,0x00,0x01,0x81,0x80,

0xE0,0x00,0x00,0x00,0x01,0x93,0xF0,0x00,

0x00,0x00,0x01,0xFF,0xF0,0x00,0x00,0x00,

0x21,0xFF,0xF0,0x00,0x00,0x00,0x21,0xF8,

0x00,0x00,0x00,0x00,0x61,0xC0,0x00,0x00,

0x00,0x00,0x61,0x80,0x00,0x00,0x00,0x00,

0xF3,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,

0x00,0x00,0x00,0x01,0xFF,0xC0,0x00,0x00,

0x00,0x03,0xFF,0xF8,0x00,0x00,0x00,0x02,

0x00,0xFC,0x00,0x00,0x00,0x04,0x02,0x1F,

0x00,0x00,0x00,0x08,0x03,0x01,0xC0,0x00,

0x00,0x38,0x03,0x00,0x7C,0x00,0x00,0xF8,

0x07,0xF8,0x3F,0xC0,0x01,0xF0,0x3F,0xFE,

0x3F,0xF8,0x03,0xC1,0xFF,0x0F,0x1F,0xF8,

0x00,0x01,0xE3,0x0F,0x0F,0xF0,0x00,0x01,

0xC3,0x0E,0x00,0x00,0x00,0x01,0x83,0xFC,

0x00,0x00,0x00,0x00,0xC7,0xF8,0x00,0x00,

0x00,0x00,0xFF,0xF8,0x00,0x00,0x00,0x00,

0x7F,0xF0,0x00,0x00,0x00,0x00,0x3F,0x03,

0x80,0x00,0x00,0x00,0x03,0x04,0x00,0x00,

0x00,0x00,0x03,0xF8,0x00,0x00,0x00,0x00,

0x1F,0xF8,0x20,0x00,0x00,0x00,0xFF,0xFF,

0xE0,0x00,0x00,0x07,0xFF,0x81,0xE0,0x00,

0x00,0x07,0xE0,0x00,0xE0,0x00,0x00,0x03,

0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

};

char zuo16H[]={

0x18,0xC0,0x18,0xC0,0x19,0x80,0x31,0xFE,

0x33,0xFE,0x76,0xC0,0xF0,0xFC,0xB0,0xFC,

0x30,0xC0,0x30,0xC0,0x30,0xFE,0x30,0xFE,

0x30,0xC0,0x30,0xC0,0x30,0xC0,0x00,0x00,

};

char zhe16H[]={

0x03,0x00,0x03,0x0C,0x1F,0xCC,0x1F,0xD8,

0x03,0x30,0xFF,0xFE,0xFF,0xFE,0x03,0x00,

0x0F,0xF8,0x3F,0xF8,0xEC,0x18,0xCF,0xF8,

0x0C,0x18,0x0F,0xF8,0x0F,0xF8,0x00,0x00,

};

char tian16H[]={

0x00,0x00,0x3F,0xFC,0x3F,0xFC,0x31,0x8C,

0x31,0x8C,0x31,0x8C,0x3F,0xFC,0x3F,0xFC,

0x31,0x8C,0x31,0x8C,0x31,0x8C,0x3F,0xFC,

0x3F,0xFC,0x30,0x0C,0x00,0x00,0x00,0x00,

};

char xue16H[]={

0x33,0x18,0x19,0x98,0x08,0xB0,0x7F,0xFC,

0x7F,0xFC,0x60,0x0C,0x1F,0xF0,0x1F,0xF0,

0x00,0xC0,0x7F,0xFC,0x7F,0xFC,0x01,0x80,

0x01,0x80,0x07,0x80,0x03,0x00,0x00,0x00,

};

char ke16H[]={

0x00,0x00,0x0C,0x18,0xFD,0x98,0xF8,0xD8,

0x18,0x58,0xFE,0x18,0xFE,0x98,0x18,0xD8,

0x3C,0x58,0x7E,0x1E,0xDB,0xFE,0x9B,0xF8,

0x18,0x18,0x18,0x18,0x18,0x18,0x00,0x00,

};

struct Food/定义结构体存储食物的属性/

{

int x; / 食物的坐标 /

int y;

int yes; / 值为0表示屏幕上没有食物,值为1表示屏幕上有食物 /

int color; / 食物颜色 /

}food;

struct Snake/定义结构体存储蛇的属性/

{

int x[N]; / 每一节蛇的坐标 /

int y[N];

int color[N];/存储每一节蛇的颜色/

int node; / 蛇的节数 /

int direction; / 蛇移动的方向 /

int life; / 蛇的生命,如果为1,蛇死,游戏结束 /

}snake;

void init(void)/图形驱动/

{

int driver=DETECT,mode=0;

registerbgidriver(EGAVGA_driver);

initgraph(&driver,&mode,"");

}

void drawmat(char mat,int matsize,int x,int y,int color)/汉字点阵/

{

int i,j,k,m;

m=(matsize-1)/8+1;

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

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

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

if(mat[jm+i]&(0x80>>k))

putpixel(x+i8+k,y+j,color);

}

void showword(void)

{/ 调用汉字点阵输出程序,显示标题和作者信息 /

drawmat(cai48H,48,249,-4,7);

drawmat(she48H,48,329,-4,7);

drawmat(tun48H,48,409,-4,7);

drawmat(dan48H,48,489,-4,7);

drawmat(cai48H,48,250,-5,4);

drawmat(she48H,48,330,-5,4);

drawmat(tun48H,48,410,-5,4);

drawmat(dan48H,48,490,-5,4);

/作者 田学科/

drawmat(zuo16H,16,515,465,7);

drawmat(zhe16H,16,530,465,7);

drawmat(tian16H,16,550,465,7);

drawmat(xue16H,16,565,465,7);

drawmat(ke16H,16,580,465,7);

}

void draw(void)/画出四周的墙/

{

if(color==15)

color=0;

setcolor(++color);

setlinestyle(SOLID_LINE,0,1);

for(i=30;i<=600;i+=10)

{

rectangle(i,40,i+10,49);

rectangle(i,451,i+10,460);

}

for(i=40;i<450;i+=10)

{

rectangle(30,i,39,i+10);

rectangle(601,i,610,i+10);

}

}

void prscore(void)

{/ 打印游戏分数 /

char str[10];

setfillstyle(SOLID_FILL,YELLOW);

bar(50,10,200,30);

setcolor(6);

settextstyle(0,0,2);

sprintf(str,"score:%d",score);

outtextxy(55,15,str);

}

void gameover(void)

{

cleardevice(); / 清屏函数 /

for(i=0;i<snakenode;i++)/ 画出蛇死时的位置 /

{

setcolor(snakecolor[i]);

rectangle(snakex[i],snakey[i],snakex[i]+10,snakey[i]+10);

}

prscore(); / 显示分数 /

draw();

showword();

settextstyle(0,0,6);

setcolor(7);

outtextxy(103,203,"GAME OVER");

setcolor(RED);

outtextxy(100,200,"GAME OVER");

}

void gameplay(void)/ 玩游戏的具体过程 /

{

int flag,flag1;

randomize();

prscore();

gamespeed=50000;

foodyes=0;/ foodyes=0表示屏幕上没有食物 /

snakelife=1;/ snakelife=1表示蛇是活着的 /

snakedirection=4;/ 表示蛇的初始方向为向右 /

snakenode=2; / 蛇的初始化为两节 /

snakecolor[0]=2; /两节蛇头初始化为绿色/

snakecolor[1]=2;

snakex[0]=100;snakey[0]=100;

snakex[1]=110;snakey[1]=100;

foodcolor=random(15)+1;

while(1)

{

while(1)

{

if(foodyes==0) / 如果蛇活着 /

{

while(1)

{

flag=1;

foodyes=1;

foodx=random(56)10+40;

foody=random(40)10+50;

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

{

if(foodx==snakex[i]&&foody==snakey[i])

flag=0;

}

if(flag) break;

}

}

if(foodyes)

{

setcolor(foodcolor);

rectangle(foodx,foody,foodx+10,foody+10);

}

for(i=snakenode-1;i>0;i--)

{

snakex[i]=snakex[i-1];

snakey[i]=snakey[i-1];

}

switch(snakedirection)

{

case 1: snakey[0]-=10;break;

case 2: snakey[0]+=10;break;

case 3: snakex[0]-=10;break;

case 4: snakex[0]+=10;break;

}

for(i=3;i<snakenode;i++)

{

if(snakex[i]==snakex[0]&&snakey[i]==snakey[0])

{

gameover();

snakelife=0;

break;

}

}

if(snakex[0]<40||snakex[0]>590||snakey[0]<50||snakey[0]>440)

{

gameover();

snakelife=0;

}

if(snakelife==0)

break;

if(snakex[0]==foodx&&snakey[0]==foody)/蛇吃掉食物/

{

setcolor(0);

rectangle(foodx,foody,foodx+10,foody+10);

snakex[snakenode]=-20;

snakey[snakenode]=-20;

snakecolor[snakenode]=foodcolor;

snakenode++;

foodyes=0;

foodcolor=random(15)+1;

score+=10;

prscore();

if(score%100==0&&score!=0)

{

for(i=0;i<snakenode;i++)/ 画出蛇 /

{

setcolor(snakecolor[i]);

rectangle(snakex[i],snakey[i],snakex[i]+10,snakey[i]+10);

}

sound(200);

delay(50000);delay(50000);delay(50000);

delay(50000);delay(50000);delay(50000);

nosound();

gamespeed-=5000;

draw();

}

else

{

sound(500);

delay(500);

nosound();

}

}

for(i=0;i<snakenode;i++)/ 画出蛇 /

{

setcolor(snakecolor[i]);

rectangle(snakex[i],snakey[i],snakex[i]+10,snakey[i]+10);

}

delay(gamespeed);

delay(gamespeed);

flag1=1;

setcolor(0);

rectangle(snakex[snakenode-1],snakey[snakenode-1],

snakex[snakenode-1]+10,snakey[snakenode-1]+10);

if(kbhit()&&flag1==1)/如果没按有效键就重新开始循环/

{

flag1=0;

key=bioskey(0);

if(key==esc)

exit(0);

else if(key==up&&snakedirection!=2)

snakedirection=1;

else if(key==down&&snakedirection!=1)

snakedirection=2;

else if(key==left&&snakedirection!=4)

snakedirection=3;

else if(key==right&&snakedirection!=3)

snakedirection=4;

}

}

if(snakelife==0)/如果蛇死了就退出循环/

break;

}

}

void main(void)

{

while(1)

{

color=0;

init();

cleardevice();

showword();

draw();

gameplay();

setcolor(15);

settextstyle(0,0,2);

outtextxy(200,400,"CONTINUE(Y/N)");

while(1)

{

key=bioskey(0);

if(key==Y||key==n||key==esc)

break;

}

if(key==n||key==esc)

break;

}

closegraph();

}

本原代码是基于C语言的原程序。是经典中的小游戏。-primitive code is based on the C language of the original procedure Classic is a small game

一个小游戏,用C语言编写的:俄罗斯方块.C原码及应用程序都在里面哦 -a small game using the C language : Russian cubes The original C code and application procedures inside oh

十全十美游戏原程序,c语言-perfect game program, language c

上数据结构时,自己用C语言做的小游戏,做得不好,请大家原谅。-structure on the data they used C language to the small games, done well, please forgive me

大家都耍过文曲星中的猜数字的游戏吧 !! 最近我在学习C语言。写了个菜鸟的C语言的猜数字的游戏程序的原代码-rings off the viewing of the game! ! I recently learning C language Wrote a birdie C language viewing of the games original code procedures

  这是我在大学二年级学习C语言课程时,作为“练笔”而编写的一个小程序(当时在我眼里可却是一个大程序!)其主要特点有:1、正真做到了全中文界面(不需要UCDOS *** 作系支持) 2、大量的图形特技(如图像的显隐技术、图像穿插技术、多任务仿真技术等) 3、纯C语言打造(不含任何C++知识) 4、实现了街机“俄罗斯方块”绝大部分功能(如动画、声音、速度变化) 5、用户可根据自据的习惯自由地调整“游戏 *** 作键” 6、方法独特,全部语句和技术都是我本人原创,没有参考过任何相关代码 7、防“跟踪调试”技术,防“版权篡改”技术 8、……-

  这个程序是模仿Windows中的扫雷小游戏制作的,该程序只是实现了扫雷游戏的主体部分,诸如计分、升级部分都没有做。这个程序可以作为初学者学习C语言绘图和游戏的实例。 该程序在Turbo C20 下编译通过 由于扫雷游戏是用鼠标 *** 作的,而Turbo C中提供的鼠标驱动程序在Windows xp下不可用,所以我随源程序提供了一个鼠标驱动的头文件,须将将该头文件复制到Turbo C20 的安装目录下的“include”文件夹中方可编译或运行,也可自行修改原文件使之包含该投文件。 注:该鼠标驱动程序是我在网上找到的,其出处我已无法考证,如果侵犯了作者的权利还请作者与我联系。 由于在我的电脑上Turbo C图形环境下显示数字会有问题(估计是系统问题),所以程序中雷周围的数字1-8我用a-h代替,看不顺眼的可以自己修改原程序。-

c语言 猜拳游戏的原代码就是这个 已经测试成功了呀-language of the original game is the code has been tested successfully ah

俄罗斯方块对战版c语言原代码。希望大家能喜欢。是比较简单的一个代码,游戏开发高手请指教。-Tetris screen version of the original C language code Hope you will like It is a relatively simple code, game development experts please advise

用linuX 下的C语言 运用CURSES编写的俄罗斯方块游戏,很好,这个是本人原创,值得参考-linuX use the C language CURSES prepared by the Russian box game, well, this is the original, worthy of reference

基本思路:

蛇每吃一个食物蛇身子就增加一格,用UP, DOWN, LEFT, RIGHT控制蛇头的运动,而蛇身子跟着蛇头走,每后一格蛇身子下一步走到上一格蛇身子的位置,以此类推。

#include <stdioh>

#include <conioh>

#include <windowsh>

#define BEG_X 2

#define BEG_Y 1

#define WID 20

#define HEI 20

HANDLE hout;

typedef enum {UP, DOWN, LEFT, RIGHT} DIR;

typedef struct Snake_body

{

COORD pos;//蛇身的位置

struct Snake_body next;//下一个蛇身

struct Snake_body prev;//前一个蛇身

}SNAKE, PSNAKE;

PSNAKE head = NULL;//蛇头

PSNAKE tail = NULL;//蛇尾

//画游戏边框的函数

void DrawBorder()

{

int i, j;

COORD pos = {BEG_X, BEG_Y};

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

{

SetConsoleCursorPosition(hout, pos);

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

{

if(i == 0)//第一行

{

if(j == 0)

printf("┏");

else if(j == WID - 1)

printf("┓");

else

printf("━");

}

else if(i == HEI - 1)//最后一行

{

if(j == 0)

printf("┗");

else if(j == WID - 1)

printf("┛");

else

printf("━");

}

else if(j == 0 || j == WID - 1)//第一列或最后一列

printf("┃");

else

printf("  ");

}

++posY;

}

}

//添加蛇身的函数

void AddBody(COORD pos)

{

PSNAKE pnew = (PSNAKE)calloc(1, sizeof(SNAKE));

pnew->pos = pos;

if(!head)

{

head = tail = pnew;

}

else

{

pnew->next = head;//新创建蛇身的next指向原先的蛇头

head->prev = pnew;//原先的蛇头的prev指向新创建的蛇身

head = pnew;//把新创建的蛇身作为新的蛇头

}

SetConsoleCursorPosition(hout, head->pos);

printf("◎");

}

//蛇身移动的函数

void MoveBody(DIR dir)

{

PSNAKE ptmp;

COORD pos = head->pos;

switch(dir)

{

case UP:

if(head->posY > BEG_Y + 1)

--posY;

else

return;

break;

case DOWN:

if(head->posY < BEG_Y + HEI - 2)

++posY;

else

return;

break;

case LEFT:

if(head->posX > BEG_X + 2)

posX -= 2;

else

return;

break;

case RIGHT:

if(head->posX < BEG_X + (WID - 2) 2)

posX += 2;

else

return;

break;

}

AddBody(pos);//添加了一个新的蛇头

ptmp = tail;//保存当前的蛇尾

tail = tail->prev;

if(tail)

tail->next = NULL;

SetConsoleCursorPosition(hout, ptmp->pos);

printf("  ");

free(ptmp);

}

int main()

{

int ctrl;

DIR dir = RIGHT;//初始蛇的方向是向右的

COORD pos = {BEG_X + 2, BEG_Y + HEI / 2};

system("color 0E");

system("mode con cols=90 lines=30");

hout = GetStdHandle(STD_OUTPUT_HANDLE);

printf("    ------------贪吃蛇的移动------------");

DrawBorder();

//自定义几个蛇的身体

AddBody(pos);

posX += 2;

AddBody(pos);

posX += 2;

AddBody(pos);

posX += 2;

AddBody(pos);

posX += 2;

AddBody(pos);

posX += 2;

AddBody(pos);

posX += 2;

AddBody(pos);

//控制蛇的移动

while(ctrl = getch())

{

switch(ctrl)

{

case 'w':

if(dir == DOWN)

continue;

dir = UP;

break;

case 's':

if(dir == UP)

continue;

dir = DOWN;

break;

case 'a':

if(dir == RIGHT)

continue;

dir = LEFT;

break;

case 'd':

if(dir == LEFT)

continue;

dir = RIGHT;

break;

case 'q':

return 0;

}

MoveBody(dir);

}

return 0;

}

扩展资料:

实现逻辑

1,可以设置光标,就能实现制定位置打印制定符号。

2,涉及一个结构体,包含两个元素坐标元素和一个结构体指针。

3,结构体串联形成链表,遍历获取成员坐标,打印符号得到蛇身。

4,不断的加头,去尾,重新遍历坐标,再打印形成蛇的移动。

5,食物产生的位置判定,不能越界,也不能与蛇身体重合。

6,蛇的转向判定,一条规则,不允许倒退。

7,转向的实现,跟行进方向决定新的关节坐标(当前头的上下左右)

8,死亡检测,是否头节点坐标是否与墙壁重合,是否与身体其他关节重合。

9,加速减速,设置刷新休眠时间实现。

参考资料来源:百度百科-C语言

1、考虑怎么存储俄罗斯方块

俄罗斯方块的形状一共有19种类型,如果拿数组来表示的话,可能会比较会浪费空间(网上有很多实现代码)

考虑到每种方块形状的范围是4 4的小方块,用 字模点阵的方式来存储,即设置一个4行4列的数组,元素置1即代表这个位置有小 

方块,元素置0即代表这个位置无小方块,这个整个的44的数组组成俄罗斯方块的形状。 

1000 

1000 

1100 

0000 

上述44来表示L形状的方块。 

44 =16 bit 正好为short类型,所以每一个方块可以用一个short类型的数据来表示。

我们把俄罗斯方块点阵的数位存在rockArray中,我们可以事先把这19种方块的字模点阵自己转化成十六进制,然后在rockArray数组的初始化时赋值进去。 

但是这种方式扩展性不好,每当有一种新方块时需要改动, 

所以可以写一个配置文件来表示19种方块。(RockShapeini)

@###@###@@######1234

从配置文件中读取方块的类型的代码在(Inith的ReadRock函数中)在下面3中解释下代码如何实现

2如何画出方块

可以使用EasyX库来画出简单的图形, 

EasyX库是在VC下实现TC的简单绘图功能的一个库,这个库很容易学会(直接 百度EasyX库,里面有详细的教程)

那么如何画出方块,方块已经存储到一个short类型中了 

从short中读取出,可以用一个掩码mask = 1来与short的每个bit位相与,结果为1,则画出一个小方块; 

函数声明:

void DisplayRock(int rockIdx,  RockLocation_t  LocatePtr, bool displayed)1

参数1:表示在数组中的下标,取出short类型的方块表示数据 

参数2:表示当前坐标,即画出方块的左上角的坐标x,y 

参数3:true表示画出该方块,false 表示擦除该方块。

//方块在图形窗口中的位置(即定位44大块的左上角坐标)  typedef struct LOCATE

{    int left;    int top;

} RockLocation_t;123456

3如何实现同一种类型方块的翻转,

在按‘↑’时应该翻转同一种类型的方块, 

比如下面的横杆和竖杆

@###@###@###@###@@@@############1234567891011

可以假想成静态循环链表来实现这种方式 

使同一种类型的方块循环起来,

用一个struct结构来表示一种方块

typedef struct ROCK

{    //用来表示方块的形状(每一个字节是8位,用每4位表示方块中的一行)

unsigned short rockShapeBits;    int          nextRockIndex;  //下一个方块,在数组中的下标  } RockType;123456

定义一个RockType类型的数组来存储19种方块 

RockType RockArray[19] = { (0, 0) };

当我们按“↑”时,把传入画方块函数DrawRock中的rockIndex变为当前方块结构体中的nextRockIndex即可。

简单解释下ReadRock函数的实现:当读取到空行的时候表示 一种方块已经读取完毕,当读取到 行时 表示同一种类型的方块读取完毕,具体看代码实现,代码中具体的注释

4、主要游戏实现的逻辑

贴一个预览图吧 

注:上述预览图的游戏控制区和游戏显示区在Drawh的DrawGameWindow()函数实现的

(1)在初始位置画出方块,在预览区画出下一次的方块 

(2)方块有两种行为:响应键盘命令UserHitKeyBoard(),自由下落 

如果敲击键盘了(w ,a ,s ,d, )空格表示暂停,如果在规定时间内没有敲击键盘的话,方块自由下落一个单位

if (kbhit()) //如果敲击键盘了 就处理按键

{

userHit = getch();

UserHitKeyBoard(userHit, &curRockIndex, &curRockLocation);

}        //没有 就自动下移一个单位 :不能用else,因为可能按键不是上下左右

DWORD newtime = GetTickCount();        if (newtime - oldtime >= (unsigned int)(300) && moveAbled == TRUE)

{

oldtime = newtime;

DisplayRock(curRockIndex, &curRockLocation, false);

curRockLocationtop += ROCK_SQUARE_WIDTH; //下落一格

}1234567891011121314

(3)当方块落地(即不能下移了)时,判断是否满行,如果满行则消除,然后再判断游戏是否结束,游戏结束的话,直接退出游戏

判断满行:FullLine()函数,从最底下的一行开始判断,直到遇到一行空行,

while (count != xROCK_SQUARE_NUM ) //遇到空行 14

{

linefull = true;        count = 0;        for (int i = 1; i <= xROCK_SQUARE_NUM; ++i)

{            if (game_board[idx][i] == 0)

{

linefull = false;                count++;

}

}        if (linefull) //满行,消除当前行,更新分数

{

DelCurLine(idx);//消除满行

game_socres += 3;

UpdateSocres(game_socres);

idx++;//因为下面要减1

}

idx--;

}123456789101112131415161718192021

(4)消除满行 

将要删除的满行擦除:即将方块化成与背景色相同的,该代码为黑色 

然后将上面的一行向下移,移一行删除一行,直到遇到空行 

具体看代码的具体实现 gameh 

void DelCurLine(int rowIdx)

(4)判断方块是否能移动 

在gameh中实现

bool MoveAble(int rockIndex, RockLocation_t currentLocatePtr, int f_direction)1

比较当前位置的坐标(左上角)开始,能否放下rockIndex的方块。 

注:f_direction为”↑”的话,则传入的rockIndex为下一个方块

如果不能移动的话,给游戏game_board设置标记表示该位置被占有

//全局变量-游戏板的状态描述(即表示当前界面哪些位置有方块)  //0表示没有,1表示有(多加了两行和两列,形成一个围墙,便于判断方块是否能够移动)  int game_board[yROCK_SQUARE_NUM + 2][xROCK_SQUARE_NUM + 2] = { 0 };123

实现过程遇到的一些问题

(1)在快速下落的时候,可能方块会掉出围墙的范围内, 

快速下落是使方块每次下落2个单位距离。 

在判断不能下落时,使当前坐标的top即y减去一个单位的距离

(2)遇到多行满行时消除不了, 

在判断满行时,循环找出满行,找出一个满行,就消除一行,然后继续判断是否满行,直到遇到空行

#include <graphicsh>

#include <conioh>

#include <timeh>

/////////////////////////////////////////////

// 定义常量、枚举量、结构体、全局变量

/////////////////////////////////////////////

#define WIDTH 10 // 游戏区宽度

#define HEIGHT 22 // 游戏区高度

#define SIZE 20 // 每个游戏区单位的实际像素

// 定义 *** 作类型

enum CMD

{

CMD_ROTATE, // 方块旋转

CMD_LEFT, CMD_RIGHT, CMD_DOWN, // 方块左、右、下移动

CMD_SINK, // 方块沉底

CMD_QUIT // 退出游戏

};

// 定义绘制方块的方法

enum DRAW

{

SHOW, // 显示方块

HIDE, // 隐藏方块

FIX // 固定方块

};

// 定义七种俄罗斯方块

struct BLOCK

{

WORD dir[4]; // 方块的四个旋转状态

COLORREF color; // 方块的颜色

} g_Blocks[7] = { {0x0F00, 0x4444, 0x0F00, 0x4444, RED}, // I

{0x0660, 0x0660, 0x0660, 0x0660, BLUE}, // 口

{0x4460, 0x02E0, 0x0622, 0x0740, MAGENTA}, // L

{0x2260, 0x0E20, 0x0644, 0x0470, YELLOW}, // 反L

{0x0C60, 0x2640, 0x0C60, 0x2640, CYAN}, // Z

{0x0360, 0x4620, 0x0360, 0x4620, GREEN}, // 反Z

{0x4E00, 0x4C40, 0x0E40, 0x4640, BROWN}}; // T

// 定义当前方块、下一个方块的信息

struct BLOCKINFO

{

byte id; // 方块 ID

char x, y; // 方块在游戏区中的坐标

byte dir:2; // 方向

} g_CurBlock, g_NextBlock;

// 定义游戏区

BYTE g_World[WIDTH][HEIGHT] = {0};

/////////////////////////////////////////////

// 函数声明

/////////////////////////////////////////////

void Init(); // 初始化游戏

void Quit(); // 退出游戏

void NewGame(); // 开始新游戏

void GameOver(); // 结束游戏

CMD GetCmd(); // 获取控制命令

void DispatchCmd(CMD _cmd); // 分发控制命令

void NewBlock(); // 生成新的方块

bool CheckBlock(BLOCKINFO _block); // 检测指定方块是否可以放下

void DrawBlock(BLOCKINFO _block, DRAW _draw = SHOW); // 画方块

void OnRotate(); // 旋转方块

void OnLeft(); // 左移方块

void OnRight(); // 右移方块

void OnDown(); // 下移方块

void OnSink(); // 沉底方块

/////////////////////////////////////////////

// 函数定义

/////////////////////////////////////////////

// 主函数

void main()

{

Init();

CMD c;

while(true)

{

c = GetCmd();

DispatchCmd(c);

// 按退出时,显示对话框咨询用户是否退出

if (c == CMD_QUIT)

{

HWND wnd = GetHWnd();

if (MessageBox(wnd, _T("您要退出游戏吗?"), _T("提醒"), MB_OKCANCEL | MB_ICONQUESTION) == IDOK)

Quit();

}

}

}

// 初始化游戏

void Init()

{

initgraph(640, 480);

srand((unsigned)time(NULL));

// 显示 *** 作说明

setfont(14, 0, _T("宋体"));

outtextxy(20, 330, _T(" *** 作说明"));

outtextxy(20, 350, _T("上:旋转"));

outtextxy(20, 370, _T("左:左移"));

outtextxy(20, 390, _T("右:右移"));

outtextxy(20, 410, _T("下:下移"));

outtextxy(20, 430, _T("空格:沉底"));

outtextxy(20, 450, _T("ESC:退出"));

// 设置坐标原点

setorigin(220, 20);

// 绘制游戏区边界

rectangle(-1, -1, WIDTH SIZE, HEIGHT SIZE);

rectangle((WIDTH + 1) SIZE - 1, -1, (WIDTH + 5) SIZE, 4 SIZE);

// 开始新游戏

NewGame();

}

// 退出游戏

void Quit()

{

closegraph();

exit(0);

}

// 开始新游戏

void NewGame()

{

// 清空游戏区

setfillstyle(BLACK);

bar(0, 0, WIDTH SIZE - 1, HEIGHT SIZE - 1);

ZeroMemory(g_World, WIDTH HEIGHT);

// 生成下一个方块

g_NextBlockid = rand() % 7;

g_NextBlockdir = rand() % 4;

g_NextBlockx = WIDTH + 1;

g_NextBlocky = HEIGHT - 1;

// 获取新方块

NewBlock();

}

// 结束游戏

void GameOver()

{

HWND wnd = GetHWnd();

if (MessageBox(wnd, _T("游戏结束。\n您想重新来一局吗?"), _T("游戏结束"), MB_YESNO | MB_ICONQUESTION) == IDYES)

NewGame();

else

Quit();

}

// 获取控制命令

DWORD m_oldtime;

CMD GetCmd()

{

// 获取控制值

while(true)

{

// 如果超时,自动下落一格

DWORD newtime = GetTickCount();

if (newtime - m_oldtime >= 500)

{

m_oldtime = newtime;

return CMD_DOWN;

}

// 如果有按键,返回按键对应的功能

if (kbhit())

{

switch(getch())

{

case 'w':

case 'W': return CMD_ROTATE;

case 'a':

case 'A': return CMD_LEFT;

case 'd':

case 'D': return CMD_RIGHT;

case 's':

case 'S': return CMD_DOWN;

case 27: return CMD_QUIT;

case ' ': return CMD_SINK;

case 0:

case 0xE0:

switch(getch())

{

case 72: return CMD_ROTATE;

case 75: return CMD_LEFT;

case 77: return CMD_RIGHT;

case 80: return CMD_DOWN;

}

}

}

// 延时 (降低 CPU 占用率)

Sleep(20);

}

}

// 分发控制命令

void DispatchCmd(CMD _cmd)

{

switch(_cmd)

{

case CMD_ROTATE: OnRotate(); break;

case CMD_LEFT: OnLeft(); break;

case CMD_RIGHT: OnRight(); break;

case CMD_DOWN: OnDown(); break;

case CMD_SINK: OnSink(); break;

case CMD_QUIT: break;

}

}

// 生成新的方块

void NewBlock()

{

g_CurBlockid = g_NextBlockid, g_NextBlockid = rand() % 7;

g_CurBlockdir = g_NextBlockdir, g_NextBlockdir = rand() % 4;

g_CurBlockx = (WIDTH - 4) / 2;

g_CurBlocky = HEIGHT + 2;

// 下移新方块直到有局部显示

WORD c = g_Blocks[g_CurBlockid]dir[g_CurBlockdir];

while((c & 0xF) == 0)

{

g_CurBlocky--;

c >>= 4;

}

// 绘制新方块

DrawBlock(g_CurBlock);

// 绘制下一个方块

setfillstyle(BLACK);

bar((WIDTH + 1) SIZE, 0, (WIDTH + 5) SIZE - 1, 4 SIZE - 1);

DrawBlock(g_NextBlock);

// 设置计时器,用于判断自动下落

m_oldtime = GetTickCount();

}

// 画方块

void DrawBlock(BLOCKINFO _block, DRAW _draw)

{

WORD b = g_Blocks[_blockid]dir[_blockdir];

int x, y;

int color = BLACK;

switch(_draw)

{

case SHOW: color = g_Blocks[_blockid]color; break;

case HIDE: color = BLACK; break;

case FIX: color = g_Blocks[_blockid]color / 3; break;

}

setfillstyle(color);

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

{

if (b & 0x8000)

{

x = _blockx + i % 4;

y = _blocky - i / 4;

if (y < HEIGHT)

{

if (_draw != HIDE)

bar3d(x SIZE + 2, (HEIGHT - y - 1) SIZE + 2, (x + 1) SIZE - 4, (HEIGHT - y) SIZE - 4, 3, true);

else

bar(x SIZE, (HEIGHT - y - 1) SIZE, (x + 1) SIZE - 1, (HEIGHT - y) SIZE - 1);

}

}

b <<= 1;

}

}

// 检测指定方块是否可以放下

bool CheckBlock(BLOCKINFO _block)

{

WORD b = g_Blocks[_blockid]dir[_blockdir];

int x, y;

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

{

if (b & 0x8000)

{

x = _blockx + i % 4;

y = _blocky - i / 4;

if ((x < 0) || (x >= WIDTH) || (y < 0))

return false;

if ((y < HEIGHT) && (g_World[x][y]))

return false;

}

b <<= 1;

}

return true;

}

// 旋转方块

void OnRotate()

{

// 获取可以旋转的 x 偏移量

int dx;

BLOCKINFO tmp = g_CurBlock;

tmpdir++; if (CheckBlock(tmp)) { dx = 0; goto rotate; }

tmpx = g_CurBlockx - 1; if (CheckBlock(tmp)) { dx = -1; goto rotate; }

tmpx = g_CurBlockx + 1; if (CheckBlock(tmp)) { dx = 1; goto rotate; }

tmpx = g_CurBlockx - 2; if (CheckBlock(tmp)) { dx = -2; goto rotate; }

tmpx = g_CurBlockx + 2; if (CheckBlock(tmp)) { dx = 2; goto rotate; }

return;

rotate:

// 旋转

DrawBlock(g_CurBlock, HIDE);

g_CurBlockdir++;

g_CurBlockx += dx;

DrawBlock(g_CurBlock);

}

// 左移方块

void OnLeft()

{

BLOCKINFO tmp = g_CurBlock;

tmpx--;

if (CheckBlock(tmp))

{

DrawBlock(g_CurBlock, HIDE);

g_CurBlockx--;

DrawBlock(g_CurBlock);

}

}

// 右移方块

void OnRight()

{

BLOCKINFO tmp = g_CurBlock;

tmpx++;

if (CheckBlock(tmp))

{

DrawBlock(g_CurBlock, HIDE);

g_CurBlockx++;

DrawBlock(g_CurBlock);

}

}

// 下移方块

void OnDown()

{

BLOCKINFO tmp = g_CurBlock;

tmpy--;

if (CheckBlock(tmp))

{

DrawBlock(g_CurBlock, HIDE);

g_CurBlocky--;

DrawBlock(g_CurBlock);

}

else

OnSink(); // 不可下移时,执行“沉底方块” *** 作

}

// 沉底方块

void OnSink()

{

int i, x, y;

// 连续下移方块

DrawBlock(g_CurBlock, HIDE);

BLOCKINFO tmp = g_CurBlock;

tmpy--;

while (CheckBlock(tmp))

{

g_CurBlocky--;

tmpy--;

}

DrawBlock(g_CurBlock, FIX);

// 固定方块在游戏区

WORD b = g_Blocks[g_CurBlockid]dir[g_CurBlockdir];

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

{

if (b & 0x8000)

{

if (g_CurBlocky - i / 4 >= HEIGHT)

{ // 如果方块的固定位置超出高度,结束游戏

GameOver();

return;

}

else

g_World[g_CurBlockx + i % 4][g_CurBlocky - i / 4] = 1;

}

b <<= 1;

}

// 检查是否需要消掉行,并标记

int row[4] = {0};

bool bRow = false;

for(y = g_CurBlocky; y >= max(g_CurBlocky - 3, 0); y--)

{

i = 0;

for(x = 0; x < WIDTH; x++)

if (g_World[x][y] == 1)

i++;

if (i == WIDTH)

{

bRow = true;

row[g_CurBlocky - y] = 1;

setfillstyle(WHITE, DIAGCROSS2_FILL);

bar(0, (HEIGHT - y - 1) SIZE + SIZE / 2 - 2, WIDTH SIZE - 1, (HEIGHT - y - 1) SIZE + SIZE / 2 + 2);

}

}

if (bRow)

{

// 延时 200 毫秒

Sleep(200);

// 擦掉刚才标记的行

IMAGE img;

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

{

if (row[i])

{

for(y = g_CurBlocky - i + 1; y < HEIGHT; y++)

for(x = 0; x < WIDTH; x++)

{

g_World[x][y - 1] = g_World[x][y];

g_World[x][y] = 0;

}

getimage(&img, 0, 0, WIDTH SIZE, (HEIGHT - (g_CurBlocky - i + 1)) SIZE);

putimage(0, SIZE, &img);

}

}

}

// 产生新方块

NewBlock();

}

#define N 200

#include <graphicsh>

#include <stdlibh>

#include <dosh>

#define LEFT 0x4b00

#define RIGHT 0x4d00

#define DOWN 0x5000

#define UP 0x4800

#define ESC 0x011b

int i,key;

int score=0;/得分/

int gamespeed=50000;/游戏速度自己调整/

struct Food

{

int x;/食物的横坐标/

int y;/食物的纵坐标/

int yes;/判断是否要出现食物的变量/

}food;/食物的结构体/

struct Snake

{

int x[N];

int y[N];

int node;/蛇的节数/

int direction;/蛇移动方向/

int life;/ 蛇的生命,0活着,1死亡/

}snake;

void Init(void);/图形驱动/

void Close(void);/图形结束/

void DrawK(void);/开始画面/

void GameOver(void);/结束游戏/

void GamePlay(void);/玩游戏具体过程/

void PrScore(void);/输出成绩/

/主函数/

void main(void)

{

Init();/图形驱动/

DrawK();/开始画面/

GamePlay();/玩游戏具体过程/

Close();/图形结束/

}

/图形驱动/

void Init(void)

{

int gd=DETECT,gm;

initgraph(&gd,&gm,"c:\\tc");

cleardevice();

}

/开始画面,左上角坐标为(50,40),右下角坐标为(610,460)的围墙/

void DrawK(void)

{

/setbkcolor(LIGHTGREEN);/

setcolor(11);

setlinestyle(SOLID_LINE,0,THICK_WIDTH);/设置线型/

for(i=50;i<=600;i+=10)/画围墙/

{

rectangle(i,40,i+10,49); /上边/

rectangle(i,451,i+10,460);/下边/

}

for(i=40;i<=450;i+=10)

{

rectangle(50,i,59,i+10); /左边/

rectangle(601,i,610,i+10);/右边/

}

}

/玩游戏具体过程/

void GamePlay(void)

{

randomize();/随机数发生器/

foodyes=1;/1表示需要出现新食物,0表示已经存在食物/

snakelife=0;/活着/

snakedirection=1;/方向往右/

snakex[0]=100;snakey[0]=100;/蛇头/

snakex[1]=110;snakey[1]=100;

snakenode=2;/节数/

PrScore();/输出得分/

while(1)/可以重复玩游戏,压ESC键结束/

{

while(!kbhit())/在没有按键的情况下,蛇自己移动身体/

{

if(foodyes==1)/需要出现新食物/

{

foodx=rand()%400+60;

foody=rand()%350+60;

while(foodx%10!=0)/食物随机出现后必须让食物能够在整格内,这样才可以让蛇吃到/

foodx++;

while(foody%10!=0)

foody++;

foodyes=0;/画面上有食物了/

}

if(foodyes==0)/画面上有食物了就要显示/

{

setcolor(GREEN);

rectangle(foodx,foody,foodx+10,foody-10);

}

for(i=snakenode-1;i>0;i--)/蛇的每个环节往前移动,也就是贪吃蛇的关键算法/

{

snakex[i]=snakex[i-1];

snakey[i]=snakey[i-1];

}

/1,2,3,4表示右,左,上,下四个方向,通过这个判断来移动蛇头/

switch(snakedirection)

{

case 1:snakex[0]+=10;break;

case 2: snakex[0]-=10;break;

case 3: snakey[0]-=10;break;

case 4: snakey[0]+=10;break;

}

for(i=3;i<snakenode;i++)/从蛇的第四节开始判断是否撞到自己了,因为蛇头为两节,第三节不可能拐过来/

{

if(snakex[i]==snakex[0]&&snakey[i]==snakey[0])

{

GameOver();/显示失败/

snakelife=1;

break;

}

}

if(snakex[0]<55||snakex[0]>595||snakey[0]<55||

snakey[0]>455)/蛇是否撞到墙壁/

{

GameOver();/本次游戏结束/

snakelife=1; /蛇死/

}

if(snakelife==1)/以上两种判断以后,如果蛇死就跳出内循环,重新开始/

break;

if(snakex[0]==foodx&&snakey[0]==foody)/吃到食物以后/

{

setcolor(0);/把画面上的食物东西去掉/

rectangle(foodx,foody,foodx+10,foody-10);

snakex[snakenode]=-20;snakey[snakenode]=-20;

/新的一节先放在看不见的位置,下次循环就取前一节的位置/

snakenode++;/蛇的身体长一节/

foodyes=1;/画面上需要出现新的食物/

score+=10;

PrScore();/输出新得分/

}

setcolor(4);/画出蛇/

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

rectangle(snakex[i],snakey[i],snakex[i]+10,

snakey[i]-10);

delay(gamespeed);

setcolor(0);/用黑色去除蛇的的最后一节/

rectangle(snakex[snakenode-1],snakey[snakenode-1],

snakex[snakenode-1]+10,snakey[snakenode-1]-10);

} /endwhile(!kbhit)/

if(snakelife==1)/如果蛇死就跳出循环/

break;

key=bioskey(0);/接收按键/

if(key==ESC)/按ESC键退出/

break;

else

if(key==UP&&snakedirection!=4)

/判断是否往相反的方向移动/

snakedirection=3;

else

if(key==RIGHT&&snakedirection!=2)

snakedirection=1;

else

if(key==LEFT&&snakedirection!=1)

snakedirection=2;

else

if(key==DOWN&&snakedirection!=3)

snakedirection=4;

}/endwhile(1)/

}

/游戏结束/

void GameOver(void)

{

cleardevice();

PrScore();

setcolor(RED);

settextstyle(0,0,4);

outtextxy(200,200,"GAME OVER");

getch();

}

/输出成绩/

void PrScore(void)

{

char str[10];

setfillstyle(SOLID_FILL,YELLOW);

bar(50,15,220,35);

setcolor(6);

settextstyle(0,0,2);

sprintf(str,"score:%d",score);

outtextxy(55,20,str);

}

/图形结束/

void Close(void)

{

getch();

closegraph();

}

以上就是关于C语言设计一个简单的猜数游戏:先由计算机“想”一个数请用户猜,如果用户猜对了,则计算机给出提示信息全部的内容,包括:C语言设计一个简单的猜数游戏:先由计算机“想”一个数请用户猜,如果用户猜对了,则计算机给出提示信息、贪吃蛇 C语言 简易程序设计、给我提供个小游戏的C 语言代码等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/10065089.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-04
下一篇2023-05-04

发表评论

登录后才能评论

评论列表(0条)

    保存