Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What I would do is create a Car class that has an Image. With this class, you can have ints that hold the x and y values representing the car's location, as well as the image of the car. Then, you just set the values or x and y and rotate the image like you are doing in your example, except for the Car object. You may do this already, I am not sure though without more code than just the input handler. To draw it, do something like this in your render:</p> <pre><code>drawImage(car.getImage(), car.getX(), car.getY()); </code></pre> <p>As far as borders, assuming you just don't want your Car to go off screen, you can do something like this:</p> <pre><code>// Check if outside left border if(car.getX() &lt; 0) { car.setX(0); } // Check if outside top border if(car.getY() &lt; 0) { car.setY(0); } // Check if outside right border if(car.getX() &gt;= SCREEN_WIDTH - car.getWidth()) { car.setX(SCREEN_WIDTH - car.getWidth()); } // Check if outside bottom border if(car.getY() &gt;= SCREEN_HEIGHT - car.getHeight()) { car.setY(SCREEN_HEIGHT - car.getHeight()); } </code></pre> <p>In this context, get width and get height are getting the width and height of the image you use for the car, and SCREEN_HEIGHT and SCREEN_WIDTH are constants that hold the width and height of the screen. You may have to fine tune a bit depending on how your game works.</p> <pre><code>public Class Car { private int x; private int y; private Image image; public Car(int x, int y) { this.x = x; this.y = y; image = new Image("location/car.png"); } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload