WeChat plane hitting-Java Edition

Source: Internet
Author: User
Tags character classes getcolor

In the world of things, everybody can fly now...

Last weekend, I was bored. I wrote a Java version of airplane hitting game .. I want to talk with you, but don't like it (there are already a lot of resources on the network)


Current functions:

1. The enemy has two types of aircraft: small aircraft and Big BOSS aircraft (1 drop of blood for small aircraft; 10 drops of blood for BOSS. We only set one drop of blood for you to modify the blood parameter)

2. Pills: blue pills and Red Pills

3: blue pills can trigger dual bullets (with time limit)

4: the number of existing red pills is displayed at the bottom of the full screen explosion screen.

5: button Description: The game is suspended. p: The game continues. s: The game starts again. r: the arrow key controls the upper, lower, and lower sides. The red pill uses spaces.

Let's take a look at some pictures (that... The plane exploded... Replace it with something yellow ..)


(Note) Details of game production:

(1) display in the lower left corner of Figure 1

(2) Dual-bullet design: if one of them hits the enemy plane, the other should move forward.


The beautification of the image is really not good. I borrowed the plane to make materials. The copyright belongs to the original author.


------------------------------------------------------------------------

1. Quick Start

In fact, in this game, you mainly plan what roles you have and what they are doing at each frame, and then use what data structure to represent them (we should say that no algorithm is designed at all, data structure)

Before lengthy descriptions, Let me give an example, for example, how to move an object over time on the interface (the boundary issue is not considered for the time being)

Implementation:
1: The real-time rendering background is black
2: a red ball falling from the top of the interface
3: As time increases, the speed increases.
4


First, we need a JFrame window. This inheritance is good, and then the painting () method is implemented to implement our painting. The background is simple, but why is it updated in real time?

Because the animation we see here is the rapid splicing of each frame of the image, and each image needs to be re-drawn, otherwise it will be re-drawn on the original image, so, if you do not refresh the interface each time, there will be the last stranded image.

The paint method is as follows:


