# Need help making a snake game

Page 1 of 1

## 4 Replies - 1599 Views - Last Post: 10 October 2013 - 02:09 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=330313&amp;s=be89f7936c69c865ed37bb2d20fce122&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 mfoster1

• New D.I.C Head

Reputation: 0
• 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

• Games, Graphs, and Auctions

Reputation: 11315
• Posts: 42,622
• 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

• D.I.C Regular

Reputation: 141
• Posts: 342
• 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

• D.I.C Head

Reputation: 2
• 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

• D.I.C Head

Reputation: 18
• Posts: 92
• 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

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }