0

我正在尝试使用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()

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

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'

4

1 回答 1

1

试图偷懒是问题所在:

data = [[0] * W] * H

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

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

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()
于 2016-03-22T17:38:10.467 回答