rect.collisionrect 两个矩形之间不工作

rect.collisionrect not working between two rects

我无法在我的游戏中使用碰撞,我使用了两个矩形,一个用作桨,一个用作球。我正在使用普通的 pygame 语句 rect1.colliderect(rect2),但由于某些原因它不起作用。

这是 rects 碰撞的代码行

    #collision of ball with paddle
    if (paddle.colliderect(ball)):
        ball_x = 10
        ball_y = 10

不确定出了什么问题。如果你想要 运行,这里是完整的编码。

#December 16, 2019
#Final Project - Breakout

#IMPORTING LIBRARIES-----
import pygame
import sys
import time

#INITIALIZING SCREEN SIZE-----
pygame.init()
screen_size = (700, 750)
screen = pygame.display.set_mode((screen_size),0)
pygame.display.set_caption("BREAKOUT")

#retrieve screen measurements
screen_w = screen.get_width()
screen_h = screen.get_height()

#retrieve position of center of screen
center_x = int(screen_w/2)
center_y = int(screen_h/2)

#COLOURS-----
WHITE = (255,255,255)
BLACK = (0, 0, 0)
GREEN = (0,255,0)
RED = (255,0,0)
BLUE = (0,0,255)
PURPLE = (154, 136, 180)

#BACKGROUND-----
screen.fill(BLACK)
pygame.display.update()

#SPEED-----
clock = pygame.time.Clock()
FPS = 60 #set frames per second
speed = [4,4]
paddle_speed = 6

#VARIABLES-----

#paddle
paddle_w = 100
paddle_h = 10
paddle_x = 500
paddle_y = 670

paddle_dx = 0
paddle_dy = 0

#ball
ball_w = 10
ball_h = 10
ball_x = center_x
ball_y = center_y


#RECTS-----
paddle = pygame.Rect(paddle_x, paddle_y, paddle_w, paddle_h)
ball = pygame.Rect(ball_x, ball_y, ball_w, ball_h)

#LOOPS-----
game = False


#loop for game
game = True
while game:
    for event in pygame.event.get():
        if event.type ==pygame.QUIT:
            game = False
            pygame.quit()
            sys.exit()
        #moving paddle with keys
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                paddle_dx = -paddle_speed
            elif event.key == pygame.K_RIGHT:
                paddle_dx = paddle_speed

    if event.type == pygame.KEYUP:
        paddle_dx = 0

    #constrain this loop to the specified FPS
    clock.tick(FPS)

    #PADDLE EVENTS-----

    #store old paddle positions
    old_paddle_x = paddle.x
    old_paddle_y = paddle.y

    #moving the paddle rect
    paddle.move_ip(paddle_dx, paddle_dy)

    #check to see if rect has left screen
    if paddle.left < 0 or paddle.right > screen_w:
        paddle.x = old_paddle_x

    #BALL EVENTS-----

    #moving ball
    ball = ball.move(speed)

    #collision left & right
    if ball.left < 0 or ball.right > screen_w:
        speed[0] = -speed[0]

    #collision top
    if ball.top < 0 or ball.bottom > screen_h:
        speed[1] = -speed[1]

    #collision of ball with paddle
    if (paddle.colliderect(ball)):
        ball_x = 10
        ball_y = 10

    #removes screen trail
    screen.fill(BLACK)

    #drawing paddle/ball inside rect
    pygame.draw.rect(screen,PURPLE,paddle,0)
    pygame.draw.rect(screen,WHITE,ball,0)

    #updating the screen
    pygame.display.update()

pygame.quit()
sys.exit()

球的位置由 pygame.Rect 对象 ball 定义。 ball_xball_y只是用来初始化ball
您必须设置 ball.x = 10ball.y = 10 而不是 ball_x = 10ball_y = 10:

if paddle.colliderect(ball):
    ball.x = 10
    ball.y = 10

要使球拍反弹,您必须反转 speed[1] 而不是通过 ball.x = 10ball.y = 10:

改变球的位置
if paddle.colliderect(ball):
    speed[1] = -speed[1]

我用它来进行矩形碰撞:

if not (x1 >= x2 + w2 or x1 + w1 <= x2 or y1 >= y2 + h2 or y1 + h1 <= y2):
    collision = True
else:
    collision = False