I'm failly new to programming in general and this is my second game ever so sorry in advance. I made a pong game with only one paddle for now, but the ball keeps bouncing even when it's not hitting the paddle. This is my draw ball function:
def draw_ball(self):
self.sc.blit(ball, (self.bx,self.by))
self.bx += self.speedx
self.by += self.speedy
if self.bx >= 1000:#check goals
self.bx = 250
self.by = 340
if self.bx <= 38: #check coalision
self.speedx *= -1
if self.by<= 12:
self.speedy *= -1
if self.by>= 725:
self.speedy *= -1
#check coalision with the paddle
#px and py are the coordinates of the paddle
if self.bx > (self.px - 35) and (self.by < (self.py + 196) and (self.by + 38) > self.py) :
self.speedx *= -1
Here is the main loop:
#global variables
WIN_HEIGHT = 1024
WIN_WIDTH = 768
SPEED = 5
PX = 956
PY = 320
MOVEMENT = 5
BALL = Ball(550, 60, screen, .5, .4, PX, PY)
running = True
while running:
redraw_screen()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
mouse_pos = pygame.mouse.get_pos()
if event.type == pygame.MOUSEBUTTONDOWN and START_BUTTON.is_clicked(mouse_pos):
running = False
playing = True
while playing:
screen.fill((0,0,0))
BALL.draw_ball()
PLAYER.draw_player()
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
playing = False
Thanks for your help.