2016-05-24 3 views
0

gibt es jemanden, der dieses Programm nehmen und es in einer sehr viel weniger Zeile schreiben kann? oder es sollte nur so geschrieben werden, ich bin irgendwie neu, und frage mich, ob ich zu viel Code für diesesKann dies in weniger Zeilen geschrieben werden, um zu tun, was es tut?

#!/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() 

ist dies zu viel oder kann diese kürzer sein?

Antwort

1

Hier 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()