0

I'm not quite sure what's going on here. I'm trying to make it play a sound when the darkest part of the feed from my web-cam is in a particular location in the display. When I run it I get this error

File "/home/phur/Documents/052617/052617.py", line 42, in piano if par[100, 200] == surface.map_rgb((0, 0, 0)): TypeError: 'NoneType' object has no attribute 'getitem' (0, 0)

Here's my code:

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()
  • Welcome to Stackoverflow! When asking questions about errors in your code you should always create a [mcve]. By doing so you're making the question easier to understand, easier to give a great answer to, more valuable for future readers and in most cases might you even find the error yourself during the process. To create a [mcve] you first need to remove redundant code. The error is in the first for loop so the remaining 3 loops are unnecessary for the question. Small thing like setting the caption and printing the mouse position can also be removed. The complete and verifiable parts are fine – Ted Klein Bergman May 28 '17 at 10:13
  • 1
    Just to demonstrate; [here](https://hastebin.com/upociwikus.py) is an example on how you could have minimized your code (I also provided some additional useful comments, but they shouldn't be in an actual question). – Ted Klein Bergman May 28 '17 at 10:15

1 Answers1

0

It looks like you aren't initializing your pygame.PixelArray correctly.

According to the pygame pixel array doc example. First, you should create a surface or screen for python to display.

size = (640, 480)
md = pygame.display.set_mode(size, 0)
surface = pygame.Surface(size)

Next, in your piano() function:

# create pixel array
par = pygame.PixelArray(surface)

In general, you shouldn't be using the double leading and double trailing underscores functions. In this case, your line par = pygame.PixelArray.surface.__init__() is suspect. If you're curious here's a good explanation of why some functions have underscores in python

Skam
  • 7,298
  • 4
  • 22
  • 31