java swing框架實現(xiàn)貪吃蛇游戲
本文實例為大家分享了java swing實現(xiàn)貪吃蛇游戲的具體代碼,供大家參考,具體內(nèi)容如下
1、編寫main方法
package game; import java.awt.Graphics; import javax.swing.*; public class snakeMain extends JFrame { public snakeMain() { snakeWin win = new snakeWin(); add(win); setTitle("̰貪吃蛇v1.0"); setSize(435,390); setLocation(200, 200); setVisible(true); } public static void main(String[] args) { new snakeMain(); } }
2、編寫實體類
package game; public class snakeAct { private int x; private int y; 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; } }
3、編寫主要程序
package game; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; import java.util.List; public class snakeWin extends JPanel implements ActionListener,KeyListener,Runnable{ int fenShu=0,Speed=0; boolean start = false; int rx=0,ry=0; int eat1=0,eat2=0; JDialog dialog = new JDialog(); JLabel label = new JLabel("你掛了!你的分?jǐn)?shù)"+fenShu+""); JButton ok = new JButton("T_T"); Random r = new Random(); JButton newGame,stopGame; List<snakeAct> list = new ArrayList<snakeAct>(); int temp=0; Thread nThread; public snakeWin() { newGame = new JButton("開始"); stopGame = new JButton("結(jié)束"); newGame.addActionListener(this); stopGame.addActionListener(this); this.addKeyListener(this); this.setLayout(new FlowLayout(FlowLayout.LEFT)); this.add(newGame); this.add(stopGame); dialog.setLayout(new GridLayout(2, 1)); dialog.add(label); dialog.add(ok); dialog.setSize(200, 200); dialog.setLocation(200, 200); dialog.setVisible(false); ok.addActionListener(this); } public void paintComponent(Graphics g){ super.paintComponent(g); g.drawRect(10, 40, 400, 300); g.drawString("分?jǐn)?shù)"+fenShu, 150, 15); g.drawString("速度"+Speed, 150, 35); g.setColor(new Color(0, 255, 0)); if(start){ g.fillRect(10+rx*10, 40+ry*10, 10, 10); for (int i = 0; i < list.size(); i++) { g.setColor(new Color(0, 0, 255)); g.fillRect(10+list.get(i).getX()*10, 40+list.get(i).getY()*10, 10, 10); } } } public void actionPerformed(ActionEvent e) { if(e.getSource()==newGame){ newGame.setEnabled(false); start = true; rx=r.nextInt(40);ry=r.nextInt(30); snakeAct tempAct = new snakeAct(); tempAct.setX(20); tempAct.setY(15); list.add(tempAct); this.requestFocus(); nThread = new Thread(this); nThread.start(); repaint(); } if(e.getSource()==stopGame){ System.exit(0); } if(e.getSource()==ok){ list.clear(); start=false; newGame.setEnabled(true); dialog.setVisible(false); fenShu=0; Speed=0; repaint(); } } private void eat() { if (rx==list.get(0).getX()&&ry==list.get(0).getY()) { rx = r.nextInt(40);ry = r.nextInt(30); snakeAct tempAct = new snakeAct(); tempAct.setX(list.get(list.size()-1).getX()); tempAct.setY(list.get(list.size()-1).getY()); list.add(tempAct); fenShu = fenShu+100*Speed+10; eat1++; if(eat1-eat2>=4){ eat2=eat1; Speed++; } } } public void otherMove(){ snakeAct tempAct = new snakeAct(); for (int i = 0; i < list.size(); i++) { if (i==1) { list.get(i).setX(list.get(0).getX()); list.get(i).setY(list.get(0).getY()); }else if(i>1){ tempAct=list.get(i-1); list.set(i-1, list.get(i)); list.set(i, tempAct); } } } public void move(int x,int y){ if (minYes(x, y)) { otherMove(); list.get(0).setX(list.get(0).getX()+x); list.get(0).setY(list.get(0).getY()+y); eat(); repaint(); }else { nThread = null; label.setText("你掛了!你的分?jǐn)?shù)"+fenShu+""); dialog.setVisible(true); } } public boolean minYes(int x,int y){ if (!maxYes(list.get(0).getX()+x,list.get(0).getY()+ y)) { return false; } return true; } public boolean maxYes(int x,int y){ if (x<0||x>=40||y<0||y>=30) { return false; } for (int i = 0; i < list.size(); i++) { if (i>1&&list.get(0).getX()==list.get(i).getX()&&list.get(0).getY()==list.get(i).getY()) { return false; } } return true; } public void keyPressed(KeyEvent e) { if(start){ switch (e.getKeyCode()) { case KeyEvent.VK_UP: move(0, -1); temp=1; break; case KeyEvent.VK_DOWN: move(0, 1); temp=2; break; case KeyEvent.VK_LEFT: move(-1, 0); temp=3; break; case KeyEvent.VK_RIGHT: move(1, 0); temp=4; break; default: break; } } } public void keyReleased(KeyEvent e) {} public void keyTyped(KeyEvent e) {} public void run() { while (start) { switch (temp) { case 1: move(0, -1); break; case 2: move(0, 1); break; case 3: move(-1, 0); break; case 4: move(1, 0); break; default: break; } repaint(); try { Thread.sleep(300-30*Speed); } catch (InterruptedException e) { e.printStackTrace(); } } } }
4、效果圖
更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解析Java內(nèi)存分配和回收策略以及MinorGC、MajorGC、FullGC
本節(jié)將會介紹一下:對象的內(nèi)存分配與回收策略;對象何時進(jìn)入新生代、老年代;MinorGC、MajorGC、FullGC的定義區(qū)別和觸發(fā)條件;還有通過圖示展示了GC的過程。2021-09-09Mybatis使用useGeneratedKeys獲取自增主鍵
這篇文章主要為大家介紹了Mybatis使用useGeneratedKeys獲取自增主鍵示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01java實現(xiàn)word文檔轉(zhuǎn)pdf并添加水印的方法詳解
這篇文章主要介紹了java實現(xiàn)word文檔轉(zhuǎn)pdf并添加水印的方法,結(jié)合實例形式詳細(xì)分析了java word文檔轉(zhuǎn)PDF相關(guān)實現(xiàn)技巧與操作注意事項,需要的朋友可以參考下2019-09-09SpringMVC用XML方式實現(xiàn)AOP的方法示例
這篇文章主要介紹了SpringMVC用XML方式實現(xiàn)AOP的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04快速學(xué)會Dubbo的配置環(huán)境及相關(guān)配置
本文主要講解Dubbo的環(huán)境與配置,文中運(yùn)用大量代碼和圖片講解的非常詳細(xì),需要學(xué)習(xí)或用到相關(guān)知識的小伙伴可以參考這篇文章2021-09-09spring無法讀取properties文件數(shù)據(jù)問題詳解
這篇文章主要介紹了spring無法讀取properties文件數(shù)據(jù)問題詳解,需要的朋友可以參考下2020-02-02