《論語》有云:知之者不如好之者,好之者不如樂之者。學(xué)習(xí)知識,樂在其中,可以激發(fā)我們的學(xué)習(xí)效率。本文將帶領(lǐng)各位小伙伴用 Java 來制作一個簡單的小游戲,來加強(qiáng)對Java語言的理解。
java簡易小游戲制作
游戲思路:設(shè)置人物移動,游戲規(guī)則,積分系統(tǒng),隨機(jī)移動的怪物,游戲勝負(fù)判定,定時器。
游戲內(nèi)容部分
package 代碼部分;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
public class TestGamePanel extends JPanel implements KeyListener, ActionListener {
//初始化人物坐標(biāo)
int p1X;
int p1Y;
int p2X;
int p2Y;
boolean isStart = false; //游戲是否開始
boolean p1isFail = false; //游戲是否失敗
boolean p2isFail = false;
String fx1; //左:L, 右:R, 上:U, 下:D
String fx2;
Timer timer = new Timer(50,this);//定時器
//積分
int p1score = 0;
int p2score = 0;
//蘋果
int AppleX;
int AppleY;
//怪物
int monster1X;
int monster1Y;
int monster2X;
int monster2Y;
int monster3X;
int monster3Y;
int monster4X;
int monster4Y;
int monster5X;
int monster5Y;
//隨機(jī)積分
Random random = new Random();
public TestGamePanel() {
init();
this.setFocusable(true);
this.addKeyListener(this);
timer.start();
}
//初始化
public void init() {
p1X = 25;
p1Y = 150;
p2X = 700;
p2Y = 550;
fx1 = "L";
fx2 = "R";
monster1X = 25*random.nextInt(28);
monster1Y = 100 + 25*random.nextInt(18);
monster2X = 25*random.nextInt(28);
monster2Y = 100 + 25*random.nextInt(18);
monster3X = 25*random.nextInt(28);
monster3Y = 100 + 25*random.nextInt(18);
monster4X = 25*random.nextInt(28);
monster4Y = 100 + 25*random.nextInt(18);
monster5X = 25*random.nextInt(28);
monster5Y = 100 + 25*random.nextInt(18);
AppleX = 25*random.nextInt(28);
AppleY = 100 + 25*random.nextInt(18);
add(kaishi);
add(chongkai);
guize.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new TestGameRule();
}
});
}
//游戲功能按鈕
JButton kaishi = new JButton("開始");
JButton chongkai = new JButton("重新開始");
JButton guize = new JButton("游戲規(guī)則");
//畫板
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
TestGameData.header.paintIcon(this,g,0,0);
g.setColor(Color.CYAN);
g.fillRect(0,100,780,520);
//畫人物
TestGameData.p1player1.paintIcon(this,g,p1X,p1Y);
TestGameData.p2player1.paintIcon(this,g,p2X,p2Y);
//畫得分
g.setFont(new Font("華文彩云",Font.BOLD,18)); //設(shè)置字體
g.setColor(Color.RED);
g.drawString("玩家1:" + p1score,20,20 );
g.drawString("玩家2:" + p2score,680,20);
//畫蘋果
TestGameData.apple.paintIcon(this,g,AppleX,AppleY);
//畫靜態(tài)怪物
TestGameData.monster.paintIcon(this,g,monster1X,monster1Y);
TestGameData.monster.paintIcon(this,g,monster2X,monster2Y);
TestGameData.monster.paintIcon(this,g,monster3X,monster3Y);
TestGameData.monster.paintIcon(this,g,monster4X,monster4Y);
TestGameData.monster.paintIcon(this,g,monster5X,monster5Y);
//游戲提示,是否開始
if(!isStart) {
g.setColor(Color.BLACK);
g.setFont(new Font("華文彩云",Font.BOLD,30));
g.drawString("請點擊開始游戲",300,300);
}
//游戲結(jié)束提示,是否重新開始
if(p2isFail || p1score == 15) {
g.setColor(Color.RED);
g.setFont(new Font("華文彩云",Font.BOLD,30));
g.drawString("玩家一獲勝,請點擊重新開始游戲",200,300);
}
if(p1isFail || p2score == 15) {
g.setColor(Color.RED);
g.setFont(new Font("華文彩云",Font.BOLD,30));
g.drawString("玩家二獲勝,請點擊重新開始游戲",200,300);
}
}
//鍵盤監(jiān)聽事件
@Override
public void keyPressed(KeyEvent e) {
//控制人物走動
//玩家1
if(isStart == true && (p1isFail == false && p2isFail == false)) {
if(e.getKeyCode() == KeyEvent.VK_D) {
fx1 = "R";
p1X += 25;
if(p1X >= 750) {p1X = 750;}
}
else if(e.getKeyCode() == KeyEvent.VK_A) {
fx1 = "L";
p1X -= 25;
if(p1X <= 0) {p1X = 0;}
}
else if(e.getKeyCode() == KeyEvent.VK_W) {
fx1 = "U";
p1Y -= 25;
if(p1Y <= 100) {p1Y = 100;}
}
else if(e.getKeyCode() == KeyEvent.VK_S) {
fx1 = "D";
p1Y += 25;
if(p1Y >= 600) {p1Y = 600;}
}
//玩家2
if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
fx2 = "R";
p2X += 25;
if(p2X >= 750) {p2X = 750;}
}
else if(e.getKeyCode() == KeyEvent.VK_LEFT) {
fx2 = "L";
p2X -= 25;
if(p2X <= 0) {p2X = 0;}
}
else if(e.getKeyCode() == KeyEvent.VK_UP) {
fx2 = "U";
p2Y -= 25;
if(p2Y <= 100) {p2Y = 100;}
}
else if(e.getKeyCode() == KeyEvent.VK_DOWN) {
fx2 = "D";
p2Y += 25;
if(p2Y >= 600) {p2Y = 600;}
}
}
repaint();
}
@Override
public void actionPerformed(ActionEvent e) {
kaishi.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
isStart = true;
}
});
chongkai.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(p1isFail) { p1isFail = !p1isFail; init(); }
if(p2isFail) { p2isFail = !p2isFail; init(); }
}
});
add(kaishi);
add(chongkai);
add(guize);
if(isStart == true && (p1isFail == false && p2isFail == false)) {
//讓人動起來
if(fx1.equals("R")) {
p1X += 25;
if(p1X >= 750) { p1X = 750; }
}
if(fx1.equals("L")) {
p1X -= 25;
if(p1X <= 0) { p1X = 0; }
}
if(fx1.equals("U")) {
p1Y -= 25;
if(p1Y <= 100) { p1Y = 100; }
}
if(fx1.equals("D")) {
p1Y += 25;
if(p1Y >= 600) { p1Y = 600; }
}
if(fx2.equals("R")) {
p2X += 25;
if(p2X >= 750) { p2X = 750; }
}
if(fx2.equals("L")) {
p2X -= 25;
if(p2X <= 0) { p2X = 0; }
}
if(fx2.equals("U")) {
p2Y -= 25;
if(p2Y <= 100) { p2Y = 100; }
}
if(fx2.equals("D")) {
p2Y += 25;
if(p2Y >= 600) { p2Y = 600; }
}
//讓怪物動起來
//怪物1
int i = random.nextInt(4) + 1;
if(i == 1) {
monster1X += 5;
if(monster1X >= 750) {monster1X = 750;}
}
if(i == 2) {
monster1X -= 5;
if(monster1X <= 0) {monster1X = 0;}
}
if(i == 3) {
monster1Y += 5;
if(monster1Y >= 600) {monster1Y = 600;}
}
if(i == 4) {
monster1Y -= 5;
if(monster1Y <= 100) {monster1Y = 100;}
}
//怪物2
int j = random.nextInt(4) + 1;
if(j == 1) {
monster2X += 5;
if(monster2X >= 750) {monster2X = 750;}
}
if(j == 2) {
monster2X -= 5;
if(monster2X <= 0) {monster2X = 0;}
}
if(j == 3) {
monster2Y += 5;
if(monster2Y >= 600) {monster2Y = 600;}
}
if(j == 4) {
monster2Y -= 5;
if(monster2Y <= 100) {monster2Y = 100;}
}
//怪物3
int k = random.nextInt(4) + 1;
if(k == 1) {
monster3X += 5;
if(monster3X >= 750) {monster3X = 750;}
}
if(k == 2) {
monster3X -= 5;
if(monster3X <= 0) {monster3X = 0;}
}
if(k == 3) {
monster3Y += 5;
if(monster3Y >= 600) {monster3Y = 600;}
}
if(k == 4) {
monster3Y -= 5;
if(monster3Y <= 100) {monster3Y = 100;}
}
//怪物4
int n= random.nextInt(4) + 1;
if(n == 1) {
monster4X += 5;
if(monster4X >= 750) {monster4X = 750;}
}
if(n == 2) {
monster4X -= 5;
if(monster4X <= 0) {monster4X = 0;}
}
if(n == 3) {
monster4Y += 5;
if(monster4Y >= 600) {monster4Y = 600;}
}
if(n == 4) {
monster4Y -= 5;
if(monster4Y <= 100) {monster4Y = 100;}
}
//怪物5
int m = random.nextInt(4) + 1;
if(m == 1) {
monster5X += 5;
if(monster5X >= 750) {monster5X = 750;}
}
if(m == 2) {
monster5X -= 5;
if(monster5X <= 0) {monster5X = 0;}
}
if(m == 3) {
monster5Y += 5;
if(monster5Y >= 600) {monster5Y = 600;}
}
if(m == 4) {
monster5Y -= 5;
if(monster5Y <= 100) {monster5Y = 100;}
}
//如果有玩家吃到食物
if(p1X == AppleX && p1Y == AppleY) {
p1score++;
AppleX = 25*random.nextInt(28);
AppleY = 100 + 25*random.nextInt(18);
} else if(p2X == AppleX && p2Y == AppleY) {
p2score++;
AppleX = 25*random.nextInt(28);
AppleY = 100 + 25*random.nextInt(18);
}
//如果有玩家碰到怪物,判定死亡,游戲結(jié)束 后續(xù)有修改,暫用
//怪物1死亡
if(p1X >= monster1X -25 && p1X <= monster1X +25) {
if(p1Y == monster1Y) { p1isFail = !p1isFail; p1score = p2score = 0;}
}
if(p1Y >= monster1Y -25 && p1Y <= monster1Y +25) {
if(p1X == monster1X) { p1isFail = !p1isFail; p1score = p2score = 0;}
}
if(p2X >= monster1X -25 && p2X <= monster1X +25) {
if(p2Y == monster1Y) { p2isFail = !p2isFail; p1score = p2score = 0;}
}
if(p2Y >= monster1Y -25 && p2Y <= monster1Y +25) {
if(p2X == monster1X) { p2isFail = !p2isFail; p1score = p2score = 0;}
}
//怪物2死亡
if(p1X >= monster2X -25 && p1X <= monster2X +25) {
if(p1Y == monster2Y) { p1isFail = !p1isFail; p1score = p2score = 0;}
}
if(p1Y >= monster2Y -25 && p1Y <= monster2Y +25) {
if(p1X == monster2X) { p1isFail = !p1isFail; p1score = p2score = 0;}
}
if(p2X >= monster2X -25 && p2X <= monster2X +25) {
if(p2Y == monster2Y) { p2isFail = !p2isFail; p1score = p2score = 0;}
}
if(p2Y >= monster2Y -25 && p2Y <= monster2Y +25) {
if(p2X == monster2X) { p2isFail = !p2isFail; p1score = p2score = 0;}
}
//怪物3死亡
if(p1X >= monster3X -25 && p1X <= monster3X +25) {
if(p1Y == monster3Y) { p1isFail = !p1isFail; p1score = p2score = 0;}
}
if(p1Y >= monster3Y -25 && p1Y <= monster3Y +25) {
if(p1X == monster3X) { p1isFail = !p1isFail; p1score = p2score = 0;}
}
if(p2X >= monster3X -25 && p2X <= monster3X +25) {
if(p2Y == monster3Y) { p2isFail = !p2isFail; p1score = p2score = 0;}
}
if(p2Y >= monster3Y -25 && p2Y <= monster3Y +25) {
if(p2X == monster3X) { p2isFail = !p2isFail; p1score = p2score = 0;}
}
//怪物4死亡
if(p1X >= monster4X -25 && p1X <= monster4X +25) {
if(p1Y == monster4Y) { p1isFail = !p1isFail; p1score = p2score = 0;}
}
if(p1Y >= monster4Y -25 && p1Y <= monster4Y +25) {
if(p1X == monster1X) { p1isFail = !p1isFail; p1score = p2score = 0;}
}
if(p2X >= monster4X -25 && p2X <= monster4X +25) {
if(p2Y == monster4Y) { p2isFail = !p2isFail; p1score = p2score = 0;}
}
if(p2Y >= monster4Y -25 && p2Y <= monster4Y +25) {
if(p2X == monster4X) { p2isFail = !p2isFail; p1score = p2score = 0;}
}
//怪物5死亡
if(p1X >= monster5X -25 && p1X <= monster5X +25) {
if(p1Y == monster5Y) { p1isFail = !p1isFail; p1score = p2score = 0;}
}
if(p1Y >= monster5Y -25 && p1Y <= monster5Y +25) {
if(p1X == monster5X) { p1isFail = !p1isFail; p1score = p2score = 0;}
}
if(p2X >= monster5X -25 && p2X <= monster5X +25) {
if(p2Y == monster5Y) { p2isFail = !p2isFail; p1score = p2score = 0;}
}
if(p2Y >= monster5Y -25 && p2Y <= monster5Y+25) {
if(p2X == monster5X) { p2isFail = !p2isFail; p1score = p2score = 0;}
}
//如果有玩家達(dá)到指定積分,判定獲勝,游戲結(jié)束
if(p1score == 15) { p2isFail = !p2isFail; }
if(p2score == 15) { p1isFail = !p1isFail; }
repaint();
}
timer.start();
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
}
游戲規(guī)則(使用彈窗)部分
package 代碼部分;
import javax.swing.*;
import java.awt.*;
public class TestGameRule extends JDialog {
private int num = 1;
public TestGameRule() {
TextArea textArea = new TextArea(20,10);
textArea.setText("游戲中有五個怪物隨機(jī)移動,碰到怪物算死亡\
游戲中有隨機(jī)出現(xiàn)的蘋果,碰到一個蘋果加一分,\
先達(dá)到十五分或者對手死亡算游戲勝利!");
JScrollPane jScrollPane = new JScrollPane(textArea);
this.add(jScrollPane);
this.setBounds(200,200,400,400);
this.setVisible(true);
textArea.setEditable(false);
setResizable(false);
textArea.setBackground(Color.PINK);
}
}
圖片素材
package 代碼部分;
import javax.swing.*;
import java.net.URL;
public class TestGameData {
public static URL headerurl = TestGameData.class.getResource("/圖片素材/header.jpg");
public static URL p1player1url = TestGameData.class.getResource("/圖片素材/1.jpg");
public static URL p2player2url = TestGameData.class.getResource("/圖片素材/2.jpg");
public static URL appleurl = TestGameData.class.getResource("/圖片素材/apple.jpg");
public static URL monsterurl = TestGameData.class.getResource("/圖片素材/monster.jpg");
public static ImageIcon p1player1 = new ImageIcon(p1player1url);
public static ImageIcon p2player1 = new ImageIcon(p2player2url);
public static ImageIcon header = new ImageIcon(headerurl);
public static ImageIcon apple = new ImageIcon(appleurl);
public static ImageIcon monster = new ImageIcon(monsterurl);
}
主函數(shù)
package 代碼部分;
import javax.swing.*;
public class TestStartGame {
public static void main(String[] args) {
//制作窗口
JFrame jFrame = new JFrame("2D對戰(zhàn)小游戲");
jFrame.setBounds(10,10,790,660);
jFrame.setResizable(false);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//添加游戲面板
jFrame.add(new TestGamePanel());
//設(shè)置可見
jFrame.setVisible(true);
}
}
實現(xiàn)效果
以上就是用 Java 語言制作一個簡單的小游戲的全部內(nèi)容,想要了解更多 Java 有趣好玩的相關(guān)內(nèi)容請搜索W3Cschool以前的文章或繼續(xù)瀏覽下面的相關(guān)文章!