I'm coding pong in python for the first time and I'm trying to make the ball bounce when it hits a paddle. Right now, the ball stops when it hits the paddle and then will only bounce off once the paddle is moved. I want the ball to bounce as soon as it hits the paddle. Any idea on how to fix this? Any help is appreciated. Thanks!
import pygame, random, sys
from pygame.locals import *
#set constants
WINDOWWIDTH = 600
WINDOWHEIGHT = 600
TEXTCOLOR = (255, 255, 255)
BACKGROUNDCOLOR = (0, 0, 255)
FPS = 40
BLACK = (0,0,0)
RED = (255, 0, 0)
WHITE = (255, 255, 255)
#set variables
rectY1 = 240
rectY2 = 240
Y1change = 0
Y2change = 0
ballX = 320
ballY = 240
ballXvel = 0
ballYvel = 0
paddle1 = pygame.Rect(18,rectY1,10,120)
paddle2 = pygame.Rect(620,rectY2,10,120)
ball = pygame.Rect(ballX,ballY,30,30)
#create shapes
def drawshapes():
pygame.draw.rect(DISPLAY, WHITE, paddle1)
pygame.draw.rect(DISPLAY, WHITE, paddle2)
pygame.draw.rect(DISPLAY, WHITE, ball)
#set up display
pygame.init
DISPLAY = pygame.display.set_mode((640,480),0,32)
pygame.display.set_caption('Pong')
fpsClock = pygame.time.Clock()
#main loop
while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN :
if event.key == K_SPACE :
ballXvel += 10
ballYvel += 10
if ball.colliderect(paddle1) :
ballX = paddle1.x
ballXvel = ballXvel * -1
if ball.colliderect(paddle2) :
ballXvel = ballXvel * -1
if ballX > 620 :
ballXvel = ballXvel * -1
## if ballX < 20 :
## ballXvel = ballXvel * -1
if ballY > 460 :
ballYvel = ballYvel * -1
if ballY < 20 :
ballYvel = ballYvel * -1
keys = pygame.key.get_pressed()
Y1change = 0
Y2change = 0
#make paddles move
if keys[K_UP]:
Y2change -= 10
if keys[K_DOWN]:
Y2change += 10
if keys[ord('w')]:
Y1change -= 10
if keys[ord('s')]:
Y1change += 10
#stop paddles at edges
if rectY1 < 5 :
rectY1 = 5
if rectY1 > 355 :
rectY1 = 355
if rectY2 < 5 :
rectY2 = 5
if rectY2 > 355 :
rectY2 = 355
ball.y = ballY
ball.x = ballX
ballX += ballXvel
ballY += ballYvel
paddle1.y = rectY1
paddle2.y = rectY2
rectY1 += Y1change
rectY2 += Y2change
DISPLAY.fill(BLACK)
drawshapes()
pygame.display.update()
fpsClock.tick(FPS)