📄 realball.cpp
字号:
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<string.h>
#include<dos.h>
#include<time.h>
#include<bios.h>
#include<alloc.h>
#include<stdlib.h>
#include"ourmouse.h"
#include"bmp16.h"
#include"realsave.h"
#include"realball.h"
#define BLOCKWIDTH 40
#define BLOCKHEIGHT 10
#define BORDERWIDTH 50
#define BORDERHEIGHT 10
#define LEFTAREA 100
#define TOPAREA 10
#define RIGHTAREA 500
#define SCOREWIDTH 100
#define SCOREHEIGHT 30
#define TREASUREHEIGHT 15
#define TREASUREWIDTH 15
#define RADIUS 5
#define LEFTKEY 1
#define TOTALBLOCKNUM 25
#define NORMALSPEED 10
/*-----------------------------------------------------------------*/
static int oldlife=-2;
static int oldscore=-2;
static int oldround=-2;
BALL *pbhead=NULL;
BORDER border;
USER user,olduser;
BLOCKS block[TOTALBLOCKNUM+1];
BUTTON RealsaveBtn={5,400,50,25,""},RealexitBtn={5,450,50,25,""};
int speed=10;
TREASURE *pthead;
void *buf_normalball;
void *buf_bigball;
void *buf_flahball;
void *buf_blockbg;
void *buf_block1;
void *buf_treasureslow;
void *buf_treasurequick;
void *buf_treasurebg;
void *buf_block2;
void *buf_ballbg;
/*----------------------------------------------------------------------*/
/* int paint_ball(BALL *,USER *);
int paint_treasure(TREASURE *,BORDER *);
void paint_border(BORDER *);
void paint_blocks();
void initblocks(int);
void loadpictures();
void freemem_realball();
void set_screen();
void show_score();
void show_life();
int play();
int pause(); */
/*-----------------------------------------------------------------------*/
int realball()
/*void main()*/
{
int mx,my,oldx,oldy,moldx,moldy;
int exit_flagrealball=0;
/*---------initialize the first ball--------------*/
user.round=1;
user.life=3;
user.score=0;
user.attribute=0;
border.height=BORDERHEIGHT;
border.width=BORDERWIDTH;
oldlife=-2;
oldscore=-2;
oldround=-2;
initmouse();
loadpictures();
olduser=user;
do{
initblocks(user.round);
paint_blocks();
set_screen();
switch (play())
{ case 1:
olduser.life=user.life;
olduser.score=user.score;
olduser.round=user.round;
user.round++;
//user.life++;
break;
case 0:
outtextxy(200,250,"TRY AGAIN ?y/n");
char y;
if(getch()=='y')
{
setfillstyle(1,0);
bar(200,250,400,300);
user.round=1;
user.life=3;
user.score=0;
user.attribute=0;
}
else
exit_flagrealball=1;
break;
case 2:
/* user=olduser;
user.score=olduser.score;
user.life=olduser.life;
user.round=olduser.round; */
break;
case 3: /*in the functione play the user wanna quit the game*/
exit_flagrealball=1;
break;
default:
break;
}
}while(exit_flagrealball!=1);
// free(pbhead->buf);
// free(pbhead->oldbuf);
free(pbhead);
pbhead=NULL;
freemem_realball();
return 1;
}
int paint_ball(BALL *pb,USER *pu)
{
int i;
switch(pu->attribute)
{
case 0:
pb->buf=buf_normalball;
break;
case 1:
pb->buf=buf_bigball;
break;
case 2:
pb->buf=buf_flahball;
break;
//.........
default:
break;
}
if((pb->x)>=RIGHTAREA-15||pb->x<=LEFTAREA+2)
{
pb->vx*=-1;
printf("\7");
}
if(pb->y<=TOPAREA)
{
pb->vy*=-1;
printf("\7");
}
if(pb->y>=479-border.height-pb->radius-10&&pb->y<=479-border.height&&pb->x>=border.x&&pb->x<=border.x+border.width)
{
if((pb->x-border.x)<border.width/3)
{
pb->vx=-3;
pb->vy=-1;
printf("\7");
}
else if((pb->x-border.x)<border.width/2)
{ pb->vx=-2;
pb->vy=-2;
printf("\7");
}
else if((pb->x-border.x)<border.width*2/3)
{
pb->vx=2;
pb->vy=-2;
printf("\7");
}
else if((pb->x-border.x)<border.width)
{
pb->vx=3;
pb->vy=-1;
printf("\7");
}
else
{
pb->vx=0;
pb->vy=-3;
printf("\7");
}
}
else if(pb->y>=479-border.height-pb->radius)
{
return 0;
}
int n=0;
for(i=1;i<=TOTALBLOCKNUM;i++)
{
if(block[i].active==1)
{
if((pb->x+2*pb->radius)>=block[i].x&&(pb->x<=block[i].x+BLOCKWIDTH)
&&(pb->y+2*pb->radius==block[i].y-2||pb->y+2*pb->radius==block[i].y-1
||pb->y+2*pb->radius==block[i].y||pb->y==block[i].y+BLOCKHEIGHT
||pb->y==block[i].y+BLOCKHEIGHT+1||pb->y==block[i].y+BLOCKHEIGHT+2))
{
pb->vy*=-1;
printf("\7");
user.score+=9;
if(block[i].treasure==1&&pthead==NULL)
{
pthead=(TREASURE *)malloc(sizeof(TREASURE));
pthead->width=TREASUREWIDTH;
pthead->height=TREASUREHEIGHT;
pthead->vx=1;
pthead->vy=1;
pthead->x=250;//block[i].x;
pthead->y=250;//block[i].y+BLOCKHEIGHT;
pthead->buf=buf_treasureslow;
pthead->oldbuf=buf_treasurebg;
pthead->treasure=1;
}
else if(block[i].treasure==2&&pthead==NULL)
{
pthead=(TREASURE *)malloc(sizeof(TREASURE));
pthead->width=TREASUREWIDTH;
pthead->height=TREASUREHEIGHT;
pthead->vx=-1;
pthead->vy=1;
pthead->x=250;//block[i].x+10;
pthead->y=250;//block[i].y+10;
pthead->buf=buf_treasurequick;
pthead->oldbuf=buf_treasurebg;
pthead->treasure=2;
}
block[i].thick--;
if(block[i].thick==1)
{
block[i].buf=buf_block1;
putimage(block[i].x,block[i].y,buf_block1,COPY_PUT);
}
if(block[i].thick==0)
{
block[i].active=0;
putimage(block[i].x,block[i].y,buf_blockbg,COPY_PUT);
}
}
if(pb->y+2*pb->radius>=block[i].y&&pb->y<=block[i].y+BLOCKHEIGHT
&&(pb->x+2*pb->radius==block[i].x||pb->x+2*pb->radius==block[i].x-1||pb->x+2*pb->radius==block[i].x-2
||pb->x==block[i].x+BLOCKWIDTH||pb->x==block[i].x+BLOCKWIDTH+1
||pb->x==block[i].x+BLOCKWIDTH+2))
{ pb->vx*=-1;
printf("\7");
block[i].thick--;
user.score+=9;
if(block[i].thick==1)
{
block[i].buf=buf_block1;
putimage(block[i].x,block[i].y,buf_block1,COPY_PUT);
}
if(block[i].thick==0)
{
block[i].active=0;
putimage(block[i].x,block[i].y,buf_blockbg,COPY_PUT);
}
}
}
else n++;
}
if(n==TOTALBLOCKNUM)
return 2;
//on the different part the vx and vy will be different
pb->x+=pb->vx;
pb->y+=pb->vy;
putimage(pb->oldx,pb->oldy,pb->oldbuf,COPY_PUT);
/* getimage(pb->x-1,pb->y-1,pb->x+2*(pb->radius)+1,pb->y+(2*pb->radius)+1,pb->oldbuf);*/
putimage(pb->x,pb->y,pb->buf,COPY_PUT);
pb->oldx=pb->x;
pb->oldy=pb->y;
return 1;
}
void paint_border(BORDER *pbo)
{
int my,key,mx,moldx;
readmouse(&key,&mx,&my);
mx=(mx>639-pbo->width)?639:mx;
my=(my>479-pbo->width)?479:my;
if(mx!=pbo->oldx)
{
putimage(pbo->oldx,479-pbo->height,pbo->oldbuf,COPY_PUT);
putimage(mx,479-pbo->height,pbo->buf,COPY_PUT);
pbo->oldx=mx;
pbo->x=mx;
}
}
int paint_treasure(TREASURE *pt)
{
if(pt->x<=LEFTAREA||pt->x+pt->width>RIGHTAREA)
pt->vx*=-1;
if(pt->y<=TOPAREA)
pt->vy*=-1;
if((pt->y+pt->height==479-border.height||pt->y+pt->height==479-border.height-1
||pt->y+pt->height==479-border.height+1)
&&pt->x+pt->width>=border.x&&pt->x<=border.x+border.width)
{
switch (pt->treasure)
{
case 1:
speed=20; /*:SLOW BALL*/
break;
case 2:
speed=8; /*:QUICK BALL*/
break;
default:
break;
}
putimage(pt->x,pt->y,pt->oldbuf,COPY_PUT);
user.score+=100;
return 0;
}
else if(pt->y>=479-border.height)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -