0

I'm trying to get my pong game to run the loop at a certain fps, but I've tried a couple things and it hasn't worked and I wasn't taught how to use pygame speed/clock so I gotta figure it out on my own

I'm trying to make the loop run at a certain speed to make it look smoother, because if I edit the dx or the position it goes to when you change it, it looks chunky, so instead of a paddle 10 x down (looks chunky) i want to move it 1 x down a bunch of times so it moves down just as fast as 10 x but smoother

Full Code

import pygame #how to fix paddle widths from hitting ball
import sys
pygame.init()
screenSize = (800,600)
screen = pygame.display.set_mode((screenSize),0)
pygame.display.set_caption("HajarPongBasicCollision")

# colours
WHITE = (255,255,255)
BLACK = (0, 0, 0)
GREEN = (0,255,0)
RED = (255,0,0)
BLUE = (0,0,255)
PURPLE = (154, 136, 180)

screen.fill(BLACK)
pygame.display.update()
# retrieve screen measurements
screenw = screen.get_width()
screenh = screen.get_height()

# retrieve position of center of screen
centerx= 400 #tried not to use hard coded values but program gives me an error when i use screenw/2 ASK MR H TO HELP WITH NO HARD CODED VALUES (HCV)
centery= 300

# variables for first paddle
p1x = 10
p1y = 10
p1w = 10
p1h = 100

p1dy = 0
p1_score = 0

# variables for second paddle
p2w = 10
p2h = 100
p2x = screenw - 20
p2y = 10

p2dy = 0
p2_score = 0

# variable for ball
bx = 400 #HCV
by = 300 #HCV
br = 9
bdx = 1
bdy = 1

# speed of loop
fpsClock = pygame.time.Clock()
FPS = 60

go = True
while go:
    fpsClock.tick(FPS)
    for event in pygame.event.get():
        if event.type ==pygame.QUIT:
            go = False
        elif event.type == pygame.KEYDOWN:
            # control for the first paddle
            if event.key == pygame.K_w:
                    p1dy = -1
            elif event.key == pygame.K_s:
                    p1dy = 1
            # controls for the second paddle
            elif event.key == pygame.K_UP:
                    p2dy = -1
            elif event.key == pygame.K_DOWN:
                    p2dy = 1
            elif event.key == pygame.K_q:
                    go = False
        # stops rectangles from going continously
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_w or event.key == pygame.K_s:
                p1dy = 0
            if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                p2dy = 0

    # stops paddle one from going off the screen
    if (p1y < 0):
        p1dy = 0
        p1y = 0
    if (p1y + p1h > screenh):
        p1dy = 0
        p1y = screenh - p1h

    # stops paddle two from going off the screen
    if (p2y < 0):
        p2dy = 0
        p2y = 0
    if (p2y + p2h > screenh):
        p2dy = 0
        p2y = screenh - p2h

    # stops ball from going off the screen
    if (bx + br >= screenw):
        bx = centerx
        by = centery
    elif (bx <= br):
        bx = centerx
        by = centery

    if (by + br >= screenh):
        bdy = -bdy
    elif (by <= br):
        bdy = -bdy

    # detects if ball hit paddles
    if bx - br <= p1x + p1w and by >= p1y and by <= p1y + p1h:
        bdx = -bdx

    if bx + br >= p2x and by >= p2y and by <= p2y + p2h:
        bdx = -bdx

    # moves the rectangles
    p1y = p1y + p1dy
    p2y = p2y + p2dy

    # moves the ball
    bx = bx + bdx
    by = by + bdy

    # removes screen trail
    screen.fill(BLACK)

    # draws the rectangles
    pygame.draw.rect(screen, WHITE,(p1x, p1y, p1w, p1h))
    pygame.draw.rect(screen, WHITE,(p2x, p2y, p2w, p2h))
    pygame.draw.circle(screen, WHITE, (bx, by), br, 0)
    pygame.display.update()

pygame.quit()
sys.exit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
IJH
  • 107
  • 1
  • 7

1 Answers1

0

Uncouple the speed of your objects from the fps.
That way you don't need to change your fps if you want objects to move faster, you just increase the speed of the object. and if a computer has performance issues and can't keep a reliable fps. Your game still runs at the same speed, it just shows less frames.

import time
...
# speed of objects
ball_speed = 50
paddle_speed = 200
...
last_update = time.time()
while True:
    ...
    dt = time.time() - last_update
    ...
    # moves the ball
    bx += bdx * ball_speed * dt
    ...
    pygame.draw.circle(screen, WHITE, (int(bx), int(by)), br, 0)
    ...
    last_update = time.time()
steviestickman
  • 1,132
  • 6
  • 17