Python rect.collisionrect在两个rect之间不工作

Python rect.collisionrect在两个rect之间不工作,python,pygame,collision,Python,Pygame,Collision,在我的游戏中,我很难让碰撞正常进行,我使用了两个矩形,一个用作划桨,另一个用作球。我使用的是普通的pygame语句rect1.collide rect(rect2),但由于某些原因,它不起作用 下面是rects冲突的代码行 #collision of ball with paddle if (paddle.colliderect(ball)): ball_x = 10 ball_y = 10 不知道怎么了。这里是完整的编码,如果你想运行它 #De

在我的游戏中,我很难让碰撞正常进行,我使用了两个矩形,一个用作划桨,另一个用作球。我使用的是普通的pygame语句rect1.collide rect(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()
#2019年12月16日
#最终项目-突破
#导入库-----
导入pygame
导入系统
导入时间
#初始化屏幕大小-----
pygame.init()
屏幕大小=(700750)
screen=pygame.display.set_模式((屏幕大小),0)
pygame.display.set_标题(“突破”)
#检索屏幕测量值
screen\u w=screen.get\u width()
screen\u h=screen.get\u height()
#检索屏幕中心的位置
中心x=int(屏幕w/2)
中心y=int(屏幕h/2)
#颜色-----
白色=(255255)
黑色=(0,0,0)
绿色=(0255,0)
红色=(255,0,0)
蓝色=(0,0255)
紫色=(154136180)
#背景-----
屏幕填充(黑色)
pygame.display.update()
#速度-----
clock=pygame.time.clock()
FPS=60#每秒设置帧数
速度=[4,4]
桨速=6
#变数-----
#划桨
桨叶w=100
桨叶h=10
桨叶_x=500
桨叶y=670
桨叶_dx=0
桨叶直径=0
#球
球w=10
球h=10
球=中心
球=中心球
#直肠-----
桨=pygame.Rect(桨x,桨y,桨w,桨h)
ball=pygame.Rect(ball_x,ball_y,ball_w,ball_h)
#循环-----
游戏=错误
#循环游戏
游戏=真实
游戏时:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
游戏=错误
pygame.quit()
sys.exit()
#用键移动桨
elif event.type==pygame.KEYDOWN:
如果event.key==pygame.K_左:
拨杆dx=-拨杆速度
elif event.key==pygame.K_RIGHT:
拨杆dx=拨杆速度
如果event.type==pygame.KEYUP:
桨叶_dx=0
#将此循环约束到指定的FPS
时钟滴答声(FPS)
#划桨项目-----
#储存旧的桨叶位置
旧桨
老桨
#将桨叶向上移动
划桨。移动ip(划桨dx,划桨dy)
#检查rect是否有左屏幕
如果左桨<0或右桨>屏幕:
桨.x=旧桨
#球类项目-----
#移动球
球=球。移动(速度)
#左右碰撞
如果ball.left<0或ball.right>屏幕:
速度[0]=-速度[0]
#碰撞顶部
如果ball.top<0或ball.bottom>screen\u h:
速度[1]=-速度[1]
#球与桨的碰撞
如果(划桨碰撞直线(球)):
球x=10
球y=10
#删除屏幕痕迹
屏幕填充(黑色)
#在矩形内绘制桨/球
pygame.draw.rect(屏幕,紫色,桨,0)
pygame.draw.rect(屏幕,白色,球,0)
#更新屏幕
pygame.display.update()
pygame.quit()
sys.exit()

球的位置由对象
球定义
ball\u x
ball\u y
仅用于初始化
ball

您必须设置
ball.x=10
ball.y=10
,而不是
ball\u x=10
ball\u y=10

如果桨叶碰撞(球):
球x=10
球y=10

要使球在桨叶上弹起,您必须反转
速度[1]
,而不是通过
ball.x=10
ball.y=10
改变球的位置:

如果桨叶碰撞(球):
速度[1]=-速度[1]

球的位置由对象
球定义
ball\u x
ball\u y
仅用于初始化
ball

您必须设置
ball.x=10
ball.y=10
,而不是
ball\u x=10
ball\u y=10

如果桨叶碰撞(球):
球x=10
球y=10

要使球在桨叶上弹起,您必须反转
速度[1]
,而不是通过
ball.x=10
ball.y=10
改变球的位置:

如果桨叶碰撞(球):
速度[1]=-速度[1]

我将其用于rect碰撞:

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

如果不是(x1>=x2+w2或x1+w1=y2+h2或y1+h1我将其用于矩形碰撞:

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

如果没有(x1>=x2+w2或x1+w1=y2+h2或y1+h1感谢这项工作,但现在我想把它改成从桨上弹起,你知道怎么做吗?@IJH我扩展了答案。谢谢这项工作,但现在我想把它改成从桨上弹起,你知道怎么做吗?@IJH我扩展了答案。问题是ab所以它打算使用,问题是关于它打算使用和