I am in a computer science course and I am struggling with loops in developing a program in Eclipse. It has taken me an embarrassing number of hours to get this far.
This homework assignment requires that the animated cars in my program for the top three houses travel from the right side of the top three houses to the left side, passing in front of the houses. The two lower houses must have the cars traveling from the left to the right, passing in the back of the houses.
When I run the program as is, the cars do not initially appear as they should and when they do appear they move across the entire screen. The cars are all moving in the appropriate directions, but do not initially appear when they should and do not stop where they are supposed to.
I realize that the program is not recognizing my loop for the car's animation. I cannot understand why this is - not to mention how to resolve the problem.
Here is my code copied from Eclipse:
import java.awt.*;
import graphics.*;
import javax.swing.*;
public class NeighborhoodDrives {
public static void main(String[] args) {
// The GraphicsWindow object
GraphicsWindow win = new GraphicsWindow("Window",1400,800);
// The Graphics object
Graphics page = win.getGraphics();
// The car facing right
Image carRight = new ImageIcon("carright.gif").getImage();
// The car facing left
Image carLeft = new ImageIcon("carleft.gif").getImage();
// The five cars' initial placements
int x1 = 0, x2 = 450, x3 = 900, x4 = 550, x5 = 990;
int y1 = 338, y2 = 338, y3 = 338, y4 = 625, y5 = 625;
Color houseColor = Color.magenta;
boolean gameOver = false;
boolean rightDirection = true;
while (!gameOver) // Creates a loop
{
win.clear(); // Clears the window
int mid = 300;
int top = 50;
if (!rightDirection) // Creates the loop to make the car drive in back of the house from the left to the right sides of the 2 lower houses
page.drawImage(carRight, x4, y4, null);
if (!rightDirection)
page.drawImage(carRight, x5, y5, null);
// Creates 5 different colored houses in 5 different places
int count = 0;
while (count < 5) {
switch (count) {
case 0:
mid = 250;
top = 50;
// Makes a magenta house
houseColor = Color.magenta;
break;
case 1:
mid = 700;
top = 50;
// Makes a yellow house
houseColor = Color.yellow;
break;
case 2:
mid = 1150;
top = 50;
// Makes a green house
houseColor = Color.green;
break;
case 3:
mid = 450;
top = 350;
// Makes a light blue, cyan, house
houseColor = Color.cyan;
break;
case 4:
mid = 900;
top = 350;
// Makes a red house
houseColor = Color.red;
break;
}
page.setColor(houseColor);
page.fillRect(mid - 125, top + 130, 200, 200); // fronts of houses
// Sets color of the roofs
page.setColor(Color.darkGray);
page.fillRect(mid - 150, top + 125, 250, 35); // roofs of houses
page.fillRect(mid - 100, top + 100, 150, 40);
page.fillRect(mid - 75, top + 85, 100, 20);
page.fillRect(mid - 45, top + 65, 50, 20);
page.fillRect(mid - 33, top + 55, 25, 20);
// Sets color of the doors
page.setColor(Color.blue);
page.fillRect(mid - 50, top + 245, 50, 85); // doors of houses
// Sets color of the door knobs
page.setColor(Color.black);
page.fillOval(mid - 15, top + 285, 10, 10); // door knobs
// Sets color of windows on the houses
page.setColor(Color.white);
page.fillRect(mid - 100, top + 185, 45, 45); // second story windows on
// left of houses
// Sets color of windows on the houses
page.setColor(Color.white);
page.fillRect(mid + 10, top + 185, 45, 45); // second story windows on
// right of houses
// Sets color of window panes on the houses
page.setColor(Color.red);
page.drawLine(150, 258, 195, 258); // panes of window on left of magenta house
page.drawLine(172, 236, 172, 281);
page.drawLine(263, 258, 303, 258); // panes of window on right of magenta house
page.drawLine(282, 236, 282, 281);
page.drawLine(600, 258, 645, 258); // panes of window on left on yellow house
page.drawLine(622, 236, 622, 281);
page.drawLine(710, 258, 755, 258); // panes of window on right on yellow house
page.drawLine(732, 236, 732, 281);
page.drawLine(1050, 258, 1095, 258); // panes of window on left on green house
page.drawLine(1072, 236, 1072, 281);
page.drawLine(1160, 258, 1205, 258); // panes of window on right on green house
page.drawLine(1182, 236, 1182, 281);
page.drawLine(350, 558, 395, 558); // panes of window on left on cyan house
page.drawLine(372, 536, 372, 581);
page.drawLine(460, 558, 503, 558); // panes of window on right on cyan house
page.drawLine(482, 536, 482, 581);
page.drawLine(800, 558, 845, 558); // panes of window on left on red house
page.drawLine(822, 536, 822, 581);
page.drawLine(910, 558, 955, 558); // panes of window on right on red house
page.drawLine(932, 536, 932, 581);
count++;
}
if (rightDirection)
x1 += 10;
else
x1 -= 10;
if (rightDirection)
x2 += 10;
else \
x2 -= 10;
if (rightDirection)
x3 += 10;
else
x3 -= 10;
if (!rightDirection)
page.drawImage(carLeft, x1, y1, null);
if (!rightDirection)
page.drawImage(carLeft, x2, y2, null);
if (!rightDirection)
page.drawImage(carLeft, x3, y3, null);
if (rightDirection)
x4 -= 10;
else
x4 += 10;
if (rightDirection)
x5 -= 10;
else
x5 += 10;
if (x1 > 450 && x2 > 900 && x3 > 1300 && x4 < 150 && x5 < 550) // Signifies when the car has completely driven across the front of the house
rightDirection = false;
win.repaint(); // Redraw the screen
win.pause(40);
if (x1 > 450 && x2 > 900 && x3 > 1300 && x4 < 150 && x5 < 550) // Signifies when the car has completely driven across the front of the house
rightDirection = false;
}
}
}
PS I am not a computer science major, in fact I am a neanderthal when it comes to this stuff. Please be understanding of my limitations.
If Dave Newton had answered this question, I would have definitely supported that answer. I would add that you want to set the conditions for when gameover
should be set to true like you do with rightDirection
(except that's false, of course).
I would add at the end of your loop:
if(!rightDirection && x1 <= 450 && x2 <= 900 && x3 <= 1300 && x4 >= 150 && x5 >=550){
gameover = true;
}
I am making the assumption you wanted the car to stop on the back-end of the house opposite from the position it started. Hope that helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.