gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 19:55:37 30 4
gpt4 key购买 nike

我在游戏中无法让碰撞发挥作用,我使用两个矩形,一个充当桨,一个充当球。我正在使用正常的 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()

最佳答案

球的位置由 pygame.Rect 定义对象ballball_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 = 10 改变球的位置和ball.y = 10 :

if paddle.colliderect(ball):
speed[1] = -speed[1]

关于python - rect.collisionrect 在两个矩形之间不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59555728/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com