while(1)
{
//这里写第一个球运动代码
if(!bioskey(1)){ //如果有键按下,用来控制第2个小球运动
key=bioskey(0); //取得键值
switch (key)
{
case Key_LEFT:
break;
case Key_RIGHT:
break;
case Key_ESC:
exit(0);
}
}
}
[此贴子已经被作者于2007-2-6 10:45:20编辑过]
[此贴子已经被作者于2007-2-6 10:45:20编辑过]
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#include "math.h"
#include "graphics.h"
#define BKC 6
#define W 4471
#define S 8051
#define A 7777
#define D 8292
#define UP 18432
#define DOWN 20480
#define LEFT 19200
#define RIGHT 19712
#define ESC 283
#define YD 50
#define DR 10
#define DC 3
#define D_MOVE 3
#define D_XL_LIMIT 50
#define D_XR_LIMIT 50
#define D_YL_LIMIT 50
#define D_YR_LIMIT 50
#define CC 4
#define CR 10
#define C_MOVE 3
#define C_XL_LIMIT 50
#define C_XR_LIMIT 50
#define C_YL_LIMIT 50
#define C_YR_LIMIT 50
int Game();
void Init();
void DrawBKView();
int GetDBkView(int x ,int y);
int GetCBkView(int x ,int y);
int PutDBkView(int x ,int y);
int PutCBkView(int x ,int y);
int SpaceTime(int Space);
void Exit();
long tNew = 1 ,tOld = 0;
int D_BkView[DR*2+1][DR*2+1] ,C_BkView[CR*2+1][CR*2+1];
int main()
{
Init();
Game();
Exit();
}
int Game()
{
int Maxx ,Maxy;
int i ,j ,r;
int Key = 0;
int Dx ,Dy ,Dd = 1 ,Cx ,Cy;
Maxx = getmaxx();
Maxy = getmaxy();
Dx = getmaxx()/2;
Dy = getmaxy()/2;
Cx = getmaxx()/2;
Cy = getmaxy()/2+YD;
GetDBkView(Dx-DR ,Dy-DR);
GetCBkView(Cx-CR ,Cy-CR);
while(1)
{
setfillstyle(1 ,DC);
fillellipse(Dx ,Dy ,DR ,DR);
while(!SpaceTime(2))
{
if(kbhit())
{
PutCBkView(Cx-CR ,Cy-CR);
PutDBkView(Dx-DR ,Dy-DR);
Key = bioskey(0);
switch(Key)
{
case W :
case UP:if(Cy-C_MOVE > C_YL_LIMIT)Cy -= C_MOVE;
break;
case S :
case DOWN:if(Cy+C_MOVE < Maxy-C_YR_LIMIT)Cy += C_MOVE;
break;
case A :
case LEFT:if(Cx-C_MOVE > C_XL_LIMIT)Cx -= C_MOVE;
break;
case D :
case RIGHT:if(Cx+C_MOVE < Maxx-C_XR_LIMIT)Cx += C_MOVE;
break;
case ESC :return 1;
default:;
}
PutDBkView(Dx-DR ,Dy-DR);
GetCBkView(Cx-CR ,Cy-CR);
setfillstyle(1 ,CC);
fillellipse(Cx ,Cy ,CR ,CR);
}
}
PutDBkView(Dx-DR ,Dy-DR);
PutCBkView(Cx-CR ,Cy-CR);
if((r = Dx+Dd*D_MOVE) < D_XL_LIMIT || r > Maxx-D_XR_LIMIT)
Dd = -Dd;
Dx += Dd*D_MOVE;
GetDBkView(Dx-DR ,Dy-DR);
setfillstyle(1 ,CC);
fillellipse(Cx ,Cy ,CR ,CR);
delay(10);
}
return 0;
}
void Init()
{
int gd = DETECT ,gm = 0;
initgraph(&gd ,&gm ,"");
srand((unsigned)time(NULL));
DrawBKView();
}
void DrawBKView()
{
int i;
for(i = 0;i < 60;i++)
circle(random(500)+100 ,random(400)+100 ,random(40)+10);
}
int GetDBkView(int x ,int y)
{
int i ,j;
for(j = 0;j <= DR*2 ;j++)
for(i = 0;i <= DR*2 ;i++)
{
D_BkView[i][j] = getpixel(x+i ,y+j);
}
}
int GetCBkView(int x ,int y)
{
int i ,j;
for(j = 0;j <= CR*2 ;j++)
for(i = 0;i <= CR*2 ;i++)
{
C_BkView[i][j] = getpixel(x+i ,y+j);
}
}
int PutDBkView(int x ,int y)
{
int i ,j;
for(j = 0;j <= DR*2 ;j++)
for(i = 0;i <= DR*2 ;i++)
{
putpixel(x+i ,y+j ,D_BkView[i][j]);
}
}
int PutCBkView(int x ,int y)
{
int i ,j;
for(j = 0;j <= CR*2 ;j++)
for(i = 0;i <= CR*2 ;i++)
{
putpixel(x+i ,y+j ,C_BkView[i][j]);
}
}
int SpaceTime(int Space)
{
tNew = biostime(0 ,tNew);
if(abs(tNew-tOld) > Space)
{
tOld = tNew;
return 1;
}
return 0;
}
void Exit()
{
return 0;
}