Top Advertisement

Your Ad Here

Sunday, August 15, 2010

Moving car simulation... in TURBO C...

Last week I said that I will teach you How to use SSLAM in improving your coding/programming logic... Now here it is... 10 mins of reading can change your programming perspective...

first you have to download borland C++ to start... you can search it in google...
create a function name printLane(int x,int y); put your code in here...
Imagine this is your screen in console mode... the x (left to right) has a total of 79 characters and y (top to bottom) has 24 characters...
print a line on 0,0 (x,y)
printf("-------------------------------------------------------"); ( this is a line )
then type gotoxy(0,3) to go to the third line then print another line
printf("-   -   -   -   -   -   -   -   -   -   -   -   -   -"); ( this is a line )
then type gotoxy(0,6) to go to the sixth line then print another line
printf("-------------------------------------------------------"); ( this is a line )

compile it then run it... you have to call it of course in console mode it should look like this...
--------------------------------------------------------------

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

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

The code looks like this...
#include <stdio.h>
#include <conio.h>

void printLane(int x, int y){
   clrscr();
   gotoxy(x,y);
   printf("-------------------------------------------------------");
   gotoxy(x,y+3)
   printf("-   -   -   -   -   -   -   -   -   -   -   -   -   -");
   gotoxy(x,y+6)
   printf("-------------------------------------------------------");
}

void main(){
   printLane(0,0);
   getch();
}


Then create another function named printCar(int x, int y).. put your codes in here too...

how to draw a car... turbo c supports a variety of characters including special characters... just hold down the alt then press the numbers on numpads...

█ - full block character ( alt + 219 )
▄ - half lower block character ( alt + 220 )
0 - plain 0 or if you like you can use other special character which is the bullet

▄███▄  = SIMPLE CAR ^___^
  0    0

print the car on the middle of the broken line and the lower line...

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


-   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -
▄███▄ print the car here...
  0    0

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

the code now should look like this


#include <stdio.h>
#include <conio.h>

void printLane(int x, int y){
   clrscr();
   gotoxy(x,y);
   printf("-------------------------------------------------------");
   gotoxy(x,y+3)
   printf("-   -   -   -   -   -   -   -   -   -   -   -   -   -");
   gotoxy(x,y+6)
   printf("-------------------------------------------------------");
}

printCar(int x, int y){
   gotoxy(x,y+4);
   printf("▄███▄");
   gotoxy(x,y+5);
   printf("  0    0  ");
}

void main(){
   printLane(0,0);
   printCar(0,0);
   getch();
}

The Question is... HOW WILL WE MOVE THE CAR? Lets focus now on the main method...

!kbhit() - means "stop if user pressed a key..." put it in a while loop then it reads... while not keyboard hit!! make sense?

create an integer variable named x with an initial value of 0 change the printCar(0,0) to printCar(x,0); after the printCar(x,0); add x++; which increments the value of x every loop... by the way delay is included in dos.h


void main(){
    int x=0;
    while ( !kbhit() ){
      delay(100);
      printLane(0,0);
      printCar(x,0);
      x++;
      if( x== 79) x=0;
   }
}

see how it goes...


HERE IS THE SIMPLE LOGIC...
Clear the Screen
Print the Lane
Print The Car on x,0
increment x
if x==79 reset the value of x;
do it all over again...


IF you look at it clearly... we have 2 objects here... the lane and the car... but the car has a function move The Whole code should look like this...


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

void printLane(int x, int y){
   clrscr();
   gotoxy(x,y);
   printf("-------------------------------------------------------");
   gotoxy(x,y+3)
   printf("-   -   -   -   -   -   -   -   -   -   -   -   -   -");
   gotoxy(x,y+6)
   printf("-------------------------------------------------------");
}

printCar(int x, int y){
   gotoxy(x,y+4);
   printf("▄███▄");
   gotoxy(x,y+5);
   printf("  0    0  ");
}


void main(){
    int x=0;
    while ( !kbhit() ){
      delay(100);
      printLane(0,0);
      printCar(x,0);
      x++;
      if( x== 79) x=0;
   }
}

HOPE YOU LEARNED SOMETHING... FROM THIS TUTORIAL... ON MY NEXT BLOG... Random and Graphics... ^____^

No comments:

Post a Comment