#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    Question Music Playing; But Screen Is Blank. Pygame help!!!


    I'm a new programmer working on a memory game for my computer science summative. I'm basically done but I really want to get the game to play music while the user is playing. I finally got the music to play, but unfortunately now the screen is just blank and I have no idea why.


    import pygame , sys
    import random
    from pygame import *
    size=[500,500]
    pygame.init()
    screen=pygame.display.set_mode(size)



    # Colours
    LIME = (0,255,0)
    RED = (255, 0, 0)
    BLACK = (0,0,0)
    PINK = (255,102,178)
    SALMON = (255,192,203)
    WHITE = (255,255,255)
    LIGHT_PINK = (255, 181, 197)
    SKY_BLUE = (176, 226, 255)
    PURPLE = (104, 34, 139)
    screen.fill(BLACK)

    # Width and Height of game box
    width=50
    height=50

    # Margin between each cell
    margin = 5

    #Loop until the user clicks the close button.
    done=False

    # Used to manage how fast the screen updates
    clock=pygame.time.Clock()

    #-------------------- MUSIC-------------------------
    pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=4096)
    print "Mixer settings", pygame.mixer.get_init()
    print "Mixer channels", pygame.mixer.get_num_channels()
    pygame.mixer.music.set_volume(1.0)
    pygame.mixer.music.load("Wings.wav")
    pygame.mixer.music.play()

    clock = pygame.time.Clock()
    while pygame.mixer.music.get_busy():
    # check if playback has finished
    clock.tick(30)


    # INSTRUCTIONS!!!!!!!!!
    # This is a font we use to draw text on the screen (size 36)
    font = pygame.font.SysFont("Times New Roman",30)
    mmfont = pygame.font.SysFont("Times New Roman",45)

    display_instructions = True
    instruction_page = 1

    # -------- Instruction Page Loop -----------
    while done==False and display_instructions:
    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
    if event.type == pygame.MOUSEBUTTONDOWN:
    instruction_page += 1
    if instruction_page == 3:
    display_instructions = False

    # Set the screen background
    screen.fill(BLACK)

    if instruction_page == 1:
    # Draw instructions, page 1
    # This could also load an image created in another program.
    # That could be both easier and more flexible.

    text=font.render("Welcome to Spatial Recall: ", True, PURPLE)
    screen.blit(text, [10, 10])
    text=font.render("The game that tests your memory!!!", True, WHITE)
    screen.blit(text, [10, 50])

    text=mmfont.render("Click for instructions", True, RED)
    screen.blit(text, [40, 150])

    if instruction_page == 2:
    # Draw instructions, page 2
    text=font.render("This is how the game goes: The", True, WHITE)
    screen.blit(text, [10, 10])

    text=font.render("computer is going to flash some green", True, WHITE)
    screen.blit(text, [10, 40])

    text=font.render("boxes at random positions. It's", True, WHITE)
    screen.blit(text, [10, 70])

    text=font.render("up to you to decide where you", True, WHITE)
    screen.blit(text, [10, 100])

    text=font.render("think they are hidden.", True, WHITE)
    screen.blit(text, [10, 130])

    text=font.render("Good Luck!!.", True, PURPLE)
    screen.blit(text, [20, 180])

    text=mmfont.render("PLAY GAME", True, RED)
    screen.blit(text, [120, 330])
    # Limit to 20 frames per second
    clock.tick(20)

    # Go ahead and update the screen with what we've drawn.
    pygame.display.flip()

    width=50
    height=50

    # Margin between each cell
    margin = 5

    coord=[]

    # Create a 2 dimensional array. A two dimesional
    # array is simply a list of lists.
    grid=[]
    for row in range(20):
    # Add an empty array that will hold each cell
    # in this row
    grid.append([])
    for column in range(20):
    grid[row].append(0) # Append a cell

    # Set row 1, cell 5 to one. (Remember rows and
    # column numbers start at zero.)
    grid[1][5] = 0

    # Set title of screen
    pygame.display.set_caption("Spatial Recall")

    #Loop until the user clicks the close button.
    done=False

    # Used to manage how fast the screen updates
    clock=pygame.time.Clock()

    #draw the grid all pink
    for row in range(20):
    for column in range(20):
    color = LIGHT_PINK
    pygame.draw.rect(screen,color,[(margin+width)*column + margin,(margin+height)*row+margin,width,height])
    pygame.display.flip()
    '''
    #create list of random coodinates
    x = random.randint(0, 10)
    y = random.randint(0, 10) '''


    #cover pink squares with green squares at the list of coodinates
    #loop through the list, for every coordinate in list, turn green

    for i in range(random.randint(2,10)):
    x = random.randint(2, 10)
    y = random.randint(2, 10)
    color = LIME
    pygame.draw.rect(screen,color,[(margin+width)*y + margin,(margin+height)*x+margin,width,height])
    coord.append((x,y))
    pygame.display.flip()
    time.sleep(2)

    for row in range(20):
    for column in range(20):
    color = LIGHT_PINK
    pygame.draw.rect(screen,color,[(margin+width)*column + margin,(margin+height)*row+margin,width,height])
    pygame.display.flip()


    clock.tick(100)


    # -------- 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
    elif event.type == pygame.MOUSEBUTTONDOWN:
    # User clicks the mouse. Get the position
    pos = pygame.mouse.get_pos()
    # Change the x/y screen coordinates to grid coordinates
    column=pos[0] // (width+margin)
    row=pos[1] // (height+margin)
    print coord
    print [row,column]
    if (row,column) in coord:
    color = LIME
    pygame.draw.rect(screen,color,[(margin+width)*column + margin,(margin+height)*row+margin,width,height])
    else:
    color = RED
    pygame.draw.rect(screen,color,[(margin+width)*column + margin,(margin+height)*row+margin,width,height])
    pygame.display.flip()

    #x = random.randint(0, 10)
    #y = random.randint(0, 10)


    pygame.quit ()
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    Please use code tags to preserve critical white space. See post at my signature.

    I changed the beginning of your program to
    Code:
    import pygame , sys
    import random
    from pygame import *
    import time
    size=[500,500]
    pygame.init()
    screen=pygame.display.set_mode(size)
    In other words, I imported time after your
    from pygame import *
    It's a good idea to not use "import *" because it makes the program hard to read and in this case you hid the time module.

    Also, not having your wonderful wave file I commented out the music instructions.

    I run your game, and get a black window.


    Then I delete the window (close it with the little window manager X by mouse click) and the game runs. I don't have much experience with pygame, but I don't think the music is the trouble, although it certainly could be an additional problem.


    Specifically I did this to the sound:
    Code:
    #-------------------- MUSIC------------------------- 
    #pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=4096)
    #print "Mixer settings", pygame.mixer.get_init()
    #print "Mixer channels", pygame.mixer.get_num_channels()
    #pygame.mixer.music.set_volume(1.0)
    #pygame.mixer.music.load("Wings.wav")
    #pygame.mixer.music.play()
    #
    #clock = pygame.time.Clock()
    #while pygame.mixer.music.get_busy():
    #   # check if playback has finished
    #   clock.tick(30)
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo