Search code examples
pythonpygamepixel

The same program again


I asked a question about this program yesterday but i have come across another problem. The code still runs and thinks it is colouring in pixels but it should stop when the bottom of the page is reached, it does not.

Here is the code:

import pygame,sys, random, time
y = 0
x = 0
keepgoing = True
pygame.init()
screen = pygame.display.set_mode((500,500))
pixel = raw_input('Please enter a NUMBER that goes into 500. ')
end = 500-(int(pixel))
int(pixel)
screen.fill((255,255,255))

while keepgoing:
    print x,y
    r = random.randint(0,255)
    g = random.randint(0,255)
    b = random.randint(0,255)
    pygame.draw.rect(screen, (r,g,b),(x,y,int(pixel),int(pixel)))
    if x >= end:
        x = 0
        y += int(pixel)
        r = random.randint(0,255)
        g = random.randint(0,255)
        b = random.randint(0,255)
    pygame.draw.rect(screen, (r,g,b),(x,y,int(pixel),int(pixel)))
    if x == end and y == end:
        print 'done'
        keepgoing == False
        break
    x += (int(pixel))

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



for event in pygame.event.get():

    if event.type == pygame.QUIT:
        pygame.quit(); sys.exit();

you will see what i mean if you run the code in python IDLE.


Solution

  • Try changing:

    if x >= end:
    

    To:

    if x >= end and y <= end:
    

    Also, change:

        keepgoing == False
    

    To:

        keepgoing = False
    

    Also, consider changing:

    if x == end and y == end:
    

    To:

    if x >= end and y >= end:
    

    Note: You should probably use a nested for-loop and cycle through x-pixels and y-pixels. It may be better than using a while-loop.