I've been learning python during one semester, and pygame in one week...
Im making a simple kid guessing game where random images appear with one word. click on the correct image that the word describes and a little green right image appears and a red cross 'wrong' if it is wrong....
It's not working, I dunno why, I think it's a logical error and I am going nuts please please help.

import pygame,os
from pygame.locals import *
import random

pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((1000,700))
pygame.display.set_caption('level 1')
background = pygame.image.load('background.jpg')
background = background.convert()
screen.blit(background,(0,0))

#random pictues
pic = ['cat.png','dog.png','fish.png','elephant.png','rabbit.png','peng.png']
num = []
for x in [150,360,580]:
    n = random.randint(0,5)
    num.append(n)
    picimage = pygame.image.load(pic[n])
    picimage = picimage.convert()
    screen.blit(picimage,(x,200))
    


catpic = pygame.image.load(pic[0])
catrect = catpic.get_rect()
catrect.centerx, catrect.centery = \
                 150,200

dogpic = pygame.image.load(pic[1])
dogrect = dogpic.get_rect()
dogrect.centerx, dogrect.centery = \
                 360,200

fishpic = pygame.image.load(pic[2])
fishrect = fishpic.get_rect()
fishrect.centerx, fishrect.centery = \
                  580,200



#random events
from random import choice
z = choice(num)
words = ['Cat','Dog','Fish','Elephant','Rabbit','Penguin']
font = pygame.font.SysFont('Arial',30)
word = font.render(words[z],True,(0,0,0))
screen.blit(word,(440,475))


#music player

play = os.path.join('mplay','play.png')
playbutton = pygame.image.load(play)
screen.blit(playbutton, (730,40))
playrect = playbutton.get_rect()
playrect.centerx, playrect.centery = \
                     730,40

pause = os.path.join('mplay','pause.png')
pausebutton = pygame.image.load(pause)
screen.blit(pausebutton, (800,40))
pauseb = pausebutton.convert()
pauserect = pauseb.get_rect()
pauserect.centerx, pauserect.centery = \
                     800,40

firsong = os.path.join('mplay','Jolly Music - With Love.wav')
splay = pygame.mixer.Sound(firsong)
splay.play()

def Wrong(screen):
        X_img = pygame.image.load('X.png').convert_alpha()
        screen.blit(X_img,(800,600))
        pygame.display.flip()
def Correct(screen):
        correct_img = pygame.image.load('correct.png').convert_alpha()
        screen.blit(correct_img,(800,600))
        pygame.display.flip()

#game loop
exit = False
while not exit:
    #handle events
    for event in pygame.event.get():
        if event.type ==QUIT:
            exit = True
        elif event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                exit = True
        elif event.type == MOUSEBUTTONDOWN:
            x,y = pygame.mouse.get_pos()
            if (playrect.collidepoint(x,y)):
                    pygame.mixer.unpause()
            elif (pauserect.collidepoint(x,y)):
                    pygame.mixer.pause()

                    
            elif (catrect.collidepoint(x,y) and z == num[0]):
                Correct(screen)
            elif (dogrect.collidepoint(x,y) and z == num[1]):
                Correct(screen)
            elif (fishrect.collidepoint(x,y) and z == num[2]):
                Correct(screen)
            else:
                Wrong(screen)

                
    pygame.display.update()
    clock.tick(20)
                
pygame.quit()

Unfortunately, without the extra image and sound files, there's no way to test your program. Thus, all we have to go on is your comments here. Can you explain just what isn't working? What is going wrong?

I gather you aren't used to these forums; it can take a while to get an answer, so please be patient here.

Edited 5 Years Ago by Schol-R-LEA: n/a

Ok, well there are 3 random pictures chosen from the list and displayed to the screen
and one word is loaded onto the screen

the catpic, dogpic, fishpic labels I just used to load 3 "rects" the pictures themselves werent displayed

catpic = pygame.image.load(pic[0])
catrect = catpic.get_rect()
catrect.centerx, catrect.centery = \
                 150,200
 
dogpic = pygame.image.load(pic[1])
dogrect = dogpic.get_rect()
dogrect.centerx, dogrect.centery = \
                 360,200
 
fishpic = pygame.image.load(pic[2])
fishrect = fishpic.get_rect()
fishrect.centerx, fishrect.centery = \
                  580,200

so a list is created called 'num' with for instance [0,4,3] in it. these numbers represent the index of the names in pic

pic =

so cat and penguin and rabbit are displayed to the screen in that order

z is a random number chosen for the list 'num'
which is for instance 0
the worded loaded on the screen would be cat

the user must click the cat image.
the rect at the cat image i defined as 'catrect'

so the next code says if the mouse clicks on 'catrect' AND z(which is zero) is equal to n[0](also zero)
call the function Correct(screen) which displays a 'correct' image.
if the next image is seleced see is z is equal n[1] and so on

the problem is that it doesnt say correct or incorrect when it should

elif (catrect.collidepoint(x,y) and z == num[0]):
                Correct(screen)
            elif (dogrect.collidepoint(x,y) and z == num[1]):
                Correct(screen)
            elif (fishrect.collidepoint(x,y) and z == num[2]):
                Correct(screen)

Edited 5 Years Ago by nooony: n/a

Odd, I was able to test it myself (after finding some suitable pictures to replace yours with), and the 'correct' and 'incorrect' images seemed to work - though it did sometimes show them overlaying each other if you clicked on two different images. I did have to disable music play, so that may be part of it. Or was it also supposed to show the words 'correct' and 'wrong' as well?

Its ok if they overlay I can fix that later,

the problem is that if the word is 'dog' and I click the picture dog, it says incorrect sometimes. also if i click on the background in the upper left corner somewhere it also responds incorrect or correct.
The rects I think are not in position. If i click the pause button to pause the music i have to click on a specific point in the middle. not anywhere on the picture.

Thanks for all the effort and for your time but I think i got it to work :D
thank you !
I still need some help though if you know how I can make a menu for the game that says play and quit when I click 'escape' help would be great

This article has been dead for over six months. Start a new discussion instead.