#include #include #include"auxil.h" #include"CMUgraphics.h" #include"apvector.h" #include #include #include"RandGen.h" window window(900,600,0,0); bool collision_detection(int startx,int starty,int shotx,int shoty, int enemposx,int enemposy,int enemradius); void main() { RandGen rnum; int herospeed=8; int weapon=0; int cx,cy,shotx,shoty,startx,starty; int herohitpoints=450; int heroposx=250,heroposy=75; int purpleshotradius=35; int shootdelay=0;//animation purposes bool end=false,shot=false; //int enemposx[0]=425,enemposy[0]=300, enemradius[0]=15; //int enemhitpoints[0]=45; apvectorenemposx(3,425); apvectorenemposy(3,300); apvectorenemradius(3,15); apvectorenemhitpoints(3,45); apvectortouchleft(3,false); apvectortouchright(3,false); apvectortouchtop(3,false); apvectortouchbottom(3,false); apvectorkilldelay(3,enemradius[0]); apvectorenemcolor(3,RED); apvectorenemalive(3,true); enemcolor[1]=BLUE; enemradius[1]=35; enemposx[1]=600; enemposy[1]=120; killdelay[1]=enemradius[1]; enemcolor[2]=ORANGE; enemradius[2]=45; enemposx[2]=100; enemposy[2]=600; killdelay[2]=enemradius[2]; window.SetBuffering(true); window.ChangeTitle("GAME"); while(!end) { herohitpoints=herohitpoints; window.SetPen(BLACK); window.SetBrush(BLACK); window.DrawRectangle(0,0,900,600); window.SetPen(WHITE,2); window.DrawRectangle(100,75,750,525); if(weapon==0)//Draws weapon icon turret gun { window.SetPen(YELLOW,1); window.DrawLine(100,25,124,49); window.DrawLine(100,37,112,49); window.DrawLine(112,25,124,37); window.DrawLine(750,25,726,49); window.DrawLine(726,37,737,25); window.DrawLine(738,49,750,37); } else if(weapon==1)//Draws weapon icon purple gun { window.SetPen(PURPLE); window.SetBrush(PURPLE); window.DrawCircle(112,37,12); window.DrawCircle(738,37,12); } window.SetPen(WHITE,2); if(herohitpoints>350)//hitpoint color determination { window.SetBrush(DARKGREEN); } else if((herohitpoints<350)&&(herohitpoints>200)) { window.SetBrush(YELLOW); } else if((herohitpoints<200)&&(herohitpoints>50)) { window.SetBrush(DARKORANGE); } else if(herohitpoints<50) { window.SetBrush(DARKRED); } window.DrawRectangle(50,525-herohitpoints,102,525); window.DrawCircle(heroposx,heroposy,10); if(GetAsyncKeyState(0x41))//change to turret gun- button 'A' { weapon=1; shootdelay=-20; } if(GetAsyncKeyState(0x44))//change to purple gun- button 'D' { weapon=0; shootdelay=-40; } if(GetAsyncKeyState(0x25))//move counterclockwise- button 'left arrow' { if(heroposx>=750) { heroposy-=herospeed; heroposx=750; } if(heroposx<=100) { heroposy+=herospeed; heroposx=100; } if(heroposy<=75) { heroposx-=herospeed; heroposy=75; } if(heroposy>=525) { heroposx+=herospeed; heroposy=525; } } if(GetAsyncKeyState(0x27))//move clockwise - button 'right arrow' { if(heroposx>=750) { heroposy+=herospeed; heroposx=750; } if(heroposx<=100) { heroposy-=herospeed; heroposx=100; } if(heroposy<=75) { heroposx+=herospeed; heroposy=75; } if(heroposy>=525) { heroposx-=herospeed; heroposy=525; } } if(!shot)//if shot not in sequence { shot=window.GetButtonState(LEFT_BUTTON,cx,cy); if(shot) { if((cx>=100)&&(cx<=750)&&(cy>=75)&&(cy<=525))//in window { if(weapon==0)//creates shot inaccuracy for turret gun { cx+=rnum.RandInt(-.1*(heroposx-cx),.1*(heroposx-cx)); cy+=rnum.RandInt(-.1*(heroposy-cy),.1*(heroposy-cy)); } if(weapon==1)//shot inaccuracy for 'purple gun' { cx+=rnum.RandInt(-.05*(heroposx-cx),.05*(heroposx-cx)); cy+=rnum.RandInt(-.05*(heroposy-cy),.05*(heroposy-cy)); for(int i=cx;i-purpleshotradius-3<100;i++) {cx=i;} for(int i=cx;i+purpleshotradius+3>750;i--) {cx=i;} for(int i=cy;i-purpleshotradius-3<75;i++) {cy=i;} for(int i=cy;i+purpleshotradius+3>525;i--) {cy=i;} } } else shot=false; } } if(shot)//fires weapon { if(weapon==0)//turret gun { window.SetPen(YELLOW,1); if(shootdelay==0) { shotx=cx;shoty=cy;startx=heroposx;starty=heroposy; } if((shootdelay>=0) && (shootdelay<=3)) { window.DrawLine(startx,starty,startx+(.33*(shotx-startx)),starty+(.33*(shoty-starty))); } else if((shootdelay>=4)&&(shootdelay<=7)) { window.DrawLine(startx+(.2*(shotx-startx)),starty+(.2*(shoty-starty)),startx+(.8*(shotx-startx)),starty+(.8*(shoty-starty))); } else if((shootdelay>=8)&&(shootdelay<=11)) { window.DrawLine(startx+(.66*(shotx-startx)),starty+(.66*(shoty-starty)),shotx,shoty); } if(shootdelay==8) { for(int i=0;i=20) { shot=false; shootdelay=0; } } if(weapon==1)//'purple gun' { if(shootdelay<=purpleshotradius && shootdelay>0) { window.SetBrush(PURPLE); window.SetPen(PURPLE); window.DrawCircle(cx,cy,0-shootdelay); } if(shootdelay>=75) { shot=false; shootdelay=0; } shootdelay+=1; } } else { shootdelay=0; shot=false; } for(int i=0; i0)//move enemy { if(touchright[i]) { enemposx[i]-=25*i; } touchleft[i]=false,touchright[i]=false,touchtop[i]=false,touchbottom[i]=false; for(int j=0; j0) { if(enemposx[i]=enemposx[j]-enemradius[j]) { touchright[i]=true; } } else//j to left { if(enemposx[i]-enemradius[i]<=enemposx[j]+enemradius[j]) { touchleft[i]=true; } } if(enemposy[i]=enemposy[j]-enemradius[j]) { touchbottom[i]=true; } } else//j on top { if(enemposy[i]-enemradius[i]<=enemposy[j]+enemradius[j]) { touchtop[i]=true; } } } } if((heroposx100)&&(!touchleft[i])) { enemposx[i]-=2; } else if((heroposx>enemposx[i])&&(enemposx[i]+enemradius[i]+3<750)&&(!touchright[i])) { enemposx[i]+=2; } if((heroposy75)&&(!touchtop[i])) { enemposy[i]-=2; } else if((heroposy>enemposy[i])&&(enemposy[i]+enemradius[i]+3<525)&&(!touchbottom[i])) { enemposy[i]+=2; } } if((enemhitpoints[i]<=0) && (killdelay[i] >=0))//death sequence { killdelay[i]--; } if (enemhitpoints[i]>0) { window.SetBrush(enemcolor[i]); window.SetPen(enemcolor[i]); window.DrawRectangle(enemposx[i]-enemradius[i],enemposy[i]-enemradius[i], enemposx[i]+enemradius[i],enemposy[i]+enemradius[i]); } } window.UpdateBuffer(); Pause(0); } } //======================================================================= //======================================================================= //======================================================================= //======================================================================= //======================================================================= bool collision_detection(int startx,int starty,int shotx,int shoty, int enemposx,int enemposy,int enemradius) { bool hit=false; if(shotx==startx)//verticle shots { if(starty=enemposy-enemradius) { hit=true; } } } else //shot up { for(int y=starty; y>75; y--) { if(y<=enemposy+enemradius && y>=enemposy-enemradius) { hit=true; } } } } else { if(starty==shoty)//horizontal shots { if(startx=enemposx-enemradius) { hit=true; } } } else //shot left { for(int x=startx; x>100; x--) { if(x<=enemposx+enemradius && x>=enemposx-enemradius) { hit=true; } } } } else //diagonal shots { double slope; slope=(double(starty-shoty))/(double(startx-shotx)); int B=-(slope*startx)+starty;// intercept b=-mx + y if(startx65 && !hit) { y=slope*x+B; if(x<=enemposx+enemradius && x>=enemposx-enemradius && y<=enemposy+enemradius && y>=enemposy-enemradius) {hit=true;} x++; } } else //shot left diagonal { int x=startx, y=starty; while(x>90 && y<535 && y>65 && !hit) { y=slope*x+B; if(x<=enemposx+enemradius && x>=enemposx-enemradius && y<=enemposy+enemradius && y>=enemposy-enemradius) {hit=true;} x--; } } } } if(hit) return true; else return false; }