Advertisement
Guest User

CLASSE CAR

a guest
Nov 27th, 2015
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.03 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Lezione27Novembre
  8. {
  9.     class car
  10.     {
  11.         int speedMax;
  12.         int retroMax; // velocità minima all'indietro (?)
  13.         int speed;
  14.         int currentGear; //marcia
  15.         int maxGear;
  16.         uint fuel;
  17.         int[] gearChange;
  18.         public car(int speedMax, int retroMax, int maxGear)
  19.         {
  20.             this.speed = 0;
  21.             this.speedMax = speedMax;
  22.             this.retroMax = retroMax;
  23.             this.maxGear = maxGear;
  24.             this.currentGear = 0;
  25.             this.fuel = 100;
  26.  
  27.             gearChange = new int[maxGear +1]; //serve anche il folle, ecco perché c'è il +1. Manca la retromarcia però
  28.  
  29.             for(int i=0; i< maxGear + 1; i++)
  30.             {
  31.                 gearChange[i] = i * (speedMax / maxGear);
  32.             }
  33.         }
  34.         public void Accelerate()
  35.         {
  36.             if (fuel == 0)
  37.                 return;
  38.  
  39.             fuel--;
  40.             if (currentGear == 0) //folle
  41.                 return;
  42.             else if (currentGear < 0)
  43.             {
  44.                 if (speed > retroMax) //torna indietro
  45.                      speed--;
  46.             }
  47.                
  48.             else
  49.             {
  50.                 if (speed < gearChange[currentGear])// va avanti
  51.                     speed++;
  52.  
  53.                 //cambio manuale
  54.                /* else if (speed == gearChange[currentGear]) ;
  55.                     GearUp();*/
  56.             }
  57.            
  58.          
  59.         }
  60.         /*max speed: 100;
  61.         0:0;
  62.         1:50;
  63.         1:100;
  64.         */
  65.  
  66.         //aumento della marcia
  67.         public void GearUp()
  68.         {
  69.             if (currentGear < maxGear)
  70.                 currentGear++;
  71.            
  72.         }
  73.         public void GearDown()
  74.         {
  75.             if (currentGear >= 0)
  76.                 currentGear--;
  77.         }
  78.         public void Brake() //lo chiamo quando premo il pedale del freno
  79.         {
  80.             if (speed == 0)
  81.                 return;
  82.  
  83.             if (speed > 0)
  84.                 speed--;
  85.  
  86.             else if (speed < 0)
  87.                 speed++;
  88.         }
  89.  
  90.         public void ManageInput()
  91.         {
  92.             ConsoleKeyInfo key=Console.ReadKey();
  93.             switch (key.Key)
  94.             {
  95.                 case ConsoleKey.UpArrow:
  96.                 this.Accelerate();
  97.                     break;
  98.                 case ConsoleKey.Spacebar:
  99.                     this.Brake();
  100.                     break;
  101.  
  102.                 case ConsoleKey.DownArrow:
  103.                     this.GearDown();
  104.                     break;
  105.  
  106.                 case ConsoleKey.W:
  107.                 this.GearDown();
  108.                         break;
  109.              }
  110.  
  111.         }
  112.  
  113.         public void Draw()
  114.         {
  115.             Console.WriteLine("Fuel: {0},", this.fuel);
  116.             Console.WriteLine("Gear: {0},", this.currentGear);
  117.             Console.WriteLine("Speed: {0},", this.speed);
  118.         }
  119.     }
  120.  
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement