改进:
1. 旧版:颜色搭配上,绿边会因为黑色变得很不明显。新版:换了一种有吸引力的颜色。比如,“黑底、白球、绿边”等。
2. 旧版:控制上不是很流畅。新版:使用流畅的按键控制。
3. 旧版:处理移动的方法,是擦掉每一个球。这样做有一个问题:画球的过程涉及到三角函数,当球很多时,擦掉全部的球会消耗很多时间,新版:用 cleardevice 清除整个屏幕更快一些。并且逻辑上也会更简单一些。
4. 旧版:造成屏幕闪烁的问题,是因为不能保证在一瞬间画出来所有物体。新版:建议你使用批量绘图指令。
5. 新版:游戏的逻辑上,用另一种方式。屏幕右侧空出一定区域,写上游戏的运行时间、接到的球的数量以及版权等信息。小球没有一次性全部落下,采用一会儿落一个,然后让屏幕上始终保持 10 个球(或其他数量)。每当接到一个球,就重新产生一个新球落下。
6. 新版:windows.h 已经在 graphics.h 里面包含了,不需要再次引用。
代码如下:
/*
盒子接球
copyright: 圣石
date: 2013.12.01
*/
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define NUM 10
#define CMD_LEFT 4
#define CMD_RIGHT 8
#define CMD_QUIT 64
int box_x = 10, box_y = 420;
struct Ball
{
int x;
int y;
int v;
bool exist;
};
int GetCommand()
{
int c = 0;
if (GetAsyncKeyState(VK_LEFT) & 0x8000) c |= CMD_LEFT;
if (GetAsyncKeyState(VK_RIGHT) & 0x8000) c |= CMD_RIGHT;
if (GetAsyncKeyState(VK_ESCAPE) & 0x8000) c |= CMD_QUIT;
return c;
}
void time(int hour,int minute, int second)
{
SYSTEMTIME stLocalagain;
GetLocalTime(&stLocalagain);
char strsec[10];
int houragain = stLocalagain.wHour, minuteagain = stLocalagain.wMinute,
secondagain = stLocalagain.wSecond, sec;
sec = (houragain - hour)*3600 + (minuteagain - minute)*60 + (secondagain - second);
itoa(sec, strsec, 10);
outtextxy(570, 110, strcat(strsec,"s"));
}
void menu(int score)
{
line(449, 0, 449, 480);
char strScore[10], runTime[] = "游戏运行时间 : ",
receiveBallNum[] = "接到的球的数量:", copyRight[] = "版权所有: 圣石 ",
finishWorkDate[] = "完成日期:2012年12月1日",
introductiona[] = "按方向键控制盒子移动", introductionb[] = "接住小球,按Esc键退出";
settextcolor(GREEN);
outtextxy(450, 10, introductiona);
outtextxy(450, 30, introductionb);
outtextxy(450, 110, runTime);
itoa(score, strScore, 10);
outtextxy(450, 210, receiveBallNum);
outtextxy(570, 210, strScore);
outtextxy(450, 310, copyRight);
outtextxy(450, 410, finishWorkDate);
}
void calculateScore(Ball ball[],int &n,int &score)
{
for(int i=0; i<NUM; i++)
{
if(ball[i].exist)
{
fillcircle(ball[i].x, ball[i].y, 8);
if(ball[i].y >= 472)
{
ball[i].exist = false;
n--; continue;
}
if(box_x+8<=ball[i].x && ball[i].x<=box_x+72 && ball[i].y>=412)
{
score++; n--;
ball[i].exist = false;
}
}
else
{
ball[i].x = 16 + 45*i;
ball[i].y = 8 + rand()%32;
ball[i].v = 1 + rand()%5;
ball[i].exist = true;
n++;
}
}
}
int main()
{
SYSTEMTIME stLocal;
GetLocalTime(&stLocal);
int hour = stLocal.wHour, minute = stLocal.wMinute, second = stLocal.wSecond;
initgraph(640, 480);
srand(time(NULL));
struct Ball ball[100];
int dx, i, n = NUM,score = 0;
bool flag = true;
for(i=0; i<NUM; i++)
{
ball[i].x = 16 + 45*i;
ball[i].y = 8 + rand()%32;
ball[i].v = 1 + rand()%5;
ball[i].exist = true;
}
char strScore[10], str[] = "your score:";
while(flag)
{
//画球和盒子
dx = 0; time(hour, minute, second);
setlinecolor(GREEN); setfillcolor(WHITE);
BeginBatchDraw();
menu(score);
calculateScore(ball,n,score);
fillrectangle(box_x, box_y, box_x+80, box_y+60);
FlushBatchDraw();
if (GetCommand() & CMD_LEFT) dx = -10;
if (GetCommand() & CMD_RIGHT) dx = 10;
if (GetCommand() & CMD_QUIT) flag = false;
Sleep(25);
//擦除球和盒子
cleardevice();
for(i=0; i<NUM; i++)
{
ball[i].y += ball[i].v;
}
box_x = box_x + dx;
}
itoa(score, strScore, 10);
outtextxy(292, 250, strcat(str, strScore));
outtextxy(290, 310, "按任意键退出");
EndBatchDraw();
getchar();
closegraph();
return 0;
}