Note that there are some explanatory texts on larger screens.

plurals
  1. POProgramming Video Games for the Evil Genius:Project 10:Radical Racing-The Cars
    text
    copied!<p><img src="https://i.stack.imgur.com/kPtKY.png" alt="enter image description here"></p> <p>Iv'e looked all over for answers to this book. And I know anyone else who has tried to read this book feels the same way. It's called "Programming Video Games for The Evil Genius" Is there anyone who has read this book? I'm on project 10:Radical Racing-The Cars. Everything compiles correctly but for some reason my cars are not showing up in the correct spot on the JFrame. They should be showing up under the two white lines. I'm positive the code is exactly the same as in the book, but the book is wrong. I have already tried changing the HEIGHT part of the point of origin but no matter what I do it does not budge. I can't attach an image because I don't have a rep of at least 10 .This is the code that the deals with the placement of the cars.</p> <pre><code>public class TheCars extends JFrame { final int WIDTH = 900; int HEIGHT = 650; double p1Speed = .5, p2Speed = .5; Rectangle p1 = new Rectangle(WIDTH/9,HEIGHT/2, WIDTH/30,WIDTH/30); Rectangle p2 = new Rectangle(((WIDTH/9)+((int)((WIDTH/9)*1.5)/2)),(HEIGHT/2)+ (HEIGHT/10),WIDTH/30,WIDTH/30); //the constructor public TheCars() { //the following code creates the JFrame super("Radical Racing"); setSize(WIDTH,HEIGHT); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); //start the inner class (which works on it's own because it is a thread) Move1 m1 = new Move1(); Move2 m2 = new Move2(); m1.start(); m2.start(); } //this will draw the cars and the racetrack public void paint(Graphics g) { super.paint(g); //set the color to blue for p1 g.setColor(Color.BLUE); //now draw the actual player g.fill3DRect(p1.x,p1.width,p1.width,p1.height,true); //set the color to red for p2 g.setColor(Color.red); //now draw the actual player g.fill3DRect(p2.x,p2.width,p2.width,p2.height,true); } private class Move1 extends Thread { public void run() //This should all be in an infinite loop so that the process repeats. { while(true) { //now put in the try block. This will let //the program exit if there is an error try { //first refresh the screen repaint(); //increase speed a bit if(p1Speed&lt;=5) p1Speed+=.2; p1.y-=p1Speed; //this delays the refresh rate Thread.sleep(75); } catch(Exception e) { //if there is an exception (an error), exit the loop break; } } } } private class Move2 extends Thread { public void run() { //this should all be in an infinite loop so the process repeats while(true) { //now put the code in a "try" block. //this will let the program exit if there is an error try { //first refresh the screen repaint(); //increase the speed a bit if(p2Speed&lt;=5) p2Speed+=.2; p2.y-=p2Speed; //this delays the refresh rate Thread.sleep(75); } catch(Exception e) { //if there is an exception (an error), exitthe loop break; } } } } public static void main(String[]args) { new TheCars(); } } </code></pre>
 

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