1

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()
1
  • 1
    If the goal is filling rect's, there is Surface.fill(Rect) Commented Sep 21, 2013 at 17:23

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the repley @unutbu the for loop is a much cleaner way of creating the bars but i am stil perplexed as to why 'pygame.draw.line(screen, colour, (start, 400), (start, 200))' (start = 0) :) then add say 400 to start this still draws "half" the line compared to the second line drawn
The coordinate (start, 400) represents a point in the middle of the line, not the lower left corner of the rectangular block drawn by the line. Pygame is drawing this line with a linewidth of 40, meaning that the line is thickened by 20 pixels to the left and 20 pixels to the right of (start, 400).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.