3

So, I've been doing a python program for movement and gravity, and everything works OK, but i doesn't set the caption and i really dont know why. I tried to insert it in the end, but doesn't work neither. Is it that i need to write it on another place, or other type of problem?

import pygame, sys

WIDTH, HEIGHT = 500, 350

pygame.init()
pygame.display.set_caption('gravity movement')
window = pygame.display.set_mode((WIDTH, HEIGHT), 32, 0)
clock = pygame.time.Clock()

player = pygame.Rect(30, 30, 32, 32)
player_speed = 5


#movement
def move(rect, x, y):
    rect.x += x
    rect.y += y

#Add gravity
def gravity(rect, g_force= 6):
    rect.y += g_force
    if rect.y + rect.h >= HEIGHT:
        rect.y = HEIGHT - rect.h


x, y = 0,0

while True:
    clock.tick(60)#means that for every second (at most) 60 frames should pass

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                x = -player_speed
            if event.key == pygame.K_RIGHT:
                x = player_speed
            if event.key == pygame.K_UP:
                y = -20
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                x = 0
            if event.key == pygame.K_RIGHT:
                x = 0
            if event.key == pygame.K_UP:
                y = 0
        
        
    #Draw
    window.fill((0, 0, 20))
    pygame.draw.rect(window, (255, 24, 10), player)

    #definitive movement
    move(player, x= x, y=y)
    gravity(player)
    
   
    pygame.display.flip()
ZussM4n
  • 35
  • 1
  • 5
  • You need to set the caption after you initialized you window, as said [here](https://stackoverflow.com/questions/40566585/how-to-change-the-name-of-a-pygame-window): "However, PyGame is based on SDL and the behavior is system-dependent. Call `set_caption()` after initializing the display window with `pygame.display.set_mode()`" – NoeXWolf Apr 11 '21 at 12:49

1 Answers1

3

The problem you're facing is because of setting flags to 32 over here:

window = pygame.display.set_mode((WIDTH, HEIGHT), 32, 0) #Flags = 32 , depth = 0

Just change flags to 0 and it will be alright

So it should be

window = pygame.display.set_mode((WIDTH, HEIGHT))

You can read more about flags and depth here

and so the final code should look something like this :

import pygame, sys

WIDTH, HEIGHT = 500, 350

pygame.init()
pygame.display.set_caption('gravity movement')
window = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()

player = pygame.Rect(30, 30, 32, 32)
player_speed = 5


#movement
def move(rect, x, y):
    rect.x += x
    rect.y += y

#Add gravity
def gravity(rect, g_force= 6):
    rect.y += g_force
    if rect.y + rect.h >= HEIGHT:
        rect.y = HEIGHT - rect.h


x, y = 0,0

while True:
    clock.tick(60)#means that for every second (at most) 60 frames should pass

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                x = -player_speed
            if event.key == pygame.K_RIGHT:
                x = player_speed
            if event.key == pygame.K_UP:
                y = -20
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                x = 0
            if event.key == pygame.K_RIGHT:
                x = 0
            if event.key == pygame.K_UP:
                y = 0
        
        
    #Draw
    window.fill((0, 0, 20))
    pygame.draw.rect(window, (255, 24, 10), player)

    #definitive movement
    move(player, x= x, y=y)
    gravity(player)
    
   
    pygame.display.flip()
CopyrightC
  • 857
  • 2
  • 7
  • 15