rect.collisionrect在两个矩形之间不起作用

问题描述 投票:1回答:2

我在让碰撞在游戏中正常工作时遇到了麻烦,我使用了两个整流罩,一个用作桨,一个用作球。我正在使用正常的pygame语句rect1.colliderect(rect2),但由于某种原因,它无法正常工作。

这里是矩形碰撞的代码行

    #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()
python pygame collision
2个回答
0
投票

[球的位置由pygame.Rect对象pygame.Rect定义。 ballball_x仅用于初始化ball_y。您必须设置ballball.x = 10,而不是ball.y = 10ball_x = 10

ball_y = 10

[要使球反弹,您必须将if paddle.colliderect(ball): ball.x = 10 ball.y = 10 反转而不是通过speed[1]ball.x = 10来改变球的位置:

ball.y = 10

0
投票

我将其用于矩形碰撞:

if paddle.colliderect(ball):
    speed[1] = -speed[1]
© www.soinside.com 2019 - 2024. All rights reserved.