程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Qixi Festival_A Python program that moves the mouse to play the confession balloon (available in August 2022)

編輯:Python

Foreword

 Qixi event, throw a code and throw it away

Effects

When the mouse moves to the left side of the screen, the music `confession balloon.mp3` will be played automatically, which can be adjusted by yourself

Records

Walk through

Creative code confession

Thoughts

1. Select your area A,
2. Get mouse coordinates in real time
3. Add a judgment, if the mouse coordinates are in area A, play the music file

Instructions

pip3 install pynput
pip3 install pygame

Run

python3 main.py

Results

Mouse the mouse to the area A on the left half of the screen to automatically play the music,
Mouse the mouse to the area B on the right half of the screen to automatically stop playing,

You can manually change it later

  • 1. For example, create a folder music, put all the songs in it, and achieve the effect of randomly selecting a song to play when the mouse moves to the left A
  • 2. Mixer.music.stop() is changed from stop to pause, and continues to play after going to the left, to achieve the effect of pause and resume playback
    You can expand it later according to your needs

Code

main.py

from pynput import mousefrom pygame import mixersize = [1920, 1680] # Modify to your own screen sizemusic_file = "Confession Balloon.mp3" # The path where the mp3 file is located, use an absolute path, or a relative path in the same folder as the filemixer.init()mixer.music.load(music_file)play_music = Falsedef mouse_move(x, y):"""mouse move event:param x: abscissa:param y: ordinate:return:"""global play_musicglobal pif x <= size[0] / 2 and not play_music:play_music = Trueprint("The mouse enters the left area A and starts playing music", music_file)mixer.music.play()if x > size[0] / 2 and play_music:play_music = Falseprint("The mouse entered the right area B, stop playing")mixer.music.stop()# Listen for mouse eventswith mouse.Listener(on_move=mouse_move, # mouse move event) as listener:listener.join()

How to use

  • 1. Put a confession balloon in the same folder, mp3
  • 2. Create a python file in the same folder - main.py
  • 3. Run
python3 main.py
  • 4. Minimize the window and move the mouse left and right

Other

If you have any questions, please ask them

If you have any questions, please leave a message

If it works, please like it

If it fails, please leave a message

Available August 2022


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved