gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:41:30 24 4
gpt4 key购买 nike

我一直在处理中制作一种抽象艺术风格的东西。左键单击放置一个点,再次单击会生成一条随机线,右键单击会生成一个新点。

当我使用向上和向下箭头选择笔颜色时,问题就出现了。当我使用这些键时,会跳过倒数第二个项目(黑色或粉色)。代码附后。

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
<b>el</b>if selector == 7:
fill(fillcolors[selector])
selector = 0
if selector > 0:
fill(fillcolors[selector])
selector -= 1
<b>el</b>if selector == 0:
fill(fillcolors[selector])
selector = 7

你的第一个条件可能应该是 if selector < 7而不是8 .

关于python - 为什么Processing.py 跳过数组的倒数第二项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42521993/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com