pygame.draw.line() 的问题

时间:2021-08-01 17:46:27

标签: python pygame

一段时间以来,我一直在使用 pygame 制作游戏。 我想添加钓鱼系统 渔夫只是一个人拿着钓竿画在背景上的照片,他什么都不做。

Float 是一个可以使用 WSAD 移动的对象

class Float(object):
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.hitbox = (self.x + 25, self.y + 50, 25, 50)
        self.vel = 10

    def draw(self):
        gameDisplay.blit(float_img, (self.x, self.y))

    def move(self):
        if self.vel > 0:
            self.x += self.vel


float = Float(200, 200, 25, 50)



this part is placed in Main Loop,

    if not Game_Paused and AtTheRiver:
        if keys[pygame.K_a] :
            float.x -= float.vel
        if keys[pygame.K_d] :
            float.x += float.vel
        if keys[pygame.K_s] :
            float.y += float.vel
        if keys[pygame.K_w] :
            float.y -= float.vel


钓鱼竿的末端是屏幕上的一个点,所以它只是作为参数提供给 FishLine 类。

那是鱼线:

class FishLine():
    def __init__(self, GameDisplay, color, fishingrod_end, float_pos, lineThickness):  # float_pos = (x, y)
        self.GameDisplay = GameDisplay
        self.color = color
        self.fishingrod_end = fishingrod_end
        self.float_pos = float_pos
        self.lineThickness = lineThickness

    def draw(self):
        pygame.draw.line(self.GameDisplay, self.color, self.fishingrod_end, self.float_pos, self.lineThickness)


FishLine = FishLine(gameDisplay, red, (383, 230), (float.x, float.y), 6)

一切都正确放置在draw()函数中(绘制函数放置在主循环中)

def draw():
    gameDisplay.blit(background, (0, 0))

    if AtTheRiver:
        River.draw()
        Quit.draw()
        float.draw()
        FishLine.draw()

目前一切正常

问题从现在开始 当我在河边时,一切都正确绘制,但移动我的浮标后,鱼线没有改变。我花了几个小时寻找错误,但我还没有找到。 也许你们中有人知道我在哪里做的。

1 个答案:

答案 0 :(得分:1)

当您移动 float 时,您也需要移动 FishLine。当您更改 FishLinefloat.x 时,float.y 的 x 和 y 坐标不会神奇地改变。

float 中存储对 FishLine 对象的引用,而不是 float.xfloat.y 的坐标。

我建议不要调用对象 floatfloat 是内置函数的名称。我将使用名称 float_obj 代替:

float_obj = Float(200, 200, 25, 50)
class FishLine():
    def __init__(self, GameDisplay, color, fishingrod_end, float_obj, lineThickness):
        self.GameDisplay = GameDisplay
        self.color = color
        self.fishingrod_end = fishingrod_end
        self.float_obj = float_obj
        self.lineThickness = lineThickness

    def draw(self):
        float_pos = (self.float_obj.x, self.float_obj.y)
        pygame.draw.line(self.GameDisplay, self.color, self.fishingrod_end, float_pos, self.lineThickness)

fishLine = FishLine(gameDisplay, red, (383, 230), float_obj, 6)