用Java语言写一个大鱼吃小鱼小游戏
时间: 2023-07-20 11:03:47 浏览: 99
小游戏大鱼吃小鱼的所有代码
好的,我可以为您提供一个简单的 Java 大鱼吃小鱼游戏的示例代码,您可以根据需要进行修改和优化。
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class FishGame extends JPanel implements KeyListener, Runnable {
// 游戏界面大小
private static final int WIDTH = 500;
private static final int HEIGHT = 500;
// 食物数量
private static final int FOOD_COUNT = 50;
// 鱼的大小
private static final int FISH_SIZE = 30;
// 食物的大小
private static final int FOOD_SIZE = 10;
// 鱼的速度
private static final int FISH_SPEED = 5;
// 鱼的坐标
private int fishX = WIDTH / 2;
private int fishY = HEIGHT / 2;
// 食物的列表
private List<Point> foodList = new ArrayList<Point>();
// 是否结束游戏
private boolean gameOver = false;
public FishGame() {
// 设置游戏窗口大小
setPreferredSize(new java.awt.Dimension(WIDTH, HEIGHT));
// 添加键盘监听器
addKeyListener(this);
// 初始化食物列表
initFoodList();
// 启动游戏主线程
new Thread(this).start();
}
@Override
public void paint(Graphics g) {
// 清空画布
g.clearRect(0, 0, WIDTH, HEIGHT);
// 绘制食物
for (Point food : foodList) {
g.setColor(Color.RED);
g.fillOval(food.x, food.y, FOOD_SIZE, FOOD_SIZE);
}
// 绘制鱼
g.setColor(Color.BLUE);
g.fillOval(fishX, fishY, FISH_SIZE, FISH_SIZE);
// 绘制游戏结束信息
if (gameOver) {
g.setColor(Color.BLACK);
g.drawString("Game Over!", WIDTH / 2 - 30, HEIGHT / 2);
}
}
private void initFoodList() {
// 随机生成食物的坐标
Random random = new Random();
for (int i = 0; i < FOOD_COUNT; i++) {
int x = random.nextInt(WIDTH - FOOD_SIZE);
int y = random.nextInt(HEIGHT - FOOD_SIZE);
foodList.add(new Point(x, y));
}
}
private void moveFish(int dx, int dy) {
// 计算鱼的新坐标
int newX = fishX + dx;
int newY = fishY + dy;
// 判断是否超出边界
if (newX < 0 || newX > WIDTH - FISH_SIZE || newY < 0 || newY > HEIGHT - FISH_SIZE) {
return;
}
// 判断是否与食物碰撞
for (Point food : foodList) {
if (newX + FISH_SIZE > food.x && newX < food.x + FOOD_SIZE && newY + FISH_SIZE > food.y && newY < food.y + FOOD_SIZE) {
foodList.remove(food);
break;
}
}
// 更新鱼的坐标
fishX = newX;
fishY = newY;
// 判断是否游戏结束
if (foodList.size() == 0) {
gameOver = true;
}
}
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
moveFish(0, -FISH_SPEED);
break;
case KeyEvent.VK_DOWN:
moveFish(0, FISH_SPEED);
break;
case KeyEvent.VK_LEFT:
moveFish(-FISH_SPEED, 0);
break;
case KeyEvent.VK_RIGHT:
moveFish(FISH_SPEED, 0);
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void run() {
while (!gameOver) {
try {
Thread.sleep(50);
repaint();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Fish Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FishGame game = new FishGame();
frame.add(game);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
game.requestFocus();
}
}
```
这个示例代码中,我们使用 Java Swing 框架实现了一个简单的游戏窗口,并在其中绘制了一个蓝色的鱼和一些红色的食物。通过键盘上下左右键控制鱼的移动,当鱼与食物发生碰撞时,食物会消失,直到所有的食物都被吃掉,游戏结束。
阅读全文