2013-01-19 71 views
0

我是一名新的程序員,爲我的計算機科學總結工作的記憶遊戲。我基本上完成了,但我真的想讓遊戲在用戶玩遊戲時播放音樂。我終於得到了音樂播放,但不幸的是現在屏幕只是空白,我不知道爲什麼。音樂播放;但屏幕是空白的

任何幫助,將不勝感激。 謝謝:)

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


       # 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) 




# 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() 

        # INSTRUCTIONS!!!!!!!!! 
# This is a font we use to draw text on the screen (size 36) 
font = pygame.font.Font(None, 36) 

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, 40]) 

     text=font.render("Click for instructions", True, RED) 
     screen.blit(text, [110, 100]) 

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, 160])   

    text=font.render("Play game", True, RED) 
    screen.blit(text, [320, 310]) 
# 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() 

回答

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

這裏有一個while循環當歌曲播放完畢即完成。你使用while循環的意圖是什麼?

它應該工作得很好,如果你刪除整個循環。

一個小提示:使用函數來封裝和刪除重複的代碼。

示例:而不是使用

text=font.render("This is how the game goes: The", True, WHITE) 
screen.blit(text, [10, 10]) 

創建一個功能

def writeText(screen,text,pos): 
    text=font.render(text,True,WHITE) 
    screen.blit(text,pos) 

那麼你可以使用writeText("This is how the game goes:",(10,10))

+0

謝謝你的回答。當你的意思是刪除循環,這是否意味着我應該只刪除單詞'while',然後unindent的一切?因爲這就是我所做的,但它仍然沒有工作........你認爲我應該讓整個程序一段時間循環嗎? – newuser

+0

既時間條件和clock.tick(),因爲你已經在稍後時間滴答作響。 –

+0

沒錯。謝謝。 – newuser

相關問題