Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package racingcar;
- import static java.lang.Math.abs;
- import java.util.Random;
- import javafx.animation.AnimationTimer;
- import javafx.application.Application;
- import javafx.geometry.Rectangle2D;
- import javafx.scene.Group;
- import javafx.scene.Scene;
- import javafx.scene.canvas.Canvas;
- import javafx.scene.canvas.GraphicsContext;
- import javafx.scene.control.Label;
- import javafx.scene.image.Image;
- import javafx.scene.paint.Color;
- import javafx.stage.Stage;
- /**
- *
- * @author Nicholas Camillo
- */
- public class RacingCar extends Application {
- private Random rand = new Random();
- private final Label speed = new Label();
- @Override
- public void start(Stage primaryStage) {
- Sprite car = new Sprite();
- Group root = new Group();
- Scene scene = new Scene(root);
- primaryStage.setScene(scene);
- Canvas canvas = new Canvas(512, 512);
- GraphicsContext gc = canvas.getGraphicsContext2D();
- car.setPosition(200, 200);
- car.drawSprite(gc);
- //car.setVelocity(100, 0);
- new AnimationTimer(){
- @Override
- public void handle(long now) {
- gc.clearRect(0, 0, 512, 512);
- drawBackground(gc);
- if (car.positionX < 10 || car.positionX > 462){
- car.setVelocity(car.getVelocityX() * -1, 0);
- }
- scene.setOnKeyPressed(e->{
- switch (e.getCode()){
- case UP:
- if (car.getVelocityX() >= 0)
- car.addVelocity(10, 0);
- else car.addVelocity(-10, 0); break;
- case DOWN:
- if (car.getVelocityX() > 0)
- car.addVelocity(-10, 0);
- else if (car.getVelocityX() < 0)
- car.addVelocity(10, 0); break;
- case SPACE:
- this.stop(); break;
- }
- });
- scene.setOnKeyReleased(e->{
- if (e.getCode().equals(e.getCode().SPACE)){
- this.start();
- }
- });
- car.update(0.01);
- car.drawSprite(gc);
- speed.setText("SPEED: " + Double.toString(abs(car.getVelocityX())) + " KMPH");
- }
- }.start();
- primaryStage.setTitle("Racing Car Lab");
- root.getChildren().add(canvas);
- root.getChildren().add(speed);
- primaryStage.show();
- }
- public void drawBackground(GraphicsContext gc){
- gc.setFill(Color.GREEN);
- gc.fillRect(0, 250, 512, 300);
- gc.setFill(Color.GRAY);
- gc.fillRect(0, 200, 512, 50);
- gc.setFill(Color.LIGHTBLUE);
- gc.fillRect(0, 0, 512, 200);
- gc.setFill(Color.WHITE);
- for (int i = 5; i < 512; i+= 70){
- gc.fillRect(i, 220, 20, 5);
- }
- gc.setFill(Color.BLUE);
- for (int i = 0; i < 10; i++)
- gc.fillOval(rand.nextInt(512), rand.nextInt(512), 5, 5);
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- launch(args);
- }
- class Sprite {
- private double velocityX;
- private double velocityY;
- private double positionX = 10;
- private double positionY = 20;
- private double width;
- private double height;
- private Image img;
- public void setImage(String path) {
- img = new Image(path);
- width = img.getWidth();
- height = img.getHeight();
- }
- private void setPosition(double x, double y) {
- positionX = x;
- positionY = y;
- }
- private void setVelocity(double x, double y) {
- velocityX = x;
- velocityY = y;
- }
- private void addVelocity(double x, double y) {
- velocityX += x;
- velocityY += y;
- }
- private double getVelocityX(){
- return velocityX;
- }
- private void update(double time) {
- positionX += velocityX * time;
- positionY += velocityY * time;
- }
- private Rectangle2D getBoundry() {
- return new Rectangle2D(positionX, positionY, width, height);
- }
- private boolean intersect(Sprite s) {
- return s.getBoundry().intersects(this.getBoundry());
- }
- private void drawSprite(GraphicsContext gc) {
- //draw roof
- gc.setFill(Color.BLACK);
- double[] xPoints = {positionX, positionX + 10, positionX + 20, positionX + 30};
- double[] yPoints = {positionY, positionY - 10, positionY - 10, positionY};
- gc.fillPolygon(xPoints, yPoints, 4);
- //draw body
- gc.setFill(Color.RED);
- gc.fillRect(positionX - 10, positionY, 50, 10);
- //draw wheels
- gc.setFill(Color.BLACK);
- gc.fillOval(positionX, positionY + 10, 10, 10);
- gc.fillOval(positionX + 20, positionY + 10, 10, 10);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement