1

I have made a very simple Pygame program in which mixer is used. I want to create a screen with a background image and a button on it. The button displays Pause. But when i run the program, the pygame window(the output) displays a black screen. And when i type e which is for exit(as you can see in the code, in the mainloop, i've added specifications for letters which i can press to either- close(e), resume(r) or pause(p)), it flashes my program for a second(i am very sure the time it displays it for is lesser than a second) and then exits. I also noticed, once i actually debugged it and it ran but the pygame window was non-responsive so i had to close and run again and to my vain, the screen was again blank. This is my code:

import pygame
import sys
from pygame import mixer
pygame.init()
mixer.init()

run = True

win = pygame.display.set_mode((690, 385))
pygame.display.set_caption("The Rhyme Library")
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREY = (30, 30, 30)
BGIMG = pygame.image.load('C:/Users/Vinod/Desktop/Pygame Learn/musicpy.jpg')
smallfont = pygame.font.SysFont('Corbel',35)
win.blit(BGIMG, (0,0))

                
def button(msg, x, y, w, h, ic,ac, action=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    smallfont = pygame.font.SysFont('Corbel',35)
    text = smallfont.render('Pause' , True , WHITE, ((x+(w/2)), (y+(h/2))))
    if x+w > mouse[0] > x and y+h > mouse[1] > y:
        pygame.draw.rect(win, ac, (x, y, w, h))  # creates a rect object 
        if click[0] == 1 and action != None:
            if action == "pause":
                mixer.music.pause()
    else:
        pygame.draw.rect(win, ic, (x, y, w, h))  # creates a rect object 
    #win.blit(text)
    #text1 = smallfont.render('Resume' , True , WHITE)
    #pygame.draw.rect(win, BLACK,(x, 160, 60, w))  # creates a rect object 
    #pygame.draw.rect(win, GREY, (x, y, w, 60))  # creates a rect object 
                    

                    

    #text2 = smallfont.render('Resume' , True , WHITE)
    #button2 = pygame.Rect(win, text2, (345, 220, 60, 1h))  # creates a rect object 



button("Pause", 345, 100, 150, 60, GREY ,BLACK, "pause")

    
for event in pygame.event.get():
    if event.type == pygame.QUIT:
        pygame.quit()
        run = False
        sys.exit()

                    

    #text2 = smallfont.render('Resume' , True , WHITE)
    #button2 = pygame.Rect(win, text2, (345, 220, 60, 150))  # creates a rect object 

#MAINLOOP                        
mixer.music.load("C:/Users/Vinod/Desktop/Pygame Learn/twinkle.mp3")
mixer.music.set_volume(0.7)
mixer.music.play()
while True:
    print("Thanks for using the mixer created by Nishita Thakur.Kindly follow these guidlines.")
    print("Press 'p' to pause, 'r' to resume")
    print("Press 'e' to exit the program")
    query=input("")
    if query=='p':
        mixer.music.pause()
        
    elif query=='r':
        mixer.music.unpause()
        
    elif query=='e':
        mixer.music.stop()
        break
    else:
        print("Invalid Input Entered")
        
pygame.display.update()
    

I tried to find answers in Stackoverflow but no one had this issue so there was no such question. I want it to display my program and i have tried several times but i could not locate its bugs. I'll be thankful to anyone who can help, as i am pretty new to pygame. Thanks in advance:)

  • You have to put the event loop in the main application loop – Rabbid76 Oct 15 '20 at 16:47
  • Do not use `input`. You have to get the pressed key by the keyboard events (`pygame.KEYDOWN`, `pygame.KEYUP` see [`pygame.event`](https://www.pygame.org/docs/ref/event.html)) – Rabbid76 Oct 15 '20 at 16:53
  • Thanks Rabbid76, just a doubt please,should i add event loop in the While loop or the Mainloop? – Nishita Thakur Oct 15 '20 at 16:54

1 Answers1

1

You have to put the event loop in the main application loop. Do not use input. You have to get the pressed key by the keyboard events (pygame.KEYDOWN, pygame.KEYUP see pygame.event):

print("Thanks for using the mixer created by Nishita Thakur.Kindly follow these guidlines.")
print("Press 'p' to pause, 'r' to resume")
print("Press 'e' to exit the program")

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False 
        
        if event.type == pygame.KEYDOWN: 
        
            if event.key == pygame.K_p:
                mixer.music.pause()
        
            elif event.key == pygame.K_r:
                mixer.music.unpause()
        
            elif event.key == pygame.K_e:
                mixer.music.stop()
                run = False
            else:
                print("Invalid Input Entered")

pygame.quit()
sys.exit()  
Rabbid76
  • 202,892
  • 27
  • 131
  • 174