user3011942
user3011942

Reputation: 5

Making rectangle move across screen with graphics file

from graphics import *

def main():
    win = GraphWin("Polygon", 500, 500)
    r= Rectangle(Point(10,500),Point(150,450))
    r.draw(win)
    while r.getP1()<=450 is False:
        rectMaker(r)
        time.sleep(1)

def rectMaker(r):
    r.undraw(win)
    r=Rectange(Point(r.getP1()+1),(Point(r.getP2()+1)))
    r.draw(win)
    return r




#win.getMouse()


main()

Why is my rectangle not moving anyone see anything? And I don't think I'm suppose to use sleep in case anyone was going to suggest that.

Any help is appreciated please and thank you

Upvotes: 0

Views: 769

Answers (1)

Vikram Saran
Vikram Saran

Reputation: 1143

It looks like you are having an odd interaction between a Point and an Integer.

Namely this line here:

r=Rectange(Point(r.getP1()+1),(Point(r.getP2()+1)))

You are trying to add 1 to a Point.

This is likely not doing what you expect it to - try re-writing this so that you get the original P1/P2 values and then increment the specific x/y as necessary.

Additionally, Rectangle is spelt incorrectly, you should probably be creating a new Rectangle and returning it rather than overriding the existing one (may also cause issues), and you could've tried debugging this by adding a print(r.getP1() + " " + r.getP2()) before and after assignment.

Another addendum: Your functions should ideally only have one purpose. rectMaker seems to not just make rectangles, but re-draw them, try to change an existing rectangle and then return it. The usual style for games is something like

def main():
     load()
     while(playing):
         input()
         update()
         draw()
     unload()

Upvotes: 2

Related Questions