Phil Choi
Phil Choi

Reputation: 67

The obstacle does not keep spawning. Only one spawns initially then the loop is cut off?

The code is not generating the object at the timer I set it at.

pygame.time.set_timer(pygame.USEREVENT+2, random.randrange(10, 500))
obstacles = obstacles(1050,300,64,64)

while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.USEREVENT+2:
            objects.append(obstacles)

            for object1 in objects:
                object1.x -= 1
                if object1.x < -100:
                    objects.pop(objects.index(object1))

No error message, however only one obstacle appears

Upvotes: 1

Views: 56

Answers (1)

Rabbid76
Rabbid76

Reputation: 211166

What you do is to .append the same object again and again.

objects.append(obstacles)

Delete the line:

obstacles = obstacles(1050,300,64,64)

This doesn't do what you expect. obstacles is a class, but after that obstacles is an object of the former class obstacles.

When an new obstacle should spawn,then you've to .append a new obstacles object:

if event.type == pygame.USEREVENT+2:
    new_obstacle = obstacles(1050,300,64,64)
    objects.append(new_obstacle )

Note, in the snippet the object (new_obstacle) has a different name then the class (obstacles) and a new object is constructed each time.

Upvotes: 1

Related Questions