Python Forum

Full Version: inconsitencies in my first code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i am trying to make a pong game i have started from scratch twice now and i am noticing that the "ball" only bounces if i hover my mouse near the point where it should bounce.

import sys
import pygame
import os

pygame.init()

win = pygame.display.set_mode((600,500))

pygame.display.set_caption("pong?")


win.fill((0, 0, 0))
pygame.display.flip()

x1 = 305
y1 = 245
xvel = .05
yvel = .05


while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        if y1 >= 490:
            yvel *= -1
            
        
    pygame.draw.rect(win, (255, 255, 255), (x1, y1, 15, 15))
    pygame.display.update()

    x1 += xvel

    y1 += yvel

    win.fill((0, 0, 0))
You need to dedent this section back into the while loop instead of the event loop
    if y1 >= 490:
        yvel *= -1
to like this
while True:
 
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
 
    if y1 >= 490:
        yvel *= -1