Python Forum
inconsitencies in my first code - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: inconsitencies in my first code (/thread-20192.html)



inconsitencies in my first code - wildbill - Jul-31-2019

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))



RE: inconsitencies in my first code - metulburr - Jul-31-2019

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