Silk
Silk

Reputation: 13

pygame draw function

Apologies in advance if this is a simple fix but I couldn't find anything on it. I am relatively new to pygame but I can't understand why when I run this the first bar that is drawn is always half cut off. To me anyway I should start a 0,400 and draw from 0 across 40 and then up. If this is not the case please enlighten a curious mind

from pygame import *
import pygame, sys, random

pygame.init()
screen = pygame.display.set_mode((1000,400))
colour = (0, 255, 0)
array = []
x, y, z, b = -80, 0, 0, 0
flag = True
for c in range(5):
    array.append(random.randint(100, 400));

for c in array:
    print c

while True:

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    if len(array) == z:
        flag = False

    if flag == True:
        b = array[z]
        x += 80
        z += 1

    pygame.draw.line(screen, colour, (x, 400), (x, (400-b)), 40)

    pygame.display.update()

Upvotes: 1

Views: 807

Answers (1)

unutbu
unutbu

Reputation: 879591

pygame is drawing a line from (0,400) to (0, 400-b), with line thickness 40.

Here is an way to shift the lines so each is fully visible:

for i, b in enumerate(array):
    x = i*80 + 20  # <-- add 20 to shift by half the linewidth
    pygame.draw.line(screen, colour, (x, 400), (x, (400-b)), 40)

import sys
import random
import pygame

pygame.init()
screen = pygame.display.set_mode((1000,400))
colour = (0, 255, 0)
array = [random.randint(100, 400) for c in range(5)]

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    linewidth = 40
    for i, b in enumerate(array):
        x = i*linewidth*2 + linewidth//2
        pygame.draw.line(screen, colour, (x, 400), (x, (400-b)), linewidth)

    pygame.display.update()

Upvotes: 1

Related Questions