如何解决如果检测到鼠标悬停在对象或元素上,我该使用什么功能来打印内容?
如果检测到鼠标悬停在对象或元素上,我该使用什么功能来打印内容?是Box.collidepoint(pos)吗?另外,我在使用from playsound import playsound
导入playsound时遇到了一些问题。它只是打开一个黑屏,其高度和宽度与我设定的高度和宽度完全不同。我该如何解决?错误是ModuleNotFoundError: No module named 'playsound'
,这是我的代码:
try:
logname = 'c:/temp/pgzrun.log'
fontname = 'arial.ttf'
import faulthandler
fh_file = open('c:/temp/fault_handler.log','a',\
encoding = 'utf-8',buffering = 1)
faulthandler.enable(fh_file)
import os,sys
script_dir = getattr(sys,'_MEIPASS',os.path.dirname(os.path.abspath(__file__)))
os.chdir(script_dir)
with open('c:/temp/debug.log',encoding = 'utf-8') as f: f.write \
(f'script_dir "{script_dir}",work_dir "{os.getcwd()}"\n')
import pgzrun
from playsound import playsound
import random
from random import randint
WIDTH = 1280
HEIGHT = 720
camultiplier = 1500
Boxcolor = "blue"
def draw():
screen.fill("green yellow")
screen.draw.filled_rect(main_Box,"sky blue")
screen.draw.filled_rect(timer_Box,"sky blue")
screen.draw.text("score: " + str(score),color="black",topleft=(10,10),fontname=fontname)
for Box in answer_Boxes:
screen.draw.filled_rect(Box,Boxcolor)
screen.draw.textBox(str(time_left),timer_Box,color=('black'),fontname=fontname)
screen.draw.textBox(question[0],main_Box,fontname=fontname)
index = 1
for Box in answer_Boxes:
screen.draw.textBox(question[index],Box,fontname=fontname)
index = index + 1
def game_over():
global question,time_left,scoredecreaserps
scoredecreaserps = 0
message = 'Game Over. You got %s questions correct' %str(numques)
question = [message,'-',5]
time_left = 0
def correct_answer():
global question,score,numques,camultiplier
numques = numques + 1
score = score + camultiplier
camultiplier = camultiplier - 80
if questions:
question = questions.pop()
time_left = 10
else:
print('End of questions')
game_over()
def on_mouse_down(pos):
global Boxcolor
index = 1
for Box in answer_Boxes:
if Box.collidepoint(pos):
print('Clicked on answer' + str(index))
if index == question[5]:
print('You got it correct!')
correct_answer()
else:
playsound('bruh_sound_effect.mp3')
game_over()
index = index + 1
def update_time_left():
global time_left
if time_left:
time_left = time_left - 1
else:
game_over()
def score_lower():
global score,scoredecreaserps
if score:
score = score - scoredecreaserps
scoredecreaserps = scoredecreaserps + 8
main_Box = Rect(50,40,820,240)
timer_Box = Rect(990,240,240)
answer_Box1 = Rect(50,358,495,165)
answer_Box2 = Rect(735,165)
answer_Box3 = Rect(50,538,165)
answer_Box4 = Rect(735,165)
answer_Boxes = [answer_Box1,answer_Box2,answer_Box3,answer_Box4]
scoredecreaserps = 80
numques = 0
score = 0
time_left = 10
q1 = ["Who was the third president of the Philippines?",'Rodrigo Duterte','Ramon Magsaysay','Jose P. Laurel','Fidel V. Ramos',3]
q2 = ['When was the Philippines granted independece from the US?','1977','1946','1935','1907',2]
q3 = ['When was the Philippines colonized by Spain','1898','1523','1654','1521',4]
questions = [q1,q2,q3]
random.shuffle(questions)
question = questions.pop(0)
clock.schedule_interval(update_time_left,1.0)
clock.schedule_interval(score_lower,1.0)
pgzrun.go()
except:
import traceback
with open(logname,encoding = 'utf-8') as f:
f.write(''.join(traceback.format_exc()) + '\n')
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。