#๐Ÿ”’ the player in my code is continously falling can any1 help?

20 messages ยท Page 1 of 1 (latest)

worldly lindenBOT
#

@vocal anvil

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.

indigo garnet
#

no idea. I added a couple lines, and if the character is above the floor it drops to it and stops.

#
import pygame
from sys import exit

surface_image='books.jpg'
snail_image='moon.jpg'
character_image='cat_ninja.png'

pygame.init()
pygame.display.set_caption('Runner')
screen = pygame.display.set_mode((800, 600))  
surface = pygame.image.load(surface_image)
font=pygame.font.SysFont('Arial',55)
text=font.render("Mygame",False,(25,255,111))
text_rect=text.get_rect(center=(400,100))

snail=pygame.image.load(snail_image)
snail_rect=snail.get_rect(midbottom=(800,480))
snail_rect.left=800

character=pygame.image.load(character_image)
character_rect=character.get_rect(midbottom=(100,480))
character_gravity=0

clock=pygame.time.Clock()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()       
            exit()
        if event.type==pygame.MOUSEBUTTONDOWN:
            print("mousedown")
            if character_rect.collidepoint(event.pos):
                print('click')
                character_gravity=-20

        if event.type==pygame.KEYDOWN:
            print("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 snail_rect.left not in range(0,800+1):
       snail_rect.left=800
    snail_rect.right-=1          
        
    print(character_rect.bottom)
    if character_rect.bottom >= 580:
        character_rect.bottom = 580
        character_gravity = 0  
    character_gravity+=1
    character_rect.y+=character_gravity
    if character_rect.colliderect(snail_rect):
        print('Game Over')
        pygame.quit()
        exit()

    pygame.display.update()
    clock.tick(60)
#

I had to change it to 580 instead of 480 because my images bottom was already at 481

#

so I never saw it move

#

but now it doprs, stops and when the snail reaches it the game ends

#

the character drops and then stops when gravity is zero and the snail moves to the left until it reaches the character

#

ah I see.

#

because once you hit the bottom, you add +1 and move it.

#

you want the floor check after that.

#

yes now it works

#
import pygame
from sys import exit

surface_image='books.jpg'
snail_image='moon.jpg'
character_image='cat_ninja.png'

pygame.init()
pygame.display.set_caption('Runner')
screen = pygame.display.set_mode((800, 600))  
surface = pygame.image.load(surface_image)
font=pygame.font.SysFont('Arial',55)
text=font.render("Mygame",False,(25,255,111))
text_rect=text.get_rect(center=(400,100))

snail=pygame.image.load(snail_image)
snail_rect=snail.get_rect(midbottom=(800,480))
snail_rect.left=800

character=pygame.image.load(character_image)
character_rect=character.get_rect(midbottom=(100,480))
character_gravity=0

clock=pygame.time.Clock()
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 snail_rect.left not in range(0,800+1):
       snail_rect.left=800
    snail_rect.right-=1          
        
    if character_rect.colliderect(snail_rect):
        print('Game Over')
        pygame.quit()
        exit()

    character_gravity+=1
    character_rect.y+=character_gravity

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

    pygame.display.update()
    clock.tick(60)
#

you'll need to update the image names.

#

but the important part is that the character reset happens after you move it.

#

also, I just added a snail_speed value and made the snail go a little faster every time you jump.

#

fun

#

decades. lol.

#

I use mostly python now, but did a lot more in c/c#/c++, perl, and javascript

worldly lindenBOT
#
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.