Calculate position in Canvas

I’m developing a copy of the classic Snake game to teach some people the concepts of Xojo.
The game area is Canvas 640480 and the Snake’s dimension are 1616 pixel cubes, so the snake fits exactly 40 times in the play area.

The cookie/coin/food (whatever it is that the snake eats) is also a 16*16 cube. I’m trying to figure out how to place the snake and cookie aligned to each other so that the snake’s path/trajectory fits exactly to the alignment of the cookie.

To place a cookie I use the following code:

[code] 'Constants
Const PLAYFIELD_WIDTH = 640
Const PLAYFIELD_HEIGHT = 480
Const SNAKEUNIT = 16

  'Randomize
  Dim r As New Random
  'ranges for x and y
  Dim FoodX As Integer = r.InRange(0,PLAYFIELD_WIDTH-(SNAKEUNIT/2))
  Dim FoodY As Integer = r.InRange(0, PLAYFIELD_HEIGHT-(SNAKEUNIT/2))
  
  'correct x and y values to fit into the matrix
  FoodX = FoodX-(FoodX mod 16) - (SNAKEUNIT/2)
  FoodY = FoodY-(FoodY mod 16) - (SNAKEUNIT/2)[/code]

So the X and Y are random and I have to correct them.
But cookie and snake do not align to each other. I use the same code a s a starting point for the snake. What is wrong here?

Remove the 16 factor from your coordinates and add the display size if needed…

[code]Const PLAYFIELD_WIDTH = 40
Const PLAYFIELD_HEIGHT = 30
Const UNIT_SCALE = 16
Const PLAYFIELD_DISPLAY_WIDTH = 640
Const PLAYFIELD_DISPLAY_HEIGHT = 480

Dim FoodX As Integer = r.InRange(0, PLAYFIELD_WIDTH - 1)
Dim FoodY As Integer = r.InRange(0, PLAYFIELD_HEIGHT - 1)[/code]

When you draw multiply by 16 to get the display coordinates, otherwise the values are ‘in the plane’, no need to offset or account for scaling.

//draw food g.DrawPicture(foodPic, FoodX * UNIT_SCALE, FOODY * UNIT_SCALE)

Ahh, I see what you mean. So your solution looks to the ratio. Thanks. I will try it this evening.
Get back to you later.

Works like a charm! Thanks!