Java制作森林冰火人代码
时间: 2023-12-19 18:07:19 浏览: 55
以下是一个简单的 Java 实现森林冰火人游戏的代码示例:
```java
import java.awt.*;
import javax.swing.*;
public class ForestGame extends JFrame {
private static final long serialVersionUID = 1L;
private int width = 640; // 游戏窗口宽度
private int height = 480; // 游戏窗口高度
private int x = 100; // 冰火人的横坐标
private int y = 350; // 冰火人的纵坐标
private int vx = 0; // 冰火人的水平速度
private int vy = 0; // 冰火人的垂直速度
private boolean isJumping = false; // 是否正在跳跃
private int jumpHeight = 100; // 跳跃高度
private int jumpCount = 0; // 跳跃计数器
private boolean isDead = false; // 是否死亡
private Image bgImg; // 背景图片
private Image iceManImg; // 冰火人图片
private Image fireManImg; // 火人图片
private Image treeImg; // 树图片
private Image gemImg; // 宝石图片
private int score = 0; // 得分
public ForestGame() {
setTitle("森林冰火人"); // 设置窗口标题
setSize(width, height); // 设置窗口大小
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置关闭窗口时退出程序
setLocationRelativeTo(null); // 设置窗口居中
setResizable(false); // 设置窗口不可缩放
setVisible(true); // 设置窗口可见
bgImg = new ImageIcon("bg.jpg").getImage(); // 加载背景图片
iceManImg = new ImageIcon("ice_man.gif").getImage(); // 加载冰火人图片
fireManImg = new ImageIcon("fire_man.gif").getImage(); // 加载火人图片
treeImg = new ImageIcon("tree.gif").getImage(); // 加载树图片
gemImg = new ImageIcon("gem.gif").getImage(); // 加载宝石图片
addKeyListener(new KeyAdapter() { // 添加键盘事件监听器
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) { // 按下左键
vx = -5;
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) { // 按下右键
vx = 5;
} else if (e.getKeyCode() == KeyEvent.VK_SPACE) { // 按下空格键
if (!isJumping) {
isJumping = true;
vy = -10;
jumpCount = 0;
}
}
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_RIGHT) { // 松开左/右键
vx = 0;
}
}
});
while (!isDead) { // 游戏循环
repaint(); // 重绘窗口
try {
Thread.sleep(20); // 等待20毫秒,控制游戏帧率
} catch (InterruptedException e) {
e.printStackTrace();
}
// 更新冰火人位置
x += vx;
y += vy;
if (isJumping) {
vy++;
jumpCount++;
if (jumpCount >= jumpHeight) {
isJumping = false;
}
}
// 检测碰撞
if (x < 0 || x > width - 50 || y < 0 || y > height - 50) { // 越界,死亡
isDead = true;
}
if (collideWithTree(x, y)) { // 碰到树,死亡
isDead = true;
}
if (collideWithGem(x, y)) { // 碰到宝石,得分+1
score++;
}
}
}
public void paint(Graphics g) {
super.paint(g);
g.drawImage(bgImg, 0, 0, null); // 绘制背景图片
g.drawImage(iceManImg, x, y, null); // 绘制冰火人图片
g.drawImage(fireManImg, width - 50, height - 50, null); // 绘制火人图片
g.drawImage(treeImg, 200, 300, null); // 绘制树图片
g.drawImage(gemImg, 400, 250, null); // 绘制宝石图片
g.setColor(Color.WHITE); // 设置字体颜色
g.setFont(new Font("微软雅黑", Font.BOLD, 24)); // 设置字体样式
g.drawString("得分:" + score, 20, 30); // 绘制得分
if (isDead) {
g.setColor(Color.RED);
g.setFont(new Font("微软雅黑", Font.BOLD, 48));
g.drawString("游戏结束", 200, 200); // 绘制游戏结束提示
}
}
private boolean collideWithTree(int x, int y) { // 检测是否碰到树
return (x >= 200 && x <= 240 && y >= 300 && y <= 340);
}
private boolean collideWithGem(int x, int y) { // 检测是否碰到宝石
return (x >= 400 && x <= 440 && y >= 250 && y <= 290);
}
public static void main(String[] args) {
new ForestGame(); // 启动游戏
}
}
```
注意:以上代码仅为示例,实际游戏开发中还需要优化和完善,例如添加更多的关卡、角色、道具等。