1

我正在学习计算机科学课程,并且在 Eclipse 中开发程序时遇到了循环问题。我花了很尴尬的几个小时才走到这一步。

这个家庭作业要求我的前三间房子的程序中的动画汽车从前三间房子的右侧行驶到左侧,经过房子的前面。下两间房子一定有汽车从左到右行驶,从房子后面经过。

当我按原样运行程序时,汽车最初并没有按应有的方式出现,当它们出现时,它们会在整个屏幕上移动。这些汽车都在向适当的方向移动,但最初并没有在应该出现的时候出现,也没有在应该出现的地方停下来。

我意识到程序没有识别我的汽车动画循环。我不明白为什么会这样——更不用说如何解决问题了。

这是我从 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我不是计算机科学专业的,事实上我是一个尼安德特人。请理解我的局限性。

4

1 回答 1

1

如果戴夫牛顿回答了这个问题,我肯定会支持这个答案。我要补充一点,你想设置条件,什么时候gameover应该设置为真,就像你做的那样rightDirection(当然,那是假的)。

我会在你的循环结束时添加:

    if(!rightDirection && x1 <= 450 && x2 <= 900 && x3 <= 1300 && x4 >= 150 && x5 >=550){
              gameover = true; 
    }

我假设您希望汽车停在与开始位置相反的房屋后端。希望有帮助!

于 2012-10-13T02:28:04.313 回答