2017-03-01 12 views
1

私は、Processingで一種の抽象アートスタイルのものを作ってきました。左クリックは点を置いて、もう一度クリックするとランダムな線になり、右クリックすると新しい点が作られます。Processing.pyが配列の最後から2番目の項目をスキップするのはなぜですか?

ペンの色を選択するために上下の矢印を使用すると問題が発生します。これらのキーを使用すると、最後から2番目の項目(黒またはピンクのいずれか)はスキップされます。コードが添付されています。

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の場合、selectorが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でなければなりません。

関連する問題