如何转换我写在下面的代码? [英] How can I transform the code I wrote down below?

查看:38
本文介绍了如何转换我写在下面的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am suppose to code the snake game in java with processing for IT classes and since I had no idea how to do it I searched for a YouTube tutorial. Now I did find one but he used the keys 'w','s','d','a' to move the snake around - I on the other hand want to use the arrow keys. Could someone explain to me how I transform this code:

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) {}
}

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

This is my entire coding so far:

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
   }
}

推荐答案

很难回答一般性的我该怎么做"类型的问题.Stack Overflow 是为更具体的我试过 X,预期是 Y,但得到 Z"类型的问题而设计的.话虽如此,我将尝试从一般意义上回答:

It's hard to answer general "how do I do this" type questions. Stack Overflow is designed for more specific "I tried X, expected Y, but got Z instead" type questions. That being said, I'll try to answer in a general sense:

尝试获取在 Internet 上找到的随机代码并尝试使其在您的草图中运行将非常困难.这不是一个很好的方法.

You're going to have a very difficult time trying to take random code you find on the internet and trying to make it work in your sketch. That's not a very good way to proceed.

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

Instead, you need to take a step back and really think about what you want to happen. Instead of taking on your entire end goal at one time, try breaking your problem down into smaller steps and taking on those steps one at a time.

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

Step 1: Can you store the state of your game in variables? You might store things like the direction the snake is traveling the location of the snake, etc.

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

Step 2: Can you write code that just prints something to the console when you press the arrow keys? You might do this in a separate example sketch instead of trying to add it directly to your full sketch.

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

Step 3: Can you combine those two steps and change the state of your sketch when an arrow key is pressed? Maybe you change the direction the snake is traveling.

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

The point is that you need to try something instead of trying to copy-paste random code without really understanding it. Break your problem down into small steps, and then post an MCVE of that specific step if you get stuck. Good luck.

这篇关于如何转换我写在下面的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