作一个小游戏,看你是不是菜鸟。

程序猿小爱 2011-11-07 12:29:09
用java作一个非常简单的游戏:
要求:
1.角色可以向怪物实施攻击,一次攻击后,怪物损失部分HP,当HP损失完后,怪物死亡。
2.角色可装备部同武器,目前有木剑、铁剑、魔剑三种。
3.木剑每次攻击,怪物损失20HP;铁剑每次攻击怪物损失50HP,魔剑每次攻击,怪物损失100HP,并有50%的概率出现暴击。
注意:暴击指攻击效能增加一倍,既魔剑若出现暴击怪物损失200HP。
不能超过三个构造方法,代码70行以内。
...全文
2837 70 打赏 收藏 转发到动态 举报
写回复
用AI写文章
70 条回复
切换为时间正序
请发表友善的回复…
发表回复
noair_ 2011-11-12
  • 打赏
  • 举报
回复

class Monster {
private int monsterHp;
public Monster(int monsterHp) {
this.monsterHp = monsterHp;
}
public int getMonsterHp() {
return this.monsterHp;
}
public void setMonsterHp(int monsterHp) {
this.monsterHp = monsterHp;
}
public boolean weatherDead() {
if(monsterHp <= 0) return true;
return false;
}
}
//修改部分---------------------------------------------------------
class Weapon {
public static final int WOODENSWORD = 0; //木剑
public static final int IRONNSWORD = 1; //铁剑
public static final int SHADOWBANE = 2; //魔剑

private int attackValue;
public int getAttackValue() {
return this.attackValue;
}
public Weapon(int weaponStyle) {
if(weaponStyle==WOODENSWORD) attackValue = 20; //木剑的攻击力
if(weaponStyle==IRONNSWORD) attackValue = 30; //铁剑攻击力
if(weaponStyle==SHADOWBANE) attackValue = 100;//魔剑攻击力
}
}
class Role {
private Weapon weapon;
public Weapon getWeapon() {
return weapon;
}
public void setWeapon(Weapon weapon) {
this.weapon = weapon;
}
public Role(Weapon weapon) {
this.weapon = weapon;
}
public void attack(Monster monster) { //方法说明:攻击一次,扣除怪物血量一次
int AttackValue = 0;
if(Math.random() > 0.5) { //设置魔剑暴击概率
AttackValue = weapon.getAttackValue()*2; //暴击,丢失200hp
System.out.print("暴击!");
} else {
AttackValue = weapon.getAttackValue(); //普通攻击,丢失100hp
}
int hp = monster.getMonsterHp() - AttackValue;
monster.setMonsterHp(hp);
if(monster.weatherDead()) {
System.out.println("攻击怪物,怪物丢失" + AttackValue + "点血量,剩余血量 0点。怪物已杀死");
} else {
System.out.println("攻击怪物,怪物丢失" + AttackValue + "点血量,剩余血量" + hp + "点");
}
}
public void autoAttack(Monster monster) { //方法说明:调用攻击方法,进行自动攻击,直到怪物死亡
while(!monster.weatherDead()) {
attack(monster);
}

}
}

public class RpgGame {
public static void main(String[] args) {
Monster monster = new Monster(500);
Weapon weapon = new Weapon(Weapon.IRONNSWORD);
Role role = new Role(weapon);
role.autoAttack(monster);
}
}



对刚才的代码做了下修改··将可选武器设置为final以供运行时调用
noair_ 2011-11-12
  • 打赏
  • 举报
回复
攻击怪物,怪物丢失100点血量,剩余血量400点
暴击!攻击怪物,怪物丢失200点血量,剩余血量200点
暴击!攻击怪物,怪物丢失200点血量,剩余血量 0点。怪物已杀死


这是测试结果
noair_ 2011-11-12
  • 打赏
  • 举报
回复

