用Java做一个篮子接星星小游戏
时间: 2024-06-13 07:08:53 浏览: 183
很高兴回答您的问题。下面是用Java实现篮子接星星小游戏的步骤:
1.创建一个窗口并设置窗口大小和标题。
```java
import javax.swing.JFrame;
public class Game extends JFrame {
public Game() {
setTitle("Basket Catch Game");
setSize(600, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new Game();
}
}
```
2.在窗口中添加一个篮子和星星。
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Game extends JFrame {
private Basket basket;
private Star star;
public Game() {
setTitle("Basket Catch Game");
setSize(600, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
basket = new Basket();
star = new Star();
addKeyListener(new KeyListener() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
basket.moveLeft();
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
basket.moveRight();
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
});
JPanel panel = new JPanel() {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillRect(basket.getX(), basket.getY(), basket.getWidth(), basket.getHeight());
g.setColor(Color.YELLOW);
g.fillOval(star.getX(), star.getY(), star.getSize(), star.getSize());
}
};
add(panel);
panel.requestFocus();
}
public static void main(String[] args) {
new Game();
}
}
class Basket {
private int x;
private int y;
private int width;
private int height;
public Basket() {
x = 250;
y = 500;
width = 100;
height = 50;
}
public void moveLeft() {
if (x > 0) {
x -= 10;
}
}
public void moveRight() {
if (x < 500) {
x += 10;
}
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}
class Star {
private int x;
private int y;
private int size;
public Star() {
x = (int) (Math.random() * 500);
y = 0;
size = 50;
}
public void fall() {
y += 10;
if (y > 550) {
x = (int) (Math.random() * 500);
y = 0;
}
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getSize() {
return size;
}
}
```
3.让星星不断下落并判断篮子是否接住了星星。
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Game extends JFrame {
private Basket basket;
private Star star;
public Game() {
setTitle("Basket Catch Game");
setSize(600, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
basket = new Basket();
star = new Star();
addKeyListener(new KeyListener() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
basket.moveLeft();
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
basket.moveRight();
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
});
JPanel panel = new JPanel() {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillRect(basket.getX(), basket.getY(), basket.getWidth(), basket.getHeight());
g.setColor(Color.YELLOW);
g.fillOval(star.getX(), star.getY(), star.getSize(), star.getSize());
}
};
add(panel);
panel.requestFocus();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
star.fall();
panel.repaint();
if (basket.catchStar(star)) {
star = new Star();
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
public static void main(String[] args) {
new Game();
}
}
class Basket {
private int x;
private int y;
private int width;
private int height;
public Basket() {
x = 250;
y = 500;
width = 100;
height = 50;
}
public void moveLeft() {
if (x > 0) {
x -= 10;
}
}
public void moveRight() {
if (x < 500) {
x += 10;
}
}
public boolean catchStar(Star star) {
if (star.getY() + star.getSize() > y && star.getY() + star.getSize() < y + height
&& star.getX() > x && star.getX() < x + width) {
return true;
}
return false;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}
class Star {
private int x;
private int y;
private int size;
public Star() {
x = (int) (Math.random() * 500);
y = 0;
size = 50;
}
public void fall() {
y += 10;
if (y > 550) {
x = (int) (Math.random() * 500);
y = 0;
}
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getSize() {
return size;
}
}
```
阅读全文