Ask coding questions

← Back to all posts
Pygame help
AriTelford1 (0)

I'm trying to make a program that will make a cat meow and display the text 'Meow' when the space button is pressed. My only problem is that only the image switches when the space button is pressed, and nothing else happens. There was code that switched the image eventually, but it didn't work. I'd also like to make it so the text can disappear eventually. Here's the code.

import pygame, sys, time, random
from pygame.locals import *
import time

pygame.init()
mainClock = pygame.time.Clock()
font = pygame.font.SysFont(None, 48)

width = 400
height = 400
windowSurface = pygame.display.set_mode((width, height), 0, 32)

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

cat = pygame.Rect(50, 100, 400, 400)
meow = pygame.Rect(50, 100, 400, 400)
catImg = pygame.image.load('normalcat.png')
meowImg = pygame.image.load('meow.png')
stretchCat = pygame.transform.scale(catImg, (400, 400))
stretchMeow = pygame.transform.scale(meowImg, (400, 400))
windowSurface.blit(stretchCat, cat)

while True:
  pygame.display.update()
  for event in pygame.event.get():
    if event.type == KEYDOWN:
      if event.key == K_SPACE:
          text = font.render('Meow', True, WHITE, BLACK)
          textRect = text.get_rect()
          textRect.centerx = windowSurface.get_rect().centerx
          textRect.centery = windowSurface.get_rect().centery
          pygame.draw.rect(windowSurface, WHITE, (textRect.left - 20, textRect.top - 20, textRect.width + 40, textRect.height + 40))
          windowSurface.blit(text, textRect)
          windowSurface.blit(stretchMeow, meow)
Commentshotnewtop
mwilki7 (284)

Try adding this:

meowSound = pygame.mixer.Sound('meow.wav') # wherever you saved your "meow" sound
meowSound.play()
AriTelford1 (0)

@mwilki7 I had done that, but it said mixer wasn't initialized, and when i did initialize it, it said that there was no available audio device