Java弹球小游戏

Java弹球小游戏

在这里插入图片描述

下面是小游戏的源码:

Stage5.class

package gui;

import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.util.Random;
import java.awt.Color;
public class Stage5 extends Thread implements MouseListener
{
    JFrame jf=new JFrame("Bouncing Ball");           //window
    Pad p=new Pad();                                 //canvas to draw sth.
    Ball tmp=null;
    int size=450;                                    //default size of window
    int life=500;                                    //default simulation times
    int interval=100;                                //for thread sleep
    int max_r=25;                                    //the maximum radius of the ball
    int max_v=7;                                     //the maximum offset of the ball

    public Stage5()                                  //constructor
    {
        p.addMouseListener(this);
        jf.add(p,BorderLayout.CENTER);
        jf.setSize(this.size,this.size);
        jf.setVisible(true);        
    }
    
    public void run()                                 //for thread usage
    {
        for(int i=0;i<this.life;i++)
        {
            try{
                this.sleep(this.interval);
            }catch(InterruptedException e){}            
            p.updateBall();                           //update all balls' status
            p.update(p.getGraphics());                //repaint all balls
        }
        
    }
    
    public void mouseClicked(MouseEvent e){          //mouse action with canvas
        Color tmp=Color.red;
        switch(new Random().nextInt(4)){             //get random color
            case 0: tmp=Color.blue;break;
            case 1: tmp=Color.yellow;break;
            case 2: tmp=Color.green;break;            
        }
        p.insertBall(new Ball(                       //create a ball with random attributes, then insert it into queue.
            e.getX(),
            e.getY(),
            new Random().nextInt(this.max_r+3),
            tmp,
            this.max_v/2-new Random().nextInt(this.max_v),
            this.max_v/2-new Random().nextInt(this.max_v)
        ));
    }
    
    public void mouseExited(MouseEvent e){
        
    }
    
    public void mouseEntered(MouseEvent e){
        
    }
    
    public void mousePressed(MouseEvent e){
        
    }
    
    public void mouseReleased(MouseEvent e){
        
    }
    
    public static void main(String [] args)
    {
        Stage5 s=new Stage5();
        s.start();
    }
}

Pad.class

package gui;

import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.util.Vector;
public class Pad extends Canvas           //Pad become a canvas
{
    Vector queue=new Vector();            //queue is a place to contain all balls
    
    public Pad(){}                        //empty constructor
    
    public void insertBall(Ball b)        //function to add ball to queue
    {
        queue.add(b);
    }    
    
    public void updateBall()              //function to change ball's status
    {
        int x,y,xoff,yoff,s=queue.size(); //a set of temporary variables
        Ball tmp=null;
        for(int i=0;i<s;i++)
        {
             tmp=(Ball)queue.get(i);      //get one ball from queue
             x=tmp.getx();                //get the status of the ball
             y=tmp.gety();
             xoff=tmp.getxoff();
             yoff=tmp.getyoff();
             if((x+xoff)>400){            //if the ball reach the border change its direction,else change its location
                 tmp.setx(400-(x+xoff-400));
                 tmp.setxoff(0-xoff);
             }else if((y+yoff)>400){
                 tmp.sety(400-(y+yoff-400));
                 tmp.setyoff(0-yoff);
             }else if((x+xoff)<0){
                 tmp.setx(0-x-xoff);
                 tmp.setxoff(0-xoff);
             }else if((y+yoff)<0){
                 tmp.sety(0-y-yoff);
                 tmp.setyoff(0-yoff);
             }else{             
                 tmp.setx(x+xoff);                   
                 tmp.sety(y+yoff);
             }            
         }
    }
    
    public void paint(Graphics g)         //paint the graphic to canvas
    {
        this.update(g);
    }
    
    public void update(Graphics g)
    {
        Graphics2D g2 = (Graphics2D)g;
        g2.clearRect(0,0,450,450);
        Ball tmp=null;
        int s=queue.size();
        Ellipse2D m=null;
        for(int i=0;i<s;i++)              //draw balls in queue one by one
        {
            tmp=(Ball)queue.get(i);                                              //get a ball from queue
            m=new Ellipse2D.Float(tmp.getx(),tmp.gety(),tmp.getr(),tmp.getr());  //create an ellipse represents a ball shape
            g2.setPaint(tmp.getc());                                             //set the color
            g2.fill(m);                                                          //draw and fill the ellipse
        }
    }    
    
}

Ball.class

package gui;


import java.awt.Color;
public class Ball 
{
    private int x=0;     //coordinate x
    private int y=0;     //coordinate y
    private int r=5;     //radius
    private Color c=null;//color
    private int xoff=1;  //offset x
    private int yoff=1;  //offset y
    
    public Ball()        //constructor1 for default usage
    {
        x=0;
        y=0;
        r=5;
        c=Color.red;
        xoff=1;
        yoff=1;
    }
    
    public Ball(int x, int y, int r)          //constructor2
    {
        this.x=x;
        this.y=y;
        this.r=r;
        c=Color.red;
        xoff=1;
        yoff=1;
    }
    
    public Ball(int x, int y, int r, Color c,int xoff,int yoff)   //constructor3
    {
        this.x=x;
        this.y=y;
        this.r=r;
        this.c=c;
        this.xoff=xoff;
        this.yoff=yoff;
    }
    
    public int getx()
    {
        return this.x;
    }
    
    public int gety()
    {
        return this.y;
    }
    
    public int getr()
    {
        return this.r;
    }
    
    public Color getc()
    {
        return this.c;
    }
        
    public int getxoff()
    {
        return this.xoff;
    }
    
    public int getyoff()
    {
        return this.yoff;
    }
    
    public void setx(int x)
    {
        this.x=x;
    }
    
    public void sety(int y)
    {
        this.y=y;
    }
    
    public void setr(int r)
    {
        this.r=r;
    }
    
    public void setc(Color c)
    {
        this.c=c;
    }  
    
    public void setxoff(int xoff)
    {
        this.xoff=xoff;
    }
    
    public void setyoff(int yoff)
    {
        this.yoff=yoff;
    }
}

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; } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

极客李华

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值