2017-06-11 64 views
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); 
    } 

} 

回答

2

Robot类,名称为命令(⌘)是KeyEvent.VK_META。您的代码应该是这样的:

Robot r = new Robot(); 
r.keyPress(KeyEvent.VK_META); //press command 
r.keyPress(KeyEvent.VK_L); //press l 
r.keyRelease(KeyEvent.VK_META); //release command 
r.keyRelease(KeyEvent.VK_L); //release l 

这按下命令键,按下大号键,然后释放他们。我已经测试过了,它应该可以正常工作。

注:使用Robot抛出AWTException,所以它需要你要么try-catch块包围,或者在你的方法头扔AWTException