Creating Snake Game

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.

1 Like

-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

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

fits best into the draw grid loop.

1 Like

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.

-a- actually a

  appleimg.resize(29,29);

in setup and a

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

looks best here.


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

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

1 Like