Home  >  Article  >  Java  >  Simple code sharing for realizing pinball game in Java

Simple code sharing for realizing pinball game in Java

黄舟
黄舟Original
2017-08-10 10:10:403334browse

This article mainly introduces the implementation of simple pinball game in Java in detail, which has certain reference value. Interested friends can refer to

Principle of pinball game implementation:

Redraw the image after a certain period of time (less than 1 second). Because the Graphics class is an abstract class, all methods involved need to be rewritten when creating a subclass, so what is used here is to create a subclass of Canvas. Just need to override its paint() method to achieve it. Here we use keyboard listening events, Timer class, etc.

Game Description:

The small ball in this pinball game will increase speed over time and the maximum speed is 10 horizontal speed and 10 vertical speed. When the y coordinate (vertical coordinate) of the ball is greater than the y coordinate (vertical coordinate) of the racket, the game is judged to be over. The console displays the speed of the ball in the x direction and the y direction.


import java.awt.*;
import java.util.Random;
import javax.swing.Timer;
import java.awt.event.*; 
public class PinBall 
{
 private Frame f=new Frame("弹球游戏");
 Random rand=new Random();
 //桌面的宽度以及高度
 private final int TABLE_WIDTH=300;
 private final int TABLE_HEIGHT=400;
 //球拍的宽度以及高度以及水平位置、垂直位置
 private final int RACKET_WIDTH=60;
 private final int RACKET_HEIGHT=20;
 private int racketX=rand.nextInt(24)*10;
 private int racketY=300;
 //小球的大小、运行速度、坐标
 private final int BALL_SIZE=16;
 private int ySpeed=1;
 private double xyRate=1;
 private int xSpeed=(int)(xyRate*ySpeed);
 private int ballX=rand.nextInt(284);
 private int ballY=1;
 //创建画布
 private MyCanvas tableArea=new MyCanvas();
 //定义时间类
 Timer timer;
 //游戏是否结束的旗标
 private boolean isLose=false;
 //设置游戏等级
 private int time_times=1;
 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()==KeyEvent.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=evt->
  {
   //小球碰到左边框或右边框
   if(ballX<=0||ballX>=TABLE_WIDTH-BALL_SIZE){
    xSpeed=-xSpeed;
   }
   if(ballY>racketY-BALL_SIZE&&(ballX<racketX||ballX>racketX+RACKET_WIDTH-BALL_SIZE)){
    timer.stop();
    isLose=true;
    tableArea.repaint();
   }else if(ballY<=0||(ballY>=racketY-BALL_SIZE&&ballX>racketX&&ballX<=racketX+RACKET_WIDTH)){
    ySpeed=-ySpeed;
   }
   ballY+=ySpeed;
   ballX+=xSpeed;
   tableArea.repaint();
   if((xSpeed<10&&xSpeed>-10)&&(ySpeed<10&&ySpeed>-10)){
    time_times++;
   }
   if(time_times==10){
    if(xSpeed>0){
     xSpeed++;
    }else{
     xSpeed--;
    }
    if(ySpeed>0){
     ySpeed++;
    }else{
     ySpeed--;
    }
    time_times-=10;
    System.out.println(xSpeed+" "+ySpeed);
   }
  };
  timer=new Timer(100,taskPerformer);
  timer.start();
  f.pack();
  f.setVisible(true);
 }
 class MyCanvas extends Canvas
 {
  public void paint(Graphics g){
   if(isLose){
    g.setColor(new Color(255,0,0));
    g.setFont(new Font("Times",Font.BOLD,30));
    g.drawString("游戏结束",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,racketY,RACKET_WIDTH,RACKET_HEIGHT);
   }
  }
 }
 public static void main(String[] args) 
 {
  new PinBall().init();
 }
}

The above is the detailed content of Simple code sharing for realizing pinball game in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn