1、存盘退出 jmi3.addActionListener(this); jmi5=new JMenuItem(接着上一次玩 jmi5.addActionListener(this); jmi4=new JMenuItem(退出 jmi4.addActionListener(this); jp=new JPanel() public void paint(Graphics g) g.setFont(new Font(宋体,Font.BOLD,40); g.drawString(play, 250, 200); ; jm1.add(jmi6); jm1.add(jmi1); jm1.add(jmi2
2、); jm1.add(jmi3); jm1.add(jmi5); jm1.add(jmi4); jmb.add(jm1); this.setJMenuBar(jmb); this.add(jp); this.setSize(650,500); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);public void actionPerformed(ActionEvent e) if(e.getSource()=jmi6) System.out.println(ok wp=new Wormpane
3、l(); this.addKeyListener(wp); this.add(wp); if(e.getSource()=jmi4) System.exit(0); if(e.getSource()=jmi2) this.wp.setSpeed(0); if(e.getSource()=jmi1) this.wp.setSpeed(1); if(e.getSource()=jmi3) ArrayList al=this.wp.vp; FileWriter fw=null; BufferedWriter bw=null; try fw=new FileWriter(D:/Recording.tx
4、t bw=new BufferedWriter(fw); for(int i=0;ial.size();i+) Wormpoint wm=al.get(i); if(i=0) int dire=wm.getDirestion(); bw.write(dire+rn int x=wm.getX(); int y=wm.getY(); bw.write(x+ +y+ catch (IOException args) args.printStackTrace(); finally try bw.flush(); bw.close(); fw.close(); System.exit(0); catc
5、h (IOException args) args.printStackTrace(); if(e.getSource()=jmi5) wp=new Wormpanel(); al=new ArrayList(); FileReader fr=null; BufferedReader br=null; fr=new FileReader( br=new BufferedReader(fr); String st; st=br.readLine(); Wormpoint head=new Wormpoint(); head.setDirestion(Integer.parseInt(st); S
6、tring shead=st.split( head.setX(Integer.parseInt(shead0); head.setY(Integer.parseInt(shead1); al.add(head); while(st=br.readLine()!=null) Wormpoint wm=new Wormpoint(); String str=st.split( wm.setX(Integer.parseInt(str0); wm.setY(Integer.parseInt(str1); al.add(wm); this.wp.vp=al; br.close(); fr.close
7、();/面板类 进行界面操作import java.awt.List;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.util.*;import java.util.Timer;public class Wormpanel extends JPanel implements KeyListener ArrayList vp=new ArrayList Wormpoint point; Wormpoint wp; String reco; int speed; boolean ismove=
8、true; Recorder recor; public int getSpeed() return speed; public void setSpeed(int speed) this.speed = speed; public Wormpanel() speed=1; recor=new Recorder(); recor.filereader(); reco=recor.recorder; point=new Wormpoint(); point.setWidth(10); point.setX(int)(Math.random()*450); point.setY(int)(Math
9、.random()*380); for(int i=0;Wormpoint.length; wp=new Wormpoint(); wp.setX(20); wp.setY(20-wp.getWidth(); vp.add(wp); Timer t=new Timer(); t.schedule(new Mytask(), 0,100); public void touch() boolean b=false; Wormpoint w=vp.get(0); switch(w.getDirestion() case 1: if(w.getX()point.getX()|(w.getX()+w.g
10、etWidth()point.getX() if(w.getY()point.getY() System.out.print(touch ismove=false; point=new Wormpoint(); point.setWidth(10); point.setX(int)(Math.random()*450); point.setY(int)(Math.random()*380); Wormpoint.length+; recor.cadd(); if(recor.getcount()*10Integer.parseInt(reco) recor.filewriter(); brea
11、k; case 2: if(w.getX() if(w.getY()+w.getWidth() point=new Wormpoint(); point.setWidth(10); point.setX(int)(Math.random()*450); point.setY(int)(Math.random()*380); Wormpoint.length+; recor.cadd(); if(recor.getcount()*10 recor.filewriter(); case 3: if(w.getY()point.getY()&w.getY()w.getY()+w.getWidth()
12、point.getX()&w.getX() if(w.getX()+w.getWidth()w.getX()+w.getWidth() public void paint(Graphics g) super.paint(g); g.fillRect(0, 0,450, 400); g.setColor(Color.cyan); if(!point.isIseate() g.fillOval(point.getX(), point.getY(), point.getWidth(), point.getWidth(); g.setColor(Color.DARK_GRAY); g.setFont(
13、new Font(,Font.BOLD,20); g.drawString(我的得分:+recor.getcount()*10, 460, 50);我的记录:+reco, 460, 80); Wormpoint w;vp.size(); w= vp.get(i); g.setColor(Color.red); g.fill3DRect(w.getX(), w.getY(), w.getWidth(),w.getWidth() ,false); g.setColor(Color.black); g.drawRect(w.getX(), w.getY(), w.getWidth(),w.getWi
14、dth() ); class Mytask extends TimerTask public void run() Wormpoint w=vp.get(0); Wormpoint xin=new Wormpoint(); int direct=w.getDirestion(); switch(direct) case 1: xin.setX(w.getX(); xin.setDirestion(w.getDirestion(); xin.setY(w.getY()-w.getWidth()*speed); if(xin.getY()=400) xin.setY(0); /down case
15、3: xin.setX(w.getX()-w.getWidth()*speed); xin.setY(w.getY(); if(xin.getX()=450) xin.setX(0); /right vp.add(0,xin); touch(); if(ismove&speed! vp.remove(vp.size()-1); ismove=true; repaint();public void keyPressed(KeyEvent e) if(e.getKeyCode()=e.VK_UP) vp.get(0).setDirestion(1); if(e.getKeyCode()=e.VK_
16、DOWN) vp.get(0).setDirestion(2);/down if(e.getKeyCode()=e.VK_LEFT) vp.get(0).setDirestion(3);/left if(e.getKeyCode()=e.VK_RIGHT) vp.get(0).setDirestion(4);public void keyReleased(KeyEvent e) / TODO Auto-generated method stubpublic void keyTyped(KeyEvent e) /单元类public class Wormpoint private int x; p
17、rivate int y; private int direstion=2; static int length=5; private boolean iseate=false; public boolean isIseate() return iseate; public void setIseate(boolean iseate) this.iseate = iseate; public int getLength() return length; public void setLength(int length) this.length = length; private int wid
18、th=8; public int getWidth() return width; public void setWidth(int width) this.width = width; public int getDirestion() return direstion; public void setDirestion(int direstion) this.direstion = direstion; public int getX() return x; public void setX(int x) this.x = x; public int getY() return y; public void setY(int y) this.y = y;/记录类public class Recorder private int count=0; public String recorder; public void cadd() count+; public int getcount() return count; public void filereader
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1