处理中java到python的转换

标签 java python-3.x processing

walker w;

void setup(){
  size(800,600);

  w=new walker();
  background(255);
}

void draw(){
  w.step();
  w.render();
}



  class walker{
  int x,y;

  walker(){
    x=width/2;
    y=height/2;
  }

  void render(){
    stroke(0);
    point(x,y);
  }

  void step(){
    int choice = int(random(4));
    if (choice==0){
      x++;
    } else if(choice==2){
      y++;
    } else if (choice==1){
      x--;
    }else{
      y--;
    }
    x=constrain(x,0,width-1);
    y=constrain(y,0,height-1);
  }
}

如何将此 java 代码转换为 python 。 这段代码是随机游走算法以及如何将此代码转换为Python。 这是在处理编辑器上完成的。 谁能隐藏此代码

最佳答案

One way to convert it is to employ someone to do the conversion work for you.

我感觉自己受雇了:

def setup ():
    global w
    size(800, 600)
    w = walker()
    background(255)

def draw ():
    w.step()
    w.render()

class walker:
    def __init__(self):
        self.x = width // 2
        self.y = height // 2 

    def render(self):
        stroke(0);
        point(self.x, self.y); 

    def step(self):
        choice = int(random(4))
        if choice == 0:
            self.x += 1
        elif choice == 2:
            self.y += 1
        elif choice == 1:
            self.x -= 1
        else:
            self.y -= 1
        self.x = constrain(self.x, 0, width-1);
        self.y = constrain(self.y, 0, height-1);

关于处理中java到python的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60813096/

相关文章:

java - Android 加载远程图像

python-3.x - 如何停止重定向并停留在页面上以抓取数据(python3、beautifulsoup、urllib)

Python - 图像处理和光学字符阅读器

java - 使用网络处理控制 arduino (javascript)

java - 如何让动画从屏幕上消失并再次出现

java - JTable:根据外部输入更改列

java - 具有 Synthetica Aluoxy 外观和感觉的 jxdatepicker 无法正常工作

python - 非成员运算符重载(特别是运算符==)在 Cython 中是否被破坏?

opengl - 处理和 OpenGL - 改变相机位置?

java - 发布不可变