1

i start my first pygame project and got stuck with collision of two moving images. One image(apple) is falling down randomly and second image(basket) is moving on command(left, right, up and down). I used distance formula d=√((x_2-x_1)²+(y_2-y_1)²) and set "if" statement. if distance_x < 128: return true. Problem is that 128 px left from basket program detects falling apple. Here is the code i wrote.

import pygame
import os
import random
import math

pygame.init()
screen = pygame.display.set_mode((800, 600))

pygame.display.set_caption("Apple Catcher")
background = pygame.image.load(os.path.join("pics", "farm.png"))
icon = pygame.image.load(os.path.join("pics", "farmer.png"))
pygame.display.set_icon(icon)

basket_img = pygame.image.load(os.path.join("pics", "basket.png"))
basket_x = 330
basket_y = 480
basket_x_change = 0
basket_y_change = 0

apple_img = pygame.image.load(os.path.join("pics", "apple.png"))
apple_x = random.randint(1, 768)
apple_y = random.randint(0, 250)
apple_y_change = random.uniform(1, 3)

score = 0

def basket(x, y):
    screen.blit(basket_img, (x, y))

def apple(x, y):
    screen.blit(apple_img, (x, y))

def collision(apple_x, apple_y, basket_x, basket_y):
    distance_y = math.sqrt((math.pow(apple_y - basket_y, 2)))
    distance_x = math.sqrt((math.pow(apple_x - basket_x, 2)))
    if distance_y < 1 and distance_x < 128:
        return True
    else:
        return False

run = True
while run:
screen.fill((0, 128, 0))
screen.blit(background, (0, 0))
for event in pygame.event.get():
    if event.type == pygame.QUIT:
        run = False

    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT:
            basket_x_change = -3
        if event.key == pygame.K_RIGHT:
            basket_x_change = 3
        if event.key == pygame.K_UP:
            basket_y_change = -3
        if event.key == pygame.K_DOWN:
            basket_y_change = 3
    if event.type == pygame.KEYUP:
        if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
            basket_x_change = 0
        if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
            basket_y_change = 0

basket_x += basket_x_change

if basket_x <= 0:
    basket_x = 0.0
elif basket_x >= 672:
    basket_x = 672

basket_y += basket_y_change

if basket_y <= 350:
    basket_y = 350
elif basket_y >= 472:
    basket_y = 472

apple_y += apple_y_change

if apple_y >= 600:
    apple_x = random.randint(0, 768)
    apple_y = random.randint(0, 230)
    apple_y_change = random.uniform(1, 2.5)

coll = collision(apple_x, apple_y, basket_x, basket_y)
if coll:
    apple_x = random.randint(0, 768)
    apple_y = random.randint(0, 230)
    apple_y_change = random.uniform(1, 2.5)
    score += 1
    print(score)

apple(apple_x, apple_y)
basket(basket_x, basket_y)
pygame.display.update()

Best regards! Tom

Tom
  • 11
  • 2
  • Is there a reason that you're not using [`abs`](https://docs.python.org/3/library/functions.html#abs) (absolute value) instead of the `math.sqrt(math.pow(..., 2))`? Those square roots are a lot slower than the builtin `abs`. – David Culbreth Jul 29 '21 at 22:49
  • There is no particular reason. – Tom Jul 29 '21 at 22:52

0 Answers0