Java--Table tennis ping-pong pinball game __java

Source: Internet
Author: User
Tags stub

"Crazy Java Handouts" practice games

Import Java.awt.Canvas;
Import Java.awt.Color;
Import java.awt.Dimension;
Import Java.awt.Font;
Import Java.awt.Frame;
Import Java.awt.Graphics;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Java.awt.event.KeyAdapter;
Import java.awt.event.KeyEvent;
Import Java.awt.event.WindowAdapter;
Import java.awt.event.WindowEvent;

Import Java.util.Random;

Import Javax.swing.Timer;
	public class Pinball {/** * @param args *//Desktop long width private final int table_width = 300;
	Private final int table_height = 400;
	The vertical position of the racket private final int racket_y = 340;
	Height and width of racket private final int racket_height = 20;
	Private final int racket_width = 60;
	Ball size private Final int ball_size = 16;
	Private frame F = new Frame ("Pinball");
	Random rand = new Random (); 
	Small ball longitudinal running speed private int yspeed = 10;
	Private double xyrate = rand.nextdouble ()-0.5;
	private int xspeed = (int) (YSPEED*XYRATE*2);
	private int ballx = Rand.nextint (200) + 20; private int BallY = Rand.neXtint (10) + 20;
	private int racketx = Rand.nextint (200);
	Private String shape = "";
	Timer timer;
	Private Boolean islose = false;
	
	Private MyCanvas Tablearea = new MyCanvas ();
		public void init () {tablearea.setpreferredsize (new Dimension (Table_width, table_height));
		F.add (Tablearea); Keyadapter keyprocessor = new Keyadapter () {public void keypressed (KeyEvent ke) {if (ke.getkeycode () = = Keyeve Nt.
				Vk_left) {if (Racketx > 0) racketx-= 10;  } if (Ke.getkeycode () = = Keyevent.vk_right) {if (Racketx < table_width-racket_width) Racketx =
				10;
		}
			}
		};
		F.addkeylistener (Keyprocessor);
		
		Tablearea.addkeylistener (Keyprocessor);
				ActionListener Taskperformer = new ActionListener () {@Override public void actionperformed (ActionEvent e) { TODO auto-generated Method Stub if (ballx <= 0 | | ballx >= table_width-ball_size) {xspeed =-xspee
				D } if (bally>= Racket_y-balL_size && (Ballx < Racketx | | ballx > RACKETX + racket_width)) {timer.stop ();
					Islose = true;
				Tablearea.repaint (); 
						else if (BallY <= 0 | |
				(BallY >= racket_y-ball_size && ballx > Racketx && ballx <= racketx + racket_width))
				{yspeed =-yspeed;
				} BallY + = Yspeed;
				Ballx + = XSpeed;
			Tablearea.repaint ();
		
		}
		};
			F.addwindowlistener (New Windowadapter () {public void windowclosing (WindowEvent e) {system.exit (0);
		}
		});
		Timer = new timer (taskperformer);
		Timer.start ();
		F.pack ();
				
	F.setvisible (TRUE);
	public static void Main (string[] args) {//TODO auto-generated Method Stub new Pinball (). Init (); Class MyCanvas extends Canvas {public void paint (Graphics g) {if (islose) {g.setcolor (new Color) (25
				5, 0, 0));
				G.setfont (New Font ("Times", Font.Bold, 30));
			g.DrawString ("Game over", 50, 200); } else {
				G.setcolor (New Color (240, 240, 80));
				G.filloval (BALLX, BallY, Ball_size, ball_size);
				G.setcolor (New Color (80, 80, 200));
			G.fillrect (Racketx, racket_y, Racket_width, racket_height); }
		}
	}

}


 

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.