package chapter11; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.Random; public class PinBallGameDemo1 { //桌面的宽度、高度 int TABLE_WIDTH=300; int TABLE_HEIGHT=400; //球拍的垂直位置 int RACKET_Y=340; //球拍的高和宽 int RACKET_HEIGHT=20; int RACKET_WIDTH=60; //小球大小 int BALL_SIZE=16; frame frame=new frame("d球游戏1.0"); Random rand=new Random(); //小球纵向运行速度 int ySpeed=10; //返回一个-0.5-0.5的比率,用于控制小球的运动方向 double xyRate=rand.nextDouble()-0.5; //小球横向运行速度 int xSpeed=(int)(ySpeed*xyRate*2); //小球的坐标 int ballY=rand.nextInt(10)+20; int ballX=rand.nextInt(200)+20; //球拍的水平位置 int racketX=rand.nextInt(200); MyCanvas3 myCanvas=new MyCanvas3(); javax.swing.Timer timer; //游戏是否结束的标志 boolean isLose=false; //难度更改按钮 JButton easyButton=new JButton("简单"); JButton midButton=new JButton("一般"); JButton difficultButton=new JButton("困难"); int easy=0; int mid=2; int difficult=5; Panel p=new Panel(); public void init(){ ActionListener easyButtonListener=e->{ ySpeed+=easy; xSpeed+=easy; }; ActionListener midButtonListener=e->{ ySpeed+=mid; xSpeed+=mid; }; ActionListener difficultButtonListener=e->{ ySpeed+=difficult; xSpeed+=difficult; }; easyButton.addActionListener(easyButtonListener); midButton.addActionListener(midButtonListener); difficultButton.addActionListener(difficultButtonListener); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); //设置桌面区域的最佳大小 myCanvas.setPreferredSize(new Dimension(TABLE_WIDTH,TABLE_HEIGHT)); frame.add(myCanvas); p.add(easyButton); p.add(midButton); p.add(difficultButton); frame.add(p,BorderLayout.SOUTH); frame.setResizable(false); //设置键盘监听器 var 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 { //如果小球碰到左右边框 if(ballX<=0 || ballX>=TABLE_WIDTH-BALL_SIZE){ xSpeed=-xSpeed; } //如果小球高度超过了球拍位置,并且横向不在球拍范围之内,游戏结束 if(ballY>=RACKET_Y-BALL_SIZE && (ballXracketX+RACKET_WIDTH)){ timer.stop(); //设置游戏是非结束的标志性为true isLose=true; myCanvas.repaint(); } //如果小球位于球拍之内,且到达球拍位置,小球反d else if (ballY<=0 || (ballY>=RACKET_Y-BALL_SIZE && ballX>racketX && ballX<=racketX+RACKET_WIDTH)){ ySpeed=-ySpeed; } //小球坐标增加 ballY+=ySpeed; ballX+=xSpeed; myCanvas.repaint(); }; timer=new Timer(100,taskPerformer); timer.start(); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { new PinBallGameDemo1().init(); } class MyCanvas3 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.fillArc(ballX,ballY,BALL_SIZE,BALL_SIZE,0,360); //设置颜色,绘制球拍 g.setColor(new Color(80,80,200)); g.fillRect(racketX,RACKET_Y,RACKET_WIDTH,RACKET_HEIGHT); } } } }
游戏开始
游戏失败结束
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)