From 349dfc55de82cd92154e67db4d81292f648d1b4a Mon Sep 17 00:00:00 2001 From: moe-assal Date: Fri, 22 May 2020 17:30:38 +0300 Subject: [PATCH] fix physics-graphics syncronization the `drawrect` function draws the paddle at x=0 if x<0; this leads to the paddle getting out of the window physically, whereas the graphical window shows it inside the window. so that if you then press the right arrow, you get a delay in paddle movement ( you wait till x > 0). If we don't allow the physical paddle to get out of the window, problem solved! --- pong.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pong.py b/pong.py index b60b86f..d4ac291 100644 --- a/pong.py +++ b/pong.py @@ -33,11 +33,7 @@ #draws the paddle. Also restricts its movement between the edges #of the window. -def drawrect(screen,x,y): - if x <= 0: - x = 0 - if x >= 699: - x = 699 +def drawrect(screen,x,y): pygame.draw.rect(screen,RED,[x,y,100,20]) @@ -81,7 +77,11 @@ def drawrect(screen,x,y): screen.fill(BLACK) rect_x += rect_change_x rect_y += rect_change_y - + if rect_x <= 0: + rect_x = 0 + if rect_x >= 699: + rect_x = 699 + ball_x += ball_change_x ball_y += ball_change_y @@ -115,4 +115,4 @@ def drawrect(screen,x,y): pygame.display.flip() clock.tick(60) -pygame.quit() \ No newline at end of file +pygame.quit()