孙小飞的回答:/*也不知道你是什么级别的,我是一个新手,刚接触编程语言,以下是我自己变得一个小程序,在所有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语言的编译器(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; } 荒漠依米的回答:速度和 接板 长度不能改
所以比较难玩
#include "graphics.h"
#include "stdio.h"
#include "conio.h" /*所需的头文件*/
int on; /*声明具有开关作用的全局变量*/
static int score; /*声明静态的记分器变量*/
/* 定义开始界面函数*/
int open()
{
setviewport(100,100,500,380,1); /*设置图形窗口区域*/
setcolor(4); /*设置作图色*/
rectangle(0,0,399,279); /*以矩形填充所设的图形窗口区域*/
setfillstyle(solid_fill,7); /*设置填充方式*/
floodfill(50,50,4); /*设置填充范围*/
setcolor(8);
settextstyle(0,0,9); /*文本字体设置*/
outtextxy(90,80,"ball"); /*输出文本内容*/
settextstyle(0,0,1);
outtextxy(110,180,"version 1.0");
outtextxy(110,190,"made by ddt");
setcolor(128);
settextstyle(0,0,1);
outtextxy(120,240,"press any key to continue......");
}
/*定义退出界面函数*/
int quitwindow()
{
char s[100]; /*声明用于存放字符串的数组*/
setviewport(100,150,540,420,1);
setcolor(yellow);
rectangle(0,0,439,279);
setfillstyle(solid_fill,7);
floodfill(50,50,14);
setcolor(12);
settextstyle(0,0,8);
outtextxy(120,80,"end");
settextstyle(0,0,2);
outtextxy(120,200,"quit? y/n");
sprintf(s,"your score is:%d",score);/*格式化输出记分器的值*/
outtextxy(120,180,s);
on=1; /*初始化开关变量*/
}
/*主函数*/
main()
{
int gdriver,gmode;
gdriver=detect; /*设置图形适配器*/
gmode=0; /*设置图形模式*/
registerbgidriver(egavga_driver);/* 注册bgi驱动后可以不需要.bgi文件的支持运行 */
initgraph(&gdriver,&gmode,"");
setbkcolor(14);
open(); /*调用开始界面函数*/
getch(); /*暂停*/
while(1) /*此大循环体控制游戏的反复重新进行*/
{
int driver,mode,l=320,t=400,r,a,b,dl=5,n,x=200,y=400,r1=10,dx=-2,dy=-2;/*初始化小球相关参数*/
int left[100],top[100],right[100],bottom[100],i,j,k,off=1,m,num[100][100];/*方砖阵列相关参数*/
static int pp;
static int phrase; /*一系列起开关作用的变量*/
int oop=15;
pp=1;
score=0;
driver=detect;
mode=vga;
registerbgidriver(egavga_driver);
initgraph(&driver,&mode,"");
setbkcolor(10);
cleardevice(); /*图形状态下清屏*/
clearviewport(); /*清除现行图形窗口内容*/
b=t+6;
r=l+60;
setcolor(1);
rectangle(0,0,639,479);
setcolor(4);
rectangle(l,t,r,b);
setfillstyle(solid_fill,1);
floodfill(l+2,t+2,4);
for(i=0,k=0;i<=6;i++) /*此循环绘制方砖阵列*/
{
top[i]=k;
bottom[i]=top[i]+20;
k=k+21;
oop--;
for(j=0,m=0;j<=7;j++)
{
left[j]=m;
right[j]=left[j]+80;
m=m+81;
setcolor(4);
rectangle(left[j],top[i],right[j],bottom[i]);
setfillstyle(solid_fill,j+oop);
floodfill(left[j]+1,top[i]+1,4);
num[i][j]=pp++;
}
}
while(1) /*此循环控制整个动画*/
{
while(!kbhit())
{
x=x+dx; /*小球运动的圆心变量控制*/
y=y+dy;
if(x+r1>r||x+r1 李晓菲的回答:#include <graphics.h> #include <conio.h> #include <time.h> ///////////////////////////////////////////// // 定义常量、枚举量、结构体、全局变量 ///////////////////////////////////////////// #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_NextBlock.id = rand() % 7; g_NextBlock.dir = rand() % 4; g_NextBlock.x = WIDTH + 1; g_NextBlock.y = 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_CurBlock.id = g_NextBlock.id, g_NextBlock.id = rand() % 7; g_CurBlock.dir = g_NextBlock.dir, g_NextBlock.dir = rand() % 4; g_CurBlock.x = (WIDTH - 4) / 2; g_CurBlock.y = HEIGHT + 2; // 下移新方块直到有局部显示 WORD c = g_Blocks[g_CurBlock.id].dir[g_CurBlock.dir]; while((c & 0xF) == 0) { g_CurBlock.y--; 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[_block.id].dir[_block.dir]; int x, y; int color = BLACK; switch(_draw) { case SHOW: color = g_Blocks[_block.id].color; break; case HIDE: color = BLACK; break; case FIX: color = g_Blocks[_block.id].color / 3; break; } setfillstyle(color); for(int i=0; i<16; i++) { if (b & 0x8000) { x = _block.x + i % 4; y = _block.y - 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[_block.id].dir[_block.dir]; int x, y; for(int i=0; i<16; i++) { if (b & 0x8000) { x = _block.x + i % 4; y = _block.y - 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; tmp.dir++; if (CheckBlock(tmp)) { dx = 0; goto rotate; } tmp.x = g_CurBlock.x - 1; if (CheckBlock(tmp)) { dx = -1; goto rotate; } tmp.x = g_CurBlock.x + 1; if (CheckBlock(tmp)) { dx = 1; goto rotate; } tmp.x = g_CurBlock.x - 2; if (CheckBlock(tmp)) { dx = -2; goto rotate; } tmp.x = g_CurBlock.x + 2; if (CheckBlock(tmp)) { dx = 2; goto rotate; } return; rotate: // 旋转 DrawBlock(g_CurBlock, HIDE); g_CurBlock.dir++; g_CurBlock.x += dx; DrawBlock(g_CurBlock); } // 左移方块 void OnLeft() { BLOCKINFO tmp = g_CurBlock; tmp.x--; if (CheckBlock(tmp)) { DrawBlock(g_CurBlock, HIDE); g_CurBlock.x--; DrawBlock(g_CurBlock); } } // 右移方块 void OnRight() { BLOCKINFO tmp = g_CurBlock; tmp.x++; if (CheckBlock(tmp)) { DrawBlock(g_CurBlock, HIDE); g_CurBlock.x++; DrawBlock(g_CurBlock); } } // 下移方块 void OnDown() { BLOCKINFO tmp = g_CurBlock; tmp.y--; if (CheckBlock(tmp)) { DrawBlock(g_CurBlock, HIDE); g_CurBlock.y--; DrawBlock(g_CurBlock); } else OnSink(); // 不可下移时,执行“沉底方块”操作 } // 沉底方块 void OnSink() { int i, x, y; // 连续下移方块 DrawBlock(g_CurBlock, HIDE); BLOCKINFO tmp = g_CurBlock; tmp.y--; while (CheckBlock(tmp)) { g_CurBlock.y--; tmp.y--; } DrawBlock(g_CurBlock, FIX); // 固定方块在游戏区 WORD b = g_Blocks[g_CurBlock.id].dir[g_CurBlock.dir]; for(i = 0; i < 16; i++) { if (b & 0x8000) { if (g_CurBlock.y - i / 4 >= HEIGHT) { // 如果方块的固定位置超出高度,结束游戏 GameOver(); return; } else g_World[g_CurBlock.x + i % 4][g_CurBlock.y - i / 4] = 1; } b <<= 1; } // 检查是否需要消掉行,并标记 int row[4] = {0}; bool bRow = false; for(y = g_CurBlock.y; y >= max(g_CurBlock.y - 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_CurBlock.y - 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_CurBlock.y - 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_CurBlock.y - i + 1)) * SIZE); putimage(0, SIZE, &img); } } } // 产生新方块 NewBlock(); } 冯磊的回答:那个dev–c++可以吗 |