KermitHQ
KermitHQ

Reputation: 33

Problems with pygame.draw.line()

I've been making a game by using pygame for some time. I'd like to add Fishing system Fisherman is just a picture of man holding fishing rod draw on the background and he is not doing anything.

Float is an object witch can be moved by using 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


End of the fishing rod is point on the screen so its just given as argument to FishLine class.

Thats 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)

everything is placed in draw() function correctly (draw function is placed in main Loop)

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

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

Everything to this moment works corectly

Problems starts now When Im at the river, everything draw correctly, but after moving my Float, Fishline doesnt change. I spent few hours looking for mistake but I havent found it yet. Maybe someone of you know where did I make it.

Upvotes: 1

Views: 101

Answers (1)

Rabbid76
Rabbid76

Reputation: 210876

When you move float you need to move FishLine, too. FishLine's x and y coordinates do not magically change when you change float.x and float.y.

Store a reference to the float object in the FishLine, instead of the coordinates of the float.x and float.y.

I recommend not calling the object float. float is the name of a built in function. I'll use the name float_obj instead:

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)

Upvotes: 1

Related Questions