0
import pygame
import random

pygame.init()
# Window setup
size = [400, 300]
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()

# player position
x = size[0] / 2
y = size[1] / 2

# ball position
ballX = random.randrange(0, size[0])
ballY = random.randrange(0, size[1])

#  colors
red = pygame.color.Color('#FF8080')
blue = pygame.color.Color('#8080FF')
white = pygame.color.Color('#FFFFFF')
black = pygame.color.Color('#000000')

width = 10
height = 10
radius = 6
# Game loop
done = False
def checkOffScreenX(x):
    if x > size[0] - width>-x:
        x -= 10
        
        sound=pygame.mixer.Sound('walltouch2.mp3')
        sound.play(loops=0)
    elif x < 0:
        x += 10
        
        sound=pygame.mixer.Sound('walltouch2.mp3')
        sound.play(loops=0)
    return x
def checkOffScreenY(y):
    if y > size[1] - height> -y:
        y -= 10
        
        sound=pygame.mixer.Sound('walltouch2.mp3')
        sound.play(loops=0)
    elif y < 0:
        y += 10
        
        sound=pygame.mixer.Sound('walltouch2.mp3')
        sound.play(loops=0)
    return y

Down here is where the issue occurs, whenever the player attempts to change their size, the ball can't be hit properly, while testing this issue I found out that the hitbox of the player is the size of how the player originally was, the hitbox won't increase in size as the width and height of the player increase

def checkTouching():
    # Causes mini explosion if the players are touching
    global x
    global ballX
    global y
    global ballY
    global width
    global height

    # Check if player and ball are touching
    if -10 < y - ballY <10 and -10 < x - ballX<10:
        # Music
        sound=pygame.mixer.Sound('collision2.mp3')
        sound.play(loops=0)

        
        #draw an explosion
        pygame.draw.circle(screen, white, [x, y], 15)
        
        xDiff = x - ballX 
        yDiff = y - ballY 

        # check if ball is on edge of screen
        if ballX == 0:
            xDiff -= 5
        elif ballX == size[0]:
            xDiff += 5
        if ballY == 0:
            yDiff -= 5
        elif ballY == size[1]:
            yDiff += 5
        # move the ball and the player
        x += xDiff * 3
        ballX -= xDiff * 3

        y += yDiff * 3
        ballY -= yDiff * 3


    
while not done:
    screen.fill(black)
    
    keys = pygame.key.get_pressed()

    #player movement

    if keys[pygame.K_w]:
        y -= 1
            
    elif keys[pygame.K_s]:
        y += 1
            
    elif keys[pygame.K_d]:
        x += 1
      
            
    elif keys [pygame.K_a]:
        x -= 1

    elif keys[pygame.K_UP]:
        height += 0.5
        if height > 130:
            height -= 5
    elif keys[pygame.K_DOWN]:
        height -= 0.5
        if height < 6:
            height += 5
    elif keys[pygame.K_RIGHT]:
        width += 0.5
        if width > 130:
            width -= 5
    elif keys[pygame.K_LEFT]:
        width -= 0.5
        if width < 6:
            width += 5
    elif keys[pygame.K_r]:
        width = 10
        height = 10
            

   

              
    clock.tick(200)

    # draw player
    pygame.draw.ellipse(screen, blue, [x, y, width, height])

    # draw ball
    pygame.draw.circle(screen, red, [ballX, ballY], 6)

    # Check if player is touching the ball
    checkTouching()
    

    # Check off-screen
    x = checkOffScreenX(x)
    y = checkOffScreenY(y)
    ballX = checkOffScreenX(ballX)
    ballY = checkOffScreenY(ballY)

        



    pygame.display.flip()

1 Answers1

1

This line checks the collision:

# Check if player and ball are touching:
if -10 < y - ballY <10 and -10 < x - ballX<10:

changing the hard coded initial value 10 to the variable values width and height will do the collision check:

if -height < y - ballY < height and -width < x - ballX < width:
zwitsch
  • 359
  • 3
  • 9