用java写一个英雄pk界面里面放一个背景图片背景图片,双方有三个英雄可以选择,选择好的英雄的图片放到上面,在选择好的每个图片英雄下面放两个按钮一个是普攻一个是技能,三次普攻之后可以释放一次技能,选择好的英雄图片中间写一个vs两个字。
时间: 2024-01-24 14:17:28 浏览: 102
这里是一个基本的Java代码,实现英雄PK界面的功能:
```java
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class HeroPK extends JFrame implements ActionListener {
private JLabel vsLabel;
private JButton[] heroButtons;
private JButton[][] skillButtons;
private ImageIcon[] heroImages;
private ImageIcon[] skillImages;
private int[] heroSelected;
private int[] heroHP;
private int[] heroSkillCount;
private int roundCount;
private JTextArea display;
private JScrollPane scrollPane;
public HeroPK() {
super("Hero PK");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 400);
setLocationRelativeTo(null);
// 初始化变量
vsLabel = new JLabel("VS", JLabel.CENTER);
heroButtons = new JButton[3];
skillButtons = new JButton[3][2];
heroImages = new ImageIcon[3];
skillImages = new ImageIcon[3];
heroSelected = new int[2];
heroHP = new int[]{100, 100};
heroSkillCount = new int[]{0, 0, 0};
roundCount = 0;
display = new JTextArea(10, 20);
scrollPane = new JScrollPane(display);
// 设置背景图片
JLabel bgLabel = new JLabel(new ImageIcon("background.jpg"));
bgLabel.setBounds(0, 0, getWidth(), getHeight());
add(bgLabel);
// 设置选择英雄的按钮和技能按钮
for (int i = 0; i < 3; i++) {
heroImages[i] = new ImageIcon("hero" + (i + 1) + ".jpg");
heroButtons[i] = new JButton(heroImages[i]);
heroButtons[i].setBounds(50 + i * 150, 50, 100, 100);
heroButtons[i].addActionListener(this);
bgLabel.add(heroButtons[i]);
skillImages[i] = new ImageIcon("skill" + (i + 1) + ".jpg");
for (int j = 0; j < 2; j++) {
skillButtons[i][j] = new JButton(skillImages[i]);
skillButtons[i][j].setBounds(25 + i * 150 + j * 75, 200, 50, 50);
skillButtons[i][j].setEnabled(false);
skillButtons[i][j].addActionListener(this);
bgLabel.add(skillButtons[i][j]);
}
}
// 设置VS标签
vsLabel.setFont(new Font("Dialog", Font.BOLD, 36));
vsLabel.setForeground(Color.WHITE);
vsLabel.setBounds(250, 100, 100, 50);
bgLabel.add(vsLabel);
// 设置显示区域
display.setEditable(false);
scrollPane.setBounds(25, 275, 550, 80);
bgLabel.add(scrollPane);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// 处理英雄选择事件
for (int i = 0; i < 3; i++) {
if (e.getSource() == heroButtons[i]) {
heroSelected[roundCount % 2] = i;
heroButtons[i].setBounds(50 + i * 150, 25, 100, 100);
heroButtons[i].setEnabled(false);
for (int j = 0; j < 2; j++) {
skillButtons[i][j].setEnabled(true);
}
roundCount++;
if (roundCount % 2 == 0) {
vsLabel.setText("VS");
} else {
vsLabel.setText("VS");
}
display.append("Round " + (roundCount / 2 + 1) + ": Player " + (roundCount % 2 + 1) + " selected hero " + (i + 1) + ".\n");
break;
}
}
// 处理技能释放事件
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
if (e.getSource() == skillButtons[i][j]) {
int playerIndex = roundCount % 2;
int enemyIndex = (playerIndex + 1) % 2;
int damage = (j + 1) * 10;
if (heroSkillCount[i] == 3) {
damage *= 2;
heroSkillCount[i] = 0;
} else {
heroSkillCount[i]++;
}
heroHP[enemyIndex] -= damage;
skillButtons[i][j].setEnabled(false);
display.append("Round " + (roundCount / 2 + 1) + ": Player " + (playerIndex + 1) + " used skill " + (j + 1) + " of hero " + (i + 1) + ", causing " + damage + " damage. Player " + (enemyIndex + 1) + " HP: " + heroHP[enemyIndex] + ".\n");
// 判断是否结束游戏
if (heroHP[enemyIndex] <= 0) {
display.append("Player " + (playerIndex + 1) + " wins!\n");
for (int k = 0; k < 3; k++) {
heroButtons[k].setEnabled(false);
for (int l = 0; l < 2; l++) {
skillButtons[k][l].setEnabled(false);
}
}
} else if (heroSkillCount[i] == 3) {
display.append("Hero " + (i + 1) + " is ready to use ultimate skill!\n");
}
break;
}
}
}
}
public static void main(String[] args) {
new HeroPK();
}
}
```
请注意,这里只是一个基本的代码示例,你需要自己添加英雄和技能的图片资源,并根据需要修改按钮的大小和位置。同时,需要注意的是,这个代码的实现只是一个非常简单的示例,实际应用中需要考虑更多的细节和异常情况。
阅读全文