Milad IDO
Milad IDO

Reputation: 21

Can this be written in fewer lines in order to do what it does?

is there anyone that can take this program and write it in a very fewer line? or it should be written just like this, I'm kinda new, and wondering if I'm using too much code for this

#!/usr/bin/python
import pygame
pygame.init()
window=pygame.display.set_mode([500,500])
pygame.display.set_caption("Drop")
clock = pygame.time.Clock()
game_running=True
y=0;yy=1
accy = [0,0,0,0,0,0,0,0]
while game_running:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            game_running=False
    window.fill((0,0,0))
    x=0
    pygame.draw.rect(window, (101,201,200), [15+x,0+y+accy[0],50,50],0)
    x+=60
    if not accy[0]==450:
        accy[0]+=1
    pygame.draw.rect(window, (101,201,200), [15+x,0+y+accy[1],50,50],0)
    x+=60
    if accy[0] >= 50 and not accy[1] == 450:
        accy[1]+=1
    pygame.draw.rect(window, (101,201,200), [15+x,0+y+accy[2],50,50],0)
    x+=60 
    if accy[1] >= 50 and not accy[2] == 450:
        accy[2]+=1  
    pygame.draw.rect(window, (101,201,200), [15+x,0+y+accy[3],50,50],0)
    x+=60
    if accy[2] >= 50 and not accy[3] == 450:
        accy[3]+=1
    pygame.draw.rect(window, (101,201,200), [15+x,0+y+accy[4],50,50],0)
    x+=60
    if accy[3] >= 50 and not accy[4] == 450:
        accy[4]+=1    
    pygame.draw.rect(window, (101,201,200), [15+x,0+y+accy[5],50,50],0)
    x+=60
    if accy[4] >= 50 and not accy[5] == 450:
        accy[5]+=1    
    pygame.draw.rect(window, (101,201,200), [15+x,0+y+accy[6],50,50],0)
    x+=60
    if accy[5] >= 50 and not accy[6] == 450:
        accy[6]+=1    
    pygame.draw.rect(window, (101,201,200), [15+x,0+y+accy[7],50,50],0)
    if accy[6] >= 50 and not accy[7] == 450:
        accy[7]+=1

    clock.tick(60)
    pygame.display.flip()

is this too much or can this be shorter?

Upvotes: 0

Views: 27

Answers (1)

catsock
catsock

Reputation: 1851

Here ya go:

#!/usr/bin/python
import pygame
pygame.init()
window=pygame.display.set_mode([500,500])
pygame.display.set_caption("Drop")
clock = pygame.time.Clock()
game_running=True
y=0;yy=1
accy = [0,0,0,0,0,0,0,0]
while game_running:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            game_running=False
    window.fill((0,0,0))

    for i in range(8):
        x = i*60
        pygame.draw.rect(window, (101, 201, 200), [15+x, 0+y+accy[i],50,50],0)
        if i==0: # Special case for first iteration
            if not accy[i]==450:
                accy[0]+=1
        else:      
            if accy[i-1] >= 50 and not accy[i]==450:
                accy[i]+=1

    clock.tick(60)
    pygame.display.flip()

Upvotes: 1

Related Questions