如何在pyprocessing中正确绘制框?

时间:2016-03-18 23:52:28

标签: python 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'

1 个答案:

答案 0 :(得分:1)

试图变得懒惰是问题:

data = [[0] * W] * H

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

由于我对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()