Learning OOP with a simple Car example
              in 
             Programming Questions 
              •  
              2 years ago    
            
 
           
             Im praticing my Object-Oriented Programming with a simple program that lets me control a "car" (its actually just a rect()).
            
            
So far i have the car being displayed and moving. But i want to make it a bit more "natural" to drive the little square.
            
At the moment i just have "if key W is pressed, X++ / if key S pressed X-- / if A angle++ / if D angle--" this is a really simple translation and rotation of the shape.
            
I'd like to use W / S to move the square always forward and backwards having in mind the front and back of the "car" and A / D would rotate the "car". THis way i could actually control the "car" in a more natural way.
            
Here's what i've got so far: (Keep in mind this is really basic and primitive stuff :X)
            
            
            
Since im going to be doing alot of this kind of programming (much like programing a game) i'd probably be better with reading about this theme.
            
I'll be needing Gravity , Inertia, Collision and so on to make the controls more fluid and natural.
            
I've been trying a few keywords on google but not getting where i'd like.
Maybe some of you can suggest a good book or anything at all i can start reading :)
 
           
 
            
           So far i have the car being displayed and moving. But i want to make it a bit more "natural" to drive the little square.
At the moment i just have "if key W is pressed, X++ / if key S pressed X-- / if A angle++ / if D angle--" this is a really simple translation and rotation of the shape.
I'd like to use W / S to move the square always forward and backwards having in mind the front and back of the "car" and A / D would rotate the "car". THis way i could actually control the "car" in a more natural way.
Here's what i've got so far: (Keep in mind this is really basic and primitive stuff :X)
- Car car;
 - void setup(){
 - size(400,400);
 - car = new Car();
 - }
 - void draw(){
 - background(200,210,200);
 - //DRAW THE SHAPE
 - car.display();
 - //CONTROL THE CAR WITH KEYBOARD (W,S,D,A)
 - if(keyPressed) {
 - if (key == 'w' || key == 'W') {
 - car.forward();
 - }
 - }
 - if(keyPressed) {
 - if (key == 's' || key == 'S') {
 - car.backward();
 - }
 - }
 - if(keyPressed) {
 - if (key == 'a' || key == 'A') {
 - car.left();
 - }
 - }
 - if(keyPressed) {
 - if (key == 'd' || key == 'D') {
 - car.right();
 - }
 - }
 - }
 
- class Car{
 - float speed, posX, posY, w, h, angle;
 - Car(){
 - posX = width/2;
 - posY = width/2;
 - w = 30;
 - h = 10;
 - speed = 5;
 - angle = radians(90);
 - }
 - void display(){
 - rectMode(CENTER);
 - translate(posX, posY);
 - rotate(angle);
 - rect(0, 0, w, h);
 - }
 - void forward(){
 - posX += 5;
 - }
 - void backward(){
 - posX -= 5;
 - }
 - void left(){
 - angle += radians(15);
 - }
 - void right(){
 - angle -= radians(15);
 - }
 - }
 
Since im going to be doing alot of this kind of programming (much like programing a game) i'd probably be better with reading about this theme.
I'll be needing Gravity , Inertia, Collision and so on to make the controls more fluid and natural.
I've been trying a few keywords on google but not getting where i'd like.
Maybe some of you can suggest a good book or anything at all i can start reading :)
 
              
              1  
            
 
            