
正文
java游戏碰撞代码 java碰撞检测算法
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
JAVA做一个作业,制作一个window框里面有三个小球碰撞,碰到边框或者小球变色加反弹,错误代码求指点
没办法了 只能注册一个马甲了
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
public class yes {
public static void main(String args[])
{
Mywindow win=new Mywindow();
Ball qiu1=new Ball(5,5);
Thread thread1=new Thread(qiu1);
Ball qiu2=new Ball(10,5);
Thread thread2=new Thread(qiu2);
Ball qiu3=new Ball(15,5);
Thread thread3=new Thread(qiu3);
thread1.start();
thread2.start();
thread3.start();
}
}
class Mywindow extends Frame {
Mywindow (){
setSize(350,350);
setVisible(true);
setBackground(Color.BLACK);
validate();
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}
class Ball extends Mywindow implements Runnable
{
int rgb=0;
Color color;
int x,y;
int dx=5,dy=5;
Ball(int x,int y){
this.x=x;
this.y=y;
}
public void doColor(){
rgb=(int)(Math.random()*0xFFFFFF);
color=new Color(rgb);
}
public void paint(Graphics g){
g.setColor(color);
g.fillOval(x,y,50,50);
}
public void run(){
while(true){
if(x=0) {dx=5;doColor();}
else if((x+50)=getWidth()) {dx=-5; doColor();}
if(y=0) {dy=5;doColor();}
else if((y+50)=getHeight()) {dy=-5; doColor();}
x=x+dx;
y=y+dy;
repaint();
try{Thread.sleep(50);}
catch(InterruptedException e) {;}
}
}
}
相关问答
Q1: JAVA如何实现小球的弹性碰撞
我没用java写过代码,所以我只说算法,代码你自己翻译下
按C的语法来:
void xiaoqiu
{
int UB=10,DB=200,LB=10,RB=200; //定义弹球范围的边界
int sh=1; //定义横向步长
int sz=1; //定义纵向步长(两步长之比决定了反弹的角度)
int x=LB,y=UB; //定义坐标
int i=10000; //循环次数(自己选择跳出手段)
while(i0)
{
i--;
x=x+sh;
if(x=RB||x=LB) sh=-sh; //碰壁后步长变反
y=y+sz;
if(y=DB||y=UB) sz=-sz; //碰壁后步长变反
(显示代码)
}
return;
}
总得来说,就是相当于横向和纵向分别处理移动、反弹的问题,碰壁后步长变为相反数
不懂请追问
Q2: 在java编写坦克大战游戏时,如何判断两辆坦克不能重叠运动,有什么简单的算法
对于这个小游里面的类的抽象很重要,对坦克及其它类我在这里面就不定义了,其实J2SE的API里面就有关于图形重叠的算法,就是这个intersects()方法,具体伪代码如下:
public boolean collidesWithTanks(java.util.ListTank tanks) {
for(int i=0; itanks.size(); i++) {
Tank t = tanks.get(i);
if(this != t) {
if(this.live t.isLive() this.getRect().intersects(t.getRect())) {
this.stay();
t.stay();
return true;
}
}
}
return false;
}
您可以根据自己的实际需求来改写,在我的百度文库里面有关于“坦克大战”的所有代码,如果有需要我可以把代码发给你,可以通过百度HI联系我。
Q3: Java碰撞检测代码无法执行
下面那个方法是想返回什么?如果是当前Tank与其他任意Tank有碰撞就返回
,可以写成
public boolean collidesWithTanks (ListTank tanks) {
tanks.remove(this);//这里删除当前对象,下面就不用老判断了
for (int i = 0; i tanks.size(); i++) {
Tank tank =tanks.get(i);
//if (this != tank ) {
if(this.collidesWithTank(tank))
return true; //如果和任意一个有碰撞就返回true
}
return false;
}
Q4: java碰撞减血代码,求大神指导。。。。
import java.awt.*;
import java.applet.*;
public class test extends Applet implements Runnable {
private static final long serialVersionUID = 1L;
int X, Y, moveX, moveY, width, height;
Thread newThread;
Image OffScreen;
Graphics drawOffscreen;
public void init() {
X = 0;
Y = 0;
moveX = 10; //水平移动的速度
moveY = 15; //垂直移动速度
width = getSize().width ; //窗口的宽度
System.out.println("width = " + width);
height = getSize().height ;
System.out.println("height = " + height);
OffScreen = createImage(width, height); //创建背景
drawOffscreen = OffScreen.getGraphics();
}
public void start()
{
newThread = new Thread(this);
newThread.start();
}
public void stop()
{
newThread = null;
}
public void paint(Graphics g)
{
drawOffscreen.setColor(Color.black); // 设置背景色
drawOffscreen.fillRect(0, 0, width, height); // 填充所在区域
drawOffscreen.setColor(Color.white); // 设置球的颜色
drawOffscreen.fillOval(X, Y, 15, 15); //画球
g.drawImage(OffScreen, 0, 0, this); // 画背景
}
public void update(Graphics g)
{
paint(g);
}
public void run()
{
while (newThread != null)
{
repaint();
try
{
Thread.sleep(50);
}
catch (InterruptedException E) {
}
X = X + moveX;
Y = Y + moveY;
if (X = (width - 15))
{
X = width - 15;
moveX = -moveX;
}
if (X = 0)
{
X = 0;
moveX = -moveX;
}
if (Y = (height - 15))
{
Y = height - 15;
moveY = -moveY;
}
if (Y = 0)
{
Y = 0;
moveY = -moveY;
}
}
}
}
关于java游戏碰撞代码和java碰撞检测算法的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







