回复 9楼 林月儿
咋还是运行不了。。
回复 2楼 林月儿
有报错。。[此贴子已经被作者于2019-1-6 17:18编辑过]
package game; import java.awt.*; import java.awt.event.*; import java.util.*; import java.util.List; import java.util.Timer; import javax.swing.*; public class Test extends JFrame implements MouseListener, MouseMotionListener { /** * */ private static final long serialVersionUID = 1L; private static final int START = 0, RUNNING = 1, PAUSE = 2, OVER = 3, DEFAULT_WIDTH = 610, DEFAULT_HEIGHT = 506; private int state = START; private MoveObj self = new MoveObj(0, 0, 40, 40); private List<MoveObj> enemys = new ArrayList<MoveObj>(); private List<MoveObj> bullets = new ArrayList<MoveObj>(); public Test() { setVisible(true); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); setDefaultCloseOperation(EXIT_ON_CLOSE); addMouseListener(this); addMouseMotionListener(this); setResizable(false); final JPanel myPanel = new MyPanel(); add(myPanel); new Timer().schedule(new TimerTask() { @Override public void run() { if(state == RUNNING) { addEnemyAction(); shootAction(); bangAction(); moveAction(); outOfBorderAction(); checkGameOver(); moveIndex++; } myPanel.repaint(); } }, 10, 10); } protected void checkGameOver() { for(MoveObj obj : enemys) { if(self.x + self.width > obj.x && self.x < obj.x + obj.width && self.y + self.height > obj.y && self.y < obj.y + obj.height) { state = OVER; enemys.clear(); bullets.clear(); return; } } } long moveIndex = 0; protected void outOfBorderAction() { // enemys = enemys.stream().filter(MoveObj::isNotOutOfBorder) // .collect(Collectors.toList()); // bullets = bullets.stream().filter(MoveObj::isNotOutOfBorder) // .collect(Collectors.toList()); for(MoveObj e:new ArrayList<MoveObj>(enemys)) { if(!e.isNotOutOfBorder()) { enemys.remove(e); } } for(MoveObj e:new ArrayList<MoveObj>(bullets)) { if(!e.isNotOutOfBorder()) { bullets.remove(e); } } } protected void moveAction() { if(moveIndex % 2 == 0) { // enemys.forEach(ele->ele.y+=1); for(MoveObj e:enemys) e.y+=1; // bullets.forEach(ele->ele.y-=2); for(MoveObj e:bullets) e.y-=2; } } protected void bangAction() { for(MoveObj b:new ArrayList<MoveObj>(bullets)) { for(MoveObj e:new ArrayList<MoveObj>(enemys)) { if(b.bang(e)) { bullets.remove(b); enemys.remove(e); break; } } } } protected void shootAction() { // 0.2s if(moveIndex % 20 == 0) { bullets.add(new MoveObj(self.x+self.width/2, self.y, 8, 12)); } } protected void addEnemyAction() { // 2s if(moveIndex % 200 == 0) { int x = new Random().nextInt(DEFAULT_WIDTH - 40); enemys.add(new MoveObj(x, 0, 40, 40)); } } // my plan Image myplanImage = new ImageIcon("image/MyPlane.png").getImage(); // area map Image gameMap = new ImageIcon("image/map/gamemap.jpg").getImage(); // enemy Image enemyImage = new ImageIcon("image/monster/PlaneOne.png").getImage(); // bullet Image bulletImage = new ImageIcon("image/bullet.png").getImage(); class MyPanel extends JPanel { /** * */ private static final long serialVersionUID = 1L; @Override public void paint(Graphics g) { g.drawImage(gameMap, 0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT, null); g.drawImage(myplanImage, self.x, self.y, self.width, self.height, null); // enemys.forEach(e->g.drawImage(enemyImage, e.x, e.y, e.width, e.height, null)); for(MoveObj e:enemys)g.drawImage(enemyImage, e.x, e.y, e.width, e.height, null); // bullets.forEach(e->g.drawImage(bulletImage, e.x, e.y, e.width, e.height, null)); for(MoveObj e:bullets)g.drawImage(bulletImage, e.x, e.y, e.width, e.height, null); Font font = new Font("gbk", Font.BOLD, 32); g.setFont(font); g.setColor(Color.RED); switch(state) { case START: g.drawString("开始", DEFAULT_WIDTH/3, DEFAULT_HEIGHT/2); break; case PAUSE: g.drawString("暂停", DEFAULT_WIDTH/3, DEFAULT_HEIGHT/2); break; case OVER: g.drawString("结束", DEFAULT_WIDTH/3, DEFAULT_HEIGHT/2); break; } } } public static void main(String[] args) { new Test(); } @Override public void mouseClicked(MouseEvent e) { if(state == START) state = RUNNING; else if(state == OVER) state = START; } @Override public void mousePressed(MouseEvent e) {} @Override public void mouseReleased(MouseEvent e) {} @Override public void mouseEntered(MouseEvent e) { if(state == PAUSE) state = RUNNING; } @Override public void mouseExited(MouseEvent e) { if(state == RUNNING) state = PAUSE; } @Override public void mouseDragged(MouseEvent e) {} @Override public void mouseMoved(MouseEvent e) { if(state == RUNNING) { self.x = e.getX() - 20; self.y = e.getY() - 40; } } class MoveObj { int x; int y; int width; int height; public MoveObj(int x, int y, int width, int height) { super(); this.x = x; this.y = y; this.width = width; this.height = height; } public boolean bang(MoveObj e) { return x+width>e.x&&x<e.x+e.width&&y+height>e.y&&y<e.y+e.height; } public boolean isNotOutOfBorder() { return !(x < 0 || y < 0 || x + width > DEFAULT_WIDTH || y + height > DEFAULT_HEIGHT); } } }