⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pet.java

📁 一个类似电子宠物的游戏`对需要做这类游戏的人有帮助`
💻 JAVA
字号:
package Petgame;

import javax.microedition.lcdui.*;

/*
 * 创建日期 2004-7-25
 *
 * 更改所生成文件模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */

/**
 * @author Administrator
 * 
 * 更改所生成类型注释的模板为 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
public class Pet extends Sprite {
	public Pet(Image pic, int height, int width) {
		super(pic, height, width);
		heart = new estate("心情度",12, 12, 10, 6, "我很快乐","我很不开心");
		posture = new estate("劳累度",12, 12, 20, 6, "身体状态不错","很累");
		glut = new estate("饱腹度",12, 12, 40, 6, "不饿","很饿");
		if (heart.getFlag()) {
			setframesq(good_ani);
		} else {
			setframesq(bad_ani);
		}
	}

	private int good_ani[] = { 14, 15, 16, 14, 15, 16 }; //好心情动画数组

	private int bad_ani[] = { 12, 13, 12, 13, 12, 13 }; //坏心情动画数组

	private int dining_ani[] = { 0, 1, 2, 2, 2, 3 }; //喂吃动画数组

	private int playgame_ani[] = { 10, 11, 10, 11, 10, 11 }; //游玩动画数组

	private int repose_ani[] = { 17, 18, 17, 18, 17, 18 }; //休息动画数组

	private int hungry_ani[] = { 4, 5, 4, 5, 4, 5 }; //饥饿动画数组

	private int posture_ani[] = { 6, 7, 8, 8, 9, 9 }; //劳累动画数组

	private estate heart; //心情

	private estate posture; //身体状态

	private estate glut; //饱腹度

	private boolean command_flag = false;

	/**
	 * 进行过程指令,负责宠物生命进程
	 *  
	 */
	public void playing() {
		heart.playing();
		posture.playing();
		glut.playing();
		if (worked() && !glut.getFlag()) {
			setframesq(hungry_ani);
			command_flag = false;
		}
		if (worked() && !posture.getFlag()) {
			setframesq(posture_ani);
			command_flag = false;
		}

		if (worked()) {
			command_flag = false;
			if (heart.getFlag()) {
				setframesq(good_ani);
			} else {
				setframesq(bad_ani);
			}
		}
		nextframe();
	}

	/**
	 * 喂吃宠物指令
	 *  
	 */
	public void dining() {
		if (!command_flag) {
			heart.value = heart.value + 3;
			glut.value = glut.value + 4;
			posture.value = posture.value - 1;
			command_flag = true;
			setframesq(dining_ani);
		}

	}

	/**
	 * 宠物游玩指令
	 *  
	 */
	public void playgame() {
		if (!command_flag) {
			heart.value = heart.value + 3;
			glut.value = glut.value - 1;
			posture.value = posture.value - 1;
			command_flag = true;
			setframesq(playgame_ani);
		}
	}
public void drawMsg(Graphics g){
	g.drawString(heart.name,0,0,Graphics.LEFT|Graphics.TOP);
	g.drawString(":",34,0,Graphics.LEFT|Graphics.TOP);
	g.drawString(heart.getValue(),40,0,Graphics.LEFT|Graphics.TOP);
	g.drawString(posture.name,60,0,Graphics.LEFT|Graphics.TOP);
	g.drawString(":",94,0,Graphics.LEFT|Graphics.TOP);
	g.drawString(posture.getValue(),100,0,Graphics.LEFT|Graphics.TOP);
	g.drawString(glut.name,120,0,Graphics.LEFT|Graphics.TOP);
	g.drawString(":",154,0,Graphics.LEFT|Graphics.TOP);
	g.drawString(glut.getValue(),160,0,Graphics.LEFT|Graphics.TOP);
	if (!glut.getFlag())
		g.drawString(glut.getMsg(),65,150,Graphics.LEFT|Graphics.TOP);
	else if (!posture.getFlag())
		g.drawString(posture.getMsg(),65,150,Graphics.LEFT|Graphics.TOP);
	else if (true)
		g.drawString(heart.getMsg(),65,150,Graphics.LEFT|Graphics.TOP);
	
}
	
	/**
	 * 宠物休息指令
	 *  
	 */

	public void repose() {
		if (!command_flag) {
			heart.value = heart.value + 1;
			posture.value = posture.value + 4;
			command_flag = true;
			setframesq(repose_ani);
		}
	}

	/**
	 * 状态类
	 * 
	 * @author Administrator
	 * 
	 * 更改所生成类型注释的模板为 窗口 > 首选项 > Java > 代码生成 > 代码和注释
	 */
	public class estate {

		public estate(String name, int value, int max, int timemax, int grade,
				String msg1, String msg2) {
			this.name = name;
			this.value = value;
			this.max = max;
			this.time = timemax;
			this.timemax = timemax;
			this.grade = grade;
			this.flag = true;
			this.msg1 = msg1;
			this.msg2 = msg2;

		}

		public String getValue(){
			String Value;
			Value= String.valueOf(this.value);
			return Value;
		}
		public String getMsg() {
			String msg;
			if (flag == true)
				msg = msg1;
			else
				msg = msg2;
			return msg;
		}

		public void playing() {
			if (value!=0){
			if (time <= 0) {
				time = timemax;
				value--;
			} else {
				--time;
			}
		}
		}
		public boolean getFlag() {
			if (value > grade) {
				flag = true;
			} else {
				flag = false;
			}
			return flag;
		}

		protected String name;

		protected int value;

		protected int max;

		protected int time;

		protected int timemax;

		protected int grade;

		protected boolean flag;

		protected String msg1;

		protected String msg2;

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -