用java写一个大鱼吃小鱼游戏
时间: 2023-12-19 16:05:08 浏览: 103
大鱼吃小鱼游戏(Java编写)
4星 · 用户满意度95%
好的,以下是一个简单的 Java 大鱼吃小鱼游戏的示例代码:
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class FishGame extends JPanel implements KeyListener {
private static final long serialVersionUID = 1L;
private static final int FRAME_WIDTH = 500;
private static final int FRAME_HEIGHT = 500;
private static final int FISH_SIZE = 20;
private static final int FOOD_SIZE = 10;
private static final int NUM_FISH = 5;
private static final int NUM_FOOD = 50;
private List<Fish> fishList;
private List<Food> foodList;
private Fish playerFish;
public FishGame() {
JFrame frame = new JFrame("Fish Game");
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addKeyListener(this);
frame.add(this);
frame.setVisible(true);
fishList = new ArrayList<>();
for (int i = 0; i < NUM_FISH; i++) {
Fish fish = new Fish((int)(Math.random() * FRAME_WIDTH),
(int)(Math.random() * FRAME_HEIGHT),
FISH_SIZE);
fishList.add(fish);
}
foodList = new ArrayList<>();
for (int i = 0; i < NUM_FOOD; i++) {
Food food = new Food((int)(Math.random() * FRAME_WIDTH),
(int)(Math.random() * FRAME_HEIGHT),
FOOD_SIZE);
foodList.add(food);
}
playerFish = new Fish(FRAME_WIDTH / 2, FRAME_HEIGHT / 2, FISH_SIZE);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (Fish fish : fishList) {
fish.draw(g);
}
for (Food food : foodList) {
food.draw(g);
}
playerFish.draw(g);
}
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_UP) {
playerFish.moveUp();
} else if (keyCode == KeyEvent.VK_DOWN) {
playerFish.moveDown();
} else if (keyCode == KeyEvent.VK_LEFT) {
playerFish.moveLeft();
} else if (keyCode == KeyEvent.VK_RIGHT) {
playerFish.moveRight();
}
repaint();
}
public void keyTyped(KeyEvent e) {
// Not used
}
public void keyReleased(KeyEvent e) {
// Not used
}
public static void main(String[] args) {
new FishGame();
}
private class Fish {
private int x;
private int y;
private int size;
public Fish(int x, int y, int size) {
this.x = x;
this.y = y;
this.size = size;
}
public void draw(Graphics g) {
g.setColor(Color.BLUE);
g.fillOval(x, y, size, size);
}
public void moveUp() {
y -= 10;
}
public void moveDown() {
y += 10;
}
public void moveLeft() {
x -= 10;
}
public void moveRight() {
x += 10;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getSize() {
return size;
}
}
private class Food {
private int x;
private int y;
private int size;
public Food(int x, int y, int size) {
this.x = x;
this.y = y;
this.size = size;
}
public void draw(Graphics g) {
g.setColor(Color.GREEN);
g.fillOval(x, y, size, size);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getSize() {
return size;
}
}
}
```
这个示例代码中,我们使用 Java Swing 创建了一个窗口,并在窗口中绘制了一些小鱼和食物。玩家控制的大鱼可以通过键盘上下左右键来移动,吃掉小鱼和食物可以增加体积。你也可以根据自己的需求来修改代码以实现更多功能。
阅读全文