4 Replies - 770 Views - Last Post: 10 October 2013 - 02:09 AM Rate Topic: -----

#1 mfoster1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 29-September 13

Need help making a snake game

Posted 29 September 2013 - 02:35 PM

I am trying to create a snake game using java and Processing, and so far all I can figure out how to do is the movement of one square. I know that I am supposed to create a matrix for the food to appear in, and for the snake to move in, but I do not know how to do that. I also know that I am supposed to have a square joint add to the snake each time the snake collides with the food but I do not know how I would do this either. If someone could please tell me how I should go about this, that would be great. Thanks, here is what I have so far.

Also, a problem that I am having with the movement is that, before you press a key, the square just moves diagonally...does anyone know how I can fix that too? Thanks

 
//Setup
void setup() {
  size(300,300);
  smooth();
  frameRate(30);
}

//Set variables
int dotSize = 10;
int speedX = 3;
int speedY = 3;
int snakeX = width/2;
int snakeY = height/2;


//Functions
void drawFood() {
  noStroke();
  fill(0);
  rect(random(300),random(300),dotSize,dotSize);
}

void moveUp() {
  speedY = -1;
  speedX = 0;
}

void moveDown() {
  speedY = 1;
  speedX = 0;
}

void moveLeft() {
  speedX = -1;
  speedY = 0;
}

void moveRight() {
  speedX = 1;
  speedY = 0;
}


void drawSnake() {
  noStroke();
  fill(0);
  rect(snakeX,snakeY,dotSize,dotSize);
}

//Game draw
void draw() {
  background(255);
  drawSnake();
  snakeX = snakeX + speedX;
  snakeY = snakeY + speedY;
}

void keyPressed() {
  if (keyCode == 37) {
    moveLeft();
  }
  else if (keyCode == 39) {
    moveRight();
  }
  else if (keyCode == 38) {
    moveUp();
  }
  else if (keyCode == 40) {
    moveDown();
  }
  else {
    speedX = 1;
    speedY = 0;
  }
} 


Is This A Good Question/Topic? 0
  • +

Replies To: Need help making a snake game

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10385
  • View blog
  • Posts: 38,434
  • Joined: 27-December 08

Re: Need help making a snake game

Posted 29 September 2013 - 03:00 PM

Processing != Java. This is a game as well, so I'll move this to Game Development.
Was This Post Helpful? 0
  • +
  • -

#3 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 135
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: Need help making a snake game

Posted 29 September 2013 - 08:02 PM

I don't exactly know what Processing is, but maybe try making a boolean for up, left, right, down being pressed. Say for example when the up key is pressed and the down boolean isn't true, you can set the up boolean to true and right and left boolean to false. and in your way for movement. check if the up boolean is true, if it is move the actor -= value on the y axis or whatever.

This post has been edited by schutzzz: 29 September 2013 - 08:08 PM

Was This Post Helpful? 0
  • +
  • -

#4 PostulateMan  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 50
  • Joined: 14-September 08

Re: Need help making a snake game

Posted 08 October 2013 - 11:14 PM

You should take a crack at defining the grid. Thinking logically about what a grid, can you think of a data structure that could be of use?

It's moving diagonally because you have initialized speedX and speedY to 3. Once you fix that, however, I think you'll find that letting go of a key makes the snake move right. I'll let you figure that one out.
Was This Post Helpful? 0
  • +
  • -

#5 Limecore  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 91
  • Joined: 28-November 12

Re: Need help making a snake game

Posted 10 October 2013 - 02:09 AM

If you are finding plain java too hard (like I did) try using a game library. Libgdx is what I use. It makes everything really simple (if you know oop) and has a tutorial that walks you through creating your first game on their wiki
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1