Home > Back-end >  Using Java to write a small game, but it is a static interface
Using Java to write a small game, but it is a static interface

Time:04-22

Package hxy_one;
Import the Java. The awt. *;
Import the Java. The awt. Event. *;
The import javax.mail. Swing. *;
Public class Game {//set the desktop width
Private int table_width=300;
Private int table_height=400;
//set the racket of width and length
Private int pai_width=60;
Private int pai_heitht=20;
//set the size of the ball
Private int table_size=16;
//initialize the ball the coordinates of the
Private int hallx=30;
Private int hally=40;
//record the movement of the ball speed
Private int speedx=10;
Private int speedy=5;
//the racket speed of movement of the
Private int relex=120;
Private static int reley=340; A Boolean flag=false; The Timer time; Public class my extends Canvas {@ Override public void paint (Graphics g) {//TODO Auto - generated method stub the if (flag) {g.s etColor (Color. BLUE); G.d rawString (" game over ", 100, 200); } else {g.s etColor (Color RED); G.f illOval (30, 40, table_size table_size);//set the following baffle g.s etColor (Color. GREEN); G.f illRect (relex, reley pai_width, pai_heitht); }}
}
Public void f () {Game. My m=new Game (). The new my (); Frame f=new Frame (" pinball game ");//the change of the racket coordinates KeyListener listener=new KeyAdapter () {@ Override public void keyPressed KeyEvent (e) {//TODO Auto - generated method stub int a=um participant etKeyCode (); If (a==KeyEvent. VK_LEFT) {if (relex> 0 {relex -=10; }} the if (a==KeyEvent. VK_RIGHT) {if (relex=(table_width - table_size)) {speedx=- speedx; } else if (hally<=0 | | (hally> Reley - table_size & amp; & Hallx> Relex& & Hallx Reley - table_size & amp; & Hallx Relex + pai_width) {//game over time. The stop (); Flag=true; M.r epaint (); } hallx +=speedx; Hally +=speedy; M.r epaint (); }}; F.a ddWindowListener (new WindowAdapter () {@ Override public void windowClosing (WindowEvent e) {//TODO Auto - generated method stub System. Exit (0); }}); Time=new Timer (100 n); Time. The start (); Margaret spellings etSize (table_width table_height); F.a dd (m); F.p ack (); F.s etVisible (true);
}
Public static void main (String [] args) {new Game () f ();
}
}
  • Related