前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >Java弹球小游戏

Java弹球小游戏

作者头像
GeekLiHua
发布2025-01-21 14:11:39
发布2025-01-21 14:11:39
4400
代码可运行
举报
文章被收录于专栏:JavaJava
运行总次数:0
代码可运行

Java弹球小游戏

下面是小游戏的源码:

Stage5.class

代码语言:javascript
代码运行次数:0
复制
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

代码语言:javascript
代码运行次数:0
复制
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

代码语言:javascript
代码运行次数:0
复制
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;
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-01-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Java弹球小游戏
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档