【问题标题】:Drawing Circle in Python isn't in correct locationPython中的绘图圈不在正确的位置
【发布时间】:2023-03-26 05:19:02
【问题描述】:

我正在制作一个塔防游戏,对于塔的范围,我以塔为中心点做了一个圆圈。虽然,当我使用 pygame.draw.rect() 画一个圆时,它似乎在一个完全不同的位置。这是它的代码。

变量:

Radius = 50
Center_X = 100
Center_Y = 100

用于检查敌人是否在圆圈范围内:

for Enemy in Enemies:
    if ((Enemy['Rect'].left + 10) - Center_X)^2 + ((Enemy['Rect'].top + (35/2)) Center_Y)^2 < Radius^2:
        print(1)

画圆:

pygame.draw.circle(Window, Black, (Center_X, Center_Y), Radius, 0)

【问题讨论】:

  • 与什么相比,绘制的圆圈似乎位于完全不同的位置?
  • 你能举个例子说明你想要什么和得到什么吗?
  • 与我使用方程式制作的圆圈相比。即使敌人不在绘制的圆圈中,该等式似乎也会检测到敌人。即使敌人没有碰到画的圆圈,它也会在控制台中打印“1”。
  • 也许你在方程中有错误的数据。更好的print x,y 两个元素并进行比较。为什么会得到.left + 10.top + (35/2)

标签: python pygame range geometry


【解决方案1】:

使用pygame.Rect() 保持精灵位置。

Center_X = 100
Center_Y = 100

positon = pygame.Rect() 
positon.centerx = Center_X
positon.centery = Center_Y

现在你可以画了

pygame.draw.circle(Window, Black, position.center, Radius, 0)

您可以将pygame.Rect()collision detection 一起使用

在 PyGame 文档中查看更多信息:pygame.Rect()

您可以使用pygame.Sprite() 拥有circle collision detection

【讨论】:

    【解决方案2】:

    在 Python 中,求幂运算符是 **,而不是 ^,而是 bitwise XOR operator

    2^2
    0
    32^1
    33
    

    改为:

    2**2
    4
    32**1
    32
    

    所以:

    if ((Enemy['Rect'].left + 10) - Center_X) ** 2 + ((Enemy['Rect'].top + (35/2)) - Center_Y)**2 < Radius**2:
    

    【讨论】:

      猜你喜欢
      • 2021-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多