java弹球源代码 三维弹球源代码

JAVA如何实现小球的弹性碰撞

我没用java写过代码,所以我只说算法,代码你自己翻译下

创新互联建站专注于企业营销型网站、网站重做改版、皮山网站定制设计、自适应品牌网站建设、H5场景定制成都做商城网站、集团公司官网建设、外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为皮山等各大城市提供网站开发制作服务。

按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;

}

总得来说,就是相当于横向和纵向分别处理移动、反弹的问题,碰壁后步长变为相反数

不懂请追问

老师给的任务,不会写,马上就要交了,求救,求java代码#(泪) #(泪) 弹球游戏,1,图形用户

这个问题主要的几点就是:移动与边界判断,小球与木板移动,这个通过修改座标来实现,边界判断,这个也通过座标值判断;移动的板跟随鼠标移动,通过MouseMotionListener监听鼠标移动,再修改木板座标实现;小球移动,设置2个方向,水平与垂直,0,座标值增加(向右或向下移动),0座标减小(向上或向左移动),到达边界后,修改方向值,使其向反方向移动。

因为,有大神先贴了代码,就不献丑了。如果需要代码,可以追问我。

java弹球游戏

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.RenderingHints;

import java.awt.event.MouseEvent;

import java.awt.event.MouseMotionListener;

import java.awt.image.BufferedImage;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class CopyOfBallCrash implements Runnable {

public static void main(final String[] args) {

new Thread(new CopyOfBallCrash()).start();

}

private final int           width     = 400;

private final int           height    = 700;

private int                 mouse_X, mouse_Y;

private final BufferedImage offscreen = new BufferedImage(width, height,

                                       BufferedImage.TYPE_4BYTE_ABGR);

private final JPanel        panel     = new JPanel();

private final Shape         ball      = new Shape(100, 100, 1, 1, 20);

private final Shape         rect      = new Shape(0, 100, 20);

public CopyOfBallCrash() {

final JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

panel.setPreferredSize(new Dimension(width, height));

ball.setBounds(width, height);

rect.setBounds(width, height);

frame.setContentPane(panel);

panel.addMouseMotionListener(new MouseMotionListener() {

@Override

public void mouseDragged(final MouseEvent e) {}

@Override

public void mouseMoved(final MouseEvent e) {

mouse_X = e.getX();

mouse_Y = e.getY();

}

});

frame.pack();

frame.setVisible(true);

}

public void paint(final Graphics g) {

final Graphics2D g2d = offscreen.createGraphics();

g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

g2d.fillRect(0, 0, width, height);

g2d.setColor(Color.blue);

rect.drawRect(g2d, mouse_X);

g2d.setColor(Color.red);

ball.drawOval(g2d, rect);

if (Shape.isLose) g2d.drawString("你输了!!!", 100, 300);

g2d.dispose();

g.drawImage(offscreen, 0, 0, null);

}

@Override

public void run() {

while (true)

paint(panel.getGraphics());

}

}

class Shape {

public int            width, height;

public int            x, y, vx, vy, r, w, h;

public static boolean isLose;

public Shape(final int x, final int y, final int vx, final int vy, final int r) {

super();

this.x = x;

this.y = y;

this.vx = vx;

this.vy = vy;

this.r = r;

}

public Shape(final int x, final int w, final int h) {

super();

this.x = x;

this.w = w;

this.h = h;

}

public final void setBounds(final int width, final int height) {

this.width = width;

this.height = height;

}

public final void drawOval(final Graphics2D g2d, final Shape shape) {

if (y + h = height) {

isLose = true;

return;

}

if (x + vx = 0 || x + vx + w = width) vx = -vx;

if (y + vy = 0) vy = -vy;

if (isCrashOutside(shape.x, shape.y, shape.w, shape.h) y + w = shape.y)

vy = -vy;

x += vx;

y += vy;

g2d.fillOval(x, y, r, r);

}

public final void drawRect(final Graphics2D g2d, final int mouseX) {

y = height - h;

if (x + w width x mouseX) x++;

if (x 0 x mouseX) x--;

g2d.fillRect(x, y, w, h);

}

public final boolean isCrashOutside(final int x, final int y, final int w,

final int h) {

return (this.x x ? this.x = w + x : x = r + this.x)

(this.y y ? this.y = h + y : y = r + this.y);

}

}


标题名称:java弹球源代码 三维弹球源代码
网站URL:http://cdiso.cn/article/hhphdo.html

其他资讯