【问题标题】:How can I transform the code I wrote down below?如何转换我在下面写下的代码?
【发布时间】:2016-05-10 10:26:49
【问题描述】:

我想用 Java 编写蛇游戏代码并处理 IT 类,因为我不知道怎么做,所以我搜索了一个 YouTube 教程。现在我确实找到了一个,但他使用键'w','s','d','a'来移动蛇 - 另一方面我想使用箭头键。有人可以向我解释一下我如何转换这段代码:

if (keyPressed == true) {
   int newdir = key=='s' ? 0 : (key=='w' ? 1 : (key=='d' ? 2 : (key=='a' ? 3 : -1)));
}


if(newdir != -1 && (x.size() <= 1 || !(x.get(1) ==x.get(0) + dx[newdir] && y.get (1) == y.get(0) + dy[newdir]))) dir = newdir;
}

变成这样:

void keyPressed () {
 if (key ==  CODED) {
  if (keyCode == UP) {}
   else if (keyCode == RIGHT) {}
    else if (keyCode == DOWN) {}
     else if (keyCode == LEFT) {}
}

这是我到目前为止的全部代码:

ArrayList<Integer> x = new ArrayList<Integer> (), y = new ArrayList<Integer> ();
int w = 900, h = 900, bs = 20, dir = 1; // w = width ; h = height ; bs = blocksize ; dir = 2 --> so that the snake goes up when it starts
int[] dx = {0,0,1,-1} , dy = {1,-1,0,0};// down, up, right, left

void setup () {
   size (900,900); // the 'playing field' is going to be 900x900px big
   // the snake starts off on x = 5 and y = 30 
   x.add(5); 
   y.add(30);
}

void draw() {
   //white background
   background (255);

   //
   // grid 
   // vertical lines ; the lines are only drawn if they are smaller than 'w'
   // the operator ++ increases the value 'l = 0' by 1 
   // 
   for(int l = 0 ; l < w; l++) line (l*bs, 0, l*bs, height); 
   //
   // horizontal lines ; the lines are only drawn if they are smaller than 'h'
   // the operator ++ increases the value 'l = 0' by 1 
   //  
   for(int l = 0 ; l < h; l++) line (0, l*bs, width, l*bs);

   //
   // snake
   for (int l = 0 ; l < x.size() ; l++) {
       fill (0,255,0); // the snake is going to be green
       rect (x.get(l)*bs, y.get(l)*bs, bs, bs);    
   }

   if(frameCount%5==0) { // will check it every 1/12 of a second -- will check it every 5 frames at a frameRate = 60
       // adding points
       x.add (0,x.get(0) + dx[dir]); // will add a new point x in the chosen direction
       y.add (0,y.get(0) + dy[dir]); // will add a new point y in the chosen direction
       // removing points
       x.remove(x.size()-1); // will remove the previous point x
       y.remove(y.size()-1); // will remove the previous point y
   }
}

【问题讨论】:

  • 您的代码中没有任何内容显示与击键的任何集成。到目前为止,您是否在代码中做过任何事情来捕获击键?您需要为您的 GUI 显示相关代码
  • 这是一个 GUI 程序 (SWING/SWT) 还是一个控制台程序?如果是控制台程序读取箭头比较复杂:stackoverflow.com/questions/9545388/…
  • @pczeus 是的 - 显然我忘了添加它我添加了 int key, keyCode;
  • @gfelisberto 它是一个图形用户界面。只是交给我的老师没什么花哨的

标签: processing


【解决方案1】:

很难回答一般的“我该怎么做”类型的问题。 Stack Overflow 专为更具体的“我尝试了 X,预期 Y,但得到了 Z”类型的问题而设计。话虽如此,我将尝试笼统地回答:

您将很难尝试获取您在互联网上找到的随机代码并尝试使其在您的草图中工作。这不是一个很好的方法。

相反,您需要退后一步,认真思考您想要发生的事情。与其一次性完成整个最终目标,不如尝试将问题分解为更小的步骤,并一次完成这些步骤。

第 1 步:您可以将游戏的状态存储在变量中吗?您可以存储诸如蛇行进的方向、蛇的位置等信息。

第 2 步:您能否编写代码,在您按下箭头键时将某些内容打印到控制台?您可以在单独的示例草图中执行此操作,而不是尝试将其直接添加到您的完整草图中。

第 3 步:您能否结合这两个步骤并在按下箭头键时更改草图的状态?也许你改变了蛇行进的方向。

关键是您需要尝试一些事情,而不是在没有真正理解的情况下尝试复制粘贴随机代码。将您的问题分解为小步骤,然后在遇到困难时发布该特定步骤的MCVE。祝你好运。

【讨论】:

    【解决方案2】:

    您应该查看 Java API KeyEvent VK_LEFT。 正如 pczeus 已经告诉你的那样,你需要实现击键的捕获!这可以检查here(来自this SO答案的链接)。

    【讨论】:

    • 这是processing 问题,不是Java 问题。 Processing 有自己的事件处理程序,你真的不应该使用 Java 的 KeyEvents。
    猜你喜欢
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 2014-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多