I am using Collision Detection in my game, the game runs but it doesn't actually detect the collision. I tried changing the variable names but and even restarting my computer. I also tried putting a print statement to check if that was working instead of the real output which is supposed to happen (That being the game ending.) I'm learning pygame by actually making a game because I find that that is the best way to learn. Here is the code(I tried cutting it down as much as possible):
# Player
...
# Making the Player Variables.
# Creating Classes
class Player():
...
class Enemy():
...
class Other():
def isCollision(enemyX, enemyY, player_x, player_y):
distance = math.sqrt(math.pow(enemyX - player_x, 2) + (math.pow(enemyY - player_y, 2)))
if distance < 27:
return True
else:
return False
# Enemy
enemies = []
number_of_enemies = 3
enemyImg = pygame.image.load('C:/Users/user/Desktop/Python/CodingBee/Pygame Application Prototype/Covid Enemy.png')
for i in range(number_of_enemies):
x = random.randint(600,1000)
y = 450
enemyX_change = -5
enemyY_change = 0
enemy = Enemy(enemyImg, x, y, enemyX_change,enemyY_change)
enemies.append(enemy)
# Main Game Loop
clock = pygame.time.Clock()
SCEEN_UPDATE = pygame.USEREVENT
pygame.time.set_timer(SCEEN_UPDATE, 150)
playing = True
while playing:
screen.fill((0, 0, 255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running, playing, credits_menu, options_menu = False, False, False, False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
isJump = True
if isJump:
...
# Enemy Boundries and Collision Check
for enemy in enemies:
if enemy.x < 20:
for j in range(number_of_enemies):
enemy.y = 450
enemy.x = random.randint(800, 2000)
Score_Value += 10
enemy.move()
collision = Other.isCollision(enemy.x,enemy.y,player_x,player_x)
if collision:
enemy.x = 2000
playerx = 2000
Other.game_over()
sys.exit()
screen.fill((0, 0, 255))
for enemy in enemies:
enemy.draw()
# draw objects
...
#Update Screen
pygame.display.update()
clock.tick(60)
Thanks for taking the time to answer this!