import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
// ===== 角色类型 =====
class GameCharacter {
private String name;
private int speed;
private int jumpPower;
public GameCharacter(String name, int speed, int jumpPower) {
this.name = name;
this.speed = speed;
this.jumpPower = jumpPower;
}
public String getName() { return name; }
public int getSpeed() { return speed; }
public int getJumpPower() { return jumpPower; }
}
// ===== 玩家类 =====
class Player {
private GameCharacter character;
private int x, y;
private double velocityY = 0;
private boolean left, right;
private boolean onPlatform;
public Player(GameCharacter character, int startX, int startY) {
this.character = character;
this.x = startX;
this.y = startY;
}
public void update(List<Platform> platforms) {
// 水平移动
if (left) x -= character.getSpeed();
if (right) x += character.getSpeed();
if (x < 0) x = 0;
if (x > 380) x = 380;
// 重力
velocityY += 0.5;
y += velocityY;
onPlatform = false;
// 平台碰撞
for (Platform p : platforms) {
if (new Rectangle(x, y, 20, 20).intersects(new Rectangle(p.x, p.y, p.w, p.h))) {
if (velocityY > 0 && y + 20 <= p.y + velocityY) {
y = p.y - 20;
velocityY = 0;
onPlatform = true;
}
}
}
}
public void jump() {
if (onPlatform) velocityY = -character.getJumpPower();
}
public void draw(Graphics g) {
g.setColor(Color.YELLOW);
g.fillRect(x, y, 20, 20);
g.setColor(Color.WHITE);
g.drawString(character.getName(), x - 10, y - 10);
}
public int getY() { return y; }
public void setLeft(boolean left) { this.left = left; }
public void setRight(boolean right) { this.right = right; }
}
// ===== 平台类 =====
class Platform {
int x, y, w, h;
public Platform(int x, int y, int w, int h) {
this.x = x; this.y = y; this.w = w; this.h = h;
}
public void draw(Graphics g) {
g.setColor(Color.GREEN);
g.fillRect(x, y, w, h);
}
}
// ===== 游戏面板 =====
class GamePanel extends JPanel implements ActionListener, KeyListener {
private Timer timer;
private Player player;
private List<Platform> platforms;
private Random random = new Random();
private int score = 0;
public GamePanel(GameCharacter character) {
setPreferredSize(new Dimension(400, 600));
setBackground(Color.BLACK);
player = new Player(character, 200, 300);
platforms = new ArrayList<>();
platforms.add(new Platform(150, 500, 100, 10));
platforms.add(new Platform(250, 400, 100, 10));
platforms.add(new Platform(100, 300, 100, 10));
timer = new Timer(16, this);
timer.start();
addKeyListener(this);
setFocusable(true);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
player.draw(g);
for (Platform p : platforms) {
p.draw(g);
}
g.setColor(Color.WHITE);
g.drawString("得分: " + score, 10, 20);
}
@Override
public void actionPerformed(ActionEvent e) {
player.update(platforms);
movePlatforms();
spawnPlatforms();
checkFall();
repaint();
}
private void movePlatforms() {
for (Platform p : platforms) {
p.y -= 2;
}
platforms.removeIf(p -> p.y < -20);
score++;
}
private void spawnPlatforms() {
if (platforms.size() < 6) {
int px = random.nextInt(300);
int py = 600 + random.nextInt(50);
platforms.add(new Platform(px, py, 100, 10));
}
}
private void checkFall() {
if (player.getY() > 600) {
timer.stop();
JOptionPane.showMessageDialog(this, "游戏结束!得分: " + score);
}
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_LEFT) player.setLeft(true);
if(e.getKeyCode() == KeyEvent.VK_RIGHT) player.setRight(true);
if(e.getKeyCode() == KeyEvent.VK_SPACE) player.jump();
}
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_LEFT) player.setLeft(false);
if(e.getKeyCode() == KeyEvent.VK_RIGHT) player.setRight(false);
}
@Override
public void keyTyped(KeyEvent e) {}
}
// ===== 角色选择界面 =====
class CharacterSelectPanel extends JPanel {
private JButton startButton;
private JComboBox<String> characterBox;
public CharacterSelectPanel(JFrame frame) {
setLayout(new GridLayout(3, 1));
JLabel label = new JLabel("选择角色", JLabel.CENTER);
add(label);
// 已经加入 彩霞
String[] names = {"老凯", "鲲鹏", "阿鹏", "紫琪", "佳慧", "斌哥", "虎子", "彩霞"};
characterBox = new JComboBox<>(names);
add(characterBox);
startButton = new JButton("开始游戏");
startButton.addActionListener(e -> {
GameCharacter selected = createCharacter((String)characterBox.getSelectedItem());
frame.setContentPane(new GamePanel(selected));
frame.revalidate();
});
add(startButton);
}
private GameCharacter createCharacter(String name) {
switch (name) {
case "老凯": return new GameCharacter(name, 5, 10);
case "鲲鹏": return new GameCharacter(name, 6, 9);
case "阿鹏": return new GameCharacter(name, 4, 12);
case "紫琪": return new GameCharacter(name, 5, 11);
case "佳慧": return new GameCharacter(name, 5, 10);
case "斌哥": return new GameCharacter(name, 7, 8);
case "虎子": return new GameCharacter(name, 4, 13);
case "彩霞": return new GameCharacter(name, 6, 12); // 彩霞属性
}
return new GameCharacter(name, 5, 10);
}
}
// ===== 程序入口 =====
public class GameMain {
public static void main(String[] args) {
JFrame frame = new JFrame("北宋领历险记");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new CharacterSelectPanel(frame));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
} 这个main 方法 怎么在华为云上说屁