0

ltcImgI am making a simple game where a car dodges some objects, all of which I am importing using pictures from the internet. What would be the best way to configure circular hitboxes that would work well when running into the square box of my car? CarImg Right now I have them put into length and width parameters, since that is all I really know how to do. For some reason, my hitboxes are ending the game good on the left side of my "ltc" circle, but when I cross the boundaries of the circle on the right side, even though I do not actually "hit" the circle, it is causing me to crash and ending the game.

I have tried adjusting my ltcw and ltch to see if it makes it better, but I can only seem to make it worse overall. I couldn't find anything online referencing specifically circular hitboxes when using imported pictures when running into square hitboxes.

import pygame
import time
import random

pygame.init()

display_width = 800
display_height = 600
car_width = 60
car_height = 113

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)

gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('TRX: FuD Racer')
clock = pygame.time.Clock()
carImg = pygame.image.load('RaceCar2.png')
ltcImg = pygame.image.load('LtcLogo.png')
xlmImg = pygame.image.load('Stell_Logo.png')


def car(x, y):
    gameDisplay.blit(carImg, (x, y))


def ltc(x, y):
    gameDisplay.blit(ltcImg, (x, y))


def stell(x, y):
    gameDisplay.blit(xlmImg, (x, y))


def text_objects(text, font):
    textSurface = font.render(text, True, BLACK)
    return (textSurface, textSurface.get_rect())


def message_display(text):
    largeText = pygame.font.Font('freesansbold.ttf', 75)
    (textSurf, textRect) = text_objects(text, largeText)
    textRect.center = (display_width / 2, display_height / 2)
    gameDisplay.blit(textSurf, textRect)

    pygame.display.update()
    time.sleep(3)
    game_loop()


def crash():
    message_display('You Sold Your Tron!')


def game_loop():
    x = display_width * 0.45
    y = display_height * 0.8
    x_change = 0
    y_change = 0

# I should adjust each coins speed in game in accordance with their transaction speeds

    ltc_startx = random.randrange(0, display_width)
    ltc_starty = -300
    ltc_speed = 2
    ltcw = 222
    ltch = 200

    stell_startx = random.randrange(0, display_width)
    stell_starty = -600
    stell_speed = 6
    stellw = 300
    stellh = 450

    gameExit = False

    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -6
                elif event.key == pygame.K_RIGHT:
                    x_change = 6

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    y_change = -6
                elif event.key == pygame.K_DOWN:
                    y_change = 6

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_UP or event.key \
                    == pygame.K_DOWN:
                    y_change = 0

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key \
                    == pygame.K_RIGHT:
                    x_change = 0

        x += x_change
        y += y_change

        gameDisplay.fill(WHITE)

        ltc(ltc_startx, ltc_starty)
        ltc_starty += ltc_speed
        car(x, y)

        stell(stell_startx, stell_starty)
        stell_starty += stell_speed
        car(x, y)

        if x > display_width - car_width or x < -15:
            crash()

        if ltc_starty > display_height:
            ltc_starty = 0 - ltch
            ltc_startx = random.randrange(0, display_width - ltcw)

        if stell_starty > display_height:
            stell_starty = 0 - stellh
            stell_startx = random.randrange(0, display_width - stellw)

        if y < 0:
            crash()

        if y < ltc_starty + ltch:
            print 'y crossover'

            if x > ltc_startx and x < ltc_startx + ltcw or x \
                + car_width > ltc_startx and x + car_width < ltc_startx \
                + ltcw:
                print 'x crossover'
                crash()

        pygame.display.update()

        clock.tick(60)

game_loop()
pygame.quit()
quit()
Cale
  • 23
  • 6
  • @everyone : I have added the two images I am making collide and am having the issues with. Ignore the xlmImg because it will basically be a rectangle and I have no issue getting those to collide correctly. It is the car with the circle that is giving me the issue. – Cale Feb 02 '19 at 07:28
  • So the problem can be solved in a non-generic way by measuring the vector between the to center-points of your circles, then measure the distance of that vector in pixels and map that against the known radian of the two objects. Now, this only works for circles obviously, but would solve your problem. Otherwise you'd need to investigate if Pygame has something similar to pixel collision between objects or not. Or create a function that remembers/builds the outline of a object and match those on each update. – Torxed Feb 02 '19 at 09:10

0 Answers0