13.6-Full stack Java notes: Flying Game Project | Shell|speed|launchframe

Source: Internet
Author: User
Tags cos rounds sin

Shell Design

Through the design of shells, we can learn more about the use of constructors and the use of containers. At the same time, it may be necessary for the reader to recall a little bit of the trigonometric functions learned in junior maths, so as to better understand the calculation principle of the projectile flight path. Of course, if you forget the knowledge, it doesn't matter, after all, in the actual development of mathematics is seldom involved in the content of the original reason.

Basic design of projectile class

Shells we implemented with a solid yellow ellipse, no longer loading new images. Of course, we can find some of the pictures of the shells in class.

Our logic is to generate shells at the window fixed position (200,200), where the shells are randomly oriented and the boundary bounces.

"Example 1" Shell class

Cn.sxt.game;

Import Java.awt.Color;

Import Java.awt.Graphics;

public class Shell extends Gameobject {

Double degree;

Public Shell () {

degree = Math.random () *math.pi*2;

x = 200;

y = 200;

width = 10;

Height = 10;

Speed = 3;

}


public void Draw (Graphics g) {

Save the state of the external incoming object G

Color C = G.getcolor ();

G.setcolor (Color.yellow);

G.filloval ((int) x, (int) y, width, height);

Shells flying along any angle

x + = Speed*math.cos (degree);

Y + = Speed*math.sin (degree);


The following code, used to achieve the boundary, the shells bounce back (the principle and play billiards game)

if (y>constant.game_height-height| | Y<30) {

degree =-degree;

}

if (x<0| | X>constant.game_width-width) {

degree = Math.pi-degree;

}

Return to external, change back to previous color

G.setcolor (c);

}

}


Arbitrary angular flight path of shells

Shells fly along any angle, with the core code on two lines:

x + = Speed*math.cos (degree);

Y + = Speed*math.sin (degree);

This is actually used in the first high school trigonometric function, through the cos/sin to the x-axis, the y-axis, so that can be accurate until the x, y coordinate changes. Junior trigonometric Functions Forget the children's shoes, self-brain repair.

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M01/9D/9D/wKioL1mCydqSTjiAAAIydJ6TPXs717.png-wh_500x0-wm_ 3-wmp_4-s_1140717924.png "title=" figure. png "alt=" wkiol1mcydqstjiaaaiydj6tpxs717.png-wh_50 "/>

Container Objects Store multiple rounds

To store multiple shells, we manage these objects by defining a container arraylist. Traverse all the objects in the container in the paint method and draw the shells.

"Example 2" Mygameframe class: Add ArrayList

public class Mygameframe extends Frame {

Image bgimg = gameutil.getimage ("images/bg.jpg");

Image planeimg = gameutil.getimage ("Images/plane.png");

Plane Plane = new Plane (planeimg,300,300,3);

arraylist<shell> shelllist = new arraylist<shell> ();

The Paint method works by drawing the entire window and internal content. Automatically called by the system.

@Override

public void Paint (Graphics g) {

G.drawimage (bgimg, 0, 0, NULL);

Plane.drawmyself (g); Draw the plane itself

Draw all the bullets in the container

for (int i=0;i<shelllist.size (); i++) {

Shell B = Shelllist.get (i);

B.draw (g);

}


}

The rest of the code, consistent with the previous version, is limited to space and is not shown here.

}

We initialize 50 rounds and add the code in example 13-20 in the window initialization method Launchframe ().

"Example 3" adding shells

Initialize, generate a bunch of shells

for (int i=0;i<50;i++) {

Shell B = new Shell ();

Shelllist.add (b);

}

Run the Mygameframe class, as shown in result 2:

650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M01/9D/9D/wKioL1mCyiWAqo5tAAGR0UVRs8w661.png-wh_500x0-wm_ 3-wmp_4-s_2486376127.png "title=" Figure 2.png "alt=" Wkiol1mcyiwaqo5taagr0uvrs8w661.png-wh_50 "/>

We can see in Figure 2 generated a number of shells, the game window lively a lot! You can extrapolate think about it, in fact, the game window of multiple monsters, multiple cars, multiple aircraft are generated multiple objects, using containers to manage the unified.



"Full stack Java notes" is a can help you from Zeto long for the full stack of Java Engineer Series of notes. The author is called Mr. G,10 years Java research and development experience, has been in the digital, Aerospace Institute of a Research and development center engaged in software design and research and development work, from small white gradually achieve engineers, senior engineers, architects. Proficient in Java Platform Software development, Master Java EE, familiar with various popular development framework.


The notes contain six parts from shallow into deep:

A-java Introductory Phase

B-Database from beginner to proficient

C-hand Edge mobile front end and web front end

D-j2ee from understanding to combat

E-java Advanced Frame Fine Solution

F-linux and Hadoop


This article is from the "full stack Java Notes" blog, be sure to keep this source http://javanew.blog.51cto.com/12931675/1953324

13.6-Full stack Java notes: Flying Game Project | Shell|speed|launchframe

Related Article

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.