Im making a pinball game in pygame however im getting an issue with the collision system where the ball just phases through the wall, it detects there is a collision as ive set it to print "collided" however it simply doesnt collide, only with certain points on the border (currently a prototype so only working things are the graphics and ball movement).
import pygame
import math
from sys import exit
pygame.init()
#create screen
#Dimentions
screen = pygame.display.set_mode((700, 1000))
screen.fill("White")
#Load background graphics.
background = pygame.image.load('C:/Users/Lucas/Documents/King Edwards/Computer Science/CourseWork/graphics/backgroundImg.png')
background = pygame.transform.scale(background, (700, 870))
pygame.display.set_caption("Pinball")
icon = pygame.image.load('C:/Users/Lucas/Documents/King Edwards/Computer Science/CourseWork/graphics/icon.png')
pygame.display.set_icon(icon)
#Sets the variable for the framerate (which is set at the end of the program)
clock = pygame.time.Clock()
#Ball
#Loads Graphics
ballImg = pygame.image.load("C:/Users/Lucas/Documents/King Edwards/Computer Science/CourseWork/graphics/ball.png")
#Sets size of ball
ballImg = pygame.transform.scale(ballImg, (60, 60))
#Balls position
ballX = 320
ballY = 760
ballY_change = 0
#Directional speed of the ball
x_speed, y_speed = 5,4
#Flipper
#Loads Graphics (flipperImg = right flipper | flipper1Img = left flipper)
flipperImg = pygame.image.load("C:/Users/Lucas/Documents/King Edwards/Computer Science/CourseWork/graphics/flipper.png")
flipperImg = pygame.transform.scale(flipperImg, (164, 45))
#Sets the position of the flippers.
flipperX = 370
flipperY = 800
flipper1Img = pygame.image.load("C:/Users/Lucas/Documents/King Edwards/Computer Science/CourseWork/graphics/flipper1.png")
flipper1Img = pygame.transform.scale(flipper1Img, (164, 45))
flipper1X = 166
flipper1Y = 800
#Slingshot
#Loads Graphics (slingshotImg = right slingshot | slingshot1Img = left slingshot)
slingshotImg = pygame.image.load("C:/Users/Lucas/Documents/King Edwards/Computer Science/CourseWork/graphics/slingshot.png")
slingshotImg = pygame.transform.scale(slingshotImg, (177, 281))
#Sets the position of the slingshots.
slingshotX = 522.5
slingshotY = 590
slingshot1Img = pygame.image.load("C:/Users/Lucas/Documents/King Edwards/Computer Science/CourseWork/graphics/slingshot1.png")
slingshot1Img = pygame.transform.scale(slingshot1Img, (177, 281))
slingshot1X = 0
slingshot1Y = 590
#Draw image
def ball(x,y):
screen.blit(ballImg, (x,y))
def flippers():
screen.blit(flipperImg, (flipperX,flipperY))
screen.blit(flipper1Img, (flipper1X,flipper1Y))
def slingshots():
screen.blit(slingshotImg, (slingshotX,slingshotY))
screen.blit(slingshot1Img, (slingshot1X,slingshot1Y))
#Gravity
def gravity():
global ballY
ballY += 2.5
#Bounce
def bouncing():
global x_speed
global y_speed
global ballX
global ballY
ballX += x_speed
ballY += y_speed
if ballX >= 700 or ballY <= 0:
x_speed *= -1
if ballY >= 1000 or ballX <= 0:
y_speed *= -1
#Collisions
def isCollision(ballX,ballY,flipperX,flipperY,flipper1X,flipper1Y):
distance = math.sqrt((math.pow(ballX - flipperX,2))+ (math.pow(ballY - flipperY,2)))
distance1 = math.sqrt((math.pow(ballX - flipper1X,2))+ (math.pow(ballY - flipper1Y,2)))
if distance < 55 or distance1 < 55:
return True
else:
return False
#Game loop
while True:
screen.fill("Black")
screen.blit(background, (0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
print("Keystroke pressed!")
if event.key == pygame.K_a:
print("A is pressed.")
ballX -= 20
if event.key == pygame.K_d:
print("D is pressed.")
if event.key == pygame.K_SPACE:
print("Spacebar is pressed.")
if event.type == pygame.KEYUP:
if event.key == pygame.K_a or event.key == pygame.K_d or event.key == pygame.K_SPACE:
print("Key released!")
#draw elements and update
#if ballY >= 1000:
#ballY = 760
#ballX = 320
#print("DIED")
#elif ballY <= 0:
#ballY = 0
#if ballX <= 0:
#ballX = 0
#elif ballX >= 700:
#ballX = 700
#collision
collision = isCollision(ballX,ballY,flipperX,flipperY,flipper1X,flipper1Y)
if collision:
#ballX = 320
#ballY = 760
print("COLLIDED!")
bouncing()
ball(ballX,ballY)
flippers()
slingshots()
#gravity()
pygame.display.update()
clock.tick(60)