0

Changing the ball_y_change or ball_x_change values to anything under 0 will make the ball stuck and not move. I've tried changing the values of the variables from 0 - 0.9 and none of them have worked. Can anyone provide a solution to this or help me make the ball slower so that you can actually see where the ball is going?

import pygame
import sys

def ball_animation(ball_x_change, ball_y_change):
    pass

def collision(player1, player2, ball):
    collide1 = pygame.Rect.colliderect(player1, ball)
    collide2 = pygame.Rect.colliderect(player2, ball)
    if collide1:
        print("hi")
    if collide2:
        print("hello")



pygame.init()
width = 1000
height = 600
screen = pygame.display.set_mode((width, height))

# Rectangles
player1 = pygame.Rect(20, 200, 15, 140)
player1_y_change = 0

player2 = pygame.Rect(965, 200, 15, 140)
player2_y_change = 0

ball = pygame.Rect(width/2 - 15, height/2 - 15, 30, 30)
ball_x_change = 0.6
ball_y_change = 0.6

while True:
    screen.fill((0, 0, 0))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w:
                player1_y_change = -1
            if event.key == pygame.K_s:
                player1_y_change = 1
            if event.key == pygame.K_i:
                player2_y_change = -1
            if event.key == pygame.K_k:
                player2_y_change = 1
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_w or event.key == pygame.K_s:
                player1_y_change = 0
            if event.key == pygame.K_i or event.key == pygame.K_k:
                player2_y_change = 0

    # Player 1 Boundaries
    player1.y += player1_y_change
    if player1.y <= 10:
        player1.y = 10
    elif player1.y >= 450:
        player1.y = 450
    
    # Player 2 Boundaries
    player2.y += player2_y_change
    if player2.y <= 10:
        player2.y = 10
    elif player2.y >= 450:
        player2.y = 450
    
    # Ball Boundaries
    ball.x += ball_x_change
    ball.y += ball_y_change
    if ball.y <= 0:
        ball_y_change *= -1
    elif ball.y >= 570:
        ball_y_change *= -1
    elif ball.x < 0 or ball.x > 970:
        ball.x = width/2 - 15
        ball.y = height/2 - 15


    pygame.draw.rect(screen, (200, 200, 200), (player1))
    pygame.draw.rect(screen, (200, 200, 200), (player2))
    pygame.draw.ellipse(screen, (200, 200, 200), (ball))

    collision(player1, player2, ball)
    pygame.display.update()
Packers718
  • 11
  • 1
  • You're using the canvas/screen coordinates of the object itself to store its actual location, which is not a good idea because then you can't get more fine detail than single pixels. Your X and Y position have to be `float`s, which you then translate to and from its internal coordinates, which are `int`s. It's good to decouple your internal model of position from the screen coordinates, since then it makes things like scaling, or porting, much more straightforward. – Random Davis Mar 09 '22 at 23:28
  • @RandomDavis I'm not quite getting what you're saying. Can you break it down into simpler terms? Sorry for the inconvenience. It'd be helpful if you provided me some code too. – Packers718 Mar 09 '22 at 23:58
  • @PaulM. I don't think I can use that for my game because I'm not using the rect.move method to move the ball. I'm simply storing the coordinates of the ball into the ball variable and increasing it every frame. – Packers718 Mar 10 '22 at 00:06
  • @Packers718 Have you looked at the accepted answer of the question I linked? The issue has to do with the fact that you're using a `pygame.Rect` to store the ball's x- and y-coordinates, which, as the accepted answer in the linked thread points out, only supports integral values, not floating point values. If you want floating-point precision for the ball's coordinates, you'll have to store the coordinates in separate variables outside of the `pygame.Rect`, do your math on those variables, and then set the rect's integral coordinates to your floats, rounded to whole integers. – Paul M. Mar 10 '22 at 00:28
  • @PaulM. Yes, I did look at the accepted answer of the question you linked. I think I was misunderstanding what it was trying to say. I tried what you said and it worked. Though, I did have to adjust the variables that make the ball get set in the center after it goes out of boundaries because I made new variables to store my x and y coordinate of the rectangle. Thank you so much! – Packers718 Mar 10 '22 at 02:49

0 Answers0