Ask coding questions

← Back to all posts
random.randint starts out with the same number every time
mordyman (3)

I am making a snake game with pyglet and am using this code to place the food

def place_food():
    global fd_x, fd_y
    fd_x = random.randint(0, (win.width//cell_size)-1)*cell_size
    fd_y = random.randint(0, (win.height//cell_size)-1)*cell_size

except it always starts off at 0,0

Comments
hotnewtop
Coder100 (17018)

Make sure (win.width//cell_size)-1 isn't 0 lol

mordyman (3)

@Coder100 it isn't lol i figured it out

Coder100 (17018)

cool what was the problem? @mordyman

mordyman (3)

i initially set fd_x and fd_y to 0 and 0 and then started the game without calling place_food() so it always started at (0,0) @Coder100

mordyman (3)

I figured it out, I was setting fd_x, and fd_y to 0 before I started. I changed it to my randomizing code and it works now