JAVA期中作业——弹球游戏

package pinball;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;



public class Pinball { 
    public  int  LENGTH=600;
    public  int  WIDTH1=600;
    public  int  START_X=100;
    public  int  START_Y=100;
    public int  BALL_SIZE=30;
     
    public int ball_x=LENGTH/2;
    public int ball_y=WIDTH1/2; 
    public int speed_x=11; 
    public int speed_y=7; 
    public int mouse_x=0; 
    public int mouse_y=0; 
    public int board1=WIDTH1/2; 
    
    
    MPanel mp = new MPanel();
    Timer t1;
  
    public static void main(String[] args) {
        new Pinball().start();    
    }

  public void start(){
      MFrame jf=new MFrame();
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);
  }
class MFrame extends JFrame{   
     MFrame(){
     setTitle("弹球游戏 By Lucky");
     setSize(LENGTH+16,WIDTH1+30);
     setLocation(START_X,START_Y); 
     setBackground(Color.black);
   
     add(mp);
     t1 = new Timer(100, move);
      t1.start();
      addMouseMotionListener(mouse);
}
   ActionListener move = new ActionListener(){
    public void actionPerformed(ActionEvent evt) {
        if((((ball_x<=20)||(ball_x>=LENGTH-BALL_SIZE-20))&&(ball_y>=board1-100)&&(ball_y<=board1))){
            speed_x=-speed_x;
        }
        else if((ball_x<=20)||(ball_x>=LENGTH-BALL_SIZE-20)){
            t1.stop();
        }
        
       else if((ball_y<=15)||(ball_y>=WIDTH1-BALL_SIZE)){
            speed_y=-speed_y;
        }
       
        ball_x+=speed_x;
        ball_y+=speed_y;
        mp.repaint();
         }
 };
  MouseMotionListener mouse=new MouseMotionListener(){
 public void mouseDragged(MouseEvent e) {
            }
 public void mouseMoved(MouseEvent e) {
     mouse_x=e.getX();
     mouse_y=e.getY();
     board1=mouse_y;
     mp.repaint(); }
   };

           }

class MPanel extends JPanel{
  public void paint(Graphics g) {
    super.paint(g);
    g.setColor(Color.black);
    g.fillRect(0, 0, LENGTH, WIDTH1);
    g.setColor(Color.red);
    g.fillOval(ball_x, ball_y, BALL_SIZE, BALL_SIZE);
    g.setColor(Color.blue);
    g.fillRect(0, board1-100, 20, 100); 
    g.fillRect(LENGTH-20, board1-100, 20, 100); 
   
   
  }  
}
}

package org.crazyit.ball; import java.awt.Image; import java.io.File; import javax.imageio.ImageIO; import java.io.IOException; /** * 小球对象 * * @author yangenxiong yangenxiong2009@gmail.com * @author Kelvin Mak kelvin.mak125@gmail.com * @version 1.0 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br>Copyright (C), 2009-2010, yangenxiong * <br>This program is protected by copyright laws. */ public class Ball extends BallComponent { // 定义球的竖向速度 private int speedY = 10; // 定义弹球的横向速度 private int speedX = 8; // 定义是否在运动 private boolean started = false; // 定义是否结束运动 private boolean stop = false; /** * m 有参数构造器 * * @param panelWidth * int 画板宽度 * @param panelHeight * int 画板高度 * @param offset * int 位移 * @param path * String 图片路径 */ public Ball(int panelWidth, int panelHeight, int offset, String path) throws IOException { // 调用父构造器 super(panelWidth, panelHeight, path); // 设置y坐标 this.setY(panelHeight - super.getImage().getHeight(null) - offset); } /** * 设置横向速度 * * @param speed * int 速度 * @return void */ public void setSpeedX(int speed) { this.speedX = speed; } /** * 设置竖向速度 * * @param speed * int 速度 * @return void */ public void setSpeedY(int speed) { this.speedY = speed; } /** * 设置是否在运动 * * @param b * boolean * @return void */ public void setStarted(boolean b) { this.started = b; } /** * 设置是否结束运动 * * @param b * boolean * @return void */ public void setStop(boolean b) { this.stop = b; } /** * 返回横向速度 * * @return int 速度 */ public int getSpeedX() { return this.speedX; } /** * 返回竖向速度 * * @return int 速度 */ public int getSpeedY() { return this.speedY; } /** * 是否在运动 * * @return boolean 是否在运动 */ public boolean isStarted() { return this.started; } /** * 是否已经结束运动 * * @return boolean 是否已经结束运动 */ public boolean isStop() { return this.stop; } }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值