2017-03-01 85 views
1

我一直在處理一種抽象藝術風格的東西。左鍵點擊放置一個點,再次點擊它會產生一個隨機線,點擊鼠標右鍵創建一個新點。爲什麼Processing.py跳過我的數組的倒數第二項?

問題出現在我用上下箭頭挑選筆的顏色時。當我使用這些鍵時,跳過倒數第二項(黑色或粉紅色)。代碼附加。

def setup(): 
    size(750, 750) 
    background(255) 
    global clicks 
    global selector 
    global fillcolors 
    fillcolors = [0x80FFFFFF, 0x80000000, 0x80FF0000, 0x8000FF00, 0x800000FF, 0x80FFFF00, 0x80FF00FF, 0x8000FFFF] 
    selector = 1 
    clicks = 0 
    ellipseMode(CENTER) 
    fill(255, 255, 255, 128) 

def draw(): 
    ellipse(50, 50, 50, 50) 

def mousePressed(): 
    global x 
    global y 
    global clicks 
    if (mouseButton == LEFT) and (clicks == 0): 
     x = mouseX 
     y = mouseY 
     clicks = 1 
    if (mouseButton == LEFT) and (0 < clicks < 11): 
     line(x, y, x+random(-300, 300), y+random(-300, 300)) 
     clicks += 1 
    if (mouseButton == LEFT) and (clicks == 11): 
     wide = random(300) 
     clicks = 1 
     line(x, y, x+random(-300, 300), y+random(-300, 300)) 
     ellipse(x, y, wide, wide) 
    if mouseButton == RIGHT: 
     clicks = 0 

def keyPressed():    # this is the color selector area. 
    global selector 
    global fillcolors 
    global clicks 
    clicks = 0 
    if key != CODED: 
     background(255) 
    elif key == CODED: 
     if keyCode == UP: 
      if selector < 8:     # something in here is causing the second-to-last item of the array to be skipped. 
       fill(fillcolors[selector]) 
       selector += 1 
      if selector == 7: 
       fill(fillcolors[selector]) 
       selector = 0 
     if keyCode == DOWN: 
      if selector > 0: 
       fill(fillcolors[selector]) 
       selector -= 1 
      if selector == 0: 
       fill(fillcolors[selector]) 
       selector = 7 

回答

3

您的第一個if在每種情況下都會影響您的第二個。對於UP,如果selector爲6,則它變爲7,然後匹配selector == 7;對於DOWN,如果選擇器爲1,則它變爲0,然後匹配selector == 0

使用elif讓他們獨家:

if selector < 8: 
    fill(fillcolors[selector]) 
    selector += 1 
elif selector == 7: 
    fill(fillcolors[selector]) 
    selector = 0
if selector > 0: 
    fill(fillcolors[selector]) 
    selector -= 1 
elif selector == 0: 
    fill(fillcolors[selector]) 
    selector = 7

和你的第一個條件可能應該是if selector < 7而非8

相關問題