case 'a':
if (x>5){
erase_ship(x,y);
x -= 7;
draw_ship(x,y);
}
ship(x,y);
break;
case 'd':
if (x<72){
erase_ship(x,y);
x += 7;
draw_ship(x,y);
}
ship(x,y);
break;
Printable View
case 'a':
if (x>5){
erase_ship(x,y);
x -= 7;
draw_ship(x,y);
}
ship(x,y);
break;
case 'd':
if (x<72){
erase_ship(x,y);
x += 7;
draw_ship(x,y);
}
ship(x,y);
break;
Anyways - I think I got all the errors fixed:
The only problem is that I can't move/shoot while the ship on the left scrolls down.
Code:#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <dos.h>
int x = 37;
int y = 25;
int posex = 5;
int posey = 1;
char b;
void ship (int x, int y);
void shoot (int x, int y);
void menu ();
void salida ();
void centership(int x, int y);
void borrar(int x, int y);
void bala(int x, int y);
void draw_ship(int x, int y);
void erase_ship(int x, int y);
void draw_enemy(int x, int y);
void erase_tail(int x, int y);
void scroll (int x, int y);
void erase_tail(int x, int y){
delay(200);
gotoxy(x,y);
printf(" ");
}
void draw_enemy(int x, int y){
gotoxy(x,y);
printf("VvvvvvV");
gotoxy(x,y+1);
printf(" VvvvV ");
gotoxy(x,y+2);
printf(" VvV ");
gotoxy(x,y+3);
printf(" V ");
}
void scroll(int x, int y){
int i = 2;
while (i<23){
draw_enemy(x,i);
erase_tail(x,i);
i++;
}
}
void salida(){
clrscr();
delay(500);
printf("Gracias por jugar\n");
exit(0);
}
void erase_ship(int x, int y){
gotoxy(x,y);
printf(" ");
gotoxy(x,y-1);
printf(" ");
gotoxy(x,y-2);
printf(" ");
gotoxy(x,y-3);
printf(" ");
}
void draw_ship(int x, int y){
gotoxy(x,y);
printf("*-----*");
gotoxy(x,y-1);
printf(" *---* ");
gotoxy(x,y-2);
printf(" *-* ");
gotoxy(x,y-3);
printf(" * ");
}
void borrar_bala(int x, int y){
int i=21;
delay(15);
while (i>0){
int ejex = x;
gotoxy(ejex+3,i);
printf(" ");
delay (7);
i--;
}
ship(x,y);
}
void bala (int x, int y){
int i=21;
while (i>0){
int ejex = x;
gotoxy(ejex+3,i);
printf("|");
delay (14);
i--;
}
borrar_bala(x,y);
}
void shoot(int x, int y){
bala(x,y);
}
void centership(int x, int y){
clrscr();
gotoxy(x,y);
printf("*-----*");
gotoxy(x,y-1);
printf(" *---* ");
gotoxy(x,y-2);
printf(" *-* ");
gotoxy(x,y-3);
printf(" * ");
ship(x,y);
delay(500);
scroll(posex,posey);
}
void ship(int x, int y){
int exit_loop = 0;
do{
if(kbhit() == 0){
b = getch();
switch(b){
case 'a':
if (x>5){
erase_ship(x,y);
x -= 7;
draw_ship(x,y);
}
break;
case 'd':
if (x<72){
erase_ship(x,y);
x += 7;
draw_ship(x,y);
}
break;
case 's':
shoot(x,y);
break;
case 'm':
menu();
break;
case 'z':
salida();
break;
default:
ship(x,y);
}
}
}while(exit_loop != 0);
}
void menu(){
clrscr();
gotoxy(22,10);
printf("Izquierda A\n");
gotoxy(22,11);
printf("Disparar S\n");
gotoxy(22,12);
printf("Derecha D\n");
gotoxy(22,13);
printf("Menu M\n");
gotoxy(22,14);
printf("Salir Z\n");
gotoxy(22,16);
printf("Para empezar a jugar, presione E\n");
gotoxy(22,17);
printf("Para salir, presione Z\n");
b = getch();
switch (b){
case 'e':
centership(x,y);
break;
case 'z':
salida();
break;
default:
menu();
}
}
void main(){
menu();
}
when I compiled ur code it throws out a error saying that gotoxy was declared, and another one saying that clear screen commmand was not declared....
I complied it using VC++ 6.0
clrscr is only availabe in borland c++
and i think gotoxy is a reserved variable in VC++
I compiled it in borland turbo c++ and it compiled fine
Check this out B)
So far so good. 80% Done :)Code:#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <dos.h>
#include <time.h>
#define ENEMY_WAIT 1
void shoot (int x, int y);
void menu ();
void salida ();
void draw_ship(int x, int y);
void erase_ship(int x, int y);
void draw_enemy(int x, int y);
void erase_enemy(int x, int y);
void scroll (int x, int y);
void erase_enemy(int x, int y){
//delay(200);
int index;
for(index=0;index<4;index++){
gotoxy(x,y+index);
printf(" ");
}
}
void draw_enemy(int x, int y){
gotoxy(x,y);
printf("VvvvvvV");
gotoxy(x,y+1);
printf(" VvvvV ");
gotoxy(x,y+2);
printf(" VvV ");
gotoxy(x,y+3);
printf(" V ");
}
void salida(){
clrscr();
//sleep(500);
printf("Gracias por jugar\n");
exit(0);
}
void erase_ship(int x, int y){
gotoxy(x,y);
printf(" ");
gotoxy(x,y-1);
printf(" ");
gotoxy(x,y-2);
printf(" ");
gotoxy(x,y-3);
printf(" ");
}
void draw_ship(int x, int y){
gotoxy(x,y);
printf("*-----*");
gotoxy(x,y-1);
printf(" *---* ");
gotoxy(x,y-2);
printf(" *-* ");
gotoxy(x,y-3);
printf(" * ");
}
void borrar_bala(int x, int y){
int i=21;
while (i>0){
int ejex = x;
gotoxy(ejex+3,i);
printf(" ");
i--;
}
}
void shoot(int x, int y){
int i=21;
while (i>0){
int ejex = x;
gotoxy(ejex+3,i);
printf("|");
delay (5);
i--;
}
borrar_bala(x,y);
}
void game_loop(){
int exit_loop = 0;
int x = 35;
int y = 25;
int posex;
int posey = 1;
char b;
time_t set_time;
time_t check_time;
time(&set_time);
srand((int) set_time);
posex = ( (rand()%10) + 1 ) * 7;
clrscr();
draw_ship(x,y);
do{
if(kbhit()){
b = getch();
switch(b){
case 'a':
if (x>7){
erase_ship(x,y);
x-=7;
draw_ship(x,y);
}
break;
case 'd':
if (x<70){
erase_ship(x,y);
x+=7;
draw_ship(x,y);
}
break;
case 's':
shoot(x,y);
break;
case 'm':
menu();
break;
case 'z':
salida();
break;
//default:
//ship(x,y);
}
}
time(&check_time);
if( (check_time-set_time) >= ENEMY_WAIT){
set_time = check_time;
erase_enemy(posex,posey);
posey+=4;
if(posey <= 20)
draw_enemy(posex,posey);
else{
posey = 1;
posex = ( (rand()%10) + 1 ) * 7;
}
}
}while(!exit_loop);
}
void menu(){
char b;
clrscr();
gotoxy(22,10);
printf("Izquierda A\n");
gotoxy(22,11);
printf("Disparar S\n");
gotoxy(22,12);
printf("Derecha D\n");
gotoxy(22,13);
printf("Menu M\n");
gotoxy(22,14);
printf("Salir Z\n");
gotoxy(22,16);
printf("Para empezar a jugar, presione E\n");
gotoxy(22,17);
printf("Para salir, presione Z\n");
b = getch();
switch (b){
case 'e':
game_loop();
break;
case 'z':
salida();
break;
default:
menu();
}
}
int main(){
menu();
}
looking good
the ship can now be hit
Code:#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <dos.h>
#include <time.h>
#define ENEMY_WAIT 1
int hit;
int hity;
int posey = 1;
void shoot (int x, int y);
void menu ();
void salida ();
void draw_ship(int x, int y);
void erase_ship(int x, int y);
void draw_enemy(int x, int y);
void erase_enemy(int x, int y);
void scroll (int x, int y);
void erase_enemy(int x, int y){
//delay(200);
int index;
for(index=0;index<4;index++){
gotoxy(x,y+index);
printf(" ");
}
}
void draw_enemy(int x, int y){
gotoxy(x,y);
hit = x;
hity = y;
printf("VvvvvvV");
gotoxy(x,y+1);
printf(" VvvvV ");
gotoxy(x,y+2);
printf(" VvV ");
gotoxy(x,y+3);
printf(" V ");
}
void salida(){
clrscr();
//sleep(500);
printf("Gracias por jugar\n");
exit(0);
}
void erase_ship(int x, int y){
gotoxy(x,y);
printf(" ");
gotoxy(x,y-1);
printf(" ");
gotoxy(x,y-2);
printf(" ");
gotoxy(x,y-3);
printf(" ");
}
void draw_ship(int x, int y){
gotoxy(x,y);
printf("*-----*");
gotoxy(x,y-1);
printf(" *---* ");
gotoxy(x,y-2);
printf(" *-* ");
gotoxy(x,y-3);
printf(" * ");
}
void borrar_bala(int x, int y){
int i=21;
while (i>0){
int ejex = x;
gotoxy(ejex+3,i);
printf(" ");
i--;
}
}
void shoot(int x, int y){
int i=21;
while (i>0){
int ejex = x;
gotoxy(ejex+3,i);
printf("|");
delay (5);
i--;
}
if (x==hit){
erase_enemy(x,hity);
posey=18;
}
borrar_bala(x,y);
}
void game_loop(){
int exit_loop = 0;
int x = 35;
int y = 25;
int posex;
char b;
time_t set_time;
time_t check_time;
time(&set_time);
srand((int) set_time);
posex = ( (rand()%10) + 1 ) * 7;
clrscr();
draw_ship(x,y);
do{
if(kbhit()){
b = getch();
switch(b){
case 'a':
if (x>7){
erase_ship(x,y);
x-=7;
draw_ship(x,y);
}
break;
case 'd':
if (x<70){
erase_ship(x,y);
x+=7;
draw_ship(x,y);
}
break;
case 's':
shoot(x,y);
break;
case 'm':
menu();
break;
case 'z':
salida();
break;
//default:
//ship(x,y);
}
}
time(&check_time);
if( (check_time-set_time) >= ENEMY_WAIT){
set_time = check_time;
erase_enemy(posex,posey);
posey+=4;
if(posey <= 20)
draw_enemy(posex,posey);
else{
posey = 1;
posex = ( (rand()%10) + 1 ) * 7;
}
}
}while(!exit_loop);
}
void menu(){
char b;
clrscr();
gotoxy(22,10);
printf("Izquierda\tA\n");
gotoxy(22,11);
printf("Disparar\tS\n");
gotoxy(22,12);
printf("Derecha\t\tD\n");
gotoxy(22,13);
printf("Menu\t\tM\n");
gotoxy(22,14);
printf("Salir\t\tZ\n");
gotoxy(22,16);
printf("Para empezar a jugar, presione E\n");
gotoxy(22,17);
printf("Para salir, presione Z\n");
b = getch();
switch (b){
case 'e':
game_loop();
break;
case 'z':
salida();
break;
default:
menu();
}
}
int main(){
menu();
return 0;
}