2017-02-23 62 views
-3

我正在用pygame製作一個piano_code程序。我已經做了一個鋼琴功能,但我想添加更多。當我用鼠標點擊鼠標時,我想將鍵盤輸入保存在一個名爲「my_code」的變量中。但是,這是行不通的在pygame中:我可以將鍵盤輸入保存在變量中嗎?

import os, sys 
import random 
import pygame 
from pygame.locals import * 


My_red_color = (255, 0, 0) 
My_blue_color = (0, 0, 255) 
My_green_color = (0, 255, 0) 
My_yellow_color = (255, 255, 0) 
My_black_color = (0,0,0) 
My_white_color = (255,255,255) 

pygame.init() 
APPLICATION_x_size = 800 
APPLICATION_y_size = 800 
screen = pygame.display.set_mode((APPLICATION_x_size, APPLICATION_y_size)) 

myImg = pygame.image.load( 'E:\성빈\고등학교\과학전람회\ pygame的鋼琴\ code_input.png')

x=200 
y=400 


pygame.display.set_caption('Piano') 
pygame.mouse.set_visible(True) 
#pygame.mouse.set_visible(False) 
green_square= pygame.Surface(screen.get_size()) 
green_square.fill((0, 255, 0)) 
screen.blit(green_square, (0, 0)) 
Weeee = True 
pygame.draw.rect(screen, My_white_color, (30,100,100,200)) 
pygame.draw.rect(screen, My_white_color, (132,100,100,200)) 
pygame.draw.rect(screen, My_white_color, (234,100,100,200)) 
pygame.draw.rect(screen, My_white_color, (336,100,100,200)) 
pygame.draw.rect(screen, My_white_color, (438,100,100,200)) 
pygame.draw.rect(screen, My_white_color, (540,100,100,200)) 
pygame.draw.rect(screen, My_white_color, (642,100,100,200)) 
pygame.draw.rect(screen, My_black_color, (81,50,100,200)) 
pygame.draw.rect(screen, My_black_color, (183,50,100,200)) 
pygame.draw.rect(screen, My_black_color, (387,50,100,200)) 
pygame.draw.rect(screen, My_black_color, (489,50,100,200)) 
pygame.draw.rect(screen, My_black_color, (591,50,100,200)) 
b=screen.blit(myImg, (x,y)) 
pygame.display.flip() 

while Weeee : 



    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
     pygame.quit() 
     if event.type == pygame.KEYDOWN: 
      if event.key == pygame.K_a: 
       print("C") 
       pygame.mixer.music.load('E:\성빈\고등학교\과학전람회\pygame piano\pygame piano\sound1.mp3') 
       pygame.mixer.music.play(1,0) 
      if event.key == pygame.K_s: 
       print("D") 
       pygame.mixer.music.load('E:\성빈\고등학교\과학전람회\pygame piano\pygame piano\sound2.mp3') 
      pygame.mixer.music.play(1,0) 
      if event.key == pygame.K_d: 
       print("E") 
       pygame.mixer.music.load('E:\성빈\고등학교\과학전람회\pygame piano\pygame piano\sound3.mp3') 
      pygame.mixer.music.play(1,0) 
      if event.key == pygame.K_f: 
       print("F") 
       pygame.mixer.music.load('E:\성빈\고등학교\과학전람회\pygame piano\pygame piano\sound4.mp3') 
      pygame.mixer.music.play(1,0) 
      if event.key == pygame.K_g: 
       print("G") 
       pygame.mixer.music.load('E:\성빈\고등학교\과학전람회\pygame piano\pygame piano\sound5.mp3') 
      pygame.mixer.music.play(1,0) 
      if event.key == pygame.K_h: 
       print("A") 
       pygame.mixer.music.load('E:\성빈\고등학교\과학전람회\pygame piano\pygame piano\sound6.mp3') 
      pygame.mixer.music.play(1,0) 
      if event.key == pygame.K_j: 
       print("B") 
       pygame.mixer.music.load('E:\성빈\고등학교\과학전람회\pygame  piano\pygame piano\sound7.mp3') 
       pygame.mixer.music.play(1,0) 



     if event.type == MOUSEBUTTONDOWN : 
      if event.button==1 : 
       print("Setting Code") 
       my_code=pygame.key.get_pressed() 
       print("My code is" + str(my_code)) 



      if event.button == 3 : 
       print("Type your Code") 
       code_try=pygame.key.get_pressed() 
       if code_try==my_code : 
        print("Done") 
       else : 
         print("You've typed wrong code") 

我該怎麼辦? 謝謝。

+4

如果您直接粘貼問題代碼而不是附加圖片,這將非常有用 – Simon

+0

當您單擊左鍵時,是否正在按任意鍵? –

+0

@PeterWood根本沒有 –

回答

0

我假設你要做的是在按下鼠標左鍵時將所有已按下的鍵保存到變量中。不過,從我的理解中,Pygame中沒有一個函數可以給你一個按下按鍵的歷史。但這是一個非常簡單的問題:在每個音符的if語句中,只需將音符添加到數組中。然後在左鍵單擊時,您可以將該陣列保存到my_code或輸出它。

我覺得你很困惑pygame.key.get_pressed()做什麼。它會在恰好被調用的時刻返回鍵盤狀態的快照。這只是基於當前正在按什麼鍵的一組布爾值。這不是按鍵的歷史。

相關問題