Java_ Pinball Game

Source: Internet
Author: User

Pinball Game Implementation principle:

Redraw the image at a certain time (less than 1 seconds) because the graphics class is an abstract class that requires rewriting all the methods involved when creating subclasses, so this is done by creating a subclass of the canvas, just overriding its paint () method. Here we use the keyboard to listen to events, Timer class and so on.

Game Description:

The ball in the pinball game increases speed over time and speeds up to 10 of the horizontal and vertical velocities. When the y-coordinate (vertical coordinate) of the ball is greater than the y-coordinate of the racket (vertical coordinate), the game ends. The console shows the speed of the X-direction of the ball and the speed of the Y-direction.

1 Importjava.awt.*;2 ImportJava.util.Random;3 ImportJavax.swing.Timer;4 Importjava.awt.event.*; 5  Public classPinball6 {7     PrivateFrame f=NewFrame ("Pinball Game");8Random rand=NewRandom ();9     //width and height of the desktopTen     Private Final inttable_width=300; One     Private Final inttable_height=400; A     //width and height of racquet, horizontal position, vertical position -     Private Final intRacket_width=60; -     Private Final intRacket_height=20; the     Private intRacketx=rand.nextint (24) *10; -     Private intrackety=300; -     //the size, speed, and coordinates of the ball -     Private Final intBall_size=16; +     Private intYspeed=1; -     Private DoubleXyrate=1; +     Private intXspeed= (int) (xyrate*yspeed); A     Private intBallx=rand.nextint (284); at     Private intBally=1; -     //Create a canvas -     PrivateMyCanvas tablearea=NewMyCanvas (); -     //Defining Time Classes - timer timer; -     //Flag of whether the game is over in     Private BooleanIslose=false; -     //set the game level to     Private intTime_times=1; +      Public voidinit () { -Tablearea.setpreferredsize (NewDimension (table_width,table_height)); the F.add (Tablearea); *         //defining keyboard Listeners $Keyadapter keyprocessor=NewKeyadapter ()Panax Notoginseng         { -              Public voidkeypressed (keyevent ke) { the                 if(Ke.getkeycode () = =keyevent.vk_left) { +                     if(racketx>0) Aracketx-=10; the                 } +                 if(Ke.getkeycode () = =keyevent.vk_right) { -                     if(racketx<table_width-racket_width) $racketx+=10; $                 } -             } -  the         }; - F.addkeylistener (keyprocessor);Wuyi         //Tablearea.addkeylistener (keyprocessor); theActionListener taskperformer=evt-> -         { Wu             //The ball touches the left or right border . -             if(ballx<=0| | ballx>=table_width-ball_size) { Aboutxspeed=-xspeed; $             } -             if(bally>rackety-ball_size&& (ballx<racketx| | ballx>racketx+racket_width-ball_size)) { - timer.stop (); -Islose=true; A tablearea.repaint (); +}Else if(bally<=0| | (bally>=rackety-ball_size&&ballx>racketx&&ballx<=racketx+racket_width)) { theyspeed=-Yspeed; -             } $bally+=Yspeed; theballx+=xspeed; the tablearea.repaint (); the             if((xspeed<10&&xspeed>-10) && (yspeed<10&&yspeed>-10)){ thetime_times++; -             } in             if(time_times==10){ the                 if(xspeed>0){ thexspeed++; About}Else{ thexspeed--; the                 } the                 if(yspeed>0){ +yspeed++; -}Else{ theyspeed--;Bayi                 } thetime_times-=10; theSystem.out.println (xspeed+ "" +yspeed); -             } -         }; theTimer=NewTimer (100, Taskperformer); the Timer.start (); the F.pack (); theF.setvisible (true); -     } the     classMyCanvasextendsCanvas the     { the          Public voidPaint (Graphics g) {94             if(islose) { theG.setcolor (NewColor (255,0,0)); theG.setfont (NewFont ("Times", font.bold,30)); theg.DrawString ("Game Over", 50,200);98}Else{ AboutG.setcolor (NewColor (240,240,80)); - G.filloval (ballx,bally,ball_size,ball_size);101G.setcolor (NewColor (80,80,200));102 G.fillrect (racketx,rackety,racket_width,racket_height);103             }104         } the     }106      Public Static voidMain (string[] args)107     {108         NewPinball (). Init ();109     } the}

Java_ Pinball Game

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.