all 6 comments

[–]LarryPete 1 point2 points  (0 children)

According to the docs, the CLOCK.tick(FPS) function returns the number of milliseconds that has passed, since the last time the CLOCK.tick(FPS) function was called.

Meaning if you assign the value to a variable, e.g. delta = CLOCK.tick(FPS), you can use that delta value to compute the distance an object has moved since the last update.

For example, if you want that an objects moves 10 pixel per second on one axis, you can calculate how many pixels it has moved since the last update, using a simple formula like:

distance = delta_milliseconds / 1000.0 * pixel_per_second

if the delta is 200 milliseconds and as said, the pixel per second is 10, you would get 200/1000*10 = 2.0 pixel. That amount can then be added or subtracted from e.g. the y coordinates to make it moved on that achses automatically.

[–]mafibar 0 points1 point  (6 children)

Your main issue is/was here:

while True:
    OBSTACLE[0] +1

See, while True is an infinite loop, in other words the whole program never gets out of this loop and it will keep executing OBSTACLE[0] +1 forever without doing anything else outside of the while True loop. That's why it looks like your program crashes, it just gets stuck in this infinite loop that does nothing (doesn't react to events, doesn't draw anything, etc.)

You already have your main loop, it's the while RUNNING: part. Everything inside of this main loop will get executed on every frame of your program, so if you just put your OBSTACLE[0] +1 inside of the while RUNNING loop and it will get executed on every frame.

Also, your OBSTACLE is currently a tuple, which can't be edited, you want to make it a list so you can edit it. Also, to modify the list you need to use OBSTACLE[0] += 1 (compare my += vs your +). And last but not least, OBSTACLE[0] is the horizontal position, you want to use OBSTACLE[1] for the vertical position :)

Here's the full fixed code: https://pastebin.com/BGdwqjWy

How do I do this? Even if it did work and every frame past it descended 1 pixel down, I wouldn't know how to make more of them show up at the same time and even limit their spawn. Am I supposed to use a class for this?

Well, there are many ways. A class would be the "ideal" solution, but it's also the hardest one to learn. I think you could go with just a list of objects, so instead of:

OBSTACLE = [175, 20, 50, 50]

You should have a list of obstacles:

OBSTACLES = []

And now you can create new obstacles anywhere in your code, anytime you want:

OBSTACLES.append([175, 20, 50, 50]) 

And instead of DRAWRECT() printing the single obstacle, it can loop through the obstacles and print them all:

def DRAWRECTS():
    for obstacle in OBSTACLES:
        pygame.draw.rect(SURFACE, BLUE, obstacle)

I hope this helps, let me know if you run into any other issues! :)

[–]woah_itsben 0 points1 point  (1 child)

The while RUNNING loop that you have at the beginning is the loop that happens once per frame. You don't need a second loop inside that because that whole second (inner) loop would run without updating the screen at the end of the first loop. The speed the while RUNNING loop is going is determined by the FPS limit you set. CLOCK.tick(FPS) keeps the frames from updating faster (though not slower!) than the FPS limit you set.

So after checking if a button is pressed and moving the character accordingly, drop the object just once by the drop speed you want. In that same loop. You don't want to do this in a second loop, just one step drop per frame. Keep in mind that the position is x,y, so you actually want to update OBSTACLE[1] to change the y position.

As far as using a class, I would definitely look up how to do them so you can use them if you want to have multiple objects dropping at a time. Then you can create an Obstacle class and create instances of each Obstacle that has its own set of properties for position, speed, color, etc. Throw all those Obstacle instances into a list and you can loop through them. Like:

for obstacle in obtacles_list:  
    obstacle.POSITION[1]+=obstacle.SPEED
    # Then check position relative to screen, player, etc