Game programming

Moderators: None (Apply to moderate this forum)
Number of threads: 2047
Number of posts: 5331

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
How to reuse this code? don't want to wright it 1000 times Posted by Juan on 26 May 2001 at 10:59 PM
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.

Report
Re: How to reuse this code? don't want to wright it 1000 times Posted by Sand_Hawk9 on 3 Jun 2001 at 11:12 AM
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!!!

Report
Re: How to reuse this code? don't want to wright it 1000 times Posted by DeepFreeze on 8 Jun 2001 at 3:21 AM
: 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? =)


Report
Re: How to reuse this code? don't want to wright it 1000 times Posted by garwain on 14 Jun 2001 at 5:31 AM
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.
:


Report
Re: How to reuse this code? don't want to wright it 1000 times Posted by SAmott on 15 Jun 2001 at 12:39 PM
If you're writing a game like this (with loads of identical objects), you should use an OO language - with CLASSES. Use C++!
////////////////////
// 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
    }
}

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!





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.