Advertisement
Nick-O-Rama

RacingCar

Oct 23rd, 2015
515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.50 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package racingcar;
  7.  
  8. import static java.lang.Math.abs;
  9. import java.util.Random;
  10. import javafx.animation.AnimationTimer;
  11. import javafx.application.Application;
  12. import javafx.geometry.Rectangle2D;
  13. import javafx.scene.Group;
  14. import javafx.scene.Scene;
  15. import javafx.scene.canvas.Canvas;
  16. import javafx.scene.canvas.GraphicsContext;
  17. import javafx.scene.control.Label;
  18. import javafx.scene.image.Image;
  19. import javafx.scene.paint.Color;
  20. import javafx.stage.Stage;
  21. /**
  22.  *
  23.  * @author Nicholas Camillo
  24.  */
  25. public class RacingCar extends Application {
  26.     private Random rand = new Random();
  27.     private final Label speed = new Label();
  28.     @Override
  29.     public void start(Stage primaryStage) {
  30.  
  31.         Sprite car = new Sprite();
  32.  
  33.         Group root = new Group();
  34.         Scene scene = new Scene(root);
  35.         primaryStage.setScene(scene);
  36.         Canvas canvas = new Canvas(512, 512);
  37.         GraphicsContext gc = canvas.getGraphicsContext2D();
  38.         car.setPosition(200, 200);
  39.         car.drawSprite(gc);
  40.         //car.setVelocity(100, 0);
  41.         new AnimationTimer(){
  42.             @Override
  43.             public void handle(long now) {
  44.                 gc.clearRect(0, 0, 512, 512);
  45.                 drawBackground(gc);
  46.                 if (car.positionX < 10 || car.positionX > 462){
  47.                     car.setVelocity(car.getVelocityX() * -1, 0);
  48.                 }
  49.                 scene.setOnKeyPressed(e->{
  50.                     switch (e.getCode()){
  51.                         case UP:                            
  52.                             if (car.getVelocityX() >= 0)
  53.                                 car.addVelocity(10, 0);
  54.                             else car.addVelocity(-10, 0); break;    
  55.                         case DOWN:
  56.                             if (car.getVelocityX() > 0)
  57.                                 car.addVelocity(-10, 0);
  58.                             else if (car.getVelocityX() < 0)
  59.                                 car.addVelocity(10, 0); break;
  60.                         case SPACE:
  61.                             this.stop(); break;
  62.                     }
  63.                 });
  64.                 scene.setOnKeyReleased(e->{
  65.                     if (e.getCode().equals(e.getCode().SPACE)){
  66.                         this.start();
  67.                     }
  68.                 });
  69.                 car.update(0.01);
  70.                 car.drawSprite(gc);
  71.                 speed.setText("SPEED: " + Double.toString(abs(car.getVelocityX())) + " KMPH");
  72.             }
  73.        
  74.     }.start();
  75.         primaryStage.setTitle("Racing Car Lab");
  76.         root.getChildren().add(canvas);
  77.         root.getChildren().add(speed);
  78.         primaryStage.show();
  79.        
  80.        
  81.     }
  82.    
  83.     public void drawBackground(GraphicsContext gc){
  84.         gc.setFill(Color.GREEN);
  85.         gc.fillRect(0, 250, 512, 300);
  86.         gc.setFill(Color.GRAY);
  87.         gc.fillRect(0, 200, 512, 50);
  88.         gc.setFill(Color.LIGHTBLUE);
  89.         gc.fillRect(0, 0, 512, 200);
  90.         gc.setFill(Color.WHITE);
  91.         for (int i = 5; i < 512; i+= 70){
  92.             gc.fillRect(i, 220, 20, 5);
  93.         }
  94.         gc.setFill(Color.BLUE);
  95.         for (int i = 0; i < 10; i++)
  96.             gc.fillOval(rand.nextInt(512), rand.nextInt(512), 5, 5);
  97.     }
  98.  
  99.     /**
  100.      * @param args the command line arguments
  101.      */
  102.     public static void main(String[] args) {
  103.         launch(args);
  104.     }
  105.  
  106.     class Sprite {
  107.  
  108.         private double velocityX;
  109.         private double velocityY;
  110.         private double positionX = 10;
  111.         private double positionY = 20;
  112.         private double width;
  113.         private double height;
  114.         private Image img;
  115.  
  116.         public void setImage(String path) {
  117.             img = new Image(path);
  118.             width = img.getWidth();
  119.             height = img.getHeight();
  120.         }
  121.  
  122.         private void setPosition(double x, double y) {
  123.             positionX = x;
  124.             positionY = y;
  125.         }
  126.  
  127.         private void setVelocity(double x, double y) {
  128.             velocityX = x;
  129.             velocityY = y;
  130.         }
  131.  
  132.         private void addVelocity(double x, double y) {
  133.             velocityX += x;
  134.             velocityY += y;
  135.         }
  136.        
  137.         private double getVelocityX(){
  138.             return velocityX;
  139.         }
  140.  
  141.         private void update(double time) {
  142.             positionX += velocityX * time;
  143.             positionY += velocityY * time;
  144.         }
  145.  
  146.         private Rectangle2D getBoundry() {
  147.             return new Rectangle2D(positionX, positionY, width, height);
  148.         }
  149.  
  150.         private boolean intersect(Sprite s) {
  151.             return s.getBoundry().intersects(this.getBoundry());
  152.         }
  153.  
  154.         private void drawSprite(GraphicsContext gc) {
  155.             //draw roof
  156.             gc.setFill(Color.BLACK);
  157.             double[] xPoints = {positionX, positionX + 10, positionX + 20, positionX + 30};
  158.             double[] yPoints = {positionY, positionY - 10, positionY - 10, positionY};
  159.             gc.fillPolygon(xPoints, yPoints, 4);
  160.  
  161.             //draw body
  162.             gc.setFill(Color.RED);
  163.             gc.fillRect(positionX - 10, positionY, 50, 10);
  164.  
  165.             //draw wheels
  166.             gc.setFill(Color.BLACK);
  167.             gc.fillOval(positionX, positionY + 10, 10, 10);
  168.             gc.fillOval(positionX + 20, positionY + 10, 10, 10);
  169.         }
  170.     }
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement