这是我的代码,我想碰撞我的两张图片,但出现以下错误。我的学校项目确实需要帮助。我的代码有什么问题以及如何修复它?
我尝试了 print(pygame.sprite.collide_rect(bg1rect, bg2rect))
并不断收到以下错误:
Traceback (most recent call last): File "/Users/tonydiao/Supermario/Super-mario333.py", line 75, in <module> print(pygame.sprite.collide_rect(bg1rect, bg2rect)) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pygame/sprite.py", line 1310, in collide_rect return left.rect.colliderect(right.rect) AttributeError: 'pygame.Rect' object has no attribute 'rect'
import pygame
pygame.init()
window = pygame.display.set_mode((1024,576))
pygame.display.set_caption("Tony's window")
music = pygame.mixer.music.load('01 Super Mario Bros. Main Theme.mp3')
pygame.mixer.music.play(-1)
x = 470
y = 305
vel = 20
mushx = 0
isjump = False
jumpcount = 10
gameloop = True
bg2sprite = pygame.sprite.Sprite()
bg2sprite.image = pygame.image.load("1200px-MushroomMarioKart8.png")
print(bg2sprite)
bg2sprite.image = pygame.transform.scale(bg2sprite.image, (40, 40))
bg2rect = bg2sprite.image.get_rect()
bg1sprite = pygame.sprite.Sprite()
bg1sprite.image = pygame.image.load('supermario.png')
bg1sprite.image = pygame.transform.scale(bg1sprite.image, (85, 200))
bg1rect = bg1sprite.image.get_rect()
while gameloop:
pygame.time.delay(-20)
for event in pygame.event.get():
if (event.type==pygame.QUIT):
gameloop = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel - 20:
x -= vel
if keys[pygame.K_RIGHT] and x < 1024 - 85 - vel + 20:
x += vel
if not(isjump):
if keys[pygame.K_SPACE]:
isjump = True
else:
if jumpcount >= -10:
neg = 1
if jumpcount < 0:
neg = -1
y -= (jumpcount ** 2) * 0.75 * neg
jumpcount -= 1
else:
isjump = False
jumpcount = 10
if mushx == 1000:
right = False
if mushx == 0:
right = True
if right:
mushx += 10
else:
mushx -= 10
pygame.display.flip()
print(pygame.sprite.collide_rect(bg1rect, bg2rect))
bg = pygame.image.load('background.jpg')
bg = pygame.transform.scale(bg, (1024, 576))
bgrect = bg.get_rect()
window.blit(bg, bgrect)
window.blit(bg1sprite.image ,[x,y])
window.blit(bg2sprite.image ,[mushx,460])
pygame.quit()
最佳答案
bg1rect
和 bg2rect
不是 pygame.sprite.Sprite
对象,但它们是 pygame.Rect
对象。
你必须使用方法colliderect
而不是pygame.sprite.collide_rect()
:
print(pygame.sprite.collide_rect(bg1rect, bg2rect))
print(bg1rect.colliderect(bg2rect))
但请注意,您必须更新矩形的位置。例如:
bg1rect.topleft = [x,y]
bg2rect.topleft = [mushx,460]
if bg1rect.colliderect(bg2rect):
print("hit")
一旦设置了矩形的位置,Rect
对象就可以用于blit
目标上的
。例如:Surface
对象表面
window.blit(bg, bgrect)
window.blit(bg1sprite.image, bg1rect)
window.blit(bg2sprite.image, bg2rect)
pygame.display.flip()
如果你希望角色在碰撞后“消失”,那么你必须停下来绘制它。请注意,整个场景是在每一帧中绘制的。
添加包含玩家生命的状态 (player_lives = 1
)。当检测到碰撞时减少状态。并根据状态绘制玩家:
player_lives = 1
while gameloop:
# [...]
bg1rect.topleft = [x,y]
bg2rect.topleft = [mushx,460]
if bg1rect.colliderect(bg2rect):
player_lives -= 1
# [...]
window.blit(bg, bgrect)
if player_lives > 0:
window.blit(bg1sprite.image, bg1rect)
window.blit(bg2sprite.image, bg2rect)
pygame.display.flip()
关于python - Pygame碰撞问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60065166/