這篇文章將為大家詳細(xì)講解有關(guān)怎么在Java中利用swing框架實(shí)現(xiàn)一個(gè)貪吃蛇游戲,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
成都創(chuàng)新互聯(lián)是一家朝氣蓬勃的網(wǎng)站建設(shè)公司。公司專注于為企業(yè)提供信息化建設(shè)解決方案。從事網(wǎng)站開(kāi)發(fā),網(wǎng)站制作,網(wǎng)站設(shè)計(jì),網(wǎng)站模板,微信公眾號(hào)開(kāi)發(fā),軟件開(kāi)發(fā),微信小程序,10多年建站對(duì)混凝土泵車等多個(gè)方面,擁有豐富的網(wǎng)站制作經(jīng)驗(yàn)。
具體代碼:
package Tcs; /** * * * * @author tx */ import java.awt.Color; import java.awt.Container; import java.awt.Font; import java.awt.Graphics; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Random; import java.util.Timer; import java.util.TimerTask; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Snack extends JPanel implements KeyListener { public JButton bt = new JButton("重新開(kāi)始"); public ArrayList<Treasure> bw = new ArrayList<Treasure>(); public body[] b = new body[5]; public String state = ""; public ArrayList<point> p = new ArrayList<point>(); public static int score; public Snack() { this.addKeyListener(this); shengc(); } public void shengc() { for (int i = 0; i < b.length; i++) { b[i] = new body(); b[i].x = 10 - i * 10; b[i].y = 150; } } public int x = 0, y = 0; public void paint(Graphics g) { super.paint(g); g.setColor(new Color(165,41,10));//RGB定義顏色的方法 g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20)); for (int i = 0; i < b.length; i++) { body z1 = b[i]; g.drawString("O", b[i].x, b[i].y); } g.setColor(Color.BLUE); g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20)); g.drawString("SCORE:" + score, 30, 30); paintjs(g); paintbw(g); } public void paintjs(Graphics g) { g.setColor(Color.BLACK); if (state.length() > 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); for (int i = 0; i < bw.size(); i++) { g.drawString("o", bw.get(i).x, bw.get(i).y); } } public boolean yj() { if ((b[0].x < 400 && b[0].x > 0) && (b[0].y < 400 && b[0].y > 0)) { return false; } else { state = "GAME OVER"; return true; } } public void stmove() { if (pzjc() == false && (yj() == false)) { b[0].speed = 8;//此處可提升速度增加難度 b[0].move(); p.add(new point(b[0].x, b[0].y, b[0].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.y)); return jl; }// 暫時(shí)無(wú)用 public void ssmove() { if (p.size() >= b.length) { for (int i = 0; i < b.length - 1; i++) { b[i + 1].fx = p.get(i).fx; b[i + 1].x = p.get(i).x; b[i + 1].y = p.get(i).y; } } } Random r = new Random(); public void bzbw() { if (bw.size() < 1) { Treasure s = new Treasure(); s.x = r.nextInt(300) + 50; s.y = r.nextInt(300) + 50; bw.add(s); } } public void bwxs() { Timer t = new Timer(); t.schedule(new TimerTask() { public void run() { } }, 0, 8000); } public boolean pzjc() { for (int i = 1; i < p.size(); i++) { if (p.get(0).equals(p.get(i))) { state = "GAME OVER"; return true; } } return false; } public void crush() { if (bw.size() > 0) { if (jl(b[0], bw.get(0)) < 8) { bw.remove(0); b = Arrays.copyOf(b, b.length + 1); b[b.length - 1] = new body(); score += 10; } } } public void gameover() { MouseListener k = new MouseAdapter() { public void mouseClicked(MouseEvent e) { super.mouseClicked(e); state = ""; b = Arrays.copyOf(b, 5); p.clear(); shengc(); score = 0; bt.setVisible(false); } }; if (state.length() > 1) { this.add(bt); bt.setVisible(true); bt.setBounds(150, 150, 100, 30); bt.addMouseListener(k); } if(bt.isVisible()==false){this.remove(bt);} this.requestFocus(); } public void zmAction() { Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() { bzbw();// 生成寶物 stmove();// 蛇頭運(yùn)動(dòng) ssmove();// 蛇身運(yùn)動(dòng) crush();// 碰撞檢測(cè) gameover(); repaint(); } }, 10, 83); } public static void main(String[] args) { JFrame jf = new JFrame("創(chuàng)新互聯(lián) - 貪吃蛇游戲測(cè)試"); 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, 0); s.setBackground(Color.ORANGE); jf.add(s); s.zmAction(); s.requestFocus(); } public void keyTyped(KeyEvent e) { } public void keyPressed(KeyEvent e) { int k = e.getKeyCode(); switch (k) { case KeyEvent.VK_UP: if (b[0].fx != "sz" && b[0].fx != "xz") { b[0].fx = "sz"; } break; case KeyEvent.VK_DOWN: if (b[0].fx != "sz" && b[0].fx != "xz") { b[0].fx = "xz"; } break; case KeyEvent.VK_LEFT: if (b[0].fx != "zz" && b[0].fx != "yz") { b[0].fx = "zz"; } break; case KeyEvent.VK_RIGHT: if (b[0].fx != "zz" && b[0].fx != "yz") { b[0].fx = "yz"; } break; } repaint(); } public void keyReleased(KeyEvent e) { } }
body類
package Tcs; public class body { public int x=0; public int y=0; public int speed; private String str; public String fx; public body(){ fx="yz"; } 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 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"){ xz(); } if(fx=="sz"){ sz(); } if(fx=="zz"){ zz(); } if(fx=="yz"){ yz(); } } }
寶物類
package Tcs; public class Treasure { public int x; public int y; public String str; }
point類
package Tcs; public class point { public int x; public int y; public String fx; public point(int x,int y,String fx){ this.x=x; this.y=y; this.fx=fx; } public boolean equals(Object o){ if(o instanceof point){ point p=(point)o; if(p.x==this.x&&p.y==this.y){ return true; } } if(o==this){return true;} if(o==null){return false;} return false;} }
關(guān)于怎么在Java中利用swing框架實(shí)現(xiàn)一個(gè)貪吃蛇游戲就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
網(wǎng)頁(yè)名稱:怎么在Java中利用swing框架實(shí)現(xiàn)一個(gè)貪吃蛇游戲
本文來(lái)源:http://muchs.cn/article18/ihpggp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營(yíng)銷、靜態(tài)網(wǎng)站、網(wǎng)站維護(hù)、標(biāo)簽優(yōu)化、小程序開(kāi)發(fā)、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)