Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6986619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:49:56+00:00 2026-05-27T18:49:56+00:00

HI i wrote a simple block collecting program that was working just fine and

  • 0

HI i wrote a simple block collecting program that was working just fine and dandy until i added sound. Then all the sudden i get a MemoryError which is something ive never seen before.

my code is attached along with the sound(wav) file that seem to be the problem. Any help would be great, and yes the code and the sound are in the same folder along with tons of other programs and pictures and stuff.

import pygame
import random
import pygame.mixer

winsound=pygame.mixer.Sound('winning.wav')
collectsound=pygame.mixer.Sound('blip.wav')
#Define colors

black    = (   0,   0,   0)
white    = ( 255, 255, 255)
red      = ( 255,   0,   0)

#Class represents ball, derives from Sprite class in pygame

class Block(pygame.sprite.Sprite):
    #Constructor. Pass in the color of the block and its x and y pos.
    def __init__(self, color, width, height):
            # Call the parent class (Sprite) constructor
            pygame.sprite.Sprite.__init__(self) 

            # Create an image of the block, and fill it with a color.
            # This could also be an image loaded from the disk.
            self.image = pygame.Surface([width, height])
            self.image.fill(color)

            # Fetch the rectangle object that has the dimensions of the image
            # image.
            # Update the position of this object by setting the values 
            # of rect.x and rect.y
            self.rect = self.image.get_rect()

# Initialize Pygame
pygame.init()

winsound=pygame.mixer.Sound('winning.wav')
collectsound=pygame.mixer.Sound('blip.wav')

#Set width and height of screen

screen_width=700
screen_height=400
screen=pygame.display.set_mode([screen_width, screen_height])
pygame.display.set_caption('Block Attack!')

#List of sprites. Each block is added to this list. List is managed by RenderPlain()

block_list=pygame.sprite.RenderPlain()

#List of every sprite

all_sprites_list=pygame.sprite.RenderPlain()

for i in range(50):
    #create instance of block
    block=Block(black,20,14)

    #set random location for the block
    block.rect.x=random.randrange(screen_width)
    block.rect.y=random.randrange(screen_height)

    #Add the block to the list of objects
    block_list.add(block)
    all_sprites_list.add(block)


#Create red player block
player=Block(red,20,15)
all_sprites_list.add(player)

done=False

clock=pygame.time.Clock()

score=0

#-------Main Program Loop-------

while done==False:
    for event in pygame.event.get(): #User did something
        if event.type==pygame.QUIT: #If user clicked close
            done=True #Flag that we are done so we exit this loop

    #Clear screen
    screen.fill(white)

    #Get current mouse position. This returns the position as a list of two numbers
    pos=pygame.mouse.get_pos()

    #Fetch x and y out of list, like letters out of strung, set player object
    #to location of the mouse

    player.rect.x=pos[0]
    player.rect.y=pos[1]


    #Check if player block has collidied with anything...were still in a loop
    block_hit_list=pygame.sprite.spritecollide(player, block_list, True)

    #Check list of collisions.
    if len(block_hit_list) >0:
        score +=len(block_hit_list)
        print( score )
        collectsound.play()
    elif score==50:
        print('YOU WIN!!\n')
        done=True
        winsound.play()



    #Draw all the sprites
    all_sprites_list.draw(screen)

    #Limit to 60 frames per second
    clock.tick(60)

    #Update screen
    pygame.display.flip()



pygame.quit()

So when i run the code i get the error

Traceback (most recent call last):
  File "/Users/zackbaker/Desktop/PythonFolder/BlockCollecter.py", line 4, in <module>
    sound=pygame.mixer.Sound('winning.wav')
MemoryError
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:49:57+00:00Added an answer on May 27, 2026 at 6:49 pm

    I think you have to initialize the mixer first, before using sounds.

    pygame.mixer.init(44100, -16, 2, 2048)
    # Read the docs to know what these numbers are
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a simple tool to generate a DBUnit XML dataset using queries that
I wrote a simple Windows Forms program in C#. I want to be able
I wrote a simple web service in C# using SharpDevelop (which I just got
I wrote a simple Sinatra app that generate an image using rmagick from some
I wrote a simple Tkinter based Python application that reads text from a serial
I wrote simple servlet but it seems to be working as a single thread
I have written a simple program that does autocorrelation as follows...I've used pgi accelerator
I wrote a simple program with java swing which suppose to start another thread
I wrote a simple script that add extra form fields when needed by the
I wrote a simple program for Maemo by Python to check some pixel's color

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.