[Python] How to put random numbers in Pygame

Discussão em 'Python' iniciado por Stack, Março 25, 2025.

  1. Stack

    Stack Membro Participativo

    import sys, pygame
    import random
    pygame.init()

    x=0.5
    speed = [x, x]
    size = width, height = 800, 600
    black = 0, 0, 0

    screen = pygame.display.set_mode(size)

    ball = pygame.image.load("intro_ball.gif")
    ballrect = ball.get_rect()

    Sair = True

    while Sair:

    for event in pygame.event.get():
    if event.type == pygame.QUIT: Sair=False
    ballrect = ballrect.move(speed)
    if ballrect.left < 0 or ballrect.right > width:
    speed[0] = -speed[0]
    x=random.uniform(0, 1)
    if ballrect.top < 0 or ballrect.bottom > height:
    speed[1] = -speed[1]
    x=random.uniform(0, 1)

    screen.fill(black)
    screen.blit(ball, ballrect)
    pygame.display.flip()

    sys.exit()


    I expected the ball to move and every time it hits the corner change velocity.

    Continue reading...

Compartilhe esta Página