0
import sys

pygame.init()
pygame.display.set_caption("Azerty")
icon = pygame.image.load("secret.png")
pygame.display.set_icon(icon)
DISPLAY=pygame.display.set_mode((800,800))

def loose():
  bg_color=(255,204,51)
  DISPLAY.fill(bg_color)
  pygame.display.update()
  while True:
    for event in pygame.event.get():
      if event.type==pygame.QUIT:
        sys.exit()

def main():
  x = 400
  y = 12
  y_tramp = 750
  x_tramp = 350
  y_velocity = 0
  acceleration = 0.1
  bg_color=(255,204,51)
  shape_color=(128,197,199)
  while True:
    for event in pygame.event.get():
      if event.type==pygame.QUIT:
        sys.exit()
      keys = pygame.key.get_pressed()
      if keys[pygame.K_RIGHT]:
        x_tramp +=100
      if keys[pygame.K_LEFT]:
        x_tramp +=-100
    DISPLAY.fill(bg_color)
    ball = pygame.draw.circle(DISPLAY, shape_color, (x,y),15)
    tramp_bar = pygame.draw.rect(DISPLAY, shape_color, (x_tramp,y_tramp,175,20))
    y+=y_velocity
    y_velocity+=acceleration
    pygame.display.update()
    pygame.time.delay(10)
    if y<5:
      y_velocity*=-1
    if y_velocity > 15:
      y_velocity = 10
    if tramp_bar.right > 800:
      x_tramp = 0
    if tramp_bar.left < 0:
      x_tramp = 800
    if ball.colliderect(tramp_bar):
      y_velocity*=-1
  
main()

Hi i have a problem, with my 2 last lines. When i launch my code, the ball falls into the bar and keep stuck, i have to move so the ball rebound. Can anyone help me fix that ? It would be great because this bug is annoying and with it i can

qwatz
  • 13
  • 3
  • `if ball.colliderect(tramp_bar): y_velocity*=-1` Doesn't this invert the y_velocity every frame? So one frame it has negative 10 velocity, next frame it has positive 10 velocity, with the effect that it just moves back and forth forever. – Nick ODell Oct 22 '22 at 18:07
  • Huh i dont really know.. i just put it there so its invert the direction of the ball so it go the other side but idk if its the good way – qwatz Oct 22 '22 at 18:48

0 Answers0