Write a pinball game in Java

Table of contents

I. Introduction

2. Pinball game

(1) Analyzing the game

(2) Game code


I. Introduction

If you want to make this game, it is best to have learned AWT or Swing programming, know the knowledge of the graphical interface, and know how to draw. This game can help you better lay a solid foundation.

2. Pinball game

(1) Analyzing the game

The rules of the pinball mini-game, the ball will move in the opposite direction when it hits the racket or the interface, and the game ends when it cannot touch the racket

 

(2) Game code

class Solution {
    //制造组件
    //桌子的大小
    private  int table_width=300;
    private  int table_height=400;
    //球拍的信息
    private int bat_width=80;
    private int bat_height=20;
    private int bat_x=120;
    private int bat_y=350;
    //球的信息
    private int ball_size=16;
    private int ball_x=100;
    private int ball_y=20;
    //定义球的移动速度
    private int speed_x=15;
    private int speed_y=15;
    //定义定时器,0.1秒刷新一次
    private Timer timer;
    //判断游戏是否结束
    private boolean flag=false;
    Frame s=new Frame("小游戏");
    //定义画布
    private class draw extends Canvas{
        @Override
        public void paint(Graphics g) {
            if(flag==true){
                //游戏结束
               g.setColor(Color.BLUE);
               g.setFont(new Font("t",Font.BOLD,30));//设置字体
               g.drawString("游戏结束!",50,200);
            }
            else{

                //绘制小球
                g.setColor(Color.red);
                g.fillOval(ball_x,ball_y,ball_size,ball_size);
                //绘制球拍
                g.setColor(Color.pink);
                g.fillRect(bat_x,bat_y,bat_width,bat_height);
            }
        }
    }
    draw drawarea=new draw();
    public void init(){
        //开始组装

        //事件监听,键盘控
        KeyListener listener=new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                int n=e.getKeyCode();
                if(n==KeyEvent.VK_RIGHT){
                    //右
                    if((bat_x)<(table_width-bat_width)){
                        bat_x+=15;
                    }

                }
                if(n==KeyEvent.VK_LEFT){
                    //左
                    if(bat_x>0){
                        bat_x-=15;
                    }
                }
            }
        };
        s.addKeyListener(listener);
        drawarea.addKeyListener(listener);
        //小球坐标的控制
        ActionListener act=new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if((ball_y>(bat_y-ball_size))&&((ball_x>bat_x+bat_width)||(ball_x<bat_x))){
                    //游戏结束
                    timer.stop();
                    flag=true;
                    drawarea.repaint();
                }
                if(ball_x<=0||(ball_x>(table_width-ball_size))){
                    //撞到界面,换方向
                   speed_x=-speed_x;
                }
                if((ball_y<=0)||(ball_x>=bat_x&&ball_x<=bat_x+bat_width&&ball_y==bat_y)){
                    speed_y=-speed_y;
                }
                //每次球的位置都会在变化
                ball_y+=speed_y;
                ball_x+=speed_x;
                //千万不要忘记刷新页面
                drawarea.repaint();
            }
        };
        timer=new Timer(100,act);//每隔0.1秒刷新一次,act是事件监听
        timer.start();//启动
        drawarea.setPreferredSize(new Dimension(table_width,table_height));
        s.add(drawarea);
        s.pack();
        s.setVisible(true);
    }
}




//游戏入口
public class Test {
  public static  void main(String[] rag){
      Solution ss=new Solution();
      ss.init();
  }
}

Guess you like

Origin blog.csdn.net/gaoqiandr/article/details/128632755
Recommended