简体   繁体   English

为什么Processing.py会跳过我的数组的倒数第二项?

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

I've been making a sort of abstract-art style thing in Processing. 我一直在加工中制作一种抽象艺术风格的东西。 Left click is place a point, click again and it'll make a random line, right click to make a new point. 左键单击放置一个点,再次单击它将生成一个随机行,右键单击以创建一个新点。

The problem comes in when I use the up and down arrows to pick a pen color. 当我使用向上和向下箭头选择笔颜色时,会出现问题。 The second to last item (either black or pink) is skipped when I use these keys. 当我使用这些键时,跳过倒数第二个项目(黑色或粉红色)。 Code is attached. 代码附后。

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

Your first if in each case affects your second. 你的第一个if在每一种情况下会影响你的第二个。 For UP , if selector is 6, it becomes 7, and then matches selector == 7 ; 对于UP ,如果selector为6,则变为7,然后匹配selector == 7 ; for DOWN , if selector is 1, it becomes 0, and then matches selector == 0 . 对于DOWN ,如果selector为1,则它变为0,然后匹配selector == 0

Use elif to make them exclusive: 使用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

and your first condition should probably be if selector < 7 rather than 8 . 你的第一个条件可能是if selector < 7而不是8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM