Java game development-a few simple lines of code have completed the programming of the driver genie

Source: Internet
Author: User

Today, I wrote a driver genie, and a few lines of code are complete. Although simple, it is very useful, many games developed using java and android use more or less the simple code.

Packagecom. csdn. code;

 

Importjava. awt. Color;

Importjava. awt. Frame;

Importjava. awt. Graphics;

Importjava. awt. event. WindowAdapter;

Importjava. awt. event. javaswevent;

Public classDriveElf extends Frame {

Private int x = 40; // the initial position of the genie on the canvas-abscissa

Private int y = 40; // the initial position of the genie on the canvas-ordinate

Private int arc = 320; // angle of combination of Sprite mouth

Public DriveElf (){

SetBounds (200,200,600,500 );

SetVisible (true );

SetBackground (Color. cyan );

AddWindowListener (new WindowAdapter (){

Public voidwindowClosing (WindowEvent e ){

System. exit (0 );

}

});

New Thread (){

Public void run (){

While (true ){

// Two for loops are used to move the genie on the canvas.

For (inti = 0; I <40; I ++ ){

Arc + = 1;

X + = 10;

Y + = 3;

}

Try {

Thread. sleep (50 );

} Catch (effectione ){

E. printStackTrace ();

}

Repaint ();

For (inti = 0; I <40; I ++ ){

Arc-= 1;

X-= 8;

Y-= 1;

Try {

Thread. sleep (100 );

} Catch (effectione ){

E. printStackTrace ();

}

Repaint ();

}

}

}

}. Start ();

}

// Spray the painting method for drawing on the drawing board

Public void paint (Graphics g ){

G. setColor (Color. red );

G. fillArc (x, y, 90, 90, 0, arc); // draw the sprite body

G. setColor (Color. gray );

G. fillArc (x + 50, y + 10, 20, 20, 0,360); // draw the sprite

G. setColor (Color. black );

G. fillArc (x + 56, y + 15, 10, 10, 0,360); // draw an eye ball

}

Public static void main (String [] args ){

New DriveElf ();

 

}

 

}

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.