How to reuse this code? don't want to wright it 1000 times - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# How to reuse this code? don't want to wright it 1000 times

Posts: 6Member
I have the player's car with his own speed N.
N goes from 1 to 15(15 = max). So I create another car with a T speed(the max speed of T is 8). Then...

if (n==0) t+=something;
if (n==1) t+=something less;
.
.
.
if (n==15)t-= something.

So I have this other car in the screen with his own speed.

And the Crash:

if ( (x,y+n,..) of the first car == (x,y+h,..) of the second) )
crash; // (this is not the real code, this is the way it works)

So, that car has own speed and can crash with the main car from any side.

finally:

the (x,y,x,y) coordinates of the car in the screen.
//it sould be at the begining

NOW:

I want to put many cars in the screen, and I dont want to repeat all that code for each of them. T has a relation with N
so I cant use it again, (if I use it, all the cars will move the same). And if I use a new variable for each car I should type this code 1000 times.(or maybe not?)

So, How can I reuse this code with other cars?

And Thanks!
Juan.

• Posts: 233Member
Yo dude,

ever heard of FUNCTIONS? A function can be called as much as you want! See the helpfile of you're compiler for more info!!!

• Posts: 18Member
: I have the player's car with his own speed N.
: N goes from 1 to 15(15 = max). So I create another car with a T speed(the max speed of T is 8). Then...
:
: if (n==0) t+=something;
: if (n==1) t+=something less;
: .
: .
: .
: if (n==15)t-= something.
:
: So I have this other car in the screen with his own speed.
:
: And the Crash:
:
: if ( (x,y+n,..) of the first car == (x,y+h,..) of the second) )
: crash; // (this is not the real code, this is the way it works)
:
: So, that car has own speed and can crash with the main car from any side.
:
: finally:
:
: the (x,y,x,y) coordinates of the car in the screen.
: //it sould be at the begining
:
: NOW:
:
: I want to put many cars in the screen, and I dont want to repeat all that code for each of them. T has a relation with N
: so I cant use it again, (if I use it, all the cars will move the same). And if I use a new variable for each car I should type this code 1000 times.(or maybe not?)
:
: So, How can I reuse this code with other cars?
: Sorry about my prehistorik english
:
: And Thanks!
: Juan.
:

First of all,your speed-algorithm should be optimized...
Make the car as object.
Then create an array, and put use loop to put all the objects in it.
After that its easy to set speed, direction etc. to all of them. with one AI code.

Easy as that eh?

• Posts: 297Member
In an OOP language such as C++, VC++, Delphi, etc I would make a class Car that would contain all functions that belong to every car, and variables and constants. Then if certain cars need more details functions, etc derive a subclass.

If you arn't using OOP, then I would look at functions and linked lists to store the information on each car

Hope this helps
Ben

: I have the player's car with his own speed N.
: N goes from 1 to 15(15 = max). So I create another car with a T speed(the max speed of T is 8). Then...
:
: if (n==0) t+=something;
: if (n==1) t+=something less;
: .
: .
: .
: if (n==15)t-= something.
:
: So I have this other car in the screen with his own speed.
:
: And the Crash:
:
: if ( (x,y+n,..) of the first car == (x,y+h,..) of the second) )
: crash; // (this is not the real code, this is the way it works)
:
: So, that car has own speed and can crash with the main car from any side.
:
: finally:
:
: the (x,y,x,y) coordinates of the car in the screen.
: //it sould be at the begining
:
: NOW:
:
: I want to put many cars in the screen, and I dont want to repeat all that code for each of them. T has a relation with N
: so I cant use it again, (if I use it, all the cars will move the same). And if I use a new variable for each car I should type this code 1000 times.(or maybe not?)
:
: So, How can I reuse this code with other cars?
: Sorry about my prehistorik english
:
: And Thanks!
: Juan.
:

• Posts: 3Member
If you're writing a game like this (with loads of identical objects), you should use an OO language - with CLASSES. Use C++!
[code]
////////////////////
// If you've not used an object-orientated language (OOP)
// like C++ before, here are a few guidelines:
//
// The idea of object-orientated languages is the ability to
// create loads of identical, independent variables, and be able
// to access them easily. The following few lines creates a
// class, which (similar to a struct) defines data related to a
// car. The car class can be used like any other data type (int,
// char, etc). So we can now create 100 cars, each with their
// own independant x, y, car_speed, max_speed variables and
// crash() functions which can be accessed like:
// cars[car_num].variable
////////////////////

//Warning this code snippet is incomplete, and not guaranteed to
//work, but you should get the general idea!

class car
{
public:
int x; //Current x coord on screen
int y; //Current y coord
int car_speed; //Contains this individual car's speed
int max_speed; //Contains this individual car's max. speed
void crash(); //Called when this car crashes
}

//Then you could do this: ------------------------

car cars[100]; //Create 100 cars

for (a = 0; a < 100; a++)
{
cars[a].crash(); //Crash all the cars!
}

//Or this: ---------------------------------------

car blue_cars[100]; //Create 100 blue cars

for (a = 0; a < 100; a++)
{
blue_cars[a].max_speed = 15; //All blue cars have a max. speed of 15
}

car blue_cars[100]; //Create 100 red cars

for (a = 0; a < 100; a++)
{
red_cars[a].max_speed = 8; //All blue cars have a max. speed of 8
}

for (a = 0; a < 100; a++)
{
for (c = 0; c < 100; c++)
{
if (red_cars[a].x == red_cars[c].x)
{
red_cars[a].crash(); //Crash this car
red_cars[c].crash(); //And the other guy!
}

//Should also test red vs. blue cars here aswell
}
}
[/code]
Note: it is also possible to make a BASE CLASS called car, and
create sub-classes, by deriving them. For example, you could
create a derived class called "ferrari", or "beetle", both of
which are derived from the car class. If you need more info on
OOP, then get a book on C++ or something. If you knew about
OOP and classes, you should have thought of this sooner! ;-)