Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do I insert set caption?

Tags:

python

pygame

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()
like image 993
ZussM4n Avatar asked Dec 12 '25 12:12

ZussM4n


1 Answers

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()
like image 137
CopyrightC Avatar answered Dec 15 '25 06:12

CopyrightC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!