Dec-12-2018, 02:33 PM
I'm writing a simple pong game with OOP
and i have this issue when i move the left and right paddle, they don't move in a continuous way, i have to press t he up down key every time to move them.
Here is the code of main loop:
Source code: https://pastebin.com/JdCrXSmJ
and i have this issue when i move the left and right paddle, they don't move in a continuous way, i have to press t he up down key every time to move them.Here is the code of main loop:
class MainLoop:
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
if pad_left.y >= 2:
pad_left.y -= 10
elif event.key == pygame.K_s:
if pad_left.y <= display_height - pad_left.height:
pad_left.y += 10
elif event.key == pygame.K_UP:
if pad_right.y >= 2:
pad_right.y -= 10
elif event.key == pygame.K_DOWN:
if pad_right.y <= display_height - pad_right.height:
pad_right.y += 10
if ball.x <= 0 or ball.x >= display_width:
ball.x = display_middle[0]
ball.y = display_middle[1]
ball.speed_x = 5
ball.speed_y = 5
if ball.x <= 0:
pad_right.score += 1
ball.speed_x = abs(ball.speed_x)
elif ball.x >= display_width:
pad_left.score += 1
ball.speed_x = -ball.speed_x
# pad colission
if ball.x <= pad_left.width + ball.radius and ball.y >= pad_left.y+10 and \
ball.y <= pad_left.y+pad_left.height+10:
if ball.y >= pad_left.y + 20 and ball.y <= pad_left.y + 60:
ball.speed_x -= 1
ball.speed_y -= 2
else:
ball.speed_x = -5
ball.speed_y = 5
ball.speed_x = -ball.speed_x
if ball.x + ball.radius >= pad_right.x and ball.y >= pad_right.y+10 and \
ball.y <= pad_right.y+pad_left.height+10:
if ball.y >= pad_right.y + 20 and ball.y <= pad_right.y + 60:
ball.speed_x += 1
ball.speed_y += 2
else:
ball.speed_x = 5
ball.speed_y = -5
ball.speed_x = -ball.speed_x
display.fill(background) # <~~
drawText(pad_left.score, white, 70, (display_middle[0]-95,0))
drawText(pad_right.score, white, 70, (display_middle[0]+60,0))
pad_left.drawPad()
pad_right.drawPad()
#pygame.draw.line(display, white, (display_middle[0],0), (display_middle[0], display_height), 2)
[ pygame.draw.rect(display, white, (display_middle[0], y, 6, 6)) for y in range(6, display_height, 15)]
ball.drawBall()
# GAME OVER SCREEN
if pad_left.score == 1 or pad_right.score == 1:
display.fill(background)
drawText('GAME OVER', white, 70, ((200, (display_middle[1]//2))))
if pad_left.score == 1:
drawText('Player 1 won', white, 40, (display_middle[0]-145, display_middle[1]))
if pad_right.score == 1:
drawText('Player 2 won', white, 40, (display_middle[0]-145, display_middle[1]))
pad_left.score = 0
pad_right.score = 0
pygame.display.update()
pygame.time.wait(1500)
if pygame.key.get_pressed()[pygame.K_RETURN]:
pass
else:
pygame.display.update()
clock.tick(30)
if __name__ == '__main__':
MainLoop()I'm learning OOP so if it's something I can do to improve on this code I'll be grateful. thank you.
Source code: https://pastebin.com/JdCrXSmJ

Thanks