2014-02-10 6 views
1

Мне интересно, почему мои игровые объекты не отображаются. Я могу сделать JPanel, если я использую g.drawRect вне цикла for-each, но вызов PipeObject внутри цикла, похоже, не работает для меня. Я здесь что-то не так? Спасибо за любую помощь или решения.PipeGame -Почему игровые объекты не двигаются?

Это обновленная версия моего старого вопроса, который можно найти here.

ОБНОВЛЕНИЕ - Трубы рисуются правильно, но не перемещаются влево, вызывая pipe.move().

игры

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
//import javax.swing.border.EmptyBorder; 
import javax.swing.SwingUtilities; 

public class Game { 

    public static void main(String[] args) { 
     Runnable r = new Runnable() { 
      @Override 
      public void run() {    
       // the GUI as seen by the user (without frame) 
       final CardLayout cl = new CardLayout(); 
       final JPanel gui = new JPanel(cl); 
       // remove if no border is needed 
       //gui.setBorder(new EmptyBorder(10,10,10,10)); 

       JPanel menu = new JPanel(new GridBagLayout()); 
       JButton playGame = new JButton("Play!"); 

       ActionListener playGameListener = new ActionListener() { 

        @Override 
        public void actionPerformed(ActionEvent e) { 
         cl.show(gui, "game"); 
        } 
       }; 
       playGame.addActionListener(playGameListener); 
       Insets margin = new Insets(20, 50, 20, 50); 
       playGame.setMargin(margin); 
       menu.add(playGame); 
       gui.add(menu); 
       cl.addLayoutComponent(menu, "menu"); 

       final JPanel pipes = new Pipes(); 
       gui.add(pipes); 
       cl.addLayoutComponent(pipes, "game"); 

       JFrame f = new JFrame("PipeGame"); 
       f.add(gui); 
       // Ensures JVM closes after frame(s) closed and 
       // all non-daemon threads are finished 
       f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
       // See https://stackoverflow.com/a/7143398/418556 for demo. 
       f.setLocationByPlatform(true); 

       // ensures the frame is the minimum size it needs to be 
       // in order display the components within it 
       f.pack(); 
       // should be done last, to avoid flickering, moving, 
       // resizing artifacts. 
       f.setVisible(true); 
      } 
     }; 
     // Swing GUIs should be created and updated on the EDT 
     // http://docs.oracle.com/javase/tutorial/uiswing/concurrency 
     SwingUtilities.invokeLater(r); 
    } 
} 

Трубы

import java.util.*; 
import java.awt.Graphics; 
import java.awt.Dimension; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JPanel; 
import javax.swing.Timer; 

public class Pipes extends JPanel { 
    boolean gameNotOver = true; 
    int x1 = 754; 
    int y1 = setHeightVal(); 
    int y2 = setHeightVal(); 
    int y3 = setHeightVal(); 

    List<PipeObject> pipes = new ArrayList<PipeObject>(); 

    public Pipes() { 
     pipes.add(new PipeObject(x1, y1)); 
     pipes.add(new PipeObject(x1 + 300, y2)); 
     pipes.add(new PipeObject(x1 + 600, y3)); 
    } 

    public void drawEndlessPipes() { 
     if (gameNotOver) { 
      Timer pipeSpeed = new Timer(10, new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        for (PipeObject pipe : pipes) { 
         pipe.move(); 
        } 
       } 
      }); 
      pipeSpeed.start(); 
     } 
    } 

    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     for (PipeObject pipe : pipes) { 
      pipe.drawPipe(g); 
     } 
    } 

    public int setHeightVal() {  //Get a random number and select a preset height 
     int num = (int)(9*Math.random() + 1); 
     int val = 0; 
     if (num == 9) 
     { 
      val = 295; 
     } 
     else if (num == 8) 
     { 
      val = 246; 
     } 
     else if (num == 7) 
     { 
      val = 216; 
     } 
     else if (num == 6) 
     { 
      val = 185; 
     } 
     else if (num == 5) 
     { 
      val = 156; 
     } 
     else if (num == 4) 
     { 
      val = 125; 
     } 
     else if (num == 3) 
     { 
      val = 96; 
     } 
     else if (num == 2) 
     { 
      val = 66; 
     } 
     else 
     { 
      val = 25; 
     } 
     return val; 
    } 

    public Dimension getPreferredSize() { 
     return new Dimension(751,501); 
    } 
} 

