简体   繁体   中英

Why doesn't this collision detection apply to objects previously created?

After hours of searching, I still can't figure out why only the most recently spawned circle is affected by the collision detection. I commented out the code in question. I experimented with sprites and that may be the answer but I still got the same results.

import pygame,random
pygame.init()
width,height,radius = 1280,720,20
class Ball(): 
    def __init__(self):  
        self.x = 0
        self.y = 0
        self.vx = 0
        self.vy = 0  
def make_ball():
    ball = Ball()
    ball.x = random.randrange(radius, width - radius)
    ball.y = random.randrange(radius, 100)
    ball.vx = random.randint(1,2)
    ball.vy = 0
    return ball
def main():
    rect_x = 60    
    display = pygame.display.set_mode((width,height))
    pygame.display.set_caption("BOUNCE")
    running = True
    ball_list = []
    ball = make_ball()
    ball_list.append(ball)
    while running:  
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    ball = make_ball()
                    ball_list.append(ball)
        for ball in ball_list: 
            ball.x += ball.vx
            ball.vy += 0.02
            ball.y += ball.vy
            if ball.y >= height - radius:
                ball.vy *= -1
            if ball.x >= width - radius or ball.x <= radius:
                ball.vx *= -1    
        display.fill((0,0,0))   
        for ball in ball_list:
            random_color = (random.randint(1,255),random.randint(1,255),random.randint(1,255))
            circle = pygame.draw.circle(display,random_color,(int(ball.x), int(ball.y)),radius)   
        rectangle = pygame.draw.rect(display,(255,255,255),(int(rect_x),660,60,60)) 
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT and rect_x > 0:
                rect_x -= 2
            if event.key == pygame.K_RIGHT and rect_x < width - 60:
                rect_x += 2
        '''if pygame.Rect(circle).colliderect(rectangle) == True:   ###THIS IS THE BAD CODE!
            print('Your Score:',pygame.time.get_ticks())
            running = False'''
        text = pygame.font.Font(None,120).render(str(pygame.time.get_ticks()),True,(255,255,255))
        display.blit(text,(50,50)) 
        pygame.display.flip()
    pygame.quit()
if __name__ == "__main__":
    main()

Indentation and code organization is the key to this. the offending section is (comments removed):

for ball in ball_list:
    random_color = (random.randint(1,255),random.randint(1,255),random.randint(1,255))
    circle = pygame.draw.circle(display,random_color,(int(ball.x), int(ball.y)),radius)   
rectangle = pygame.draw.rect(display,(255,255,255),(int(rect_x),660,60,60)) 
if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_LEFT and rect_x > 0:
        rect_x -= 2
    if event.key == pygame.K_RIGHT and rect_x < width - 60:
        rect_x += 2
if pygame.Rect(circle).colliderect(rectangle) == True:
    print('Your Score:',pygame.time.get_ticks())
    running = False

You had all the correct pieces, but the order in which you are doing them is off as well as the indentation:

    for ball in ball_list:
        random_color = (random.randint(1,255),random.randint(1,255),random.randint(1,255))
        circle = pygame.draw.circle(display,random_color,(int(ball.x), int(ball.y)),radius)
        rectangle = pygame.draw.rect(display,(255,255,255),(int(rect_x),660,60,60))
        if pygame.Rect(circle).colliderect(rectangle):
            print('Your Score:',pygame.time.get_ticks())
            running = False

This will now run through every ball in the list and check each one for collision. notice the colliderect if statement is indented into the for loop. Also notice i removed the KEYDOWN check from in the middle of it all

Speaking of that I would recommend using:

pressed = pygame.key.get_pressed()
if pressed[pygame.K_LEFT] and rect_x > 0:
    rect_x -= 2
if pressed[pygame.K_RIGHT] and rect_x < width - 60:
    rect_x += 2

for ball in ball_list:
    # for loop from above

instead of what you had. This works best for when you want to allow for holding a key down. pygame.key.get_pressed() gets the state of all the keys all the time, not just when an event happens

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