90行java小游戏源代码
时间: 2023-10-05 16:04:19 浏览: 106
以下是一个更复杂的 Java 小游戏代码,包括了鼠标交互、音效和动画效果。
```java
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Game extends JPanel {
private static final long serialVersionUID = 1L;
private static final int WIDTH = 800;
private static final int HEIGHT = 600;
private static final int FPS = 60;
private Image background;
private Image player;
private Image enemy;
private Image bullet;
private boolean gameRunning = true;
private int score = 0;
private Point playerPos = new Point(WIDTH / 2 - 25, HEIGHT - 75);
private Point enemyPos = new Point(0, 0);
private Point bulletPos = new Point(0, 0);
private List<Point> enemyPath = new ArrayList<Point>();
private boolean firing = false;
private Random random = new Random();
private int enemySpeed = 2;
private int bulletSpeed = 5;
private SoundPlayer soundPlayer = new SoundPlayer();
private Font font = new Font("Arial", Font.BOLD, 30);
public Game() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setBackground(Color.BLACK);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (!firing) {
bulletPos.x = playerPos.x + 22;
bulletPos.y = playerPos.y - 10;
firing = true;
soundPlayer.play("shoot.wav");
}
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent e) {
playerPos.x = e.getX() - 25;
playerPos.y = e.getY() - 25;
}
});
loadImages();
startGameLoop();
}
private void loadImages() {
try {
URL bgUrl = getClass().getResource("background.png");
background = ImageIO.read(bgUrl);
URL playerUrl = getClass().getResource("player.png");
player = ImageIO.read(playerUrl);
URL enemyUrl = getClass().getResource("enemy.png");
enemy = ImageIO.read(enemyUrl);
URL bulletUrl = getClass().getResource("bullet.png");
bullet = ImageIO.read(bulletUrl);
} catch (IOException e) {
e.printStackTrace();
}
}
private void startGameLoop() {
Thread gameThread = new Thread() {
public void run() {
while (gameRunning) {
long startTime = System.currentTimeMillis();
update();
repaint();
try {
long sleepTime = 1000 / FPS - (System.currentTimeMillis() - startTime);
if (sleepTime > 0) {
Thread.sleep(sleepTime);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
gameThread.start();
}
private void update() {
if (enemyPath.isEmpty()) {
int x = random.nextInt(WIDTH - 50);
int y = random.nextInt(200) - 400;
enemyPath.add(new Point(x, y));
enemyPos.setLocation(x, 0);
}
Point nextPoint = enemyPath.get(0);
if (enemyPos.x < nextPoint.x) {
enemyPos.x += enemySpeed;
} else if (enemyPos.x > nextPoint.x) {
enemyPos.x -= enemySpeed;
}
if (enemyPos.y < nextPoint.y) {
enemyPos.y += enemySpeed;
} else if (enemyPos.y > nextPoint.y) {
enemyPos.y -= enemySpeed;
}
if (enemyPos.x == nextPoint.x && enemyPos.y == nextPoint.y) {
enemyPath.remove(0);
}
if (firing) {
bulletPos.y -= bulletSpeed;
if (bulletPos.y < 0) {
firing = false;
}
if (bulletPos.x > enemyPos.x - 50 && bulletPos.x < enemyPos.x + 50
&& bulletPos.y > enemyPos.y - 50 && bulletPos.y < enemyPos.y + 50) {
score++;
firing = false;
soundPlayer.play("explode.wav");
}
}
if (enemyPos.y > HEIGHT - 50) {
gameRunning = false;
soundPlayer.play("gameover.wav");
}
}
public void paint(Graphics g) {
super.paint(g);
g.drawImage(background, 0, 0, null);
g.drawImage(player, playerPos.x, playerPos.y, null);
g.drawImage(enemy, enemyPos.x, enemyPos.y, null);
if (firing) {
g.drawImage(bullet, bulletPos.x, bulletPos.y, null);
}
g.setColor(Color.WHITE);
g.setFont(font);
g.drawString("Score: " + score, 20, 30);
if (!gameRunning) {
g.drawString("Game Over", WIDTH / 2 - 75, HEIGHT / 2 - 30);
g.drawString("Score: " + score, WIDTH / 2 - 75, HEIGHT / 2);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Shooter Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(new Game());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
```
这个游戏是一个射击游戏,玩家通过鼠标控制角色的移动和射击,击中敌人得分,游戏结束条件是敌人到达底部或者玩家被击败。游戏中还包括了背景音乐和音效。
阅读全文