Python编译器IDE版本7个按键小钢琴程序代码

import pygame
import sys

初始化 Pygame

pygame.init()

设置窗口大小

screen_width = 640
screen_height = 480
screen = pygame.display.set_mode((screen_width, screen_height))

设置标题

pygame.display.set_caption(‘Play Sound Example’)

加载音乐文件

sound_files = [‘s1.wav’, ‘s2.wav’, ‘s3.wav’, ‘s4.wav’, ‘s5.wav’, ‘s6.wav’, ‘s7.wav’]
sounds = [pygame.mixer.Sound(file) for file in sound_files]

按钮的设置

buttons = [pygame.Rect(400 + i * 100, 100, 90, 400) for i in range(7)]
button_colors = [(0, 0, 255), (0, 255, 0), (255, 0, 0), (255, 255, 0), (0, 255, 255), (255, 0, 255), (0, 25, 25)]
button_clicked_states = [False] * 7

主循环

running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
sys.exit()

    # 检测鼠标点击事件
    if event.type == pygame.MOUSEBUTTONDOWN:
        for i, button in enumerate(buttons):
            if button.collidepoint(event.pos):
                button_clicked_states[i] = True

for i, button_clicked in enumerate(button_clicked_states):
    if button_clicked:
        sounds[i].play()
        button_clicked_states[i] = False

{}
# 绘制按钮
screen.fill((255, 255, 255)) # 填充背景色
for i, button in enumerate(buttons):
pygame.draw.rect(screen, button_colors[i], button)
pygame.display.flip()

# 控制游戏循环速度
pygame.time.Clock().tick(60)

Python小钢琴程序

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EYYLTV

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值