View the step-by-step solution to:

How to I get the buttons in this code to stop just opening the main immediately when I run the code. I need it to run through the main when I click...

How to I get the buttons in this code to stop just opening the main immediately when I run the code. I need it to run through the main when I click the "start" button





gameDisplay = pygame.display.set_mode((display_width,display_height))

pygame.display.set_caption('Maze Dash')

clock = pygame.time.Clock()




def text_objects(text, font, font_color):

   textSurface = font.render(text, True, font_color)

   return textSurface, textSurface.get_rect()


def button(msg, x, y, w, h, color, dark_color, action = None):

   mouse = pygame.mouse.get_pos()

   click = pygame.mouse.get_pressed()


   if x+w > mouse[0] > x and y+h > mouse[1] > y:

       pygame.draw.rect(gameDisplay, color,(x,y,w,h))


       if click[0] == 1: #and action != None:

           if action == "instructions":

               instruction_page()



   else:

       pygame.draw.rect(gameDisplay, dark_color,(x,y,w,h))


   smallText = pygame.font.SysFont("freesansbold.ttf",40)

   textSurf, textRect = text_objects(msg, smallText, black)

   textRect.center = ( (x+(w/2)), (y+(h/2)) )

   gameDisplay.blit(textSurf, textRect)


def instruction_page():

   instr = True

   while instr:

           for event in pygame.event.get():

               if event.type == pygame.QUIT:

                   pygame.quit()

                   quit()

           gameDisplay.fill(black)

           title = pygame.font.Font("freesansbold.ttf", 90)

           description = pygame.font.Font("freesansbold.ttf", 25)

           text = "Use the arrow keys to move your player to solve the maze."

           text2 = "Play as fast as you can to win!"

           TextSurf, TextRect = text_objects("INSTRUCTIONS", title, white)

           text_Surf, text_Rect = text_objects(text, description, white)

           Text_Surf, Text_Rect = text_objects(text2, description, white)

           TextRect.center = ((display_width/2), display_height/4)

           text_Rect.center = ((display_width/2), display_height/2)

           Text_Rect.center = (display_width/2, 350)

           gameDisplay.blit(TextSurf, TextRect)

           gameDisplay.blit(text_Surf, text_Rect)

           gameDisplay.blit(Text_Surf, Text_Rect)

           button("START", 200, 400, 150, 75, purple, dark_purple, start)

           button("QUIT", 450, 400, 150, 75, blue, dark_blue, pygame.quit)

           pygame.display.update()


def main_screen():

   intro = True

   while intro:

       for event in pygame.event.get():

           #print(event)

           if event.type == pygame.QUIT:

               pygame.quit()

               quit()


       gameDisplay.fill(black)

       title = pygame.font.Font('freesansbold.ttf',115)

       TextSurf, TextRect = text_objects("MAZE DASH", title, green)

       TextRect.center = ((display_width/2),(display_height/3))

       gameDisplay.blit(TextSurf, TextRect)



       button("QUIT", 450, 300, 150, 75, blue, dark_blue, pygame.quit)

       instruction_page() = button("INSTRUCTIONS", 260, 420, 300, 75, yellow, dark_yellow, instruction_page)

       start() = button("START", 200, 300, 150, 75, purple, dark_purple, start)

       pygame.display.update()

       clock.tick(15)

def start():

   main


main_screen()

pygame.quit()

quit()



Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question