【问题标题】:what is wrong with the tuples元组有什么问题
【发布时间】:2014-07-12 10:02:43
【问题描述】:

我试图让 shootRect 出现在 playerRect 的中间,并且在前面一个像素。在代码中,我尝试绘制 shootRect

我的代码:

MESH = ((playerRect.x + 1), (playerRect.x + 1), ((playerRect.topleft.y + playerRect.bottomleft.y) / 3), ((playerRect.topleft.y + playerRect.bottomleft.y) / 2))
shootRect = (screen, WHITE, MESH)
if event.type == KEYDOWN:
    if event.key == ord(' '):
pygame.draw.shootRect

错误:

    Traceback (most recent call last):
  File "C:\Users\***\***\***\***.py", line 76, in <module>
    MESH = ((playerRect.x + 1), (playerRect.x + 1), ((playerRect.topleft.y + playerRect.bottomleft.y) / 3), ((playerRect.topleft.y + playerRect.bottomleft.y) / 2))
AttributeError: 'tuple' object has no attribute 'y'

【问题讨论】:

标签: python python-3.x pygame tuples


【解决方案1】:

playerRect.topleft(x, y) 格式的元组。而不是playerRect.topleft.y 来获取y 值,你想要playerRect.topleft[1]

MESH = ((playerRect.x + 1), (playerRect.x + 1), ((playerRect.topleft[1] + playerRect.bottomleft[1]) / 3), ((playerRect.topleft[1] + playerRect.bottomleft[1]) / 2))
shootRect = (screen, WHITE, MESH)
if event.type == KEYDOWN:
    if event.key == ord(' '):
pygame.draw.shootRect

【讨论】:

    【解决方案2】:

    您可以使用playerRect.left 代替playerRect.topleft.yplayerRect.bottomleft.y。是同一个值。

    所以代替

    playerRect.topleft.y + playerRect.bottomleft.y
    

    你应该有

    playerRect.top + playerRect.bottom
    

    顺便说一句:如果你需要halfway ... playerRect,而不是playerRect.centeryplayerRect.centerx 或两者都有playerRect.center

    【讨论】:

    • 我使用的是中点公式,所以现在感觉我查看了它,它应该是(((playerRect.topleft[1] + playerRect.bottomleft[1]) / 2) + 10), (((playerRect.topleft[1] + playerRect.bottomleft[1]) / 2)) - 10),上面和下面的前 10 个像素使其厚度为 20 个像素。
    • 我更正了我的答案 - 代替 .topleft[1],您可以使用 .top,代替 .bottomleft[1],您可以使用 .bottom。但最后你可以用playerRect.centery+10,playerRect.centery-10
    猜你喜欢
    • 2017-10-21
    • 2017-01-19
    • 1970-01-01
    • 2021-05-11
    • 1970-01-01
    • 2022-06-14
    • 2010-12-25
    • 2020-01-24
    • 1970-01-01
    相关资源
    最近更新 更多