@Overridepublic void paint(Graphics g) {//super.paint(arg0);//before draw, move firsttime++;y += velocity + time;//drawColor c = g.getColor();g.setColor(Color.BLACK);g.fillRect(0, 0, 480, 800);g.setColor(Color.RED);g.fillOval(x, y, 10, 10);g.setColor(c);}

However, if there are too many objects to be drawn, we will find that there is a choppy phenomenon, which is also a problem all the display will encounter. We generally use double buffering to solve this problem. We are not a large game here, simply use the following method to implement the dual-buffering mechanism, that is, we first draw the next frame of the image and then display it, rather than draw the edge display.

We declare a public variable Image buffer first, which stores the next frame of the Image and implements the rendering in the update () function.

@ Overridepublic void update (Graphics g) {// super. update (g); if (buffer = null) buffer = this. createImage (480,800); Graphics gBuffer = buffer. getGraphics (); paint (gBuffer); // first draw in the buffer g. drawImage (buffer, 0, 0, null );}

Of course, the g parameter passed in by update () is the paint brush of the window.

As time increases, the speed increases, which has been simply implemented in the paint () function ..

Then there is how to implement real-time rendering. Of course, a timer is needed .. So it's not long before I start a thread dedicated to updating images?

The key update here is to use mf. update (mf. getGraphics (); then call the update () We just implemented, and then call the paint ()

class MainThread implements Runnable{MainFrame mf = null;public MainThread(MainFrame mf) {this.mf = mf;}@Overridepublic void run() {while(true){try{Thread.sleep(50);mf.update(mf.getGraphics());}catch(Exception e){e.printStackTrace();}}}}

The above process is very clear, that is, let the thread take charge of the time refresh, and then the notification window, to be updated !! The window object calls the UPDATE function, and the update function notifies the logic to draw the function paint (). You have the final say about what to update... Therefore, this program is divided into two parts:

UI update: logic Update (currently implemented in paint)

So this app is encapsulated. We need to play the game in the future. Soon, we just need to do something in the paint () function. Isn't that enough?


The code for this demo is given below

Package ylf. graphics; import java. awt. color; import java. awt. graphics; import java. awt. image; import javax. swing. JFrame;/*** @ author ylf */public class MainFrame extends JFrame {int time; int velocity; int x; int y; Image buffer = null; public MainFrame () {init (); this. setSize (480,800); this. setVisible (true); new Thread (new MainThread (this )). start () ;}public void init () {time = 0; velocity = 0; x = 240; y = 0 ;}@ Overridepublic void paint (Graphics g) {// super. paint (arg0); // before draw, move firsttime ++; y ++ = velocity + time; // drawColor c = g. getColor (); g. setColor (Color. BLACK); g. fillRect (0, 0,480,800); g. setColor (Color. RED); g. fillOval (x, y, 10, 10); g. setColor (c) ;}@ Overridepublic void update (Graphics g) {// super. update (g); if (buffer = null) buffer = this. createImage (480,800); Graphics gBuffer = buffer. getGraphics (); paint (gBuffer); // first draw in the buffer g. drawImage (buffer, 0, 0, null);} class MainThread implements Runnable {MainFrame mf = null; public MainThread (MainFrame mf) {this. mf = mf ;}@ Overridepublic void run () {while (true) {try {Thread. sleep (50); mf. update (mf. getGraphics ();} catch (Exception e) {e. printStackTrace () ;}}} public static void main (String [] args) {MainFrame m = new MainFrame ();}}


2. Aircraft hitting Logic

Now, you can implement the above section by yourself: you are about to fly!

Next we will teach you how to make a comfortable

Now that we can extract all the logic services of an airplane, we will not disturb this Frame class. We have an independent Controller class that is responsible for game business control, for example, what data structure is used to hold the object of an airplane and a group of aircraft objects of an enemy? Of course, the linked list is used ..) This logic is also used to control scores, as well as the overall logic for starting, pausing, and restarting some peripherals and running the game.

I posted the Controller's onDraw () function so that everyone can understand the game logic.

Scheduled enemy planes

Regularly arrange pill appearance

Refresh our aircraft location status

Refresh enemy aircraft location status

Refresh explosion Animation

Refresh the pill Count display

Refresh score

Public void onDraw (Graphics g) {////// game logic /////////////// schedular produce plane-other // schedule flights on a regular basis, the departure plane type has an airplane factory to generate if (++ readyOther) % 10 = 0) {readyOther = 0; Random rand = new Random (); OtherPlaneFactory. getPlanes (rand. nextInt (2), others, this);} // schedular produce equipment // schedule the pill to appear on a regular basis. In fact, you can also get a pill factory if (++ readyPowerful) % 600 = 0) {Random rand = new Random (); if (rand. nextBoolean () equipments. add (new PowerEquipment (rand. nextInt (MainFrame. FRAME_WIDIH), 0, this); elseequipments. add (new BombEquipment (rand. nextInt (MainFrame. FRAME_WIDIH), 0, this);} // The logic is assigned to these game roles with Color oldColor = g. getColor (); myPlane. onDraw (g); // logic control of our aircraft for (int I = 0; I
 
  

3. CAPTCHA

The main thing is that role settings are the way to learn java.

Here is how to use some java design principles.

The encapsulation of interfaces and the inheritance of character classes, I think it is best to use interfaces when using interfaces. Some functions are too undefined. It is best to use interfaces in the form of plugging interfaces.

For example, an object must be able to motion. Instead of inheriting a parent class with onMove () for all roles, we can use the Moveable interface. This flexibility is much better than the usage class. Of course, I also have many defects in implementation for your reference only:

Http://download.csdn.net/detail/ylf13/6870955

You are still working on git .. Not very useful. Download the address after uploading it.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.