Orange Juice
Orange Juice

Reputation: 11

How to get my program to read third if statement

I am trying to get my program to move a "sushi plate" clockwise in a rectangular fashion although it won't seem to continue moving after my third if statement:

from graphics import * 
win =GraphWin("Draw and Animate",640,480)
win.setBackground("wheat1")

#Right
xmove = 1
ymove = 0
#Down
xmove2 = 0
ymove2 = 1
#Left
xmove3 = -1
ymove3 = 0
#Up
xmove4 = 0
ymove4 = -1

sushiplate = Rectangle(Point(300,160),Point(340,180))
sushiplate.setFill("white")
sushiplate.draw(win)

leftsushi = Circle(Point(310,170), 5)
leftsushi.setOutline("Green")
leftsushi.setFill("orange")
leftsushi.draw(win)

rightsushi = Circle(Point(330,170), 5)
rightsushi.setOutline("Green")
rightsushi.setFill("orange")
rightsushi.draw(win)

while(True):
    #Move sushi

    sushiplate.move(xmove4, ymove4)
    leftsushi.move(xmove4, ymove4)
    rightsushi.move(xmove4, ymove4)
    sushicenter = sushiplate.getCenter()
    if (sushicenter.getY()<=130):
        xmove4 = xmove
        ymove4 = ymove 
    if (sushicenter.getX()>=510):
        xmove = xmove2
        ymove = ymove2
    if (sushicenter.getY()>=360):
        xmove2 = xmove3
        ymove2 = ymove3


    update(60)

My sushi plate will move up, right, down but will continue moving down and my third if statement will not run:

if (sushicenter.getY()>=360):
    xmove2 = xmove3
    ymove2 = ymove3

Upvotes: 0

Views: 49

Answers (3)

cdlane
cdlane

Reputation: 41895

The problem is that once a condition like this triggers:

 if (sushicenter.getX()>=510):

It continues to trigger every time as the result is a change to the Y direction and the object is fixed in the X direction. Cases after this one never trigger. We can fix this by first "testing the waters" to see if the move might trigger a change and then make the movements afterward. My rewrite of your code below does this but also tries to get rid of numbers in favor of symbols as all the numbered variables were confusing, even to you given the bugs in the code:

from graphics import *

WIDTH, HEIGHT = 640, 480
BORDER = 130
RADIUS = 5
DIAMETER = RADIUS * 2

# Right
XMOVE_RIGHT = 1
YMOVE_RIGHT = 0
# Down
XMOVE_DOWN = 0
YMOVE_DOWN = 1
# Left
XMOVE_LEFT = -1
YMOVE_LEFT = 0
# Up
XMOVE_UP = 0
YMOVE_UP = -1

win = GraphWin("Draw and Animate", WIDTH, HEIGHT)
win.setBackground('wheat1')

sushiplate = Rectangle(Point(BORDER - DIAMETER * 2, BORDER - DIAMETER), Point(BORDER + DIAMETER * 2, BORDER + DIAMETER))
sushiplate.setFill('white')
sushiplate.draw(win)

leftsushi = Circle(Point(BORDER - DIAMETER, BORDER), RADIUS)
leftsushi.setOutline('green')
leftsushi.setFill('orange')
leftsushi.draw(win)

rightsushi = Circle(Point(BORDER + DIAMETER, BORDER), RADIUS)
rightsushi.setOutline('green')
rightsushi.setFill('orange')
rightsushi.draw(win)

xmove = XMOVE_RIGHT
ymove = YMOVE_RIGHT

while True:
    # Move sushi

    center = sushiplate.getCenter()
    x, y = center.getX() + xmove, center.getY() + ymove

    if x > WIDTH - BORDER:
        xmove, ymove = XMOVE_DOWN, YMOVE_DOWN
    elif y > HEIGHT - BORDER:
        xmove, ymove = XMOVE_LEFT, YMOVE_LEFT
    elif x < BORDER:
        xmove, ymove = XMOVE_UP, YMOVE_UP
    elif y < BORDER:
        xmove, ymove = XMOVE_RIGHT, YMOVE_RIGHT

    for meal_object in (sushiplate, leftsushi, rightsushi):
        meal_object.move(xmove, ymove)

    update(60)

Upvotes: 0

razdi
razdi

Reputation: 1440

These conditions should be at the same level and not nested. Since the second level if checks sushicenter.getY() <= 130, then your fourth level condition can only be triggered if getY() is both >=360 and <=130 and that can never happen. I think you meant to keep them at the same level so they can be independently triggered and not depend on the other conditions.

from graphics import * 
win =GraphWin("Draw and Animate",640,480)
win.setBackground("wheat1")

#Right
xmove = 1
ymove = 0
#Down
xmove2 = 0
ymove2 = 1
#Left
xmove3 = -1
ymove3 = 0
#Up
xmove4 = 0
ymove4 = -1

sushiplate = Rectangle(Point(300,160),Point(340,180))
sushiplate.setFill("white")
sushiplate.draw(win)

leftsushi = Circle(Point(310,170), 5)
leftsushi.setOutline("Green")
leftsushi.setFill("orange")
leftsushi.draw(win)

rightsushi = Circle(Point(330,170), 5)
rightsushi.setOutline("Green")
rightsushi.setFill("orange")
rightsushi.draw(win)

while(True):
    #Move sushi

    sushiplate.move(xmove4, ymove4)
    leftsushi.move(xmove4, ymove4)
    rightsushi.move(xmove4, ymove4)
    sushicenter = sushiplate.getCenter()
    if (sushicenter.getY()<=130):
        xmove4 = xmove
        ymove4 = ymove 
    if (sushicenter.getX()>=490):
        xmove = xmove2
        ymove = ymove2
    if (sushicenter.getY()>=360):
        xmove2 = xmove3
        ymove2 = ymove3
    if(sushicenter.getX()<=120):
        xmove3 = xmove4
        ymove3 = ymove4

    update(60)

Upvotes: 1

John Gordon
John Gordon

Reputation: 33335

You're already under the if (sushicenter.getY()<=130): branch. sushicenter.getY()>=360 can't possibly be true in that case.

Upvotes: 0

Related Questions