如何在 pyprocessing 中正确绘制框?

How correctly draw boxes in pyprocessing?

我正在尝试使用 pyprocessing 编写一个非常基本的框绘图程序, 但是当逻辑看起来正常时,检查鼠标是否在框内的条件失败:

#!/usr/bin/env python
from pyprocessing import *

S = 20
W = 5
H = 5

data = [[0] * W] * H

def setup():
    size(W*(S+5),H*(S+5))

def draw():
    background(0)
    for y in xrange(H):
        for x in xrange(W):
            fill(data[x][y] * 255)
            rect(x*S,y*S,S,S)

def mouseDragged():
    for y in xrange(H):
        for x in xrange(W):
            xs = x * S
            ys = y * S
            # this doesn't behave as expected: it should draw a single box if the condition is met, not the whole row
            if (mouse.x >= xs) and (mouse.x <= (xs+S)) and (mouse.y >= ys and mouse.y <= (ys+S)):
                if key.pressed:
                    data[x][y] = 0
                else:   
                    data[x][y] = 1

run()

我已经使用 Processing 的 Java 版本尝试了相同的方法,它按预期工作:

int S = 20;
int W = 5;
int H = 5;

int[][] data = new int[W][H];

void setup(){
  size(100,100);
  noStroke();
}

void draw(){
  background(0);
  for (int y = 0 ; y < H; y++){
    for (int x = 0 ; x < W; x++){
      fill(data[x][y] * 255);
      rect(x*S,y*S,S,S);
    }
  }
}
void mouseDragged(){
  for (int y = 0 ; y < H; y++){
    for (int x = 0 ; x < W; x++){
      int xs = x * S;
      int ys = y * S;
      if ((mouseX > xs) && (mouseX < (xs+S)) && (mouseY >= ys && mouseY <= (ys+S))){
        data[x][y] = 1;
      }
    }
  }
}

JS 中的类似行为:

var S = 20;
var W = 5;
var H = 5;

var data = new Array(W);

function setup(){
  createCanvas(100,100);
  noStroke();
  for (var i = 0 ; i < H; i++) data[i] = [0,0,0,0,0];
  
}

function draw(){
  background(0);
  for (var y = 0 ; y < H; y++){
    for (var x = 0 ; x < W; x++){
      fill(data[x][y] * 255);
      rect(x*S,y*S,S,S);
    }
  }
}
function mouseDragged(){
  for (var y = 0 ; y < H; y++){
    for (var x = 0 ; x < W; x++){
      var xs = x * S;
      var ys = y * S;
      if ((mouseX > xs) && (mouseX < (xs+S)) && (mouseY >= ys && mouseY <= (ys+S))){
        data[x][y] = 1;
      }
    }
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.4.23/p5.min.js"></script>

我在 Python 中是否正确地写了框边界条件?如果是这样, pyprocessing 是否存在错误?我怎样才能克服它?

我正在使用 pyprocessing.version '0.1.3.22'

试图偷懒是问题所在:

data = [[0] * W] * H

这不是简单地创建一个嵌套数组,它复制了第一个数组 ([0]) 的 references,所以当我修改一行中的一个值时, 整行被修改。

由于我对 python 非常有经验,所以我以一种可能非 pythonic 的方式初始化了数组:

data = []
for y in xrange(H):
    data.append([])
    for x in xrange(W):
        data[y].append(0)

所以完整的工作代码是:

#!/usr/bin/env python
from pyprocessing import *

S = 20
W = 5
H = 5

# data = [[0] * W] * H #trouble
data = []
for y in xrange(H):
    data.append([])
    for x in xrange(W):
        data[y].append(0)

def setup():
    size(W*(S),H*(S))

def draw():
    background(0)
    for y in xrange(H):
        for x in xrange(W):
            fill(data[x][y] * 255)
            rect(x*S,y*S,S,S)

def mouseDragged():
    for y in xrange(H):
        for x in xrange(W):
            xs = x * S
            ys = y * S
            if (mouse.x >= xs) and (mouse.x <= (xs+S)) and (mouse.y >= ys and mouse.y <= (ys+S)):
                if key.pressed:
                    data[x][y] = 0
                else:   
                    data[x][y] = 1

run()