Jordan Shackelford
Jordan Shackelford

Reputation: 11

Variable returns old value after changing it

I simply want to draw a triangle using the pygame.draw.line method.

I define a function drawTriangle that takes the starting x and y coordinates as parameters, puts them in a tuple, then creates two more tuples whose values are dependent on the starting x and y coordinates.

These three points will be the vertices of the triangle. The function then draws 3 lines - from point1 to point2, from point2 to point3, then from point3 back to point1.

The problem is that when I run the program, only one line is being drawn.

def drawTriangle(startx, starty):
    point1 = (startx, starty)
    point2 = (startx + 20, starty + 20)
    point3 = (startx -30, starty - 30)

    pygame.draw.line(SURFACE, GREEN, point1, point2)
    pygame.draw.line(SURFACE, GREEN, point2, point3)
    pygame.draw.line(SURFACE, GREEN, point3, point1)

Upvotes: 0

Views: 67

Answers (1)

Stan Zeez
Stan Zeez

Reputation: 1188

All your points are located on the same line. May be need:

def drawTriangle(startx, starty):
    point1 = (startx, starty)
    point2 = (startx + 20, starty + 20)
    point3 = (startx -30, starty + 30)

    pygame.draw.line(SURFACE, GREEN, point1, point2)
    pygame.draw.line(SURFACE, GREEN, point2, point3)
    pygame.draw.line(SURFACE, GREEN, point3, point1)

Upvotes: 1

Related Questions