I am participating in the Mid-Autumn Festival Creative Submission contest, please see: Mid-Autumn Festival Creative Submission Contest for details
preface
When I saw the Mid-Autumn Festival creative submission contest, my first idea was to make a small game related to Mid-Autumn Festival. After my meditation, finally established the content of the game is to catch moon cakes.
Watch with me how I achieve this mooncake catch game!
Tips: The following code has been open source on Github: Java Catch mooncake game
I. Game content
The moon cake mini game is mainly divided into the following two parts:
- Scenes and Objects
- The specific style
Scenes and Objects
The moon cake is set in a starry sky, so the background is a picture of the starry sky, as shown below:
There are three kinds of objects in the game: Chang ‘e, moon and moon cake. The pictures are as follows:
The overall game looks like this:
The specific style
Chang ‘e: As a character in the game, for the player to move
Moon: Obstacles in the game. If chang ‘e touches the moon while moving, the game stops
Moon cake: score props in the game. If chang ‘e touches the moon cake in the process of moving, score +1
Players can control chang ‘e to eat moon cakes, until the moon, the end of the game and prompts a happy Mid-Autumn Festival tips and eat the number of moon cakes
Try moving figure
A GIF of the demo looks like this:
Second, implementation ideas
In theory, the idea of making an H5 mini game is very good and can be tried online. However, considering that JS and CSS will not be used to create animation effects, this scheme is abandoned.
After some thought, I realized that Java can use Swing to draw graphics, so it is not difficult to achieve animation effects. So finally choose Swing to achieve this small game.
The general idea of implementation is divided into the following parts:
The goddess of the moon
You can view it as a square, with the arrow keysplease
和-
The mobileThe moon
和The moon cake
You can view it as a circle, moving from top to bottom, fromy = 0
Move to they = Height
- when
The goddess of the moon
encounterThe moon
When the game ends, whenThe goddess of the moon
encounterThe moon cake
When the score+ 1
Code implementation
Moon
The moon class
/** * obstacles - moon, touch will die */
public class Moon extends BaseGameObj {
private int speed = 10;
public Moon(int x, int y, String imageFile) {
super(x, y, imageFile, BaseGameObj.CIRCLE);
}
public void move(a) {
setY(y += speed);
}
// getter and setter
public int getSpeed(a) {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed; }}Copy the code
Cake
The moon cake class
/** * score object - moon cake, touch plus one point */
public class Cake extends BaseGameObj {
private int speed = 5; // Mooncakes run slowly, which is good for scoring
public Cake(int x, int y, String imageFile) {
super(x, y, imageFile, CIRCLE);
}
public void move(a) {
++speed;
setY(y += speed);
}
// getter and setter
public int getSpeed(a) {
returnspeed; }}Copy the code
ChangE
Chang e class
public class ChangeE extends BaseGameObj {
private final int frameWith;
public ChangeE(int x, int y, String imageFile, int frameWith) {
super(x, y, imageFile, BaseGameObj.RECTANGLE);
this.frameWith = frameWith;
}
public void move(int deltaX)
{
// Determine whether the left and right boundaries will be exceeded
int nextX = getX() + deltaX;
if (nextX + getWidth() > frameWith) {
nextX = frameWith - getWidth();
} else if (nextX < 0) {
nextX = 0; } setX(nextX); }}Copy the code
JPanel
Panel to draw the main code of the image
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Draw the background
ImageIcon icon=new ImageIcon(Thread.currentThread().getContextClassLoader().getResource("background.png"));
Image img=icon.getImage();
g.drawImage(img, 0.0.this.getWidth(), this.getHeight(), this);
// Draw the player
g.drawImage(myChangeE.getImage(), myChangeE.getX(), myChangeE.getY(), this);
// Draw the moon
for(Moon moon : moons) {
g.drawImage(moon.getImage(), moon.getX(), moon.getY(), this);
}
// Draw moon cakes
for (Cake cake : cakes) {
g.drawImage(cake.getImage(), cake.getX(), cake.getY(), this); }}Copy the code
- The main code for the game logic
When the moon moves out of the screen, remove the moon. When the moon moon moves out of the screen, remove the moon moon. If the list is empty, add two more moons and one moon moon
/** * update window */
private void updateFrame(a) {
ticks++;
for(int i = 0; i < moons.size(); i++)
{
Moon moon = moons.get(i);
if(ticks % 25= =0 && moon.getSpeed() < 10)
{
moon.setSpeed(moon.getSpeed() + 2);
}
}
Iterator<Moon> moonIterator = moons.iterator();
while (moonIterator.hasNext()) {
Moon moon = moonIterator.next();
// Off screen
if(moon.getY() > HEIGHT) {
moonIterator.remove();
} else
moon.move();
}
Iterator<Cake> cakeIterator = cakes.iterator();
while (cakeIterator.hasNext()) {
Cake cake = cakeIterator.next();
// Off screen
if(cake.getY() > HEIGHT) {
cakeIterator.remove();
} else
cake.move();
}
if(moons.size() == 0) { addMoonAndCake(); }}Copy the code
Judge chang ‘e is met the moon, if met the moon, the game is over and prompted a happy Mid-Autumn Festival! Determine if chang ‘e touched the moon cake. If so, add +1 to the score and remove the moon cake.
private boolean checkCollision(a) {
Rectangle rectangle = (Rectangle) changeE.getShape();
for(Moon moon : moons) {
Ellipse2D circle = (Ellipse2D) moon.getShape();
// Determine whether the circle collided
if (circle.intersects(rectangle)) {
gameOver = true;
}
}
Iterator<Cake> cakeIterator = cakes.iterator();
while (cakeIterator.hasNext()) {
Cake cake = cakeIterator.next();
Ellipse2D circle = (Ellipse2D) cake.getShape();
if (circle.intersects(rectangle)) {
score ++; / / scorecakeIterator.remove(); }}return gameOver;
}
Copy the code
Third, summary
In fact, the game is more about providing an idea, and there is still a lot of room for improvement.
Thank you for being here. It’s my pleasure to help you! ❤