1
我目前正在嘗試在java中創建tamogatchi遊戲,但是,我需要一種方法來清除輸出屏幕,以便健康欄/食物欄在停留時停止相同的位置。我發現按下Command + L(在Mac上)將清除輸出。但是,我無法找到它的KeyEvent命令。這是我目前有模擬在Mac上按下「Command」鍵
package tamogatchu;
import java.util.*;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
public class Tamogatchu {
public static void main(String[] args) throws InterruptedException, AWTException {
initialize();
}
public static void initialize() throws InterruptedException, AWTException {
int full = 10;
int health = 10;
int happy = 10;
int idk = 0;
int poop = 0;
String[] food = new String[10];
String[] healthbar = new String[10];
String[] happyness = new String[10];
for (int i = 0; i < 10; i++) {
food[i] = "◼";
}
for (int i = 0; i < 10; i++) {
healthbar[i] = "◼";
}
for (int i = 0; i < 10; i++) {
happyness[i] = "◼";
}
Print(food, full, healthbar, health, idk, happyness, happy, poop);
}
public static void tamogatchu(String[] food, int full, String[] healthbar, int health, int idk, String[] happyness, int happy, int poop) throws InterruptedException, AWTException {
//int timer = 0;
Scanner input = new Scanner(System.in);
String care = "";
String hungery = "";
String healthery = "";
String happery = "";
while (health > 0) {
if (full != 0) {
Thread.sleep(10000);
} else {
Thread.sleep(5000);
}
Robot r = new Robot();
r.keyPress(KeyEvent.VK_L);
// the command⌘ key should be pressed here r.keyPress(KeyEvent.);
//r.keyRelease(KeyEvent.);
r.keyRelease(KeyEvent.VK_L);
String Line = Arrays.toString(food);
String result = Line.replaceAll("[-+.^:, ]", "");
hungery = "Hunger: " + result;
System.out.println(hungery);
String Line2 = Arrays.toString(healthbar);
String result2 = Line2.replaceAll("[-+.^:, ]", "");
healthery = "Health: " + result2;
System.out.println(healthery);
String Line3 = Arrays.toString(happyness);
String result3 = Line3.replaceAll("[-+.^:, ]", "");
happery = "Happieness: " + result3;
System.out.println(happery);
if (idk == 0) {
System.out.println(" .^._.^.\n"
+ " | . . |\n"
+ " ( --- )\n"
+ " .' '.\n"
+ " |/ \\|\n"
+ " \\ /-\\ /\n"
+ " V V");
idk = 1;
} else {
System.out.println(" .^._.^.\n"
+ " | . . |\n"
+ " ( --- )\n"
+ " |/ \\|\n"
+ " \\ /-\\ /\n"
+ " V V");
idk = 0;
}
System.out.println("Poops: " + poop);
System.out.println("take care of your pet? -type 'feed' to feed or 'clean' to get rid of poops-");
care = input.nextLine();
if (care.equalsIgnoreCase("feed")) {
full += 5;
if (full > 10) {
full = 10;
}
} else if (care.equalsIgnoreCase("clean")) {
poop = 0;
}
if (full % 2 == 0) {
poop++;
happy -= poop;
}
if (full > 7 && health < 10 || full > 5 || poop == 0) {
if (full > 7 && health < 10) {
health++;
if (health > 10) {
health = 10;
}
}
if (full > 5) {
happy++;
if (happy > 10) {
happy = 10;
}
}
if (poop == 0) {
happy++;
if (happy > 10) {
happy = 10;
}
}
}
if (full != 0) {
full--;
if (full <= 5) {
happy--;
}
if (full == 0) {
happy--;
health--;
} else if (happy == 0) {
health--;
}
Print(food, full, healthbar, health, idk, happyness, happy, poop);
}
if (full == 0 || happy == 0) {
if (full == 0) {
happy--;
health--;
} else if (happy == 0) {
health--;
}
Print(food, full, healthbar, health, idk, happyness, happy, poop);
}
}
System.out.println("Hunger: [◻◻◻◻◻◻◻◻◻◻]");
System.out.println("Health: [◻◻◻◻◻◻◻◻◻◻]");
System.out.println("Happieness: [◻◻◻◻◻◻◻◻◻◻]");
System.out.println(" .^._.^.\n"
+ " | x x |\n"
+ " ( --- )\n"
+ " .' '.\n"
+ " |/ \\|\n"
+ " \\ /-\\ /\n"
+ " V V");
System.out.println("YOU KILLED IT");
System.exit(0);
}
public static void Print(String[] food, int full, String[] healthbar, int health, int idk, String[] happyness, int happy, int poop) throws InterruptedException, AWTException {
for (int i = 0; i < full; i++) {
food[i] = "◼";
}
if (-1 + full >= 0) {
for (int i = 9; i > -1 + full; i--) {
food[i] = "◻";
}
} else {
for (int i = 0; i < 10; i++) {
food[i] = "◻";
}
}
for (int i = 0; i < health; i++) {
healthbar[i] = "◼";
}
for (int i = 9; i > -1 + health; i--) {
healthbar[i] = "◻";
}
for (int i = 0; i < happy; i++) {
happyness[i] = "◼";
}
if (-1 + happy >= 0) {
for (int i = 9; i > -1 + happy; i--) {
happyness[i] = "◻";
}
} else {
for (int i = 0; i < 10; i++) {
happyness[i] = "◻";
}
}
tamogatchu(food, full, healthbar, health, idk, happyness, happy, poop);
}
}