Mouse click movements? [UPDATED] - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Mouse click movements? [UPDATED] (/thread-9287.html) |
Mouse click movements? [UPDATED] - minnaadel - Mar-31-2018 I added the full code in case anyone wanted to see it. Hi there, I'm having a HUGE issue figuring out how to make my object move left,right,up,down depending on if it the row or column it is clicked in. I can kind of get it to work but sometimes the movements start making no sense when I click. The goal is to get the green square to the red square, so if you click in the row it either moves right or left, and if you click in the column it moves up or down. clicks INSIDE the square are ignored and clicks ANYWHERE ELSE are also ignored. Please any tips or advice are suggested I"ve been trying to figure this out forever. I'm really new to programming and python so I'm struggling. I'm adding a picture of what the grid looks like and the code that I have so far for this animation part.. and the code i have so far is follows: while True: userclick =field.getMouse() if (userclick.getY()< pete.getCenter().getY()) and ((userclick.getX() > cmin) and (userclick.getX() < cmax)): dx=0 dy = -1 pete.move(dx,dy) else: dx=0 dy=0 pete.move(dx,dy) #clicking above the y val in column will move it if (userclick.getY()> pete.getCenter().getY()) and ((userclick.getX() > cmin) and (userclick.getX() < cmax)): dx=0 dy = +1 pete.move(dx,dy) else: dx=0 dy=0 pete.move(dx,dy) #if you click to the left of the square it'll move the square 1 to the right if (userclick.getX()< pete.getCenter().getX()) and ((userclick.getY() > rmin) and (userclick.getY() < rmax)): dx = -1 dy=0 pete.move(dx,dy) else: dx=0 dy=0 pete.move(dx,dy) #if you click to the right of the square it'll move the square 1 to the left if userclick.getX()> pete.getCenter().getX() and ((userclick.getY() > rmin) and (userclick.getY() < rmax)): dx = +1 dy = 0 pete.move(dx,dy) else: dx=0 dy=0 pete.move(dx,dy)FULL CODE #BoilerMazer is a game to celebrate the 150th anniversay of Purdue. #The aim of the game is for players to complete a maze , but only #with the least number of moves possible, and crossing the least number of #trip wires. BoilerMazer keeps track of the names, moves, and scores. Then #will also display the top players and scores. from graphics import * #creating the game panel window def panel(): #grey window, with coordinates flipped, with banners etc win = GraphWin("Start Panel", 300,200) win.setCoords(0,0,300,200) win.setBackground("light grey") #drawing the BoilerMazer banner with text boilermazer = Rectangle(Point(0,200),Point(300,160)) boilermazer.setFill("white") boilermazer.draw(win) #text inside banner1 = Text(Point(150,180),"BoilerMazer") banner1.setStyle("bold") banner1.setSize(20) banner1.draw(win) #initial game panel is going to have two buttons and a top scores object #top score "screen" toprec = Rectangle(Point(60,140),Point(240,50)) toprec.setFill("white") toprec.draw(win) #text inside toprec topscores = Text(Point(150,130),"TOP SCORES") topscores.setSize(8) topscores.draw(win) border = Text(Point(150,120),"======") border.draw(win) bigmac = Text(Point(150,110),"Big Mac 21") bigmac.setSize(8) bigmac.draw(win) tt = Text(Point(150,90),"T.T 23") tt.setSize(8) tt.draw(win) cshell = Text(Point(150,75),"C-Shell 25") cshell.setSize(8) cshell.draw(win) macmac = Text(Point(150,55),"MacMac 27") macmac.setSize(8) macmac.draw(win) #new player button that will eventually be clicked new1 = Point(90,35) new2 = Point(210,0) newrec = Rectangle(new1,new2) newrec.setFill("chartreuse2") newrec.draw(win) #new player button text newplayer = Text(Point(150,18),"NEW PLAYER") newplayer.draw(win) #reset button Exitrec = Rectangle(Point(240,35),Point(300,0)) Exitrec.setFill("red") Exitrec.draw(win) #resettext Exit = Text(Point(270,18),"EXIT") Exit.draw(win) #setting up the checkmouse, when a mouse is clicked inbetween the coordinates newplayer will be undrawn while True: clicknew = win.getMouse() if (clicknew.getX()>90 and clicknew.getX()<210) and (clicknew.getY()<35 and clicknew.getY() > 0): newplayer.undraw() toprec.undraw() topscores.undraw() border.undraw() bigmac.undraw() tt.undraw() cshell.undraw() macmac.undraw() break #game panel after clicknew player #playername text playername = Text(Point(90,120),"Player Name: ") playername.setSize(12) playername.setStyle("bold") playername.draw(win) #creating the entry box with white fill and no initial text playerbox = Entry(Point( 200,120),10) playerbox.setFill("white") playerbox.draw(win) #replacing the new player button with start button start = Text(Point(150,18),"START!!") start.draw(win) #creating a list that we will add gamer names to #gamer name is an empty list gamernames = [] #have to add in getmouse bc the entrybox waits for a click win.getMouse() gamer = playerbox.getText() #adding gamer to gamer names list gamernames.append(gamer) while True: startclick = win.checkMouse() if (clicknew.getX()>90 and clicknew.getX()<210) and (clicknew.getY()<35 and clicknew.getY() > 0) and gamernames != []: start.undraw() break #creating the third and final panel #redrawing the NEW PLAYER BUTTON newplayer2 = Text(Point(150,18),"NEW PLAYER") newplayer.draw(win) #need to add on a reset button on the right side resetbox = Rectangle(Point(0,35), Point(60,0)) resetbox.setFill("yellow") resetbox.draw(win) #adding in the reset text box retext = Text(Point(30,18),"RESET") retext.draw(win) #removing the entrybox playerbox.undraw() #showing the name that was enter in the box playername3 = Text(Point(200,120), gamernames) playername3.draw(win) #adding in the score text beneath player name scoretext = Text(Point(90,90),"Score: ") scoretext.setStyle("bold") scoretext.draw(win) #creating the field function def thefield(): #graphics window with a white background and the coordinates flipped field = GraphWin("Field", 400,400) field.setBackground("white") field.setCoords(0,0,10,10) #setting up the grid on the page #grid will be 10x10 for i in range(10): Line(Point(0,i),Point(10,i)).draw(field).setFill("light grey") for x in range(10): Line(Point(x,0),Point(x,10)).draw(field).setFill("light grey") #drawing 40b40 green rectangle with with light grey outline #fill will be green greenrec = Rectangle(Point(0,10),Point(1,9)) greenrec.setFill("green") greenrec.setOutline("light grey") greenrec.draw(field) #drawing a red rectangle with light grey outline redrec = Rectangle(Point(9,1),Point(10,0)) redrec.setFill("red") redrec.setOutline("light grey") redrec.draw(field) #drawing pete which a 36b36 gold rectangle in the top left grid pete = Rectangle(Point(.05,9.95),Point(.95,9.05)) pete.setOutline("black") pete.setFill("gold") pete.draw(field) return pete,field #creating the function that allows pete to be moved def movingpete(): pete,field = thefield() #width from center to act as a perimeter for colum cmin is lowerbound cmax is upperbound cmin = pete.getCenter().getX() - .5 cmax = pete.getCenter().getX() + .5 #width from center to act as a perimeter for row rmin is lowerbound rmax is upperbound rmin = pete.getCenter().getY() -.5 rmax = pete.getCenter().getY() +.5 #userclick #click below the why value in the column will move it down while True: userclick =field.getMouse() if (userclick.getY()< pete.getCenter().getY()) and ((userclick.getX() > cmin) and (userclick.getX() < cmax)): dx=0 dy = -1 pete.move(dx,dy) else: dx=0 dy=0 pete.move(dx,dy) #clicking above the y val in column will move it if (userclick.getY()> pete.getCenter().getY()) and ((userclick.getX() > cmin) and (userclick.getX() < cmax)): dx=0 dy = +1 pete.move(dx,dy) else: dx=0 dy=0 pete.move(dx,dy) #if you click to the left of the square it'll move the square 1 to the right if (userclick.getX()< pete.getCenter().getX()) and ((userclick.getY() > rmin) and (userclick.getY() < rmax)): dx = -1 dy=0 pete.move(dx,dy) else: dx=0 dy=0 pete.move(dx,dy) #if you click to the right of the square it'll move the square 1 to the left if userclick.getX()> pete.getCenter().getX() and ((userclick.getY() > rmin) and (userclick.getY() < rmax)): dx = +1 dy = 0 pete.move(dx,dy) else: dx=0 dy=0 pete.move(dx,dy) def main(): panel() movingpete() main() RE: Mouse click movements? - buran - Mar-31-2018 You must provide minimal runnable code that reproduce the problem. Don't make us guess what package(s) you use. RE: Mouse click movements? [UPDATED] - minnaadel - Mar-31-2018 @buran I updated it RE: Mouse click movements? [UPDATED] - woooee - Mar-31-2018 No one wants to step through 200+ lines, especially if the program uses a package that no one actually uses, like Graphics. The problem here is that you have 200+ lines of code that has not been tested. So the only thing that I am willing to spend time on is to say to add print statements so you know what is going on. So you can figure out what the move is actually doing, add print statements to the moving_pete() function. The 2 simple print statements below should show you where you are going wrong. Also, the else statements move the object zero x and zero y. This is obviously not necessary and can be deleted. """ NOTE that the right side of some your code was truncated don't let lines go longer than 70 */- characters """ while True: print("\n---------- new mouse click") userclick =field.getMouse() if (userclick.getY()< pete.getCenter().getY()) and ( (userclick.getX() > cmin) and (userclick.getX() < cmax)): print("moving y -1") dx=0 dy = -1 pete.move(dx,dy) ## else: ## dx=0 ## dy=0 ## pete.move(dx,dy) #clicking above the y val in column will move it if (userclick.getY()> pete.getCenter().getY()) and ( (userclick.getX() > cmin) and (userclick.getX() < cmax)): print("moving y +1") dx=0 dy = +1 pete.move(dx,dy) ## else: ## dx=0 ## dy=0 ## pete.move(dx,dy) |