java弹球游戏

老师给的任务,不会写#(泪)#(泪),求救,求代码#(泪)#(泪)弹球游戏,1,图形用户界面。2、窗口上画出一个大的方框,和一个可以随鼠标或键盘移动的板。3、小球在窗口和... 老师给的任务,不会写#(泪) #(泪) ,求救,求代码#(泪) #(泪) 弹球游戏,1,图形用户界面。
2、窗口上画出一个大的方框,和一个可以随鼠标或键盘移动的板。
3、小球在窗口和木板中运动,弹来弹去,木板只能水平移动。如果木板没有
接住小球则游戏失败,结束。
展开
 我来答
百度网友9c57ed1
2015-09-04 · TA获得超过120个赞
知道答主
回答量:95
采纳率:89%
帮助的人:23.8万
展开全部

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

}

}


推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式