Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
moving objects
#1
hey i need help in my code to make an object move where i click with the mouse, please
Reply
#2
For start, post your code (in Python code tags).
Reply
#3
from graphics import*
from math import sin, cos, radians
class Balcao:
    def __init__(self, window, p1):
        self.mesa = Circle(p1, 100)
        self.mesa.setFill('brown')
        self.chao = Circle(p1, 50)
        self.chao.setFill('white')
        self.mesa.draw(window)
        self.chao.draw(window)
        self.balcao = [self.mesa, self.chao]
        
class Mesa:
    def __init__(self, window, p2, p3):
        self.mesa = Rectangle(p2, p3)
        self.mesa.setFill('black')
        self.mesa.draw(window)
        self.table = [self.mesa]
        

        
class Robot:
    def __init__(self, window, center, radius):
        self.tola = Circle(center, radius)
        self.tola.setFill('purple')
        self.tola.draw(window)
        
def mesas(win):
    mesa1 = Mesa(win, Point(640, 445), Point(580, 385))
    mesa2 = Mesa(win, Point(640, 260), Point(580, 320))
    mesa3 = Mesa(win, Point(640, 135), Point(580, 195))
    mesa4 = Mesa(win, Point(540, 60), Point(600, 0))
    mesa5 = Mesa(win, Point(415, 60), Point(475, 0))
    mesa6 = Mesa(win, Point(290, 60), Point(350, 0))
    mesa7 = Mesa(win, Point(165, 60), Point(225, 0))
    mesa8 = Mesa(win, Point(40, 60), Point(100, 0))
    mesa11 = Mesa(win, Point(0, 445), Point(60, 385))
    mesa10 = Mesa(win, Point(0, 260), Point(60, 320))
    mesa9 = Mesa(win, Point(0, 135), Point(60, 195))
    mesa12 = Mesa(win, Point(290, 210), Point(350, 270))
    
class Docstation:
    def _init_(self, win):
        self.ds = Rectangle(Point(100, 480), Point(200, 470))
        self.ds.setFill('green')
        self.ds.draw(window)
    
    
class Robot:
    def __init__(self, window, center, size):
        handSize = .15* size
        handOff = size
        handSize = size *.15
        handOff = size 
        self.head = Circle(center, size)
        self.head.draw(window)
        self.leftHand = Circle(center, handSize)
        self.leftHand.move(-handOff, -handOff)
        self.rightHand = Circle(center, handSize)
        self.rightHand.move(handOff, -handOff)
        self.leftHand.draw(window)
        self.rightHand.draw(window)
        self.cara = [self.head, self.rightHand, self.leftHand]
        
        
        
    def movrobot(self, dx, dy):
       # for i in range(1000):
            
            
                for i in range(80):
                    #x = self.window.getMouse()
                    #self.head.move()
                    #if self.head.getCenter().getX()+size<=100:
                        dy = 0
                        dx = 1
                for i in range(40):
                    dx=0
                    dy=-1
                    if self.head.getCenter().getY()-size>=415:
                        dx = 0
                        dy = -1
                    
                    self.cara[x].move(dx, dy)
                    update(300)
                    
        
         
   
            
            
def main():
    win = GraphWin("cafe central", 640, 480)
    balc1 = Balcao (win, Point(320, 480))  
    mesas(win)
  
    
    robot = Robot(win, Point(320, 360), 15)
    ponto=win.getMouse()
    x,y=ponto.getX(),ponto.getY()
    movrobot()
    win.getMouse()
    win.close()
main()
Reply
#4
j.crater asked you to use python and output tags when posting code and results. I put them in for you this time. Here are instructions for doing it yourself next time.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
x does not change, nor does dx or dy (in statement below). Create a simple example that you can test with first. The problem here is not that you can't move the object, the problem is that you have 100+ lines of code that have not been tested.

                    self.cara[x].move(dx, dy) 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Moving with objects simultaneously kom2 1 2,966 Apr-20-2019, 07:12 PM
Last Post: SheeppOSU

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020