class Monster {
private int monsterHp;
public Monster(int monsterHp) {
this.monsterHp = monsterHp;
}
public int getMonsterHp() {
return this.monsterHp;
}
public void setMonsterHp(int monsterHp) {
this.monsterHp = monsterHp;
}
public boolean weatherDead() {
if(monsterHp <= 0) return true;
return false;
}
}
class Weapon {
private int attackValue;
public int getAttackValue() {
return this.attackValue;
}
public Weapon(int i) {
if(i<=0) attackValue = 20; //木剑的攻击力
if(i==1) attackValue = 30; //铁剑攻击力
if(i>=2) attackValue = 100;//魔剑攻击力
}
}
class Role {
private Weapon weapon;
public Weapon getWeapon() {
return weapon;
}
public void setWeapon(Weapon weapon) {
this.weapon = weapon;
}
public Role(Weapon weapon) {
this.weapon = weapon;
}
public void attack(Monster monster) { //方法说明:攻击一次,扣除怪物血量一次
int AttackValue = 0;
if(Math.random() > 0.5) { //设置魔剑暴击概率
AttackValue = weapon.getAttackValue()*2; //暴击,丢失200hp
System.out.print("暴击!");
} else {
AttackValue = weapon.getAttackValue(); //普通攻击,丢失100hp
}
int hp = monster.getMonsterHp() - AttackValue;
monster.setMonsterHp(hp);
if(monster.weatherDead()) {
System.out.println("攻击怪物,怪物丢失" + AttackValue + "点血量,剩余血量 0点。怪物已杀死");
} else {
System.out.println("攻击怪物,怪物丢失" + AttackValue + "点血量,剩余血量" + hp + "点");
}
}
public void autoAttack(Monster monster) { //方法说明:调用攻击方法,进行自动攻击,直到怪物死亡
while(!monster.weatherDead()) {
attack(monster);
}

}
}


代码60行左右,大家给点意见
cheniwantyou 2011-11-11
  • 打赏
  • 举报
回复
我写的:枚举,英雄,怪物,角色接口,角色基类,主类。
cuseventeen 2011-11-10
  • 打赏
  • 举报
回复

import java.util.*;


public class GameTest
{
public static void main(String[] args)
{
Player player = new Player();
boolean cont = true;
Scanner input = new Scanner(System.in);
while(cont)
{
Monster monster = new Monster((int)(Math.random() * 1000) + 200);
System.out.println("前方出现一个怪物,开始攻击怪物。。。");
System.out.print("请选择攻击武器(0:木剑 , 1 :铁剑 2:魔剑:)");
while(!(player.attack(monster , input.nextInt())))
{
System.out.print("请选择攻击武器(0:木剑 , 1 :铁剑 2:魔剑:)");
}
System.out.println("怪物被打死,是否继续打怪?: 1:是 2:否");
if(input.nextInt() != 1)
{
cont = false;
}
}
}
}

class Player
{
public boolean attack(Monster monster,int i)
{
return monster.beAttack(i);
}
}

class Monster
{
private int HP = 1000;
public Monster(int hp)
{
HP = hp;
// TODO 自动生成构造函数存根
}
public boolean beAttack(int type)
{
switch(type)
{
case 0:
HP -= 20;
break;
case 1:
HP -= 50;
break;
default:
if((Math.random() * 10) > 8)
{
HP -= 200;
}
else
HP-= 100;
}
if(HP < 0)
{
System.out.println("当前怪物已死亡");
return true;
}
System.out.println("当前怪物血量剩余" + HP);
return false;
}
}


小小代码,请指教。
xiangfeidecainiao 2011-11-10
  • 打赏
  • 举报
回复
[Quote=引用 28 楼 werosh 的回复:]

