123 Eng

Engineering the engineers™


Latest Jobs   Forum Map

 


Home
Source Codes
Engineering Colleges

Training  Reports
Seminar Reports
Placement Papers

Forums

   Computer Science / IT
   Electronics
   Electrical
   Mechanical
   Chemical
   Civil

   CAT / MBA

   GMAT / Foreign MBA
Latest Jobs

Engineering Jobs / Technical Jobs
Management Jobs

Sitemap
Terms of use

Displaying  Source Code(s)  
 

 
Space Jam (in text mode) (Mini Project)

--------------------------------------------------------------------------------

Description : Player is in a plane, can shoot the enemy planes using bullets(left click) or laser(right click), using mouse interface.

Code :

#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
#include<string.h>

int
shots=0,now_x,now_y,player_life=200,num_of_ships=0,blt_cntr=0,op_blt=0,op_
msl=0,level=0;
long score=0;

struct{
int type;
int x_coord;
int y_coord;
int life;
int speed;
}ships[15];

struct{
int x;
int y;
}bullets[50],opp_bullets[100],msl[50];

void create_ships(){
int j;
ships[num_of_ships].type=random(level);
ships[num_of_ships].y_coord=2;
if(level<6){
do{
ships[num_of_ships].speed=random(3);
}while(ships[num_of_ships].speed<1);
}
else
ships[num_of_ships].speed=2;
do{
ships[num_of_ships].x_coord=random(53);
for(j=0;j<15;j++)

if(ships[j].life>0&&ships[j].y_coord==2&&abs(ships[num_of_ships].x_coord-s
hips[j].x_coord)<1)
break;
}while(ships[num_of_ships].x_coord<2||j<15);
do{
ships[num_of_ships].life=random(20);
}while(!ships[num_of_ships].life);
num_of_ships=(num_of_ships+1)%15;
}

void opp_shoot(int i){
if(ships[i].type<2||ships[i].type==4){
opp_bullets[op_blt].x=ships[i].x_coord+1;
opp_bullets[op_blt].y=ships[i].y_coord+1;
op_blt=(op_blt+1)%100;
}
else{
msl[op_msl].x=ships[i].x_coord+1;
msl[op_msl].y=ships[i].y_coord+1;
op_msl=(op_msl+1)%50;
}
}

void disp_ships(){
int i;
for(i=0;i<15;i++){
if(ships[i].life>0){
gotoxy(ships[i].x_coord,ships[i].y_coord-1);
printf(" ");
if(ships[i].y_coord<24){
gotoxy(ships[i].x_coord,ships[i].y_coord);
printf(" %c ",0xb1);
gotoxy(ships[i].x_coord+1,ships[i].y_coord+1);
printf(" ");
}
else{
gotoxy(ships[i].x_coord+1,ships[i].y_coord);
printf(" ");
ships[i].life=0;
}
if(!random(5))
opp_shoot(i);
}
}
}

void shoot(){
bullets[shots].x=now_x+2;
bullets[shots].y=now_y-2;
shots=(shots+1)%50;
}

void print_op_bullets(){
int i;
for(i=0;i<100;i++){
if(opp_bullets[i].y<25){
gotoxy(opp_bullets[i].x,opp_bullets[i].y-1);
printf(" ");
gotoxy(opp_bullets[i].x,opp_bullets[i].y);
printf(".");
}
else if(opp_bullets[i].y==25){
gotoxy(opp_bullets[i].x,24);
printf(" ");
}
}
}

void print_msl(){
int i;
for(i=0;i<50;i++){
if(msl[i].y<25){
gotoxy(msl[i].x,msl[i].y-1);
printf(" ");
gotoxy(msl[i].x,msl[i].y);
printf("|");
}
else if(msl[i].y==25){
gotoxy(msl[i].x,24);
printf(" ");
}
}
}

