Reputation: 359
I am trying to visualize an array using lines in pygame but it is drawing random lines on the surface. Here is the code :
import pygame
import random
pygame.init()
array = [100, 256, 132, 151, 493]
white = (255, 255, 255)
black = (0, 0, 0)
gameDisplay = pygame.display.set_mode((800,600))
gameDisplay.fill(black)
pygame.display.set_caption("test")
x1 = 0
y1 = 600
x2 = x1
for number in array:
pygame.draw.line(gameDisplay, white, (x1, y1), (x2, number), 2)
x1 += 100
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
for number in array:
pygame.draw.line(gameDisplay, white, (x1, y1), (x2, number), 2)
x1 += 100
pygame.display.update()
I tried putting the for loop outside the while loop and it is the same thing but not drawing infinitely the lines.
Upvotes: 1
Views: 243
Reputation: 210889
If you want to draw vertical lines, then the x coordinate of the start and the end of the lies has to be equal).
If the lines are draw in the main loop, then the start coordinates wich progressively incremented (like x
) have to be initialized inside the loop.
e.g.
y1 = 600
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
x = 0
for number in array:
pygame.draw.line(gameDisplay, white, (x, y1), (x, number), 2)
x += 100
pygame.display.update()
Upvotes: 1