1、import javax.swing.JFrame;import javax.swing.JPanel;public class Snack extends JPanel implements KeyListener public JButton bt = new JButton(重新开始); public ArrayList bw = new ArrayList p = new ArrayList public static int score; public Snack() this.addKeyListener(this); shengc(); public void shengc()
2、for (int i = 0; i 1) g.drawString(state, 140, 200); public void paintbw(Graphics g) g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 25); g.setColor(Color.RED); bw.size();o, bw.get(i).x, bw.get(i).y); public boolean yj() if (b0.x 0) & (b0.y 0) return false; else state = GAME OVER return true; public v
3、oid stmove() if (pzjc() = false & (yj() = false) b0.speed = 10; b0.move(); p.add(new point(b0.x, b0.y, b0.fx); if (p.size() b.length) p.remove(p.get(0); / System.out.println(p.size(); public int jl(body a, Treasure b) int jl = 0; jl = (int) Math.sqrt(a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b
4、.y); return jl; / 暂时无用 public void ssmove() = b.length) b.length - 1; bi + 1.fx = p.get(i).fx; bi + 1.x = p.get(i).x; bi + 1.y = p.get(i).y; Random r = new Random(); public void bzbw() if (bw.size() 0) if (jl(b0, bw.get(0) 8) bw.remove(0); b = Arrays.copyOf(b, b.length + 1); bb.length - 1 = new body
5、(); score += 10; public void gameover() MouseListener k = new MouseAdapter() public void mouseClicked(MouseEvent e) super.mouseClicked(e); b = Arrays.copyOf(b, 5); p.clear(); score = 0; bt.setVisible(false); ; this.add(bt); bt.setVisible(true); bt.setBounds(150, 150, 100, 30); bt.addMouseListener(k)
6、;if(bt.isVisible()=false)this.remove(bt); this.requestFocus(); public void zmAction() Timer timer = new Timer(); timer.schedule(new TimerTask() bzbw();/ 生成宝物 stmove();/ 蛇头运动 ssmove();/ 蛇身运动 crush();/ 碰撞检测 gameover(); repaint(); , 10, 83); public static void main(String args) JFrame jf = new JFrame(贪
7、吃蛇 jf.setBounds(0, 0, 400, 400); jf.setVisible(true); jf.setLayout(null); Container c = new Container(); c = jf.getContentPane(); c.setBackground(Color.WHITE); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Snack s = new Snack(); s.setVisible(true); s.setBounds(0, 0, 600, 600); s.setLocation(0,
8、0); s.setBackground(Color.ORANGE); jf.add(s); s.zmAction(); s.requestFocus(); public void keyTyped(KeyEvent e) / TODO Auto-generated method stub Override public void keyPressed(KeyEvent e) int k = e.getKeyCode(); switch (k) case KeyEvent.VK_UP: if (b0.fx != sz & b0.fx !xz) b0.fx = break; case KeyEve
9、nt.VK_DOWN: case KeyEvent.VK_LEFT:zzyz case KeyEvent.VK_RIGHT: public void keyReleased(KeyEvent e) 2、public class body public int x=0;public int y=0;public int speed;private String str;public String fx;public body() fx=public int getX() return x;public void setX(int x) this.x = x;public int getY() r
10、eturn y;public void setY(int y) this.y = y;public String getStr() return str;public void setStr(String str) this.str = str;public void sz() this.y+=-speed;public void xz() this.y+=speed;public void zz() this.x+=-speed;public void yz() this.x+=speed;public void move() if(fx=) xz(); sz(); zz(); yz();3、public class point public int x;public int y;public point(int x,int y,String fx)