Java弹球小游戏
Java弹球小游戏
Java弹球小游戏下面是小游戏的源码:Stage5.class代码语言:javascript代码运行次数:0运行复制package gui;
import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.event.MouseListener;
import java.awt.event.Mo
Java弹球小游戏
下面是小游戏的源码:
代码语言:javascript代码运行次数:0运行复制package gui;
import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.MouseListener;
import java.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() //ctructor
{
p.addMouseListener(this);
jf.add(p,BorderLayout.CETER);
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(_r+),
tmp,
_v/2-new Random().nextInt(_v),
_v/2-new Random().nextInt(_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();
}
}
代码语言: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 ctructor
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;
(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
}
}
}
代码语言: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() //ctructor1 for default usage
{
x=0;
y=0;
r=5;
c=Color.red;
xoff=1;
yoff=1;
}
public Ball(int x, int y, int r) //ctructor2
{
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) //ctructor
{
this.x=x;
this.y=y;
this.r=r;
=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 ;
}
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)
{
=c;
}
public void setxoff(int xoff)
{
this.xoff=xoff;
}
public void setyoff(int yoff)
{
this.yoff=yoff;
}
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2025-01-20,如有侵权请联系 cloudcommunity@tencent 删除javaintpublicvoid小游戏 #感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
上传时间: 2025-07-20 07:14:31
下一篇:快手校招一面讲解
推荐阅读
留言与评论(共有 14 条评论) |
本站网友 冠心病饮食禁忌 | 22分钟前 发表 |
tmp=Color.yellow;break; case 2 | |
本站网友 php视频教程下载 | 5分钟前 发表 |
int r | |
本站网友 tag什么意思 | 22分钟前 发表 |
y | |
本站网友 谁主浮沉 | 17分钟前 发表 |
int y | |
本站网友 女人吃鱼腥草的好处 | 13分钟前 发表 |
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 | |
本站网友 sql文件查看器 | 6分钟前 发表 |
_v/2-new Random().nextInt(_v) | |
本站网友 脖子除皱 | 2分钟前 发表 |
tmp=Color.yellow;break; case 2 | |
本站网友 seul | 17分钟前 发表 |
原始发表:2025-01-20 | |
本站网友 珠海营养师 | 25分钟前 发表 |
int xoff | |
本站网友 咬住下唇 | 7分钟前 发表 |
int yoff) //ctructor { this.x=x; this.y=y; this.r=r; =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 ; } 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) { =c; } public void setxoff(int xoff) { this.xoff=xoff; } public void setyoff(int yoff) { this.yoff=yoff; } }本文参与 腾讯云自媒体同步曝光计划 | |
本站网友 华冠购物中心 | 11分钟前 发表 |
int yoff) //ctructor { this.x=x; this.y=y; this.r=r; =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 ; } 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) { =c; } public void setxoff(int xoff) { this.xoff=xoff; } public void setyoff(int yoff) { this.yoff=yoff; } }本文参与 腾讯云自媒体同步曝光计划 | |
本站网友 模仿 | 16分钟前 发表 |
_v/2-new Random().nextInt(_v) | |
本站网友 回龙观租房信息 | 24分钟前 发表 |
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 |