I have a problem with the game I'm currently working on. The point of the game is to hit a ball with a plank so that the ball doesn't drop. There are a lot of bugs with the code but i figured i should fix this one first. Basically, once the ball bounces off the plank i increase the velocity increment. However once the increment gets too high, you can see the ball 'jumping' instead of smoothly moving through the screen. I tried increasing the FPS and decreasing the velocity increment but it would go too slow. Below is my code:
import pygame
pygame.init()
win = pygame.display.set_mode((750, 750))
pygame.display.set_caption('Ball Plank')
clock = pygame.time.Clock()
x = 305
y = 650
width = 150
height = 10
vel = 30
score = 0
lives = 3
radius = 35
ball_x = 377
ball_y = 297
ball_vely = 10
ball_velx = 10
font = pygame.font.SysFont('comicsans', 40, True)
def drawing():
win.fill((0, 0, 0))
text_score = font.render('Score: ' + str(score), 1, (255, 255, 255))
text_lives = font.render('Lives: ' + str(lives), 1, (255, 255, 255))
win.blit(text_score, (570, 20))
win.blit(text_lives, (30, 20))
circle = pygame.draw.circle(win, (0, 255, 0), (int(ball_x), int(ball_y)), radius)
rect = pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
pygame.display.update()
run = True
while run:
clock.tick(40)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel:
x -= vel
if keys[pygame.K_RIGHT] and x < 750 - width - vel:
x += vel
ball_y += ball_vely
ball_x += ball_velx
if ball_y - radius - 10 <= 0:
ball_vely *= -1
elif ball_y + radius + 8 > y and ball_y <= y and ball_x > x - radius and ball_x < x + width + radius:
ball_velx = 10
if ball_vely > 0:
ball_vely += 0.5
else:
ball_vely -= 0.5
ball_vely *= -1
score += 1
if ball_x > x + 75:
ball_velx *= 1 + (ball_x - x + 75)/150
elif ball_x < x + 75:
ball_velx *= -(1 + (ball_x - x + 75)/150)
if ball_x - radius - 10 <= 0 or ball_x + radius + 10 >= 750:
ball_velx *= -1
if ball_y > 760 + radius:
lives -= 1
if lives == 0:
run = False
drawing()
pygame.display.quit()
pygame.quit()
Not sure what exactly is the problem, so sorry for the abundance of the code.