一个文字版Java双人对抗游戏源码

一个文字版Java双人对抗游戏源码_第1张图片一个文字版Java双人对抗游戏源码_第2张图片一个文字版Java双人对抗游戏源码_第3张图片一个文字版Java双人对抗游戏源码_第4张图片

Main.java 

import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class Main {
	public static void main(String[] args) {
		Warrior p1 = new Warrior(
			JOptionPane.showInputDialog(null, p("请输入第一位玩家的昵称"), "第一位玩家", JOptionPane.QUESTION_MESSAGE)
		);
		Warrior p2 = new Warrior(
				JOptionPane.showInputDialog(null, p("请输入第二位玩家的昵称"), "第二位玩家", JOptionPane.QUESTION_MESSAGE)
		);
		p1.hit(p2);
	}
	
	private static JLabel p(String text){
		//使用HTML包装字符串,以改变原字体
		return new JLabel("

" + text + "

"); } }

Warrior.java

import javax.swing.*;

public class Warrior {
	private String name;
	private int blood = 200;
	private static int sum = 0;
	
	public void hit(Warrior enemy){
		sum ++;
		String referee = "";
		int attack;
		int trick = random(0, 7);
		switch (trick) {
		case 0:
			attack = random(2, 10);
			referee = this + "炫耀了他的大块肌肉," + enemy + "损失了" + attack + "点血量";
			break;
		case 1:
			attack = random(10, 15);
			referee = this + "使用了魅惑技能," + enemy + "被迷的神魂颠倒,瞬间掉了2W的粉丝,损失了" + attack + "点血量";
			break;
		case 2:
			attack = random(10, 20);
			referee = this + "查看了葵花宝典,逼格提升了" + attack + "%," + enemy + "被吓破了胆,损失了" + attack + "点血量";
			break;
		case 3:
			attack = random(6, 10);
			referee = this + "发起了猛烈的进攻,打的" + enemy + "满地找牙,让他损失了" + attack + "点血量";
			break;
		case 4:
			attack = random(10, 25);
			referee = this + "狠狠的鄙视了" + enemy + "一下,给" + enemy + "留下了心理阴影,使他损失了" + attack + "点血量";
			break;
		case 5:
			attack = random(10, 50);
			referee = this + "使用了必杀技," + enemy + "的假发被打掉了!让他损失了" + attack + "点血量";
			break;
		case 6:
			attack = random(1, 5);
			referee = this + "绊了一脚,只对" + enemy + "造成了" + attack + "点伤害";
			break;
		default:
			attack = random(13, 23);
			referee = this + "使用了情侣花式虐狗秀恩爱,一下子击中了" + enemy + "的小心脏,打掉了他" + attack + "点血量";
			break;
		}
		enemy.setBlood(getBlood() - attack);
		JOptionPane.showMessageDialog(null, p(referee, enemy), "第" + sum + "回合", JOptionPane.INFORMATION_MESSAGE);
		if(enemy.blood <= 0){
			JOptionPane.showMessageDialog(null, enemy + "精疲力尽,挂掉了!", "K.O.", JOptionPane.ERROR_MESSAGE);
			System.out.println();
			return;
		}
		enemy.hit(this);
	}
	
	private JLabel p(String text, Warrior enemy){
		//使用HTML包装字符串,以改变原字体
		return new JLabel("

" + text + "

" + this + ":" +this.getBlood() + " & " + enemy + ":" +enemy.getBlood() + "

"); } public int random(int min,int max){ return (int)(Math.random() * (max - min + 1)) + min; } @Override public String toString() { return "【" + this.name + "】"; } public Warrior(String name) { this.name = name; } public String getName() { return name; } public int getBlood() { return blood; } public void setBlood(int blood) { this.blood = blood; } }

 

你可能感兴趣的:(Java,PK)