Flashing when re-drawing?

edited August 2017 in Python Mode

I am creating a small maze game. Right now I have the very basics of it. I have a rectangle that will move around the screen, and a wall around the window. The only problem is that every time the rectangle moves, the walls also get redrawn and it flashes.

Do I need to put the walls in a separate function? What would be the best way around this? Loop or no loop?

Here is the code :

add_library("minim")

x1=100
x2=25
y1=20
y2=25

def setup():
    global sf
    size(200, 200)
    background(255) # white
    minim=Minim(this)
    sf=minim.loadFile("light ping.mp3", 2048)

def draw():
    global x1,y1,x2,y2
    fill(155)
    stroke(1)
    rect(x1, y1, x2, y2)
    noStroke()

    rect(0, 0, 5, height-30)
    rect(0, height-30, width, 5)
    rect(width-5, 0, 5, height-30)
    rect(0,0, width, 5)


def keyReleased():

    global x1, y1, x2, y2
    rect(x1, y1, x2, y2)
    if (key == CODED):
        if (keyCode == LEFT) and x1>=1:
            x1=x1-10
            print(x1)
            background(255)
            rect(x1, y1, x2, y2)


        elif (keyCode == RIGHT) and x1<=169:
            x1=x1+10
            print(x1)
            background(255)
            rect(x1,y1,x2,y2)

        elif (keyCode == UP) and y1>=1:
            y1=y1-10
            print(y1)
            background(255)
            rect(x1,y1,x2,y2)


        elif (keyCode == DOWN) and y1<=169:
            y1=y1+10
            print(y1)
            background(255)
            rect(x1,y1,x2,y2)
        else:
            sf.play()
            sf.rewind()
Tagged:

Answers

  • edited August 2017 Answer ✓

    Never mind, I figured it out. I need to put the background(255) in the DRAW function instead and take it out of the keyReleased.

Sign In or Register to comment.