问题描述
此问题曾被问过,但他们回答的代码不适用于我.
This question has been asked before, but the code they answer doesn't apply to me.
我想检查游戏中两个矩形何时发生碰撞,对于圆形,我在圆形后面放置了一个矩形(颜色为黑色).
I want to check when two rectangles collide in my game, and for the circle, I put a rectangle behind the circle(it's colored black).
但是,当我运行时,出现一个错误,提示矩形需要具有rect属性.
However, when I run, I get an error saying the rectangle needs to have a rect attribute.
这是我的代码:
clock = pygame.time.Clock()
pygame.init()
change = False
screen = pygame.display.set_mode((400, 300))
done = False
x = 100
y = 30
bound = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN and event.key == pygame.K_f:
color = (255, 255, 255)
pygame.draw.rect(screen, color, (110, 30, 60, 60))
bound = True
elif event.type == pygame.KEYDOWN and event.key == pygame.K_z:
change = not change
if bound:
pressed = pygame.key.get_pressed()
if pressed[pygame.K_DOWN] or pressed[pygame.K_s]:
y += 5
elif pressed[pygame.K_RIGHT] or pressed[pygame.K_d]:
x += 5
elif pressed[pygame.K_LEFT] or pressed[pygame.K_a]:
x -= 5
elif pressed[pygame.K_UP] or pressed[pygame.K_w]:
y -= 5
elif change:
color2 = (0, 128, 86)
else:
color2 = (0, 128, 10)
screen.fill((0, 0, 0))
pygame.draw.rect(screen, color2, (x, y, 60, 60))
#pygame.draw.rect(screen, (255, 255, 255), (150, 30, 10, 60))
pygame.draw.rect(screen, (255, 255, 255), (x+ 65, y + 24, 20, 10))
else:
pressed = pygame.key.get_pressed()
if pressed[pygame.K_DOWN] or pressed[pygame.K_s]:
y += 5
elif pressed[pygame.K_RIGHT] or pressed[pygame.K_d]:
x += 5
elif pressed[pygame.K_LEFT] or pressed[pygame.K_a]:
x -= 5
elif pressed[pygame.K_UP] or pressed[pygame.K_w]:
y -= 5
elif change:
color2 = (0, 128, 86)
else:
color2 = (0, 128, 10)
screen.fill((0, 0, 0))
player = pygame.draw.rect(screen, color2, (x, y, 60, 60))
back = pygame.draw.rect(screen, (0, 0, 0), (210, 210, 40, 40))
gun1 = pygame.draw.circle(screen, (0, 0, 255), (250, 250), 40, 1)
pygame.draw.rect(screen, (255, 255, 255), (240, 245, 20, 10))
clock.tick(60)
pygame.display.flip()
有人可以给我代码为我的矩形分配rect属性吗?
Can someone please give me code that assigns the rect attribute to my rectangles?
谢谢!
推荐答案
操作 pygame.sprite.collide_rect()
, pygame.sprite.spritecollide()
, pygame.sprite.groupcollide()
等用于 pygame.sprite.Sprite
对象和 pygame.sprite.Group
对象.
此操作使用 Sprite
对象的内部 .rect
属性来检测冲突.
The operations pygame.sprite.collide_rect()
, pygame.sprite.spritecollide()
, pygame.sprite.groupcollide()
etc. are for the use with pygame.sprite.Sprite
objects and pygame.sprite.Group
objects.
This operations use the internal .rect
attribute of the Sprite
objects, to detect collisions.
您虽然有一个 Sprite
或一个 Group
,但是却有 pygame.Rect
个对象.
You nether have a Sprite
nor a Group
, but you have pygame.Rect
objects.
验证是否2个 pygame.Rect
对象发生碰撞,您必须使用 pygame.Rect.colliderect()
.例如:
To verify if 2 pygame.Rect
objects are colliding, you have to use pygame.Rect.colliderect()
. For instance:
player = pygame.draw.rect(screen, color2, (x, y, 60, 60))
back = pygame.draw.rect(screen, (0, 0, 0), (210, 210, 40, 40))
gun1 = pygame.draw.circle(screen, (0, 0, 255), (250, 250), 40, 1)
if player.colliderect(back):
print("player collide back")
if player.colliderect(gun1):
print("player collide gun1")
这篇关于您如何分配"rect"字词?Pygame中的pygame.sprite.rect矩形的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!