您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關如何在java項目中利用swing框架實現一個貪吃蛇游戲,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
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("你掛了!你的分數"+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("結束"); 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("分數"+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("你掛了!你的分數"+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(); } } } }
以上就是如何在java項目中利用swing框架實現一個貪吃蛇游戲,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。