import pygame pygame.mixer.init() screen=pygame.display.set_mode((1100,600)) pygame.display.set_caption("不良人") p1=pygame.image.load("picture/li1.jpg") p2=pygame.image.load("picture/li2.jpg") rect1=p1.get_rect() rect2=p2.get_rect() limusic = pygame.mixer.Sound('Sound/liSound.MP3') limusic.set_volume(20) if pygame.mouse.get_focused(): #检查程序界面是否获得鼠标焦点 curx, cury = pygame.mouse.get_pos() #获取鼠标光标的位置 screen.blit(p2, (-88, -180)) while True: for event in pygame.event.get(): if pygame.Rect.collidepoint(rect2, (curx, cury)) and event.type==pygame.MOUSEBUTTONDOWN: screen.blit(p1, (-80, 0)) limusic.play() pygame.display.flip() if pygame.Rect.collidepoint(rect1, (curx, cury)) and event.type==pygame.MOUSEBUTTONUP: screen.blit(p2, (-150, -100)) pygame.display.flip(), if event.type==pygame.QUIT: pygame.quit() pygame.display.flip()
运行窗口后在窗口内按下鼠标键即会切换图片并且播出一段李星云经典语录,松开鼠标键就会切回之前的图片!
截图: