代码改变世界

电子宠物(线程,实现方法)

2017-12-27 12:41  澄序源  阅读(257)  评论(0编辑  收藏  举报

动物类(接口)

每种宠物都有不同的属性增减值

所以 方法需要各自的宠物类去实现

package com.hanqi.main;

public abstract class Pet {
	//定义一个 动物 接口类 
	private String name ; //名字
	private int age; // 年龄
	private int hp ; // 血量
	private int hungry ; // 饥饿度
	private int happy; // 心情
	private int $ ;  // 钱
	private int L ;  // 力量 
	private boolean b = true;
	
	static double monster = 20;  // 怪物血量 
	
	public Pet () {}
	public Pet (String name, int hp , int hungry , int happy , int $ , int L) {
		this.name = name;
		this.hp = hp;
		this.hungry = hungry;
		this.happy = happy;
		this.$ = $;
		this.L = L;
	}
	
	// 添加几个需要被实现的方法
	public abstract void combat () ; // 战斗
	public abstract void eat () ; // 吃饭
	public abstract void cure () ;  // 治疗
	public abstract void play () ; // 玩
	public abstract void  att ();  // 属性
	
	// 判断 宠物是否还活着
	public boolean panduan () {
		if ( hp <= 0 ) {
			System.out.println("宠物挂了!!");
			System.out.println(name + "临死前把你一起带走了!?");
			b =  false;
		} else if (hungry <= 0 ){
			if (hungry <= -50 ) {
				System.out.println(name + "饿死了!!");
				System.out.println(name + "临死前把你一起带走了!?");
				b =  false;
			}else {
				System.out.println("快饿死了!!");
			}
		} else if (happy <= 0 ){
			System.out.println(name + "很不高兴!!");
			if (happy <= -50 ) {
				System.out.println(name + "郁闷死了!!");
				System.out.println(name + "临死前把你一起带走了!?");
				b = false;
			}
		}
		return b;
	}
	
	
	//get,set方法
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public int getHp() {
		return hp;
	}
	public void setHp(int hp) {
		this.hp = hp;
	}
	public int getHungry() {
		return hungry;
	}
	public void setHungry(int hungry) {
		this.hungry = hungry;
	}
	public int getHappy() {
		return happy;
	}
	public void setHappy(int happy) {
		this.happy = happy;
	}
	public int get$() {
		return $;
	}
	public void set$(int $) {
		this.$ = $;
	}
	public int getL() {
		return L;
	}
	public void setL(int l) {
		L = l;
	}
	public static double getMonster() {
		return monster;
	}
	public static void setMonster(double monster) {
		Pet.monster = monster;
	}
	public boolean isB() {
		return b;
	}
	public void setB(boolean b) {
		this.b = b;
	}
	
}

 

线程类 用来后台运行宠物随时间增长的属性等

package com.hanqi.main;

import java.util.TimerTask;
// 线程类
public class Mytimertask extends TimerTask {
	
	public Pet p ;
	public Mytimertask() {
		super();
	}
	public Mytimertask(Pet p) {
		super();
		this.p = p;
	}
	
	
	Main main1 = new Main();

	//需要被重写的方法
	@Override
	public void run() {
		//System.out.println("测试");
		
		p.setAge(p.getAge() + 1);
		p.setHappy(p.getHappy() - 1);
		p.setHungry(p.getHungry() - 1);
		if (p.panduan() == false) {
			main1.pd();
		}
	}

}

 

main 方法类 用来运行"宠物"

package com.hanqi.main;

import java.util.Scanner;
import java.util.Timer;

public class Main {
	static Timer  timer ;
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("1 . 人头狗  2 . 飞天猫  3 . 装甲猪  4 .迷你鸡");
		String s = scan.nextLine();
		System.out.println("起个名字");
		String s1 = scan.nextLine();
		Pet p = null;
		boolean b = true;
		timer = new Timer();
		
		// 定义 宠物类的属性初始值
		//选择谁实例化谁
		if ("1".equals(s)) {
			p = new Dog(s1 , 400 , 110 , 110 ,100, 8); // 狗  血  饥饿  心情  钱 力量;
		} else if ("2".equals(s)){
			p = new Cat(s1, 300 , 120 , 120 ,100, 7);  // 猫
		}else if ("3".equals(s)){
			p = new Pag(s1, 500 , 80 , 100 ,100, 10);  // 猪
		}else if ("4".equals(s)){
			p = new Chicken(s1, 20 , 150 , 150 ,100, 6);  // 鸡
		} else {
			System.out.println("你没有选择宠物 !");
			System.out.println("缺少宠物的保护,你的脑子被怪物吃了!!");
		}

		// 实例化线程类
		Mytimertask task = new Mytimertask(p);
		//调用方法  隔2s 运行一下 task;
		timer.schedule(task, 0 , 2000);
		
		while (b) {
			System.out.println("1 . 战斗  2 . 吃饭  3 . 治疗  4 . 玩耍  5 . 属性  6 . 退出");
			String s2 = scan.nextLine();
			if ("1".equals(s2)) {
				p.combat();
			} else if ("2".equals(s2)) {
				p.eat();
			} else if ("3".equals(s2)) {
				p.cure();
			}else if ("4".equals(s2)) {
				p.play();
			}else if ("5".equals(s2)) {
				p.att();
			}else if ("6".equals(s2)) {
				System.out.println("再见来不及挥手!");
				b = false;
				timer.cancel();
			}else {
				System.out.println("输入错了");
			}
		}
	}
	
	// 用来结束程序
	public void pd () {
		timer.cancel();
		System.exit(0);
	}
	
}

 

