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()