Processing 如何转换我在下面写下的代码?

Processing 如何转换我在下面写下的代码?,processing,Processing,我想用java编写snake游戏,其中包含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 != -

我想用java编写snake游戏,其中包含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;
}
这是我到目前为止的全部编码:

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
   }
}
ArrayList x=newarraylist(),y=newarraylist();
int w=900,h=900,bs=20,dir=1;//w=宽度;h=高度;bs=块大小;dir=2-->以便蛇在启动时上升
int[]dx={0,0,1,-1},dy={1,-1,0,0};//下,上,右,左
无效设置(){
大小(900900);//比赛场地将是900x900px大
//蛇从x=5和y=30开始
x、 增加(5);
y、 增加(30);
}
作废提款(){
//白色背景
背景(255);
//
//网格
//垂直线;仅当线小于“w”时才绘制线
//运算符++将值“l=0”增加1
// 
对于(int l=0;l
很难回答一般的“我该怎么做”类型的问题。堆栈溢出是为更具体的“我尝试了X,期望是Y,但得到了Z”类型的问题而设计的。话虽如此,我将试着从一般意义上回答:

你将很难在网上找到随机代码,并试图让它在你的草图中发挥作用。这不是一个很好的方法

相反,你需要后退一步,真正思考你想要发生什么。不要一次就完成你的全部目标,试着把你的问题分解成更小的步骤,一次一个地完成这些步骤

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

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

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


关键是你需要尝试一些东西,而不是在没有真正理解的情况下复制粘贴随机代码。把你的问题分解成几个小步骤,如果你被卡住了,就贴出具体步骤的说明。祝你好运。

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

代码中没有显示任何与按键笔划的集成。到目前为止,您在代码中是否做过任何事情来捕获击键?您需要显示GUI的相关代码这是GUI程序(SWING/SWT)还是控制台程序?如果它是一个控制台程序,那么读取箭头是复杂的:@pczeus yes-显然我忘了添加它,我添加了int-key,keyCode@它是一个图形用户界面。这是一个问题,不是Java问题。处理有自己的事件处理程序,您真的不应该使用Java的KeyEvents。
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
   }
}