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.
Code tidied away in spoiler tags to improve thread readability - Binky
Any help would be appreciated. Thanks
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 ()
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 ()
Any help would be appreciated. Thanks
Last edited by a moderator: