Abigail
Abigail

Reputation: 57

How can I transform the code I wrote down below?

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

into something like this:

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

Upvotes: 1

Views: 97

Answers (2)

Luis Morales
Luis Morales

Reputation: 126

You should take a look into Java API KeyEvent VK_LEFT. And as pczeus already told you, you need to implement a capturing of the keystrokes! This can be checked here (Link from this SO answer).

Upvotes: 0

Kevin Workman
Kevin Workman

Reputation: 42174

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:

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.

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.

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.

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.

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.

Upvotes: 1

Related Questions