It's a man who insists on 20 seconds. Java Swing Pinball mini game code

Source: Internet
Author: User

Package Com.day09.am01;import Java.awt.color;import Java.awt.graphics;import java.awt.event.keyevent;import Java.awt.event.keylistener;import Java.awt.event.mouseevent;import Java.awt.event.mousemotionlistener;import Java.util.timer;import Java.util.timertask;import Javax.swing.jframe;import Javax.swing.joptionpane;import javax.swing.jpanel;/* * window + panel + fixed * 2--draw a small ball on the panel (central) * 3--let the ball move * 4--to determine the direction and direction of the ball movement, and the correction of the direction of processing, to achieve the ball rebound * 5--  Add bezel and implement mouse control bezel movement * 6--small ball touch bezel and do not touch the bezel case Processing * * */public class Ttqgame extends JFrame implements Mousemotionlistener{private Static final Long serialversionuid = 1l;private int fw = 800; The size of the window private int fh =;p rivate TTQP ttqp = null;p rivate int bx = 200; The initial position of the ball is private int by =;p Rivate int b2r = 50; The size of the ball is private String direction = "Right_down";  Initial direction right lower private timer timer = new timer ();p rivate int block_x =;p rivate int block_y = 580;p rivate int block_w = 100 ;p rivate int block_h =;p rivate int speed = 1;p rivate int score = 0;Public Ttqgame () {this.setalwaysontop (true); this.setundecorated (true); This.getcontentpane (). SetBackground ( Color.Black); This.setsize (FW, FH); This.setlocationrelativeto (null); this.setresizable (false); ttqp = new Ttqp (); This.add (TTQP); this.setvisible (true); This.addmousemotionlistener (this); This.addkeylistener (new KeyListener () {@ overridepublic void keytyped (KeyEvent e) {} @Overridepublic void keyreleased (KeyEvent e) {} @Overridepublic void keypressed (KeyEvent e) {if (E.getkeycode () ==32) {system.exit (0);}});} Class Ttqp extends jpanel{private static final long serialversionuid = 1l;public ttqp () {This.setopaque (false); Timer.sche Dule (New TimerTask () {@Overridepublic void Run () {//Implement the Movement of the ball */* * Defines the coordinate processing */if ("Right_down") for each direction. Equalsignorecase (  direction) {//lower right position processing bx + = speed; by + = speed;  if ("Right_up". Equalsignorecase (direction)) {//the position in the upper right direction handles BX + = speed; by-= speed;  if ("Left_up". Equalsignorecase (direction)) {//The position of the upper left direction handles BX-= speed; by-= speed; if ("Left_down". Equalsignorecase (DirectION) {/////lower left direction handle BX-= speed; by + = speed; /* * Define what happens, how to correct the direction of the ball *///if the ball bottom y coordinate value = the y coordinate of the top of the bezel by + b2r >= block_y//And the small ball bottom X coordinate value = x coordinate of the top of the bezel bx + B2R/2 >= block_x// and the X coordinate value of the top of the bezel + the width of the bezel bx + b2r/2 <= block_x + block_w//Then normal direction of change//otherwise the program stop GAME over//if (BY+B2R&GT;=FH) {//Bottom of the judgment//i F ("Right_down". Equalsignorecase (direction)) {//ball from lower right//direction = "right_up";//}else{//ball from lower left//direction = "Left_ Up ",//}//}if (by + b2r >= block_y && bx + b2r/2 >= block_x && bx + b2r/2 <= block_x + block_w) {if ("Right_down". Equalsignorecase (direction)) {//ball from lower right direction = "right_up"; speed + +; score + +;} else{//ball from lower left direction = "left_up"; speed + +; score + +;}} if (by + b2r >= FH) {joptionpane.showmessagedialog (TTQP, "Game Over!", "Player tip info", joptionpane.default_option); Runtime.getruntime (). exit (0);} if (BX+B2R&GT;=FW) {//The right side of the judgment if ("Right_up". Equalsignorecase (direction)) {//ball from upper right direction = "left_up";} else{//ball from lower right direction = "Left_down";}} if (by<=0) {//top-side judgment if ("Left_uP ". Equalsignorecase (direction)) {//ball from upper left direction =" Left_down ";} else{//ball from upper right direction = "Right_down";}} if (bx<=0) {//left to determine if ("Left_up". Equalsignorecase (direction)) {//ball from upper left direction = "right_up";} else{//ball from lower left direction = "Right_down";}} TTQP.this.repaint ();}}, 0, 10);} @Overridepublic void Paint (Graphics g) {G.setcolor (color.white); G.filloval (bx, by, B2R, B2R); G.setcolor (Color.pink); G.fillrect (block_x, block_y, Block_w, block_h); G.setcolor (color.red); g.DrawString ("Score:" + score, 20, 20);}} public static void Main (string[] args) {new Ttqgame ();} @Overridepublic void mousedragged (MouseEvent e) {//drag-and-drop method for controlling the movement of the bezel block_x = E.getx (); if (block_x <=0) {block_x = 0;} if (Block_x+block_w >=fw) {block_x = Fw-block_w;} This.repaint ();} @Overridepublic void mousemoved (MouseEvent e) {}}

It's a man who insists on 20 seconds. Java Swing Pinball mini game code

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.