Java_ Pinball Games

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.

1Import java.awt.*;2ImportJava.util.Random;3ImportJavax.swing.Timer;4Import java.awt.event.*;5PublicClassPinball6{7Private Frame f=New Frame ("Pinball Game");8 Random rand=NewRandom ();9//Width and height of the desktop10PrivateFinalint table_width=300;11PrivateFinalint table_height=400;12//Width and height of racquet, horizontal position, vertical position13PrivateFinalint racket_width=60;14PrivateFinalint racket_height=20;15Privateint Racketx=rand.nextint (24) *10;16Privateint rackety=300;17//The size, speed, and coordinates of the ball18PrivateFinalint ball_size=16;19Privateint yspeed=1;20PrivateDouble xyrate=1;21stPrivateint xspeed= (int) (xyrate*Yspeed);22Privateint Ballx=rand.nextint (284);23Privateint Bally=1;24//Create a canvas25Private MyCanvas tablearea=NewMyCanvas ();26//Defining Time Classes27Timer timer;28//Flag of whether the game is over29PrivateBoolean islose=False;30//Set the game level31Privateint Time_times=1;32PublicvoidInit () {Tablearea.setpreferredsize (NewDimension (table_width,table_height));34F.add (Tablearea);35//Defining keyboard listenersKeyadapter keyprocessor=NewKeyadapter ()37{38Publicvoidkeypressed (KeyEvent ke) {39if (ke.getkeycode () = =Keyevent.vk_left) {40if (racketx>0)racketx-=10;42}43if (ke.getkeycode () = =Keyevent.vk_right) {44if (racketx<table_width-Racket_width)racketx+=10;46}47}4849};50F.addkeylistener (Keyprocessor);51//Tablearea.addkeylistener (Keyprocessor);ActionListener taskperformer=evt->53{54//The ball touches the left or right border.55if (ballx<=0| | ballx>=table_width-Ball_size) {xspeed=-XSpeed;57}58if (bally>rackety-ball_size&& (ballx<racketx| | ballx>racketx+racket_width-Ball_size)) {59Timer.stop ();Islose=True;61Tablearea.repaint ();62}Elseif (bally<=0| | (bally>=rackety-ball_size&&ballx>racketx&&ballx<=racketx+Racket_width)) {yspeed=-Yspeed;64} Bally+=yspeed; Ballx+=xspeed; Tablearea.repaint (); (xspeed<10&&xspeed>-10 && (yspeed<10&&yspeed>-10)) {time_times++; 70} if (time_times==10) {xspeed>0) {xspeed++;}else{, xspeed--, and (yspeed>0) {yspeed++,}e lse{yspeed--, Bayi} time_times-=10; System.out.println (xspeed+ "" +yspeed); 84} 85}; Timer=new timer (100,taskperformer); Timer.start (); F.pack (); F.setvisible (TRUE); The MyCanvas extends Canvas (Graphics g) {94 if (Islose) {G.setcolor 255,0 (new Color, 0)); G.setfont (New Font ("Times", font.bold,30)); g.DrawString ("Game Over", 50,200); 98}else{G.setcolor (New Color (240,240,80)), G.filloval (ballx,bally,ball_size,ball_size); 101 G.setcolor (new Color (80,80,200)); 102 G.fillrect (Racketx,rackety,racket_width,racket_height); 103}104}105}106 public static void Main (string[] args) 107 {108 New Pinball (). Init (); 109}110}

Java_ Pinball Games

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.