Python Forum
Thread Rating:
  • 3 Vote(s) - 3.67 Average
  • 1
  • 2
  • 3
  • 4
  • 5
make it to move to mouse
#1
import tkinter,random,time
class game():
	def __init__(self):

		self.canvas= tkinter.Canvas(
			width=500,
			height=700,
			bg='white'
			)
		self.canvas.pack()

		self.x=250
		self.y=400
		self.interval=0.01

		self.z=800

		self.x1=random.randint(0, 500)
		self.y1=random.randint(0, 800)
		self.r=random.randint(5, 16)

		self.canvas.bind_all('<Left>',self.moveleft)
		self.canvas.bind_all('<Right>',self.moveright)
		self.canvas.bind_all('<Up>',self.moveup)
		self.canvas.bind_all('<Down>',self.movedown)

		self.canvas.create_oval(self.x-7,self.y+300,self.x+7,self.y+270,tags='plane')
		self.canvas.create_oval(self.x-14,self.y+280,self.x+14,self.y+290,tags='plane')

		self.obstacles()

		self.cycle()
		self.canvas.mainloop()

	def obstacles(self):

		self.x2=random.randint(0, 500)
		self.y2=random.randint(0, 800)
		self.r1=random.randint(5, 16)
		self.canvas.create_rectangle(self.x2-self.r1,self.y2-self.z-self.r1,self.x2+self.r1,self.y2-self.z+self.r1,fill='red',tags='obs')
		self.canvas.after(200,self.obstacles)

	def cycle (self) :
		while True:
			time.sleep(self.interval)
			self.canvas.move('obs', 0, +2)

			tagged_objects = self.canvas.find_withtag('obs')
			overlapping_objects = self.canvas.find_overlapping(*self.canvas.coords('plane'))
			
			for item in overlapping_objects:
				if item in tagged_objects:
					return False

			self.canvas.update()

	def moveleft(self,event):
		self.canvas.move('plane', -15, 0)
	def moveright(self,event):
		self.canvas.move('plane', 15, 0)
	def movedown(self,event):
		self.canvas.move('plane', 0, 15)
	def moveup(self,event):
		self.canvas.move('plane', 0, -15)
game()
can u remake this so it will move to my mouse?
Reply
#2
Im not sure why people insist on making games in tkinter when pygame was made specifically for things like this?

here is a pygame example in a tutorial format
https://python-forum.io/Thread-PyGame-En...ion-part-6

But you basically need to create a vector between the object moving and the target. And then start moving the object. In something like pygame this is easier as there are built in functions like pygame.mouse.get_pos() that returns the mouse position on every frame.

Quote:can u remake this so it will move to my mouse?
That would be your job, especially since you chose tkinter. But you have to make the effort
Recommended Tutorials:
Reply
#3
Metulburr is right, tkinter is the wrong tool.

This is wrong:
           #for item in overlapping_objects:
           #    if item in tagged_objects:
           #        return False
Reply
#4
yeah i know doing it like this is wrong but I'm doing it for class so i need it to be it in python and not in pygame.
Reply
#5
tkinter is not apart of the python standard library either. It is packaged alongside python in windows only.

But that makes sense....if your doing it for a class, there is no choice then.
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  mouse move event/cinfiguration ttk/python janeik 4 1,077 Jul-03-2023, 05:30 PM
Last Post: deanhystad
  shutil.move make data corrupt kucingkembar 0 801 Feb-01-2023, 01:30 PM
Last Post: kucingkembar
  Move mouse and click in particular position biprabu 3 2,500 Sep-01-2020, 08:23 PM
Last Post: deanhystad
  Change mouse move speed in guibot script rulltartan 1 2,733 Mar-30-2020, 01:51 PM
Last Post: pevogam
  mouse 0.7.0 - mouse polling hate 125-1000hz penahuse 1 2,536 Dec-06-2019, 09:51 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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