care to explain that part please ? with the number of enemies :) I'm having a hard time making more than one on the screen at a time .. help is greatly appreciated :)
Main Topics
Browse All TopicsThis is what I got working so far .. next problem is .. I'd like an event when my ship hits an enemy ship .. also .. I'd like a #defined number of enemies on my screen at all times .. I don't know how to do any of the above things .. any example or modifications on my lil thingy are welcome..
#include <stdio.h>
#include <dos.h>
#include <graphics.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#define PAS 20
struct time t;
int keye=250;
int coordX=0;
int coordXe=0;
int coordY=0;
int coordYe=0;
int tmp=0;
int tmp2=0;
int i;
void moveMyShipLeft(void);
void moveMyShipRight(void);
void moveMyShipUp(void);
void moveMyShipDown(void);
void desenMyShip(int, int, int);
void getConflict(int coordX, int coordXe, int coordY, int coordYe){
int stuffz,stuffy;
stuffz=coordX;
}
int getSecTime(void){
gettime(&t);
return t.ti_hund;
}
int pressedKey(void){
keye=getch();
return(keye);
}
void desenEnemyShip(int coordXe, int coordYe, int culoare){
setfillstyle(1,culoare);
bar(0+coordXe,0+coordYe,60
}
void initDefault(void){
}
void desenMyShip(int coordX, int coordY, int culoare){
setfillstyle(1,culoare);
bar(coordX,coordY,60+coord
getConflict(coordX,coordY,
}
void moveMyShip(void){
if ((keye==75) && coordX > getmaxx()-1023){
moveMyShipLeft();
}
if ((keye==77) && coordX < getmaxx()-60){
moveMyShipRight();
}
if ((keye==80) && coordY < getmaxy()-40){
moveMyShipUp();
}
if ((keye==72) && coordY > getmaxy()-767){
moveMyShipDown();
}
}
void moveEnemyShips(int coordXe, int coordYe, int culoare){
desenEnemyShip(coordXe,coo
coordYe=coordYe+1;
desenEnemyShip(coordXe,coo
tmp=getSecTime();
}
void initEnamies(void){
}
void moveMyShipLeft(void){
desenMyShip(coordX,coordY,
coordX=coordX-PAS;
desenMyShip(coordX,coordY,
}
void moveMyShipRight(void){
desenMyShip(coordX,coordY,
coordX=coordX+PAS;
desenMyShip(coordX,coordY,
}
void moveMyShipUp(void){
desenMyShip(coordX,coordY,
coordY=coordY+PAS;
desenMyShip(coordX,coordY,
}
void moveMyShipDown(void){
desenMyShip(coordX,coordY,
coordY=coordY-PAS;
desenMyShip(coordX,coordY,
}
int huge DetectVGA256(void){
int vid=4;
return vid;
}
void doGame(void){
initDefault();
coordYe = 1000;
do{
if (coordYe > 800){
coordXe = rand() % 1023;
coordYe = 40;
}
if (kbhit()){
pressedKey();
moveMyShip();
}
desenMyShip(coordX,coordY,
if (getSecTime()+10 != tmp+10){
desenEnemyShip(coordXe,coo
moveEnemyShips(coordXe,coo
coordYe=coordYe+15;
}
}while (keye != 27);
}
void main(void){
int gdriver, gmode, errorcode;
gdriver = installuserdriver("svga256
gdriver = DETECT;
initgraph(&gdriver, &gmode, "..\bgi\svga256.bgi");
errorcode = graphresult();
if (errorcode != grOk){
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
doGame();
}
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Do research on arrays.
You should have an array of a struct for your ships:
a struct is a logical collection of data, a little bit into OOP.
eg:
STRUCT FIELDS:
coordX coordY Health etc.
ship 1 0 0 100
ship 2 5 0 100
ship 3 0 5 100
for clarity I would keep ship 1 as your ship, 2 thru N as enemy ships. (If more than one human player, create a variable num human players and keep them as low as possible)
the reason your having a hard time making more than one is because you only have 2 variables for keeping track of the enemy ship.
Is there really any difference between enemy ship or your ship?
I would redo some of the code like this
//create a struct for the ship. Because this is C there are no classes.
struct ship
{
int coordX;
int coordY;
int health;
//any other things you want to keep track of
};
//array for number of ships
#define max_ships 10
ships[max_ships];
assuming your ship is ship 0, 1-9 are possible enemy ships.
MoveShipRight(int ship_num)
{
//example of right, you'll need to do rest.
ships[ship_num].coordX = ships[ship_num].coordX + 1;
detect_colision(ship_num);
}
void detect_collision(int ship_num)
{
for (int i = 0; i < max_ships; i++)
{
if (i == ship_num)
continue;
//else
if (ships[i].coordX == ships[ship_num].CoordX &&
ships[i].coordY == ships[ship_num].coordY)
{
// COLLISION
}
} // for
// no colision . . . contine on
}
in your doGame()
if (kbhit()){
pressedKey();
/*
OPTIONAL todo:
can add if's here to determine multipe set of keys, eg: player one a,w,s,d
and player 2 j,i,k,l
if(a,w,s,d pressed)
moveship(0)
if (j,i,k,l)
moveship(1)
*/
moveShip(0); //keyboard interupt, human ship is being moved.
}
Note: for games like these you usually have a menu first before you begin playing.
EG:
==========================
(initialization phase)
select # of human players (1,2)?
select # of enemies (= max_ships - human players)
. . . other options . . . sound, keyboard keys, etc.
<< press enter to begin >>
==========================
(event based "GAME" part)
call dogame();
==========================
Business Accounts
Answer for Membership
by: ged325Posted on 2007-01-04 at 11:09:38ID: 18245572
for the event when your ship collides with an enemy:
Add a function
void detect_collision()
{
if (coordX==int coordXe && coordY=0 int coordYe=0)
{
//collision
}
}
add a call to this function at the end of functions moveenemyships() and movemyship()
there should be some kind of drawstring that you can use to display the number of enemies. You might just want to make that a public int rather then a #define.