java Simple game making

Game ideas : Set up character movement , Rules of the game , Integral system , Random moving monster , Game decision , timer .

Game content
package Code section ; 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 {
// Initialize person coordinates int p1X; int p1Y; int p2X; int p2Y; boolean isStart = false; // Does the game start
boolean p1isFail = false; // Is the game a failure boolean p2isFail = false; String fx1; // Left :L,
right :R, upper :U, lower :D String fx2; Timer timer = new Timer(50,this);// timer // integral int
p1score= 0; int p2score = 0; // Apple int AppleX; int AppleY; // monster int monster1X;
int monster1Y; int monster2X; int monster2Y; int monster3X; int monster3Y; int
monster4X; int monster4Y; int monster5X; int monster5Y; // Random integral Random random =
new Random(); public TestGamePanel() { init(); this.setFocusable(true); this.
addKeyListener(this); timer.start(); } // initialization 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(); } }); } // Game function button JButton kaishi = new JButton(" start ");
JButton chongkai= new JButton(" restart "); JButton guize = new JButton(" Rules of the game "); // Drawing board
@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); // Figure painting TestGameData.p1player1.paintIcon(this,g,p1X,p1Y);
TestGameData.p2player1.paintIcon(this,g,p2X,p2Y); // Draw score g.setFont(new Font(
" Colorful Chinese ",Font.BOLD,18)); // Set font g.setColor(Color.RED); g.drawString(" game player 1:" +
p1score,20,20 ); g.drawString(" game player 2:" + p2score,680,20); // Draw apples TestGameData.apple
.paintIcon(this,g,AppleX,AppleY); // Draw static monsters 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); // Game tips , Do you want to start if(!isStart) { g.setColor(Color.BLACK);
g.setFont(new Font(" Colorful Chinese ",Font.BOLD,30)); g.drawString(" Please click to start the game ",300,300); }
// End of game tips , Do you want to start over if(p2isFail || p1score == 15) { g.setColor(Color.RED); g.setFont
(new Font(" Colorful Chinese ",Font.BOLD,30)); g.drawString(" As soon as the player wins , Please click to restart the game ",200,300); } if(
p1isFail|| p2score == 15) { g.setColor(Color.RED); g.setFont(new Font(" Colorful Chinese ",
Font.BOLD,30)); g.drawString(" Player two wins , Please click to restart the game ",200,300); } } // Keyboard monitoring events @Override
public void keyPressed(KeyEvent e) { // Control the movement of characters // game player 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;} } // game player 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)) { // Make people move 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; } } // Let the monster move // monster 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;} } // monster 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;} } // monster 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;} } // monster 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;} } // monster 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 a player eats food 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); }
// If a player encounters a monster , Judgment of death , game over There are subsequent changes , Temporary use // monster 1 death 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;} } // monster 2 death 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;} }
// monster 3 death 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;} } // monster 4 death 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;} } // monster 5 death 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;} }
// If a player reaches the specified point , Decide to win , game over 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) { }
}
Rules of the game ( Using pop ups ) part
package Code section ; 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(
" Five monsters move randomly in the game , It's death to meet a monster \\\n There are random apples in the game , If you come across an apple, add one point ,\\\n 15 points or the opponent's death is the game's victory !");
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); } }
Picture material
package Code section ; import javax.swing.*; import java.net.URL; public class
TestGameData { public static URL headerurl = TestGameData.class.getResource(
"/ Picture material /header.jpg"); public static URL p1player1url = TestGameData.class.
getResource("/ Picture material /1.jpg"); public static URL p2player2url = TestGameData.class.
getResource("/ Picture material /2.jpg"); public static URL appleurl = TestGameData.class.
getResource("/ Picture material /apple.jpg"); public static URL monsterurl = TestGameData.
class.getResource("/ Picture material /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); }
Principal function
package Code section ; import javax.swing.*; public class TestStartGame { public static
void main(String[] args) { // Production window JFrame jFrame = new JFrame("2D Battle games "); jFrame.
setBounds(10,10,790,660); jFrame.setResizable(false); jFrame.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Add game panel jFrame.add(new
TestGamePanel()); // Set visible jFrame.setVisible(true); } }
Implementation effect

Technology
©2019-2020 Toolsou All rights reserved,
Hikvision - Embedded software written test questions C Language application 0 The length of array in memory and structure is 0 In depth analysis data structure --- The preorder of binary tree , Middle order , Subsequent traversal How to do it ipad Transfer of medium and super large files to computer elementui Shuttle box el-transfer Display list content text too long 2019 The 10th Blue Bridge Cup C/C++ A Summary after the National Games ( Beijing Tourism summary )unity Shooting games , Implementation of first person camera python of numpy Module detailed explanation and application case Study notes 【STM32】 Digital steering gear Horizontal and vertical linkage pan tilt Vue Used in Element Open for the first time el-dialog Solution for not getting element