# Creating Snake Game

**URL:** <https://discourse.processing.org/t/creating-snake-game/14954>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [October 26, 2019, 1:48pm UTC](https://discourse.processing.org/t/creating-snake-game/14954 "2019-10-26T13:48:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ephilorex](https://avatars.discourse-cdn.com/v4/letter/e/7ba0ec/32.png) [@Ephilorex](https://discourse.processing.org/u/Ephilorex)\
**Post date:** [October 26, 2019, 1:48pm UTC](https://discourse.processing.org/t/creating-snake-game/14954/1 "2019-10-26T13:48:25Z")

</div>

Hi everyone!

I am a beginner in Processing and for my assignment I would like to create a snake game. Here are my steps:

- Draw background
- Create apple system; spawn one in on the grid when another is eaten
- Death system; check if snake head has hit sides or itself
- Controls
- Restart
- Growing; if snake eats apple, grow by one unit
- Make the snake

I have already drawn the background. I would like help with the apple system; how can I get the apple to spawn on one of the squares in my grid? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [October 26, 2019, 2:05pm UTC](https://discourse.processing.org/t/creating-snake-game/14954/2 "2019-10-26T14:05:13Z")

</div>

-step1-  
you could load a image of a apple `appleimg`  
resize it so it fits into the grid ( and not need to be resized at every draw again )

-step2-  
need a array or arrayList where you store  
on what ?random? position of the grid you have “spawned” one.

minimal/best a boolean array apple[many]  
as the position of each you know already from the rectangle grid math  
so no need to store x,y of each.

* * *

show us your combined code with the background…  
as the apple as

```auto
if ( apple[i] ) image(appleimg,x,y);

```

fits best into the draw grid loop.

---

<div class="post-metadata">

**Author:** ![Ephilorex](https://avatars.discourse-cdn.com/v4/letter/e/7ba0ec/32.png) [@Ephilorex](https://discourse.processing.org/u/Ephilorex)\
**Post date:** [October 26, 2019, 2:20pm UTC](https://discourse.processing.org/t/creating-snake-game/14954/3 "2019-10-26T14:20:37Z")

</div>

I plan on the apple just being a 27x27 red square that spawns on a square in a grid. I haven’t learnt arrays yet, is there a way I could use the `random()` function and do something with that? I don’t exactly know what I’m doing. Sorry.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [October 26, 2019, 2:30pm UTC](https://discourse.processing.org/t/creating-snake-game/14954/4 "2019-10-26T14:30:51Z")

</div>

-a- actually a

```auto
  appleimg.resize(29,29);

```

in setup and a

```auto
 if ( apple[i] ) image(appleimg,xi+1,yi+1);

```

looks best here.

* * *

-b- yes you need a random set of a apple position?

```auto
apple[(int)random(many)] = true;

```

 ![applegrid](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/be5e9ac6074aae56a18735909097870d2649c7d9.png)