PipeObject

import java.awt.Graphics; 

public class PipeObject { 
    //Declare and initialiaze variables 
    int x1; 
    int x2 = 75;    //pipe width, total is 83 
    int y1 = -1;    // Y should be -1 
    int y2; 
    int gap = 130;    //gap height 

    public PipeObject(int x, int y) { 
     this.x1 = x; 
     this.y2 = y; 
    } 

    public void drawPipe(Graphics g/*, int x1, int y2*/) { 
     g.drawRect(x1,y1,x2,y2);      //Draw part 1 
     g.drawRect(x1-3,y2-1,x2+6,25);     //Draw part 2 
     g.drawRect(x1-3,y2+25+gap,x2+6,25);    //Draw part 3 
     g.drawRect(x1,y2+25+gap+25,x2,500-y2-49-gap); //Draw part 4 
    } 

    public void move() { 
     x1--; 
    } 

    public int getMyX() { //To determine where the pipe is horizontally 
     return x1-3; 
    } 

    public int getMyY() { //To determine where the pipe is vertically 
     return y2+25; 
    } 
} 
+0

Позвольте мне знать, если вы» все еще есть проблемы? Не просматривая весь ваш код, ответы ниже выглядели довольно корректно. Если он все еще не работает, сообщите мне. –

ответ

1

Вы красите трубы, выходящие за рамки визуальных границ панели. Размер панели Pipes составляет 751x501, но трубы начинаются с x1 = 754. Попробуйте изменить x1 на 1 в Pipes, и вы должны увидеть три трубы. Или вы можете максимизировать рамку, и вы должны видеть недостающие трубы далеко на правой стороне.

+0

Вы видели таймер? –

+0

Теоретически трубы должны двигаться влево. –

+0

Я попробую это хотя –

1

Как сказал CyberStorm, вы никогда не вызывали drawEndlessPipes(), чтобы запустить таймер качания вообще. Также я не вижу вызов панели «Панели» в вашем таймере, просто изменение значения x не приведет к магическому перемещению, вам нужно вызвать repaint() в Pipes.java, чтобы перерисовать его и показать анимация.

Так вот он идет:

Модифицированный Runnable в Game.java

Runnable r = new Runnable() { 
     @Override 
     public void run() { 
      // the GUI as seen by the user (without frame) 
      final CardLayout cl = new CardLayout(); 
      final JPanel gui = new JPanel(cl); 
      // remove if no border is needed 
      //gui.setBorder(new EmptyBorder(10,10,10,10)); 

      JPanel menu = new JPanel(new GridBagLayout()); 
      JButton playGame = new JButton("Play!"); 

      ActionListener playGameListener = new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        cl.show(gui, "game"); 
       } 
      }; 
      playGame.addActionListener(playGameListener); 
      Insets margin = new Insets(20, 50, 20, 50); 
      playGame.setMargin(margin); 
      menu.add(playGame); 
      gui.add(menu); 
      cl.addLayoutComponent(menu, "menu"); 

      final JPanel pipes = new Pipes(); 
      gui.add(pipes); 
      cl.addLayoutComponent(pipes, "game"); 

      JFrame f = new JFrame("PipeGame"); 
      f.add(gui); 
      // Ensures JVM closes after frame(s) closed and 
      // all non-daemon threads are finished 
      f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
      // See http://stackoverflow.com/a/7143398/418556 for demo. 
      f.setLocationByPlatform(true); 

      // ensures the frame is the minimum size it needs to be 
      // in order display the components within it 
      f.pack(); 
      // should be done last, to avoid flickering, moving, 
      // resizing artifacts. 
      f.setVisible(true); 

      ((Pipes) pipes).drawEndlessPipes(); 
     } 
    }; 

Модифицированный drawEndlessPipes() в Pipes.java

public void drawEndlessPipes() { 
    if (gameNotOver) { 
     Timer pipeSpeed = new Timer(10, new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       for (PipeObject pipe : pipes) { 
        pipe.move(); 
        Pipes.this.repaint(); 
       } 
      } 
     }); 
     pipeSpeed.start(); 
    } 
} 
+0

Его все еще только рисует один канал, и мне нужно его рисовать все три, и мне также нужно, чтобы случайные каналы продолжали прокручиваться бесконечно –

Смежные вопросы