簡體   English   中英

為什么Processing.py會跳過我的數組的倒數第二項?

[英]Why is Processing.py skipping the second-to-last item of my array?

我一直在加工中制作一種抽象藝術風格的東西。 左鍵單擊放置一個點,再次單擊它將生成一個隨機行,右鍵單擊以創建一個新點。

當我使用向上和向下箭頭選擇筆顏色時,會出現問題。 當我使用這些鍵時,跳過倒數第二個項目(黑色或粉紅色)。 代碼附后。

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

你的第一個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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM