我不太确定这里发生了什么。当我的网络摄像头的最暗部分位于显示器的特定位置时,我正试图让它发出声音。当我运行它时,我收到此错误
文件“/home/phur/Documents/052617/052617.py”,第42行,钢琴 如果par [100,200] == surface.map_rgb((0,0,0)): TypeError:'NoneType'对象没有属性' getitem ' (0,0)
这是我的代码:
import pygame
import pygame.camera
from pygame.locals import *
import time
import random
import Image
pygame.init()
pygame.camera.init()
size = (640, 480)
md = pygame.display.set_mode(size, 0)
pygame.display.set_caption("Piano")
def piano():
stop_playing = False
camera = pygame.camera.Camera('/dev/video0', size)
camera.start()
while not stop_playing:
captured = camera.get_image(md)
md.blit(captured, (0, 0))
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
stop_playing = True
mp = pygame.mouse.get_pos()
par = pygame.PixelArray.surface.__init__()
print mp
for x in range(0, 213):
for y in range(0, 240):
if par[x, y] == pygame.surface.map_rgb((0, 0, 0)):
pygame.mixer.music.load("C1.wav")
pygame.mixer.music.play()
time.sleep(1)
for x in range(214, 426):
for y in range(0, 240):
if par[x, y] == pygame.surface.map_rgb((0, 0, 0)):
pygame.mixer.music.load("D1.wav")
pygame.mixer.music.play()
time.sleep(1)
for x in range(427, 640):
for y in range(0, 240):
if par[x, y] == pygame.surface.map_rgb((0, 0, 0)):
pygame.mixer.music.load("E1.wav")
pygame.mixer.music.play()
time.sleep(1)
for x in range(0, 320):
for y in range(241, 480):
if par[x, y] == pygame.surface.map_rgb((0, 0, 0)):
pygame.mixer.music.load("F1.wav")
pygame.mixer.music.play()
time.sleep(1)
for x in range(321, 640):
for y in range(241, 480):
if par[x, y] == pygame.surface.map_rgb((0, 0, 0)):
pygame.mixer.music.load("G1.wav")
pygame.mixer.music.play()
time.sleep(1)
piano()
quit()
答案 0 :(得分:0)
您好像没有正确初始化pygame.PixelArray
。
根据pygame pixel array doc example。首先,您应该为python创建一个表面或屏幕来显示。
size = (640, 480)
md = pygame.display.set_mode(size, 0)
surface = pygame.Surface(size)
接下来,在您的piano()
函数中:
# create pixel array
par = pygame.PixelArray(surface)
通常,您不应该使用双前导和双尾下划线功能。在这种情况下,您的第par = pygame.PixelArray.surface.__init__()
行是可疑的。
如果你好奇,这是对why some functions have underscores in python