#๐Ÿ”’ guys the player in my code is continously falling i dont why u can also try this code

20 messages ยท Page 1 of 1 (latest)

cedar knotBOT
#

Hey @warm lark!

Please edit your message to use a code block

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
#

@warm lark

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

thorn warren
#

Hi @warm lark. I can't help you a lot because I never used PyGame before but can you tell us what is your problem.

#
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
                pygame.quit()
                exit()
        if event.type==pygame.MOUSEBUTTONDOWN:
             if character_rect.collidepoint(event.pos):
                                        character_gravity=-20

        if event.type==pygame.KEYDOWN:
            if event.key==pygame.K_SPACE:
                    character_gravity=-20

    screen.blit(surface,(0,0))
    pygame.draw.rect(screen,'#c0e8ec',text_rect)
    pygame.draw.rect(screen,'#c0e8ec',text_rect,20)
    screen.blit(text,text_rect)
    screen.blit(snail,snail_rect)
    screen.blit(character,character_rect)
    mouse_pos=pygame.mouse.get_pos()
    if not snail_rect.left in range(0,800+1):
                            snail_rect.left=800


    if character_rect.bottom>=480:
            character_rect.bottom=480

    character_rect.left+=1
    snail_rect.right-=5
    character_gravity+=1
    character_rect.y+=character_gravity
    pygame.display.update()
    clock.tick(60)
#

Types your code like this please

#

no

tiny summit
#

We would not be able to try this code directly, there are pieces missing.

cedar knotBOT
#

Hey @warm lark!

Please edit your message to use a code block

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
tiny summit
#

I suspect that setting the character_gravity to -20, and then only increasing it by 1 before then updating the display and calling clock.tick(60) (is that a wait call?), then the y coord of the player rectangle would be constantly going down by 19 then 18 then so on?

#

Also, where is the origin point for (0,0), top left or bottom left?

#

I don't have the assets you have either, sorry.
Can you walk through how the gravity is supposed to affect the player position?
(and again, where (0,0) is on the rectangle would have an effect)

#

You don't need to keep deleting your messages, you can edit them, instead.

tiny summit
#

So, I did get it working (had to download some little image assets to not have to change too much ๐Ÿ™‚ ).
The order of operations seems to be important here:

if character_rect.bottom >= 480:
    character_rect.bottom = 480

character_gravity += 1
character_rect.y += character_gravity

Now, you don't end up resetting the character_gravity to 0 anywhere, so that value keeps increasing.
The bottom of the rect is set at 480, and then you're changing the y value, which can push the rectangle down further.

So what's happening here is the bottom is set to 480, but then we add say a gravity value of 1 to y, which can push that bottom down to 481.
As it cycles around again, again the bottom is set to 480, but now the gravity is 2, so the bottom becomes 482.
As that keeps cycling around, it makes the player continously "fall".

#

To fix it, just rearrange the operations:

character_gravity += 1
character_rect.y += character_gravity
if character_rect.bottom >= 480:
    character_rect.bottom = 480

Now, regardless of the gravity value, the last change in the position is to set the bottom to 480.

#

Another way would be to use the min value of either the calculated value or the new one:

character_gravity += 1
character_rect.bottom = min(480, character_rect.bottom + character_gravity)
#

@warm lark ๐Ÿ‘†

#

Although, it would be a good idea to reset that character gravity as well, so you aren't dealing with ever-increasing numbers while the game might sit idle.

#
character_gravity += 1
character_rect.bottom += character_gravity
if character_rect.bottom >= 480:
    character_gravity = 0
    character_rect.bottom = 480
cedar knotBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.