简体   繁体   English

Processing.py 空白窗口问题

[英]Processing.py blank window issue

I tried writing an A* alg using processing.py but I am having an issue with the beginning of the code: my window is totally blank我尝试使用 processing.py 编写 A* alg,但代码开头有问题:我的窗口完全空白

So, I want a grid to appear waiting for the user to click on a cell and then to fill that cell with a black rectangle.所以,我希望出现一个网格,等待用户单击一个单元格,然后用黑色矩形填充该单元格。 But, I only want this to run at the beginning of my code so I didn't put it in the draw function.但是,我只希望它在我的代码的开头运行,所以我没有把它放在 draw 函数中。

Here is my code:这是我的代码:

taille = 400
pas = taille // 20

def setup():
    size(taille, taille)
    background(255, 255, 255)
    stroke(0)
    strokeWeight(2)
    frameRate(20)
    for i in range(pas, taille, pas):
        line(i, 0, i, taille)
        line(0, i, taille, i)
    drawRect()

def drawRect():    
    x, y = pressed()
    for i in range(1, taille // pas - 1):
        for j in range(1, taille // pas - 1):
            if i * pas <= x and x <= (i + 1) * pas:
                if j * pas <= y and y <= (j + 1) * pas:
                    rect(i * pas, j * pas, pas, pas)

def pressed():
    while True:
        if mousePressed:
            return (mouseX, mouseY)        

I highly suspect that the error is coming from the drawRect function since I managed to display the grid before adding it.我高度怀疑错误来自 drawRect 函数,因为我在添加网格之前设法显示了它。

So, I want a grid to appear waiting for the user to click on a cell and then to fill that cell with a black rectangle.所以,我希望出现一个网格,等待用户单击一个单元格,然后用黑色矩形填充该单元格。 But, I only want this to run at the beginning of my code so I didn't put it in the draw function.但是,我只希望它在我的代码的开头运行,所以我没有把它放在 draw 函数中。

Anyway I recommend to use a draw function, to continuously draw the scene dependent on the current states of your program.无论如何,我建议使用draw功能,根据程序的当前状态连续绘制场景。

Note, your program hangs in an endless loop.请注意,您的程序会无限循环。 The variables mousePressed , mouseX and mouseY are never updated.变量mousePressedmouseXmouseY永远不会更新。 This variables don't change their state magically.这个变量不会神奇地改变它们的状态。 They change their state between 2 frames, after the draw function has been executed Processing does the event handling and changes the built in variables.它们在 2 帧之间更改状态,在执行draw函数后, Processing会进行事件处理并更改内置变量。 You don't give Processing any chance to do this job.您不会给Processing任何机会来完成这项工作。

Create to variables, which notice the x and y window coordinate of the "click":创建变量,注意“点击”的 x 和 y 窗口坐标:

enter_x = -1
enter_y = -1

Implement the mousePressed event to receive the "click":实现mousePressed事件以接收“点击”:

def mousePressed():
global enter_x, enter_y
if enter_x < 0 or enter_y < 0:
    enter_x = mouseX
    enter_y = mouseY 

Draw the black rectangle to the if the "click" coordinates are valid ( >= 0 ) in the draw function :如果“单击”坐标在draw function中有效( >= 0 ),则将黑色矩形绘制到:

def draw():   
    global enter_x, enter_y

    if enter_x >= 0 and enter_y >= 0:
        stroke(0)  
        fill(0)
        ix = enter_x // pas
        iy = enter_y // pas
        rect(ix * pas, iy * pas, pas, pas)

The full code may look like this:完整的代码可能如下所示:

taille = 400
pas = taille // 20

def setup():
    size(taille, taille)
    background(255, 255, 255)
    stroke(0)
    strokeWeight(2)
    frameRate(20)
    for i in range(pas, taille, pas):
        line(i, 0, i, taille) 
        line(0, i, taille, i)

enter_x = -1
enter_y = -1

def mousePressed():
    global enter_x, enter_y
    if enter_x < 0 or enter_y < 0:
        enter_x = mouseX
        enter_y = mouseY    

def draw():   
    global enter_x, enter_y

    if enter_x >= 0 and enter_y >= 0:
        stroke(0)  
        fill(0)
        ix = enter_x // pas
        iy = enter_y // pas
        rect(ix * pas, iy * pas, pas, pas)

Note, it may be necessary to draw the grid in the draw function too.请注意,也可能需要在draw函数中绘制网格。 In general it is better to redraw the scene every frame, than "undoing" something what has been draw.一般来说,最好每帧重新绘制场景,而不是“撤消”已绘制的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM