java吧 关注:1,303,817贴子:12,860,792
  • 12回复贴,共1

求助 俄罗斯方块实在搞不懂了

只看楼主收藏回复

package game;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.util.Timer;
import java.util.TimerTask;
public class Game extends JPanel{
public static final int WIDTH = 400;
public static final int HEIGHT = 600;
int intervel = 3000;
public static BufferedImage background;
int ROW = WIDTH/background.getWidth();
int COL = HEIGHT/background.getHeight();
Cell[] cells1=new Cell[]{new Cell(1,3),new Cell(1,2)};
static {
try {
background = ImageIO.read(Game.class.getResource("background.png"));
} catch (Exception e) {
e.printStackTrace();
}
}
public void paintCell(Graphics g){
for(int i=0;i<2;i++) {g.drawImage(cells1[i].getImage(),cells1[i].getCol()*background.getWidth(),cells1[i].getRow()*background.getHeight(),null);
}
}
public void paint(Graphics g){//画笔
paintCell(g);
}
public void step(){
for(int i=0;i<cells1.length;i++){
cells1[i].drop();
}
}
private Timer timer;
public void action(){
timer = new Timer();
timer.schedule(new TimerTask(){
public void run(){
step();
repaint();
}
},intervel,intervel);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Fly");
Game game = new Game();
frame.add(game);//将面板添加到窗口
frame.setSize(WIDTH,HEIGHT);
frame.setAlwaysOnTop(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);//设置窗口可见 尽快调用paint()方法
game.action();
}
}


1楼2015-09-24 22:08回复




    3楼2015-09-24 22:13
    回复
      2026-06-02 00:04:27
      广告
      不感兴趣
      开通SVIP免广告
      package game;
      import java.awt.image.BufferedImage;
      public class Cell {
      private int row;
      private int col;
      private BufferedImage image;
      public Cell(int row, int col) {
      this.row = row;
      this.col = col;
      this.image = Game.background;
      }
      public int getRow(){
      return row;
      }
      public int getCol(){
      return col;
      }
      public BufferedImage getImage(){
      return image;
      }
      public void drop(){
      row++;
      }
      public void drop_(){
      row--;
      }
      public void left(){
      col--;
      }
      public void right(){
      col++;
      }
      }


      4楼2015-09-24 22:13
      回复
        为什么越来越多


        5楼2015-09-24 22:13
        回复
          你每次重画时画错地方导致的


          来自Android客户端6楼2015-09-24 22:19
          收起回复
            在调用paintCell前加一行:super.paint(g);


            IP属地:河北来自Android客户端7楼2015-09-25 01:58
            回复