void print_bullets(){
int i;
for(i=0;i<50;i++)
if(bullets[i].y>0){
if(bullets[i].y>1){
gotoxy(bullets[i].x,bullets[i].y+1);
printf(" ");
gotoxy(bullets[i].x,bullets[i].y);
printf(".");
}
else if(bullets[i].y==1){
gotoxy(bullets[i].x,2);
printf(" ");
}
bullets[i].y--;
}
}

void del_plane(int c, int d){
gotoxy(c,d);
printf(" ");
gotoxy(c+2,d-1);
printf(" ");
}

void chk_opp_life(){
int i,j;
for(i=0;i<15;i++)
if(ships[i].life>0)
for(j=0;j<50;j++)
if(bullets[j].y>1)

if(ships[i].x_coord<=bullets[j].x&&bullets[j].x<=ships[i].x_coord+2)

if(ships[i].y_coord==bullets[j].y||bullets[j].y==ships[i].y_coord+1){
gotoxy(ships[i].x_coord,ships[i].y_coord);
printf(" ");
gotoxy(ships[i].x_coord+1,ships[i].y_coord+1);
printf(" ");
ships[i].life-=2;
if(ships[i].type%2){
if(random(2)&&ships[i].x_coord<52)
ships[i].x_coord++;
else if(ships[i].x_coord>2)
ships[i].x_coord--;
}
gotoxy(bullets[j].x,bullets[j].y+1);
printf(" ");
bullets[j].y=0;
if(ships[i].life<1){
score+=(10+(5*ships[i].type));
if(ships[i].speed==2)
score+=5;
}
}
}

void chk_plyr_life(){
int i,j,k;
for(i=0;i<100;i++)
if(opp_bullets[i].y<25){
j=opp_bullets[i].x;
if(j>=now_x&&j<=now_x+4){
j=opp_bullets[i].y;
if(j==now_y){
player_life-=2;
gotoxy(opp_bullets[i].x,opp_bullets[i].y-1);
printf(" ");
opp_bullets[i].y=26;
}
else if(j==now_y-1&&opp_bullets[i].x==now_x+2){
player_life-=2;
gotoxy(opp_bullets[i].x,opp_bullets[i].y-1);
printf(" ");
opp_bullets[i].y=26;
}
}
}
for(i=0;i<50;i++)
if(msl[i].y<25){
j=msl[i].x;
if(j>=now_x&&j<=now_x+4){
j=msl[i].y;
if(j==now_y){
player_life-=5;
gotoxy(msl[i].x,msl[i].y-1);
printf(" ");
msl[i].y=26;
}
else if(j==now_y-1&&msl[i].x==now_x+2){
player_life-=5;
gotoxy(msl[i].x,msl[i].y-1);
printf(" ");
msl[i].y=26;
}
}
}
for(i=0;i<15;i++)
if(ships[i].life>0){
j=ships[i].y_coord,k=ships[i].x_coord;
if((k<=now_x&&now_x<=k+2)||(now_x<=k&&k<=now_x+4))
if((j<=now_y&&now_y<=j+1)||(now_y<=j&&j<=now_y+1)){
player_life-=(10+(2*ships[i].type));
ships[i].life=0;
score+=(10+(5*ships[i].type));
if(ships[i].speed==2)
score+=5;
gotoxy(k,j);
printf(" ");
gotoxy(k+1,j+1);
printf(" ");
}
}
}

void laser(){
int i;
for(i=2;i<now_y-1;i++){
gotoxy(now_x+2,i);
printf("%c",186);
}
}

int rem_laser(int las_cntr){
int i;
for(i=2;i<now_y-1;i++){
gotoxy(now_x+2,i);
printf(" ");
}
i=(las_cntr+1)%100;
return i;
}

void chk_laser(){
int i;
for(i=0;i<15;i++)

if(ships[i].life>0&&(now_x+2>=ships[i].x_coord&&now_x+2<=ships[i].x_coord+
2)&&now_y-1>ships[i].y_coord+1){
gotoxy(ships[i].x_coord,ships[i].y_coord);
printf(" ");
gotoxy(ships[i].x_coord+1,ships[i].y_coord+1);
printf(" ");
ships[i].life=0;
score+=(10+(5*ships[i].type));
if(ships[i].speed==2)
score+=5;
}
}

