写一段代码 Java实现弹球小游戏
时间: 2024-01-25 15:03:09 浏览: 104
好的,以下是一个简单的弹球小游戏的Java代码实现:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BallGame extends JFrame implements ActionListener {
Timer timer;
Ball ball;
Paddle paddle;
Brick[] bricks;
public BallGame() {
setTitle("Ball Game");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
ball = new Ball();
paddle = new Paddle();
bricks = new Brick[30];
for (int i = 0; i < bricks.length; i++) {
bricks[i] = new Brick(40 * i + 30, 50);
}
timer = new Timer(10, this);
timer.start();
setSize(400, 600);
setLocationRelativeTo(null);
}
public void actionPerformed(ActionEvent e) {
ball.move();
paddle.move();
for (int i = 0; i < bricks.length; i++) {
if (ball.getBounds().intersects(bricks[i].getBounds())) {
ball.reverseY();
bricks[i].setDestroyed();
}
}
if (ball.getBounds().intersects(paddle.getBounds())) {
ball.reverseY();
}
if (ball.getY() > getHeight()) {
JOptionPane.showMessageDialog(this, "Game Over");
timer.stop();
dispose();
}
repaint();
}
public void paint(Graphics g) {
super.paint(g);
ball.draw(g);
paddle.draw(g);
for (int i = 0; i < bricks.length; i++) {
bricks[i].draw(g);
}
}
public static void main(String[] args) {
new BallGame();
}
class Ball {
int x, y, dx, dy, size;
public Ball() {
x = 200;
y = 500;
dx = 2;
dy = -2;
size = 20;
}
public void move() {
x += dx;
y += dy;
if (x < 0 || x > getWidth() - size) {
dx = -dx;
}
if (y < 0) {
dy = -dy;
}
}
public void reverseY() {
dy = -dy;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Rectangle getBounds() {
return new Rectangle(x, y, size, size);
}
public void draw(Graphics g) {
g.setColor(Color.RED);
g.fillOval(x, y, size, size);
}
}
class Paddle {
int x, y, dx, sizeX, sizeY;
public Paddle() {
x = 150;
y = 550;
dx = 0;
sizeX = 100;
sizeY = 10;
}
public void move() {
x += dx;
if (x < 0) {
x = 0;
}
if (x > getWidth() - sizeX) {
x = getWidth() - sizeX;
}
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Rectangle getBounds() {
return new Rectangle(x, y, sizeX, sizeY);
}
public void draw(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect(x, y, sizeX, sizeY);
}
}
class Brick {
int x, y, sizeX, sizeY;
boolean destroyed;
public Brick(int x, int y) {
this.x = x;
this.y = y;
sizeX = 40;
sizeY = 20;
destroyed = false;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Rectangle getBounds() {
return new Rectangle(x, y, sizeX, sizeY);
}
public boolean isDestroyed() {
return destroyed;
}
public void setDestroyed() {
destroyed = true;
}
public void draw(Graphics g) {
if (!destroyed) {
g.setColor(Color.GREEN);
g.fillRect(x, y, sizeX, sizeY);
}
}
}
}
```
这个游戏包含了一个小球,一个挡板和一些砖块,玩家可以通过控制挡板来让小球弹起并击碎砖块。
阅读全文
相关推荐
package org.crazyit.ball;
import java.awt.Image;
import java.io.File;
import javax.imageio.ImageIO;
import java.io.IOException;
/**
* 小球对象
*
* @author yangenxiong yangenxiong2009@gmail.com
* @author Kelvin Mak kelvin.mak125@gmail.com
* @version 1.0
*
网站: 疯狂Java联盟 *
Copyright (C), 2009-2010, yangenxiong *
This program is protected by copyright laws. */ public class Ball extends BallComponent { // 定义球的竖向速度 private int speedY = 10; // 定义弹球的横向速度 private int speedX = 8; // 定义是否在运动 private boolean started = false; // 定义是否结束运动 private boolean stop = false; /** * m 有参数构造器 * * @param panelWidth * int 画板宽度 * @param panelHeight * int 画板高度 * @param offset * int 位移 * @param path * String 图片路径 */ public Ball(int panelWidth, int panelHeight, int offset, String path) throws IOException { // 调用父构造器 super(panelWidth, panelHeight, path); // 设置y坐标 this.setY(panelHeight - super.getImage().getHeight(null) - offset); } /** * 设置横向速度 * * @param speed * int 速度 * @return void */ public void setSpeedX(int speed) { this.speedX = speed; } /** * 设置竖向速度 * * @param speed * int 速度 * @return void */ public void setSpeedY(int speed) { this.speedY = speed; } /** * 设置是否在运动 * * @param b * boolean * @return void */ public void setStarted(boolean b) { this.started = b; } /** * 设置是否结束运动 * * @param b * boolean * @return void */ public void setStop(boolean b) { this.stop = b; } /** * 返回横向速度 * * @return int 速度 */ public int getSpeedX() { return this.speedX; } /** * 返回竖向速度 * * @return int 速度 */ public int getSpeedY() { return this.speedY; } /** * 是否在运动 * * @return boolean 是否在运动 */ public boolean isStarted() { return this.started; } /** * 是否已经结束运动 * * @return boolean 是否已经结束运动 */ public boolean isStop() { return this.stop; } }
网站: 疯狂Java联盟 *
Copyright (C), 2009-2010, yangenxiong *
This program is protected by copyright laws. */ public class Ball extends BallComponent { // 定义球的竖向速度 private int speedY = 10; // 定义弹球的横向速度 private int speedX = 8; // 定义是否在运动 private boolean started = false; // 定义是否结束运动 private boolean stop = false; /** * m 有参数构造器 * * @param panelWidth * int 画板宽度 * @param panelHeight * int 画板高度 * @param offset * int 位移 * @param path * String 图片路径 */ public Ball(int panelWidth, int panelHeight, int offset, String path) throws IOException { // 调用父构造器 super(panelWidth, panelHeight, path); // 设置y坐标 this.setY(panelHeight - super.getImage().getHeight(null) - offset); } /** * 设置横向速度 * * @param speed * int 速度 * @return void */ public void setSpeedX(int speed) { this.speedX = speed; } /** * 设置竖向速度 * * @param speed * int 速度 * @return void */ public void setSpeedY(int speed) { this.speedY = speed; } /** * 设置是否在运动 * * @param b * boolean * @return void */ public void setStarted(boolean b) { this.started = b; } /** * 设置是否结束运动 * * @param b * boolean * @return void */ public void setStop(boolean b) { this.stop = b; } /** * 返回横向速度 * * @return int 速度 */ public int getSpeedX() { return this.speedX; } /** * 返回竖向速度 * * @return int 速度 */ public int getSpeedY() { return this.speedY; } /** * 是否在运动 * * @return boolean 是否在运动 */ public boolean isStarted() { return this.started; } /** * 是否已经结束运动 * * @return boolean 是否已经结束运动 */ public boolean isStop() { return this.stop; } }