How to Track the Mouse Position in PyGame! (Python GUI Tutorial)

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ธ.ค. 2024

ความคิดเห็น • 41

  • @rethanon
    @rethanon 2 ปีที่แล้ว +8

    Nice short and to the point, good job as usual :)

    • @lemastertech
      @lemastertech  2 ปีที่แล้ว

      Thanks Dale! Tried to put together some useful tutorials ahead of time! Seems like the previous one has gotten a bit of attention as well!

  • @theaslam9758
    @theaslam9758 ปีที่แล้ว +1

    amazing video, I can see this video being lengthened to 8 minutes whilst still being quick useful as heck. I honestly wouldn't mind.

    • @lemastertech
      @lemastertech  ปีที่แล้ว

      Hey thank you for that super nice comment! I felt like I covered what I needed to cover nicely in this time but I definitely have a lot of longer PyGame tutorials on the channel too! 😁

  • @mattsimkins1101
    @mattsimkins1101 ปีที่แล้ว +2

    hey just wondering is there a way to make the circle stop flickering

    • @lemastertech
      @lemastertech  ปีที่แล้ว

      Hi, the circle doesn’t flicker for me - check to make sure it’s not underneath some conditional like over indented, sometimes I leave commands that should run every loop indented under the for event in PyGame.event.get()

  • @vedhriyer
    @vedhriyer ปีที่แล้ว +1

    Very useful and quick because I was trying to make an
    if clicked destroyer game to protect something. Good job

    • @lemastertech
      @lemastertech  ปีที่แล้ว

      Glad you found it useful, that sounds like an awesome game!! Goodluck making it!

  • @Nzargnalphabet
    @Nzargnalphabet 6 หลายเดือนก่อน +1

    I'm actually making a fighting game in which the mouse is used for the directional inputs, first, I wonder if there's a way to track the mouse position even if it's not hovering over the screen, in fact, I really want to get the motion of it instead of the position because then it wouldn't be bound by even the size of the monitor, and it would let me set mouse sensitivity to anything I want, instead of what the computer offers, is there any way to track that raw motion?

    • @lemastertech
      @lemastertech  6 หลายเดือนก่อน

      Yeah basically you would just want an array of the past mouse positions and calculate a vector showing direction and speed based on several past mouse positions! If you watch my video on realistic physics in Python with Pygame I do something exactly like thisb

  • @FacundoMartinez-sf1bq
    @FacundoMartinez-sf1bq ปีที่แล้ว +1

    Hello do you know how i can get the position and show the X coordinate and the Y of a sprite? Thanks you very much

    • @lemastertech
      @lemastertech  ปีที่แล้ว

      Hi yeah, try the command .get_rect with your sprite! So sprite_img.get_rect() which will give you the x and y top left coordinates and also the width and height! Goodluck!

  • @ThangHoang-fl9cg
    @ThangHoang-fl9cg ปีที่แล้ว +1

    Is there any way to get the position x and position y of the mouse position?

    • @lemastertech
      @lemastertech  ปีที่แล้ว +1

      Yes try coords = pygame.mouse.get_pos() it will give you x and y coords as a tuple

    • @ThangHoang-fl9cg
      @ThangHoang-fl9cg ปีที่แล้ว +1

      @@lemastertech tysm

    • @lemastertech
      @lemastertech  ปีที่แล้ว

      @@ThangHoang-fl9cg no problem!

  • @Mark00747
    @Mark00747 2 ปีที่แล้ว +1

    thank you!! exactly what I needed :)

    • @lemastertech
      @lemastertech  2 ปีที่แล้ว

      Awesome!! You are welcome, thanks for watching! :)

  • @Dyotar0
    @Dyotar0 ปีที่แล้ว

    i tried this but it doesn't work
    pygame.draw.rect(surface, (255,255,255), pygame.Rect(mouseposition, 120, 30))

    • @lemastertech
      @lemastertech  ปีที่แล้ว

      Hi, you need to make sure your rect definition is formatted Rect((xstart, ystart), (width,height))
      It looks like you may be missing an apostrophe layer around your width and height!

    • @Dyotar0
      @Dyotar0 ปีที่แล้ว

      @@lemastertech it's working when i boot like that
      pygame.draw.rect(surface, (255,255,255), pygame.Rect(30,30, 120, 30))
      but i cant figure out how to set mouse position

    • @lemastertech
      @lemastertech  ปีที่แล้ว

      @@Dyotar0 well you can find mouse position using pygame.mouse.get_pos(), and that will return the x and y coord of the mouse as a tuple

    • @Dyotar0
      @Dyotar0 ปีที่แล้ว +1

      @@lemastertech sorry that's my first rodeo

    • @lemastertech
      @lemastertech  ปีที่แล้ว

      @@Dyotar0 no problem! Welcome to the rodeo!

  • @Tech_Brigadier
    @Tech_Brigadier 2 ปีที่แล้ว +3

    i think you should have made short for this

    • @lemastertech
      @lemastertech  2 ปีที่แล้ว

      Might have been a good idea! I am on holiday this whole week traveling so I prerecorded this tutorial!

  • @Tech_Brigadier
    @Tech_Brigadier 2 ปีที่แล้ว +1

    how can i communicate with you

    • @lemastertech
      @lemastertech  2 ปีที่แล้ว

      Hi, if you want to check out my patreon, my patreon subscribers get direct messaging access. When I make a discord server it will probably be available to all tiers of my Patreon supporters!

    • @lemastertech
      @lemastertech  2 ปีที่แล้ว

      But if it’s something simple you can always send me a TH-cam comment!

    • @Tech_Brigadier
      @Tech_Brigadier 2 ปีที่แล้ว

      @@lemastertech bro it is not possible for me to be your Patreon supporter

    • @lemastertech
      @lemastertech  2 ปีที่แล้ว +1

      @@Tech_Brigadier that is fine I'm not trying to force anyone to be one!

  • @undeadpresident
    @undeadpresident 2 ปีที่แล้ว

    My computer is 10 years old and the latest python version i can run is 3.4.3 and no modules work when i try to install them. :(
    I'm having second thoughts about learning Python now....

    • @lemastertech
      @lemastertech  2 ปีที่แล้ว

      Well my friend that’s less of an issue with python and more a problem with your computer! If you were to try and run C#, C++, visual studio, Unity, or even sql on a 10 year old machine you may have some compatibility problems! Good luck!

    • @undeadpresident
      @undeadpresident 2 ปีที่แล้ว

      @@lemastertech yeah I just figured if I could download a python version from that time and modules from that time that it would still work. But appently not.
      Would have saved me a lot of time if they just didn't have the old versions available for download in the first place since I just sunk a month into learning python and finding out I can't do much with it.

    • @undeadpresident
      @undeadpresident 2 ปีที่แล้ว +1

      @@lemastertech I found the solution.
      The .whl files are no different than .zip files and they just have to be renamed to .zip and extracted into the Lib\site-packages directory.
      Seems like this should be among the first things to do when pip doesn't work, rather than all the other useless technical bullshit people recommend.
      I found only one post mentioning doing this. Everyone else was talking about doing all sorts of complicated crap that didn't work.

    • @lemastertech
      @lemastertech  2 ปีที่แล้ว

      @@undeadpresident great job!! Thank you for the info I’m glad you found it out, sorry I hadn’t seen that before!!

  • @saadedi75
    @saadedi75 2 ปีที่แล้ว

    good job