java swing框架實(shí)現(xiàn)貪吃蛇游戲
本文實(shí)例為大家分享了java swing實(shí)現(xiàn)貪吃蛇游戲的具體代碼,供大家參考,具體內(nèi)容如下
1、編寫(xiě)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、編寫(xiě)實(shí)體類(lèi)
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、編寫(xiě)主要程序
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("開(kāi)始");
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)專(zhuān)題,分享給大家:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
從java反編譯及字節(jié)碼角度探索分析String拼接字符串效率
這篇文章主要介紹了從java反編譯及字節(jié)碼角度探索分析String拼接字符串效率,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
Spring Boot + thymeleaf 實(shí)現(xiàn)文件上傳下載功能
最近同事問(wèn)我有沒(méi)有有關(guān)于技術(shù)的電子書(shū),我打開(kāi)電腦上的小書(shū)庫(kù),但是郵件發(fā)給他太大了,公司又禁止用文件夾共享,于是花半天時(shí)間寫(xiě)了個(gè)小的文件上傳程序,部署在自己的Linux機(jī)器上,需要的朋友可以參考下2018-01-01
Java實(shí)現(xiàn)音頻添加自定義時(shí)長(zhǎng)靜音的示例代碼
這篇文章主要介紹了一個(gè)Java工具類(lèi),可以實(shí)現(xiàn)給一個(gè)wav音頻添加自定義時(shí)長(zhǎng)靜音。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編學(xué)習(xí)一下2022-01-01
spring boot加載資源路徑配置和classpath問(wèn)題解決
這篇文章主要介紹了spring boot加載資源路徑配置和classpath問(wèn)題解決,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
java學(xué)習(xí)DongTai被動(dòng)型IAST工具部署過(guò)程
被動(dòng)型IAST被認(rèn)為是DevSecOps測(cè)試階段實(shí)現(xiàn)自動(dòng)化安全測(cè)試的最佳工具,而就在前幾天,洞態(tài)IAST正式開(kāi)源了,這對(duì)于甲方構(gòu)建安全工具鏈來(lái)說(shuō),絕對(duì)是一個(gè)大利好2021-10-10
深入淺出分析Java抽象類(lèi)和接口【功能,定義,用法,區(qū)別】
這篇文章主要介紹了Java抽象類(lèi)和接口,結(jié)合實(shí)例形式深入淺出的分析了java抽象類(lèi)與接口的功能功能,定義,用法及區(qū)別,需要的朋友可以參考下2017-08-08