如果按正常的写法,至少需要 武器工厂、英雄工厂、怪物工厂 3个工厂类,光构造工厂类都不下70行代码了。压缩代码的话只能放弃工厂类了。下面代码拟用枚举代替工厂,去掉注释和main函数,大概50多行。
Java code
import java.util.Random;
public class Game2 {
public enum Weapons {
Wood,Iro……
[/Quote]

厉害
sniffer12345 2011-11-10
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct _hero{
int weapon;
} hero;

typedef struct _monster{
int hp;
} monster;

static char* weapons[3] = {"Wood","Iron","Magic"};
static int power[3] = {20,50,100};
static float critical[2] = {1.0f,2.0f};
int main(void)
{
srand(time(NULL));
hero h1;
h1.weapon = rand()%3;
monster m1;
m1.hp = power[h1.weapon] * 6 + rand()%100;
int damage = 0;
while(m1.hp >= 0){
m1.hp -= damage = power[h1.weapon] * critical[rand()%2];
printf("hero uses %s sword to attack the monster cause %d damage.monster left %d.\n",
weapons[h1.weapon],damage,m1.hp);
}
printf("win\n");
return 0;
}
看c多简洁干净
ccsdsx 2011-11-10
  • 打赏
  • 举报
回复
楼主果然是培训机构的,这代码写得够紧凑
ccsdsx 2011-11-10
  • 打赏
  • 举报
回复
不超过3个构造函数不行,超过了我的能力,我写了半个小时,用了5个类,1个接口,还有一些东西没实现,比如暴击的表现形式
使用时候倒是3个构造方法

Hero hero = new Hero(); //英雄
hero.addWeapon(new Shadowbane());//装备武器
hero.target(new Enemy());//敌人
hero.attack();//攻击

public class Hero {
private IWeapon weapon;

private Enemy enemy;

public void addWeapon(IWeapon weapon){
this.weapon = weapon;
if(weapon != null){
System.out.println("你装备了武器:" + weapon.getName());
}
}

public void target(Enemy enemy){
this.enemy = enemy;
if(enemy != null){
System.out.println("你观察着你的敌人");
}
}

private void retarget(){
this.enemy = null;
}

public void attack(){
System.out.println("开始进攻");
if(enemy == null){
System.out.println("无攻击目标,进攻动作结束");
}else if(weapon == null){
System.out.println("未装备武器,救命啊啊啊");
}else{
if(enemy.isDie()){
System.out.println("目标已经死亡,深藏功与名");
}else{
int dam;
do{
dam = weapon.damage();
System.out.println("你发动了进攻,造成" + dam + "点伤害");
enemy.getDamage(dam);
}while(!enemy.isDie());

System.out.println("目标已消灭");
retarget();
}

}
}
}

public class Enemy {

private int life = 1000;

public boolean isDie(){
return life <= 0;
}

public void getDamage(int life){
this.life = this.life - life;

this.life = Math.max(0,this.life);
}
}


public interface IWeapon {
public String getName();
public int damage();
}


public class IronSwords implements IWeapon {

@Override
public int damage() {
return 50;
}

@Override
public String getName() {
return "铁剑";
}

}
public class Shadowbane implements IWeapon {
private Random r;

public Shadowbane(){
r = new Random(new Date().getTime());
}

@Override
public int damage() {
boolean doubleDama = r.nextInt(2) == 0;

return doubleDama?100:200;
}

@Override
public String getName() {
return "魔剑";
}

}
public class WoodenSword implements IWeapon {

@Override
public int damage() {
return 20;
}

@Override
public String getName() {
return "木剑";
}

}
Q1219991042 2011-11-10
  • 打赏
  • 举报
回复
楼主人才啊!!!!!O(∩_∩)O哈!
同道中人
zl8522115 2011-11-09
  • 打赏
  • 举报
回复
又见这个题目.这个是我出PHP面试题的时候出一题目,
这个答案不是要你实际,而是看你的设计模式.
需要用策略模式.
LinApex 2011-11-09
  • 打赏
  • 举报
回复

package com.linapex.test;

import java.util.Random;

/**
* @author :LinApex
* @date :2011-11-9上午10:05:42
* @function:
*/

class Monster {
public Monster(int hp) {
this.hp = hp;
}

public int hp;
}

class Hero {
public int weapon; // 武器
private int[] attackArr = { 20, 50, 100 }; // 攻击力
private String[] weaponNameArr = { "木剑", "铁剑", "魔剑" }; // 攻击力

public Hero(int weapon, Monster monster) {
this.weapon = weapon;
this.attack(monster);
}

public void attack(Monster monster) {
while (monster.hp > 0) {
int weaponAttack = attackArr[weapon]; // 得到攻击力
if (weapon == 2) {
boolean crit = new Random().nextBoolean(); // 得到暴击率
weaponAttack = crit ? weaponAttack * 2 : weaponAttack; // 攻击力计算
System.out.print(crit ? "暴击!" : "");
}
monster.hp = monster.hp - weaponAttack < 0 ? 0 : monster.hp - weaponAttack;
System.out.println("玩家使用" + weaponNameArr[weapon] + "攻击怪物(-"
+ weaponAttack + "),怪物剩余血量"+monster.hp);
}
System.out.println("怪物死亡,英雄胜利");
}
}

public class Attack {
public static void main(String[] args) {
new Hero(2, new Monster(1000));
}

}


LinApex 2011-11-09
  • 打赏
  • 举报
回复
package com.linapex.test;

import java.util.Random;

/**
* @author :LinApex
* @date :2011-11-9上午10:05:42
* @function:
*/

class Monster {
public Monster(int hp) {
this.hp = hp;
}

public int hp;
}

class Hero {
public int weapon; // 武器
private int[] attackArr = { 20, 50, 100 }; // 攻击力
private String[] weaponNameArr = { "木剑", "铁剑", "魔剑" }; // 攻击力

public Hero(int weapon, Monster monster) {
this.weapon = weapon;
this.attack(monster);
}

public void attack(Monster monster) {
while (monster.hp > 0) {
int weaponAttack = attackArr[weapon]; // 得到攻击力
if (weapon == 2) {
boolean crit = new Random().nextBoolean(); // 得到暴击率
weaponAttack = crit ? weaponAttack * 2 : weaponAttack; // 攻击力计算
System.out.print(crit ? "暴击!" : "");
}
monster.hp = monster.hp - weaponAttack < 0 ? 0 : monster.hp - weaponAttack;
System.out.println("玩家使用" + weaponNameArr[weapon] + "攻击怪物(-"
+ weaponAttack + "),怪物剩余血量"+monster.hp);
}
System.out.println("怪物死亡,英雄胜利");
}
}

public class Attack {
public static void main(String[] args) {
new Hero(2, new Monster(1000));
}

}
xxpp688 2011-11-09
  • 打赏
  • 举报
回复
[Quote=引用楼主 public_calss 的回复:]
用java作一个非常简单的游戏:
要求:
1.角色可以向怪物实施攻击,一次攻击后,怪物损失部分HP,当HP损失完后,怪物死亡。
2.角色可装备部同武器,目前有木剑、铁剑、魔剑三种。
3.木剑每次攻击,怪物损失20HP;铁剑每次攻击怪物损失50HP,魔剑每次攻击,怪物损失100HP,并有50%的概率出现暴击。
注意:暴击指攻击效能增加一倍,既魔剑若出现暴击怪物损失200HP。
不……
[/Quote]
我觉得java写代码不在于代码的少,而在于是否面向对象,或者面向方面编程。当然也要求代码简洁(比如耦合性,是否冗余……),但绝不是一味的追求少。
  • 打赏
  • 举报
回复
import java.util.Random;

public class GameLogic {
// 攻击者
private Role attacker;
// 被攻击者
private Role defender;

public GameLogic(String attackType, int attackHp, Arms arms,
String defenderType, int defenderHp) {
attacker = new Role(attackType, attackHp);
attacker.equip(arms);
defender = new Role(defenderType, defenderHp);
}

public void start() {
attack(attacker, defender);
}

private void attack(Role attack, Role defender) {
Random random = new Random();
while (defender.getHp() > 0) {
// 计算伤害
int hurt = attack.getHurt() * (random.nextBoolean() ? 1 : 2);
if (hurt > defender.getHp())
System.out.println(attack.getType() + "攻击" + defender.getType()
+ " 伤害 :" + defender.getHp());
else
System.out.println(attack.getType() + "攻击" + defender.getType()
+ " 伤害 :" + hurt);
defender.setHp(defender.getHp() - hurt);
}
System.out.println(defender.getType() + "死亡");
}

public static void main(String[] args) {
// .....用户 提示选项 确定初始参数

GameLogic logic = new GameLogic("a", 10, new Arms(100, "魔剑"), "d", 1001);
logic.start();

}
}

class Role {
private int hp;

private String type;

private Arms arms;

public int getHp() {
return hp;
}

public synchronized void setHp(int hp) {
this.hp = hp;
}

public String getType() {
return type;
}

public Arms getArms() {
return arms;
}

public Role(String type, int hp) {
this.type = type;
this.hp = hp;
}

public synchronized void equip(Arms arms) {
this.arms = arms;
}

public int getHurt() {
// 默认无武器装备时,默认伤害为1
return null == arms ? 1 : arms.getHurt();
}
}

class Arms {
private int hurt;

private String name;

public Arms(int hurt, String name) {
this.hurt = hurt;
this.name = name;
}

public String getName() {
return name;
}

public int getHurt() {
return hurt;
}
}
liujunxie 2011-11-09
  • 打赏
  • 举报
回复
厉害啊
hollin1988 2011-11-09
  • 打赏
  • 举报
回复

public static void main(String args[]){
System.out.println("楼主出场");
System.out.println("怪物悲剧");
System.out.println("此据结束,龙套观众继续");
}
xxxxxxbin 2011-11-09
  • 打赏
  • 举报
回复
看了下,学习了,有好玩的例子收藏起来
lkm0422 2011-11-08
  • 打赏
  • 举报
回复
我也发上我自己做的,只做了个大概思路,写得不太全。

//这个是英雄类
public class Hero {

private Weapon weapon;
private String name;

public Hero(String name) {
this.name = name;
}

public void equip(Weapon weapon) {
this.weapon = weapon;
}

public int attack(Monster monster) {
int basicAttack = weapon.getAttack();
int actualAttack = basicAttack * getAttackFactor();

monster.addDamage(actualAttack);
return actualAttack;
}

public String getName() {
return name;
}

//暴击的计算
private int getAttackFactor() {
double rand = Math.random();
int res = 1;

if (rand >= 0.0 && rand < 0.5) {
res = 2;
}

return res;
}
}


//怪物的接口
public interface Monster {

public void addDamage(int damage);

public boolean isAlive();
}


//武器的接口
public interface Weapon {

public int getAttack();
}


//武器:木剑
public class WoodSword implements Weapon{

private final int ATTACK_POINT = 20;

@Override
public int getAttack() {
return ATTACK_POINT;
}

}

其他的武器都实现了Weapon接口,大致与WoodSword相同,只是ATTACK_POINT不同而已。

//一个普通的怪物
public class CommonMonster implements Monster {

private int heartPoint;
private boolean alive;

public CommonMonster(int hp) {
heartPoint = hp;
alive = true;
}

@Override
public void addDamage(int damage) {
heartPoint -= damage;
if (heartPoint <= 0) {
heartPoint = 0;
alive = false;
}
}

@Override
public boolean isAlive() {
return alive;
}
}

接下来可以测试一下了:

public class TestGame {

public static void main(String[] args) {
Hero hero = new Hero("Hero");
Monster monster = new CommonMonster(1000);
Weapon woodSword = new IronSword();//武器可以换成其他你喜欢的

hero.equip(woodSword);
while(monster.isAlive()) {
int attack = hero.attack(monster);

System.out.println("击掉怪物" + attack + "点血");
if(!monster.isAlive()) {
System.out.println(hero.getName() + "打败了monster");
break;
}
}
}
}
叶子的翅膀 2011-11-08
  • 打赏
  • 举报
回复
学习了,自己想想
加载更多回复(44)

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