主题:Turboc2.0下的射击模拟
呵呵,乘着51大好的春光,又做了个东西,本来想做成cs的awp甩枪训练的(里面的presslag就是想达到这种效果),可是反复实验,根本甩不到,所以就作为一个射击的模拟程序了。
当然,还有一些地方应该改进的,比如太阳应该会动、路上应该有车在开、背景应该再做漂亮一点,可是睡了一觉醒来,就不想改动了。呵呵,以后会发上改进的版本,这里先发个Demo了。
另外程序没有注解,呵呵,因为就这么几行,如果大家觉得其中的一些计算方法应该改进,请指出,谢谢!!!
#include<graphics.h>
#include<dos.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#define Timer 0x1c
#ifdef __cplusplus
#define __CPPARGS ...
#else
#define __CPPARGS
#endif
int TimerCounter=0;
void interrupt ( *oldhandler)(__CPPARGS);
void interrupt newhandler(__CPPARGS){
TimerCounter++;
oldhandler();
}
void SetTimer(void interrupt(*IntProc)(__CPPARGS)){
oldhandler=getvect(Timer);
disable();
setvect(Timer,IntProc);
enable();
}
void CloseTimer(){
disable();
setvect(Timer,oldhandler);
enable();
}
void initmouse(int x,int y){
union REGS reg;
reg.x.ax=1;
int86(0x33,®,®);
reg.x.ax=7;
reg.x.cx=100;
reg.x.dx=540;
int86(0x33,®,®);
reg.x.ax=8;
reg.x.cx=50;
reg.x.dx=300;
int86(0x33,®,®);
reg.x.ax=4;
reg.x.cx=x;
reg.x.dx=y;
int86(0x33,®,®);
}
void mouseh(){
union REGS reg;
reg.x.ax=2;
int86(0x33,®,®);
}
void getmxy(int *x,int *y){
union REGS reg;
reg.x.ax=3;
int86(0x33,®,®);
*x=reg.x.cx;
*y=reg.x.dx;
}
int leftbtnp(){
union REGS reg;
reg.x.ax=3;
int86(0x33,®,®);
if(reg.x.bx & 1)
return 1;
else
return 0;
}
void printbg(int x,int y){
int i;
setcolor(5);
rectangle(x,y,x+440,y+250);
if(x>0){
line(0,210,x,y+150);
if(y>0)
line(0,0,x,y);
if(y<100)
line(0,349,x,y+250);
}
if(x<200){
line(640,210,x+440,y+150);
if(y>0)
line(640,0,x+440,y);
if(y<100)
line(640,350,x+440,y+250);
}
setcolor(1);
setfillstyle(1,9);
bar(x+1,y+1,x+439,y+100);
setfillstyle(1,6);
bar(x+1,y+101,x+439,y+249);
setcolor(4);
setfillstyle(1,4);
pieslice(x+350,y+50,0,360,10);
circle(x+350,y+50,12);
setcolor(2);
setfillstyle(1,10);
for(i=0;i<10;i++){
pieslice(x+200-i*5,y+101+i*15,90,180,15);
pieslice(x+240+i*5,y+101+i*15,0,90,15);
}
}
printob(int x,int y,int b){
setcolor(1);
setfillstyle(1,3);
if(b){
circle(x,y,15);
floodfill(x,y,1);
}
else{
circle(x,y-7,5);
floodfill(x,y-7,1);
circle(x-7,y-1,5);
floodfill(x-7,y-1,1);
circle(x-7,y+5,5);
floodfill(x-7,y+5,1);
circle(x+8,y+3,8);
floodfill(x+8,y+3,1);
}
}
void printfocus(int b,int c){
if(!b && !c)
setcolor(14);
else
setcolor(4);
line(320,170,320,180);
line(310,175,330,175);
circle(320,175,10);
}
void printsc(int x,int y,char *st,char *ss){
setcolor(15);
outtextxy(x+40,y+10,st);
outtextxy(x+360,y+10,ss);
}
main(){
int gdriver=EGA,gmode=EGAHI;
int px=100,py=50,mx=320,my=175,by=230,bx=0,vx=0,vy=0;
int total=0,score=0,pressed=0,presslag=0,canhit=1;
int curpage=0;
char strt[3]="0",strs[3]="0",*str="You have hit balls.";
randomize();
initgraph(&gdriver,&gmode,"d:\\software\\turboc2");
setbkcolor(1);
settextstyle(0,0,2);
SetTimer(newhandler);
initmouse(mx,my);
printbg(px,py);
while(total<101){
nosound();
if(kbhit())break;
if(TimerCounter>0){
TimerCounter=0;
getmxy(&mx,&my);
px=420-mx;
py=225-my;
if(!pressed && !presslag && leftbtnp()){
sound(500);
pressed=1;
presslag=7;
}
if(!presslag && pressed){
pressed=0;
presslag=3;
}
if(pressed && canhit){
if((pow((px+bx-320),2)+pow((py+by-175),2))<225){
sound(50);
canhit=0;
vx=0;
vy=0;
score++;
sprintf(strs,"%d",score);
}
}
if(by>=230){
canhit=1;
by=random(100)+130;
vy=-30;
if(random(2)){
bx=random(100)+20;
vx=15;
}
else{
bx=random(100)+320;
vx=-15;
}
total++;
sprintf(strt,"%d",total);
}
by+=vy;
vy+=4;
bx+=vx;
curpage=1-curpage;
setactivepage(curpage);
cleardevice();
printbg(px,py);
printob(px+bx,py+by,canhit);
printfocus(pressed,presslag);
printsc(px,py,strt,strs);
setvisualpage(curpage);
if(presslag>0)
presslag--;
}
}
CloseTimer();
cleardevice();
if(total==101){
if(score==100)
str="You hit them all!!!";
else{
str[13]=strs[0];
str[14]=strs[1];
}
}
else{
getch();
str="Game is over by kbhit.";
}
outtextxy(100,100,str);
getch();
setvisualpage(1-curpage);
getch();
mouseh();
closegraph();
}
当然,还有一些地方应该改进的,比如太阳应该会动、路上应该有车在开、背景应该再做漂亮一点,可是睡了一觉醒来,就不想改动了。呵呵,以后会发上改进的版本,这里先发个Demo了。
另外程序没有注解,呵呵,因为就这么几行,如果大家觉得其中的一些计算方法应该改进,请指出,谢谢!!!
#include<graphics.h>
#include<dos.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#define Timer 0x1c
#ifdef __cplusplus
#define __CPPARGS ...
#else
#define __CPPARGS
#endif
int TimerCounter=0;
void interrupt ( *oldhandler)(__CPPARGS);
void interrupt newhandler(__CPPARGS){
TimerCounter++;
oldhandler();
}
void SetTimer(void interrupt(*IntProc)(__CPPARGS)){
oldhandler=getvect(Timer);
disable();
setvect(Timer,IntProc);
enable();
}
void CloseTimer(){
disable();
setvect(Timer,oldhandler);
enable();
}
void initmouse(int x,int y){
union REGS reg;
reg.x.ax=1;
int86(0x33,®,®);
reg.x.ax=7;
reg.x.cx=100;
reg.x.dx=540;
int86(0x33,®,®);
reg.x.ax=8;
reg.x.cx=50;
reg.x.dx=300;
int86(0x33,®,®);
reg.x.ax=4;
reg.x.cx=x;
reg.x.dx=y;
int86(0x33,®,®);
}
void mouseh(){
union REGS reg;
reg.x.ax=2;
int86(0x33,®,®);
}
void getmxy(int *x,int *y){
union REGS reg;
reg.x.ax=3;
int86(0x33,®,®);
*x=reg.x.cx;
*y=reg.x.dx;
}
int leftbtnp(){
union REGS reg;
reg.x.ax=3;
int86(0x33,®,®);
if(reg.x.bx & 1)
return 1;
else
return 0;
}
void printbg(int x,int y){
int i;
setcolor(5);
rectangle(x,y,x+440,y+250);
if(x>0){
line(0,210,x,y+150);
if(y>0)
line(0,0,x,y);
if(y<100)
line(0,349,x,y+250);
}
if(x<200){
line(640,210,x+440,y+150);
if(y>0)
line(640,0,x+440,y);
if(y<100)
line(640,350,x+440,y+250);
}
setcolor(1);
setfillstyle(1,9);
bar(x+1,y+1,x+439,y+100);
setfillstyle(1,6);
bar(x+1,y+101,x+439,y+249);
setcolor(4);
setfillstyle(1,4);
pieslice(x+350,y+50,0,360,10);
circle(x+350,y+50,12);
setcolor(2);
setfillstyle(1,10);
for(i=0;i<10;i++){
pieslice(x+200-i*5,y+101+i*15,90,180,15);
pieslice(x+240+i*5,y+101+i*15,0,90,15);
}
}
printob(int x,int y,int b){
setcolor(1);
setfillstyle(1,3);
if(b){
circle(x,y,15);
floodfill(x,y,1);
}
else{
circle(x,y-7,5);
floodfill(x,y-7,1);
circle(x-7,y-1,5);
floodfill(x-7,y-1,1);
circle(x-7,y+5,5);
floodfill(x-7,y+5,1);
circle(x+8,y+3,8);
floodfill(x+8,y+3,1);
}
}
void printfocus(int b,int c){
if(!b && !c)
setcolor(14);
else
setcolor(4);
line(320,170,320,180);
line(310,175,330,175);
circle(320,175,10);
}
void printsc(int x,int y,char *st,char *ss){
setcolor(15);
outtextxy(x+40,y+10,st);
outtextxy(x+360,y+10,ss);
}
main(){
int gdriver=EGA,gmode=EGAHI;
int px=100,py=50,mx=320,my=175,by=230,bx=0,vx=0,vy=0;
int total=0,score=0,pressed=0,presslag=0,canhit=1;
int curpage=0;
char strt[3]="0",strs[3]="0",*str="You have hit balls.";
randomize();
initgraph(&gdriver,&gmode,"d:\\software\\turboc2");
setbkcolor(1);
settextstyle(0,0,2);
SetTimer(newhandler);
initmouse(mx,my);
printbg(px,py);
while(total<101){
nosound();
if(kbhit())break;
if(TimerCounter>0){
TimerCounter=0;
getmxy(&mx,&my);
px=420-mx;
py=225-my;
if(!pressed && !presslag && leftbtnp()){
sound(500);
pressed=1;
presslag=7;
}
if(!presslag && pressed){
pressed=0;
presslag=3;
}
if(pressed && canhit){
if((pow((px+bx-320),2)+pow((py+by-175),2))<225){
sound(50);
canhit=0;
vx=0;
vy=0;
score++;
sprintf(strs,"%d",score);
}
}
if(by>=230){
canhit=1;
by=random(100)+130;
vy=-30;
if(random(2)){
bx=random(100)+20;
vx=15;
}
else{
bx=random(100)+320;
vx=-15;
}
total++;
sprintf(strt,"%d",total);
}
by+=vy;
vy+=4;
bx+=vx;
curpage=1-curpage;
setactivepage(curpage);
cleardevice();
printbg(px,py);
printob(px+bx,py+by,canhit);
printfocus(pressed,presslag);
printsc(px,py,strt,strs);
setvisualpage(curpage);
if(presslag>0)
presslag--;
}
}
CloseTimer();
cleardevice();
if(total==101){
if(score==100)
str="You hit them all!!!";
else{
str[13]=strs[0];
str[14]=strs[1];
}
}
else{
getch();
str="Game is over by kbhit.";
}
outtextxy(100,100,str);
getch();
setvisualpage(1-curpage);
getch();
mouseh();
closegraph();
}