void foll_plyr(){
int i,dist;
for(i=0;i<15;i++)
if(ships[i].type>3&&ships[i].y_coord<23){
dist=now_x-ships[i].x_coord;
if(dist){
ships[i].x_coord+=(dist/abs(dist));
if(dist<0){
gotoxy(ships[i].x_coord+3,ships[i].y_coord);
printf(" ");
gotoxy(ships[i].x_coord+2,ships[i].y_coord+1);
printf(" ");
}
else{
gotoxy(ships[i].x_coord-1,ships[i].y_coord);
printf(" ");
gotoxy(ships[i].x_coord,ships[i].y_coord+1);
printf(" ");
}
}
}
}

main(){
FILE *fp;
int
b4_x,b4_y,click=0,i,ship_cntr=0,crt_ships,blt_cntr=0,las_cntr=0,charge=0,j
,k,lvl_arr[10];
long scr_cntr=200,lvl_scr=0,scr_arr[10];
char *plyrnm,buf[5],buf_arr[10][5];
b4_x=35,b4_y=12;
clrscr();
if((fp=fopen("spcj_scr.txt","r+"))!=NULL){
i=1;
while(fscanf(fp,"%s%ld%d",plyrnm,&score,&j)!=EOF)
if(j>i)
i=j;
fclose(fp);
if(i>1){
printf("Enter the level, u wanna start from:(less than %d)",i+1);
do{
scanf("%d",&level);
if(level<0||level>i)
printf("
Entry should be valid:");
}while(level<0||level>i);
level--;
}
}
score=0;
for(i=0;i<level;i++)
score+=(10000+(500*level)+((player_life/100)*50));
lvl_scr=score;
scr_cntr=score;
for(i=0;i<level;i++)
scr_cntr+=(25*i);
for(i=0;i<player_life;i+=100)
scr_cntr+=50;
clrscr();
for(i=0;i<15;i++)
ships[i].life=0;
for(i=0;i<50;i++)
bullets[i].y=0;
for(i=0;i<100;i++)
opp_bullets[i].y=26;
for(i=0;i<50;i++)
msl[i].y=26;
printf("%c",201);
for(i=2;i<55;i++)
printf("%c",205);
printf("%c",187);
for(i=2;i<25;i++){
gotoxy(1,i);
printf("%c",186);
gotoxy(55,i);
printf("%c",186);
}
gotoxy(1,25);
printf("%c",200);
for(i=2;i<55;i++)
printf("%c",205);
printf("%c",188);
randomize();
while(player_life>0){
if(kbhit()&&getch()==27)
asm jmp label;
if(score>=lvl_scr&&level<6){
level++;
if(level<6){
lvl_scr+=(10000+(500*level)+((player_life/100)*50));
gotoxy(57,20);
printf("NXT LVL CHNG AT:%ld",lvl_scr);
}
else{
gotoxy(57,20);
printf("LAST LEVEL!! ");
}
gotoxy(57,23);
printf("PRESS ENTER TO CONTINUE");
do{
gotoxy(57,19);
printf(" ");
delay(300);
gotoxy(57,19);
printf("LEVEL: %d",level);
delay(300);
}while(!kbhit()||getch()!=13);
}
if(!(ship_cntr%30)){
crt_ships=random(3);
for(i=0;i<crt_ships;i++)
create_ships();
ship_cntr=0;
}
if(score>=scr_cntr){
player_life+=100;
scr_cntr+=200;
for(i=0;i<player_life;i+=100)
scr_cntr+=50;
for(i=0;i<level;i++)
scr_cntr+=(25*i);
}
asm{
mov ax, 3;
int 33h;
mov click, bx;
mov now_x, cx;
mov now_y, dx
}
now_x=(now_x/8)+1,now_y=(now_y/8)+2;
if(now_x>50)
now_x=50;
else if(now_x==1)
now_x=2;
if(now_y>24)
now_y=24;
else if(now_y<3)
now_y=3;
if(b4_x!=now_x||b4_y!=now_y){
del_plane(b4_x,b4_y);
b4_x=now_x,b4_y=now_y;
gotoxy(b4_x,b4_y);
printf(" %c%c%c ",0xb1,0xb1,0xb1);
gotoxy(b4_x+2,b4_y-1);
printf(" ");
}
chk_plyr_life();
chk_opp_life();
print_op_bullets();
print_bullets();
print_msl();
if(click==1)
shoot();
else if(click==2&&!charge)
las_cntr++,charge++;
if(las_cntr&&click==2&&charge==1){
laser();
chk_laser();
}
else if(las_cntr&&!click&&charge==1)
las_cntr=0;
if(blt_cntr){
for(i=0;i<100;i++)
if(opp_bullets[i].y<26)
opp_bullets[i].y++;
for(i=0;i<50;i++)
if(msl[i].y<26)
msl[i].y++;
}
for(i=0;i<15;i++)
if(ships[i].life>0&&ship_cntr%(10/ships[i].speed)==0)
ships[i].y_coord++;
disp_ships();
delay(40);
if(level>4)
foll_plyr();
if(las_cntr)
las_cntr=rem_laser(las_cntr);
else if(charge)
charge=(charge+1)%100;
ship_cntr++,blt_cntr=(blt_cntr+1)%2;
gotoxy(57,2);
printf("SCORE : %ld ",score);
gotoxy(57,5);
printf("LIFE : %d ",player_life);
gotoxy(57,10);
printf("NEXT LIFE ENHANCEMENT ");
gotoxy(57,11);
printf("AT SCORE:%ld",scr_cntr);
gotoxy(57,15);
printf("LASER AVAILABLE");
gotoxy(57,23);
printf("PRESS ESC KEY TO EXIT!! ");
if(charge>1&&!(click==2&&las_cntr)){
gotoxy(63,15);
printf("NOT");
}
}
label:
gotoxy(57,23);
printf("GAME OVER!!! ");
getch();
clrscr();
fp=fopen("spcj_scr.txt","r+");
if(fp==NULL){
fp=fopen("spcj_scr.txt","w+");
if(fp==NULL){
printf("ERROR!!");
exit(-1);
}
}
fseek(fp,0,SEEK_SET);
clrscr();
i=0,j=-1;

while(fscanf(fp,"%s%ld%d",buf_arr[i],&scr_arr[i],&lvl_arr[i])!=EOF&&i<10){
if(score<scr_arr[i])
j=i;
i++;
}
rewind(fp);
for(k=0;k<=j;k++)
fprintf(fp,"%s %ld %d<BR>,buf_arr[k],scr_arr[k],lvl_arr[k]);
if(k<10){
printf("You get a chance!!");
do{
printf("
Enter your name(4 letters only!): ");
scanf("%s",plyrnm);
}while(strlen(plyrnm)>4);
fprintf(fp,"%s %ld %d",plyrnm,score,level);
}
else
printf("SORRY.... U DON'T GET A CHNC!!!");
printf("
Your Score is : %ld",score);
getch();
for(k=j+1;(k<9&&k<i&&k>-1);k++)
fprintf(fp,"
%s %ld %d",buf_arr[k],scr_arr[k],lvl_arr[k]);
clrscr();
printf("The 10 highest scorers are:

<BR>);
printf("PLAYERS SCORE LEVEL
<BR>);
rewind(fp);
while(fscanf(fp,"%s%ld%d",buf,&score,&level)!=EOF)
printf("%s %ld %d<BR>,buf,score,level);
getch();
fclose(fp);
return 0;
}

 

 

 

Contribute content or training reports / feedback / Comments
job placement papers
All rights reserved © copyright 123ENG