If you have several elements (objects) on the screen which should stay in a relationship with each other (one is above the other, one is to the right of the other), it can be a pain to move them on the screen. If each object has its own Point or Points for its location, you have to change each one by hand and it's hard to maintain the relationship exactly.
Instead, create a couple variables which act as an "anchor Point" for all the objects.
circle1 = Circle (Point(250, 250), 150) circle1.draw(win) circle2 = Circle (Point(250, 250), 75) circle2.draw(win) circle3 = Circle (Point(250, 250), 25) circle3.draw(win)These three circles are all centered on the same point. If you want to move them to somewhere else on the screen, you have to edit all three constructor lines. Instead,
centerPoint = Point(250, 250) circle1 = Circle (centerPoint, 150) circle1.draw(win) circle2 = Circle (centerPoint, 75) circle2.draw(win) circle3 = Circle (centerPoint, 25) circle3.draw(win)and you only have to change the coordinates one time.
two boxes of different sizes which are aligned at their bottoms
r1 = Rectangle(Point(100, 200), Point(150, 170)) r1.draw(win) r2 = Rectangle(Point(50, 200), Point(100, 100)) r2.draw(win)Instead do:
bottomY = 200 r1 = Rectangle(Point(100, bottomY), Point(150, 170)) r1.draw(win) r2 = Rectangle(Point(50, bottomY), Point(100, 100)) r2.draw(win)
If you need to display one of two messages to the user, don't create TWO objects, one with each message in it. If you do it this way, you will have to use another if statement later when you UNdraw the object - to find out which one you drew.
Instead, create one Text object, put one message into it as default and then when you have decided that the other message needs to be displayed, use setText to change the text in the object. The object still has the same name, and can be undrawn with one statement.
msg = Text(Point(150, 200), "message1") if points < p: msg.setText("message2") # later msg.undraw()
If you have an object which needs different settings based on some variable, do not do the complete create/set/draw process in each branch of the if. Instead, factor out the common code and do it before or after the if statement. The if should only concern the part that is different.
if score > 0: win_msg = Text(Point(100, 200), "You won!") win_msg.setFill("hot pink") win_msg.draw(win) else: lose_msg = Text(Point(100, 200), "You lose!") lose_msg.setFill("dark blue") lose_msg.draw(win)Instead of that, do this
msg = Text(Point(100, 200), "You won!") if score > 0: msg.setFill("hot pink") else: msg.setText("You lose!") msg.setFill("dark blue") msg.draw(win)