简体   繁体   中英

How do I change the hitbox of one of my sprites?

So when the game starts there will be boxes for the user to dodge, but currently, the hitbox of the objects is a bit off.. for example when you move the ship across a box it registers as a hit, ending in a game over.

This is for a Software Design and Development HSC Assessment task I'm not quite sure what to do to fix this problem

Here's the code!

#This program was created by Tadiwa Mooyo
#more of the car game has been worked on, now there are boxes for the user to "dodge" and it will display the crash message when the boxes are hit
#This program was started on the 22/02/2019
import pygame
import time
import random

pygame.init()

display_width = 1200
display_height = 700
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)

car_width = 100

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('The Great Space Escape!')
clock = pygame.time.Clock()
carImg = pygame.image.load('Ships_directory\\Green_black_ship.png')

def things_dodged(count):
    font = pygame.font.SysFont(None, 25)
    text = font.render("Score: "+str(count), True, white)
    gameDisplay.blit(text,(0,0))


def things(thingx, thingy, thingw, thingh, color):
    pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])

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

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

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

    pygame.display.update()

    time.sleep(2)

    game_loop()



def crash():
    message_display("You Crashed")

def game_intro():

    intro = True

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

        gameDisplay.fill(black)
        largeText = pygame.font.Font('freesansbold.ttf',115)
        TextSurf, TextRect = text_objects("A bit Racey", largeText)
        TextRect.center = ((display_width/2),(display_height/2))
        gameDisplay.blit(TextSurf, TextRect)
        pygame.display.update()
        clock.tick(15)




def game_loop():
    x = (display_width * 0.45)
    y = (display_height * 0.5)

    x_change = 0

    thing_startx = random.randrange(0, display_width)
    thing_starty = -600
    thing_speed = 4
    thing_width = 200
    thing_height = 200

    dodged = 0

    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 = -10
                if event.key ==pygame.K_RIGHT:
                    x_change = 10

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

        x += x_change

        gameDisplay.fill(black)


        #things(thingx, thingy, thingw, thingh, color)
        things(thing_startx, thing_starty, thing_width, thing_height, white)
        thing_starty += thing_speed
        car(x,y)
        things_dodged(dodged)

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

        if thing_starty > display_height:
            thing_starty = 0 - thing_height
            thing_startx = random.randrange(0,display_width)
            dodged += 1
            thing_speed += 0.5
            thing_width += (dodged * 1.4)        

        if y < thing_starty+thing_height:
            print('y crossover')

            if x > thing_startx and x < thing_startx + thing_width or x+car_width > thing_startx and x + car_width < thing_startx+thing_width:
                print('x crossover')
                crash()


        pygame.display.update()
        clock.tick(60)

game_loop()
pygame.quit()
quit()

Use pygame.Rect to simplify your code and implement the collision test by .colliderect() :

eg

things_rect = pygame.Rect(thing_startx, thing_starty, thing_width, thing_height)
car_rect    = pygame.Rect(x, y, *carImg.get_size())

if car_rect.colliderect(things_rect):
    crash()

If you want to implement your "own" test, which checks if 2 rectangles are intersecting, the you've to check if the rectangles are "overlapping" in both dimensions.

2 ranges [ x1 , x1+w1 ] and [ x2 , x2+w2 ] are overlapping if x1 < x2+w2 and x2 < x1+w1 .

So a intersection test for rectangles can be implmented as follows:

car_w, car_h = carImg.get_size()
if (thing_startx < x+car_w and x < thing_startx+thing_width and 
    thing_starty < y+car_h and y < thing_starty+thing_height):
    crash() 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM