JAVA Zero Foundation-tank wars (I)

Source: Internet
Author: User

 

I spent a whole day watching the video of the tank war standalone version that Jack Ma told me. As a java beginner, I have gained a lot,

In particular, I would like to summarize the object-oriented method for future use.

(The overall code will be attached and detailed comments will be provided at the end.) Note that this mini-game is just familiar with the java basics, not to mention the project ~

The function that has been implemented by now is to draw a tank that can be flexibly moved in eight directions, and press CTRL to create a shell.

Step 1: Practice basic GUI knowledge

Code of this phase

1ImportJava. awt.*;
2ImportJava. awt. event.*;
3
4Public ClassTankClientExtendsFrame {
5
6 Public VoidLauchFrame (){
7 This. SetLocation (400,300);//Set the initial position
8 This. SetSize (800,600);//SET SIZE
9 This. SetTitle ("TankWar");//Set title
10 This. AddWindowListener (NewWindowAdapter (){
11 Public VoidWindowClosing (WindowEvent e ){
12System. exit (0);
13}
14});//Close the Anonymous class implementation window
15 This. SetResizable (False);//Fix the window
16SetVisible (True);//Display the window
17}
18
19 Public Static VoidMain (String [] args ){
20TankClient tc= NewTankClient ();
21Tc. lauchFrame ();//Start window
22}
23
24}

Phase 2 draws a tank and moves it in four directions: After this stage is completed, the tank moves in four directions according to the keyboard control, but the movement is not flexible.

(1) override the paint method and draw the solid circle representing the tank. The paint () method is automatically called when it is activated in each window, And the repaint () method also automatically calls the paint () method.

This Code 

1Public VoidPaint (Graphics g ){//G is equivalent to a paint brush.
2Color c=G. getColor ();//Get the current color to restore
3G. setColor (Color. RED );
4G. fillOval (x, y,30,30);//Draw a circle
5G. setColor (c );//Restore to initial color
6}

(2) Let the tank move. In fact, I thought it was very simple. Click the direction key and change the values of x and y, for example, x + = 5, y + = 5, and then re-draw the image, can the tank be moved? However, if you press the key once

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.