## 编程知识 cdmana.com

### Java basic national husband eat hot dog games

Image verification code  -- Pay Extension Education

Chapter one ： Hot dog eating game

Game effect

1. Encapsulating hot dogs

package cn.tx.main;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;
import java.util.Random;

/**
*  author ： Mr. Sakura
*  Hot dog
*/
public class HotDog {

//  Birth position x
private int x;
//  Birth position  y
private int y;
//  Pictures of hot dogs
private BufferedImage image;
//  random number
private Random random = new Random();
//  Hot dogs list aggregate
private List<HotDog> list;

/**
*  Where hot dogs appear at random
*/
public HotDog(List<HotDog> list){
try {
this.list = list;
//  Default appears from the top
y = 0;
// x The abscissa appears randomly
x = random.nextInt(400);
//  Store the hot dog in list Collection
} catch (IOException e) {
e.printStackTrace();
}
}

/**
*  The way hot dogs move
*/
public void move(){
//  The ordinate moves down
y += 10;
//  Judge y Whether the coordinate value is greater than the maximum height , If it is greater than , from list Remove the current hotdog object from the collection
if(y >= 600){
this.list.remove(this);

}
}

/**
*  Eat hot dog collision , Eliminate data
*  Return the amount of elimination
* @param wsc
*/
public int clear(Wsc wsc){
//  Start judging
//  Get the hotdog's abscissa
//  Get the left vertex abscissa of the hotdog
int dogx2 = x;
//  Get the right abscissa of the hot dog
int dogx3 = dogx2+66;

//  Get the hotdog's ordinate
int dogy = y;
//  Get the ordinate of the hotdog image plus the length of the hotdog
int dogy2 = dogy+102;

//  Get Wang Sicong's mouth
int wscx = wsc.getX();
int wscx2 = wscx+30;
int wscx3 = wscx + 80;

int wscy = wsc.getY();

//  Eliminate
// int flagx2 = wscx2 - dogx2;
int flagx2 = dogx2 - wscx2;
// int flagx3 = wscx3 - dogx3;
int flagx3 = dogx3 - wscx3;

// int flagy = wscy - dogy2;
int flagy = dogy2 - wscy;

if(flagx2 > -20 && flagx3 < 20 && flagy > 20 && flagy < 40){
boolean remove = this.list.remove(this);
if(remove){
return 1;
}
}
return 0;
}

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 BufferedImage getImage() {
return image;
}

public void setImage(BufferedImage image) {
this.image = image;
}
}

2. Encapsulate the Sicong class

package cn.tx.main;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
*  author ： Mr. Sakura
*  Sicong
*/
public class Wsc{

//  Horizontal and vertical coordinates of the picture
private int x;
private int y;
//  picture
private BufferedImage image;

public Wsc() {
try {
//  Default display position
x = 180;
y = 500;
} catch (IOException e) {
e.printStackTrace();
}
}

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 BufferedImage getImage() {
return image;
}

public void setImage(BufferedImage image) {
this.image = image;
}

}

Programming the main program

package cn.tx.main;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;

/**
*  Pay Extension Education  --  Tencent course certification body
*  Mr. Sakura
*/
public class DogGame extends JPanel implements Runnable,MouseMotionListener {

//  Construct the object of Sicong
private Wsc wsc = new Wsc();
//  Hot dogs list aggregate
private List<HotDog> list = new ArrayList<>();
//  The total score is used
int sum = 0;

//  Add the background image through the construction method
public DogGame(){
//  Set the background image
Icon icon = new ImageIcon(DogGame.class.getResource("/111.png"));
JLabel label = new JLabel(icon);
}

/**
*  In the form of a timer , Create a lot of hotdog objects , Deposit to list Collection
*  Go back and practice the code yourself
*  Use Java It's a timer API, Timer intervals 1 The method is executed once a second
*/
public void createHotDogs() {
@Override
public void run() {
//  Create a hotdog object
HotDog dog = new HotDog(list);
}
};
//  Timer
java.util.Timer timer = new java.util.Timer();
}

/**
*  The drawing method of rewriting panel
* @param g
*/
@Override
public void paint(Graphics g) {
super.paint(g);
//  Painting Wang Sicong
g.drawImage(wsc.getImage(),wsc.getX(),wsc.getY(),null);

//  Drawing hot dogs
for (int i = 0; i < list.size(); i++) {
HotDog hotDog = list.get(i);
//  Let the hot dog move
hotDog.move();
//  Eliminate hot dogs
int n = hotDog.clear(wsc);
//  score
sum += n;
//  Drawing hot dogs
g.drawImage(hotDog.getImage(),hotDog.getX(),hotDog.getY(),null);
}
g.setColor(Color.PINK);
g.setFont(new Font(" Official script ",Font.BOLD,20));
//  Draw numbers
g.drawString(" You ate "+sum+" A hot dog ",20,30);
if(sum >= 5){
g.drawString(" frigging awesome , It's delicious ！",200,30);
}
}

/**
*/
@Override
public void run() {
while(true){
//  Repaint
this.repaint();
try {
//  Sleep manipulation
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

@Override
public void mouseDragged(MouseEvent e) {

}

@Override
public void mouseMoved(MouseEvent e) {
//  Get the coordinates of the move
int x = e.getX();
//  Set the coordinates for Sicong
wsc.setX(x);
}

/**
*  The main function , Run the main function to start the game
* @param args
*/
public static void main(String[] args) {
//  create a window
JFrame frame = new JFrame(" Hot dog eating game ");
//  Set width and height
frame.setSize(500,650);
//  centered
frame.setLocationRelativeTo(null);
//  Set off
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//  Create game main class
DogGame dogGame = new DogGame();

//  Create multiple hot dogs
dogGame.createHotDogs();

//  Draw Sicong up , Call the redraw method
dogGame.repaint();

//  Start thread , Refresh regularly

//  Bind mouse to listen to events

//  Display window
frame.setVisible(true);
}

}

Based on my many years of working experience and learning experience , Recorded a new set of Java Focus on video tutorials , If you are learning now Java, At the beginning of learning Java There is no systematic learning course in the process , You can add my WeChat renlliang2013 Exchange of learning , Access to learning materials , Interview questions , Development tools, etc .

Scroll to Top