宠物类 要实现pet(动物类)的方法

例:

package com.hanqi.main;

public class Pag extends Pet{
	String name;
	
	int hp ; // 血量
	int hungry ; // 饥饿度
	int happy ; // 心情
	static double monster = 20;
	int $ ; // 钱
	int L ; // 力量
	
	public Pag () {}
	public Pag(String s1, int hp , int hungry , int happy , int $ , int L) {
		super(s1, hp ,  hungry ,  happy ,  $ ,  L);
		 this.name = s1;
		 this.hp = hp;
		 this.hungry = hungry;
		 this.happy = happy;
		 this.$ = $;
		 this.L = L;
	}
	
	@Override
	public void combat() {
		// 战斗
		//System.out.println(hp);
		monster = monster + 1;
		int z;
		if (monster  < L) {
			hp = hp - 1;
			z = -1;
		} else {
			hp = hp - (int)(monster - L);
			z = (int)monster - L;
		}
		System.out.println("==============================");
		System.out.println(name + "进行了一次战斗 ! " );
		System.out.println("HP -" + z + "  力量 + 2  饥饿 - 5  心情 - 4   $ + 50");
		System.out.println("==============================");
		super.setHp(super.getHp() -  (int)(monster - L));
		super.setL(super.getL() + 2);
		super.setHungry( super.getHungry() - 5);
		super.setHappy(super.getHappy());
		super.set$(super.get$() + 50);
		/*L = L + 2;
		hungry = hungry - 5;
		happy = happy - 4;*/
		$ = $ +50;
		
	}

	@Override
	public void eat() {
		// 吃
		if ( $ <= 0) {
			System.out.println("钱不够了 , 打点怪去!");
		} else {
			System.out.println("==============================");
			System.out.println(name + "吃了根骨头 ! " );
			System.out.println("Hp + 1  饥饿 + 18   心情 + 5   $ - 10");
			System.out.println("==============================");
			super.setHp(super.getHp() + 1);
			super.setHungry( super.getHungry() + 18);
			super.setHappy(super.getHappy() + 5);
			super.set$(super.get$() - 10);
			/*hp = hp + 1;
			hungry = hungry + 20;
			happy = happy + 5;*/
			$ = $  - 10;
		}
	}

	@Override
	public void cure() {
		// 治疗
		System.out.println("==============================");
		if ( $ <= 0) {
			System.out.println("钱不够了 , 打点怪去!");
		} else {
			System.out.println(name + "去接受了治疗 ! " );
			System.out.println("Hp + 50    心情 - 5   $ - 50");
			System.out.println("==============================");
			super.setHp(super.getHp() + 50);
			super.setHappy(super.getHappy() - 5);
			super.set$(super.get$() - 50);
			/*hp = hp + 50;
			happy = happy - 5;*/
			$ = $  - 50;
		}
	}

	@Override
	public void play() {
		// 玩
		System.out.println("==============================");
		System.out.println(name + "玩了一会 ! " );
		System.out.println("饥饿 - 10   心情 + 15");
		System.out.println("==============================");
		super.setHungry( super.getHungry() - 10);
		super.setHappy(super.getHappy() + 15);
		super.set$(super.get$() - 1);
		/*hungry = hungry - 10;
		happy = happy + 15;*/
		$ = $  - 1 ;
	}

	@Override
	public void att() {
		// 属性
		System.out.println("=========================");
		System.out.print("金币" + super.get$());
		System.out.print("力量" + super.getL());
		System.out.print("血量" + super.getHp());
		System.out.print("心情" + super.getHappy());
		System.out.println("饥饿" + super.getHungry());
		System.out.println("=========================");
		
		
		
	}

}