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

1 回答 1

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

于 2013-01-19T15:38:33.103 回答