-1
from random import randint
import pyglet
from pyglet import shapes, clock
from time import sleep

window = pyglet.window.Window(960, 540)

batch = pyglet.graphics.Batch()

...

dot = shapes.Circle(dot_rng_pos_x, dot_rng_pos_y, 5, color=rng_color, batch = batch)

This is just to generate the food circle

def foods(num_food):
    for i in range(num_food):
        food_pos_x = randint(0, window.width-10)
        food_pos_y = randint(0, window.height-10)
        gen_food = shapes.Circle(food_pos_x, food_pos_y, 3, color=(255, 0, 0), batch = None)
    return gen_food

food = foods(50)


Here I don't know what to do to get the result I want:

def spawn_food(dt):
    food.draw()


@window.event
def on_draw():
    
    window.clear()
    batch.draw()


pyglet.clock.schedule_interval(rng_move, 1/120)
pyglet.clock.schedule_interval(spawn_food, 2)
pyglet.app.run()

I can draw "food" in the "on_draw" function, but that draws just one circle

Elphie
  • 1

1 Answers1

0

Is it possible your food is getting garbage collected? I don't see you storing the shapes in any list, dict, or class.

Try making your function generate 1 circle. When returning that, add it to a list. Then you can loop however many circles you want to create.

Something like this:

def get_food():
    food_pos_x = randint(0, window.width-10)
    food_pos_y = randint(0, window.height-10)
    gen_food = shapes.Circle(food_pos_x, food_pos_y, 3, color=(255, 0, 0), batch = batch)
    return gen_food

all_food = [get_food() for i in range(50)]

EDIT: Also your gen_food has batch=None. It needs to be in your batch (batch=batch) for it to even be displayed.

Charlie
  • 680
  • 4
  • 11