“This is the 25th day of my participation in the First Challenge 2022. For details: First Challenge 2022”
❤️ Author’s home page: Xiao Xu Zhu
❤️ About the author: Hello everyone, I am Xiao Xu Zhu. Java field quality creator 🏆, CSDN blog expert 🏆, Huawei cloud enjoy expert 🏆, Nuggets of the year popular author 🏆, Ali Cloud expert blogger 🏆
❤️ technology live, the appreciation
❤️ like 👍 collect ⭐ look again, form a habit
preface
“Gold Miner” game is a classic gold catching game, it can exercise people’s reaction ability. In this game, you can earn points by “mining”, game props: there are 3 bottles of potion, in the recovery of the rope to catch gold when the speed is a little faster.
The main design
- Design the game interface, with Swing implementation
- Random creation of gold block algorithm
- Random block creation algorithm
- Timing system design
- Integral system design
- Set the mouse event, the left mouse button hook; Right mouse button start game, confirm to eat liquid medicine and other functions.
Screenshot function
The game begins:
gold
Code implementation
Game Core
public class GameWin extends JFrame { // Inherit JFrame to listen for mouse and keyboard events
// Set static variables to indicate game state 0 indicates not started 1 Running 2 store 3 failed 4 win
static int state;
// Create a linked list to store gold blocks
List<Object> objectList = new ArrayList<>();
Bg bg = new Bg();/ / the background
Line line = new Line(this);/ / line
{
// Can be placed
boolean isPlace = true;
// Loop to create gold
for (int i = 0; i < 11; i++){
double random = Math.random();
Gold gold;// Store the currently generated bullion
if (random<0.3){
gold = new GoldMini();
}else if(random<0.7){
gold = new Gold();
}else{
gold = new GoldPlus();
}
for (Object obj:objectList){
if (gold.getRoc().intersects(obj.getRoc())) {
// The overlap cannot be placed and needs to be regenerated
isPlace=false; }}// Determine whether the loop can be placed
if (isPlace){
objectList.add(gold);
}else {
isPlace=true; i--; }}// Loop to create a block
for (int i = 0; i < 5; i++){
Rock rock = new Rock();
for (Object obj:objectList){
if (rock.getRoc().intersects(obj.getRoc())) {
isPlace = false; }}if (isPlace){
objectList.add(rock);
}else {
isPlace=true;
i--;
}
}
}
Image offScreenImage;
void launch(a){ The launch method initializes the window information
this.setVisible(true);// Make the window visible
this.setSize(768.1000);
this.setLocationRelativeTo(null);// Window position: center
this.setTitle("The gold miners");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);// Close the window method
// Set the mouse event to change the state
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
// Different states issue different instructions
switch (state){
case 0:
if (e.getButton()==3){
state=1;
bg.startTime = System.currentTimeMillis();
}
break;
case 1:
// Swing left and right and click the left button
if(e.getButton() == 1 && line.state==0){
line.state = 1;
}
// Crawl back to right click
if (e.getButton()==3 && line.state==3 && Bg.waterNum>0){
Bg.waterFlag=true;
Bg.waterNum--;
}
break;
case 2:
if (e.getButton()==1){
bg.shop=true;
}
if (e.getButton()==3){
state=1;
bg.startTime = System.currentTimeMillis();
}
break;
case 3:
case 4:
if (e.getButton()==1){
state=0;
bg.reGame();
line.reGame();
}
break;
default:}}});// Make the line swing
while (true){
repaint();
nextLevel();
try {
Thread.sleep(10);
} catch(InterruptedException e) { e.printStackTrace(); }}}/ / the next level
public void nextLevel(a){
if (bg.gameTime() && state==1) {if (Bg.count >= bg.goal){// If the current integral is greater than or equal to the target integral
if (Bg.level==1){
state=4;
}else {
state=2;
Bg.level++;// Number of levels increased by one}}else {
state=3;
}
dispose();// Release the passed form
GameWin gameWin = new GameWin();
gameWin.launch();Call the launch method to draw a new window}}@Override
public void paint(Graphics g) {
// The canvas is the same size as the form
offScreenImage = this.createImage(768.1000);
// Add a brush to the canvas
Graphics gImage = offScreenImage.getGraphics();
bg.paintSelf(gImage);
if (state == 1) {// Draw the object first
for(Object obj:objectList){
obj.paintSelf(gImage);
}
Draw a line after the / /
line.paintSelf(gImage);
}
// Draw the canvas into the window
g.drawImage(offScreenImage,0.0.null);
}
public static void main(String[] args) {
GameWin gameWin = newGameWin(); gameWin.launch(); }}Copy the code
Hook type
public class Line {
// The starting point of the line
int x = 380;
int y = 180;
// End coordinates
int endx = 500;
int endy = 500;
// Line length
double length = 100;
// Minimum line length
double MIN_length = 100;
// Maximum line length
double MAX_length = 750;
double n = 0;
// Direction parameters
int dir = 1;
// State 0 swing state 1 grab state 2 Retrieve state 3 grab return state
int state;
// Picture of a claw
Image hook = Toolkit.getDefaultToolkit().getImage("imgs/hook.png");
// grab the judgment method
GameWin frame;
Line(GameWin frame){
this.frame=frame;
}
// Claw collision detection detects whether objects are captured
void logic(a){
for (Object obj:this.frame.objectList){
if(endx>obj.x && endx<obj.x+obj.width
&& endy>obj.y && endy<obj.y+obj.height){
state=3;
obj.flag=true; }}}// Draw method
void lines(Graphics g){
// Changes to endx and endy
endx = (int) (x + length*Math.cos(n*Math.PI));
endy = (int) (y + length*Math.sin(n*Math.PI));
g.setColor(Color.red);
// Make a bold red line
g.drawLine(x-1,y,endx-1,endy);
g.drawLine(x,y,endx,endy);
g.drawLine(x+1,y,endx+1,endy);
// Place the middle of the claw at the top of the red line
g.drawImage(hook,endx-36,endy-2.null);
}
void paintSelf(Graphics g){
logic();
// State of the line
switch (state){
case 0:
if(n<0.1){
dir = 1;
}else if(n>0.9){
dir = -1;
}
n = n + 0.005*dir;
lines(g);
break;
case 1:
// The length of the line is given a range
if(length<MAX_length){
// State 1 indicates the length of the left mouse button
length = length+5;
lines(g);
}else {
state = 2;
}
break;
case 2:
// The retractable line length becomes shorter
if(length>MIN_length){
// State 1 indicates the length of the left mouse button
length = length-5;
lines(g);
}else {
state = 0;
}
break;
case 3:
int m=1;
// Let the red line return
if(length>MIN_length){
length = length-5;
lines(g);
for (Object obj:this.frame.objectList){
// Determine whether to move
if (obj.flag){
m=obj.m;
obj.x = endx-obj.getWidth()/2;
obj.y = endy;
if (length<=MIN_length){
obj.x=-150;
obj.y=-150;
obj.flag=false;
Bg.waterFlag=false;
/ / points
Bg.count+=obj.count;
state = 0;
}
// Determine whether to use potions when fetching
if (Bg.waterFlag){
if (obj.type==1) {// If you catch gold
m=1;
}
if (obj.type==2) {// If you catch a rock
obj.x=-150;
obj.y=-150;
obj.flag=false;
Bg.waterFlag=false;
state = 2;
}
}
}
}
}
try {
Thread.sleep(m);
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
default:}}// Reset the line element
void reGame(a){
n=0;
length=100; }}Copy the code
conclusion
Through this “Gold Miner” implementation, let me have a further understanding of JAVA related knowledge, the Language of JAVA also have a deeper understanding than before.
Basic Java syntax, such as data types, operators, program flow control, and arrays, is better understood. Java is the core of the core object oriented thought, for this concept, finally realized some.
The source code for
After clicking “like” and following the blogger, the private blogger will get it for free