1 Replies - 942 Views - Last Post: 10 July 2015 - 02:27 PM Rate Topic: -----

#1 slayerodd   User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 79
  • Joined: 10-May 15

pygame tone play on button press

Posted 06 July 2015 - 03:44 PM

I am making simple program that works like a piano. But it's not showing a piano, instead it's a guy who opens his mouth differently each tone. I don't know how to make the sound play when i hold the specific button and stop the sound when i release that button. I don't know how to make that i can play two or three tones simultaneously. I have not yet added different face expressions for the different tones. I am also adding a definition for the main loop later.

This is the code:

import pygame
import sys
import winsound
pygame.init()

display_width = 800
display_height = 600

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Open The Mouth")
clock = pygame.time.Clock()

faceImg = pygame.image.load("face_shut.png")
faceOpenImg = pygame.image.load("face_open.png")
def face(x,y):
    gameDisplay.blit(faceImg,(face_x,face_y))
def faceOpen(x,y):
    gameDisplay.blit(faceOpenImg,(faceOpen_x,faceOpen_y))
faceOpen_x = (1)
faceOpen_y = (1)

face_x = (1)
face_y = (1)

programRunning = True

face(face_x,face_y)
pygame.display.update()

while programRunning:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            programRunning = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_Q:
                faceOpen(faceOpen_x,faceOpen_y)
                pygame.display.update()
                winsound.Beep(16.35,1)
            if event.key == pygame.K_W:
                faceOpen(faceOpen_x,faceOpen_y)
                pygame.display.update()
                winsound.Beep(18.35,1)

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_Q or event.key == pygame.K_W:
                face(face_x,face_y)
                pygame.display.update()
                #stopthesound





    clock.tick(60)

pygame.quit()
quit()


Is This A Good Question/Topic? 0
  • +

Replies To: pygame tone play on button press

#2 witeboy724   User is offline

  • D.I.C Head
  • member icon

Reputation: 86
  • View blog
  • Posts: 209
  • Joined: 21-June 12

Re: pygame tone play on button press

Posted 10 July 2015 - 02:27 PM

Why call winsound when pygame has midi already built in? You can pick any midi instrument you want, so it could be sound effects rather than tones if that's what you're going for.
import pygame.midi
import time

pygame.midi.init()
player = pygame.midi.Output(0)
player.set_instrument(0)
player.note_on(64, 127)
time.sleep(1)
player.note_off(64, 127)
del player
pygame.midi.quit()


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1