vendredi 20 mai 2016

How to make images move to random position on clicking in pygame?

I want to move the apple image to a random position whenever i click it.The first time i click it , it works well but next time i try to do it , it gives me no response.
Here is the code:

import pygame
pygame.init()
foodimg=pygame.image.load("food.png")
foodrect=foodimg.get_rect()
white = (255,255,255)
#Game Display
display_width = 1080
display_height  = 720
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Click It! ~ Snapnel Productions')
gameDisplay.fill((white))
running=True
while running:
    gameDisplay.fill((white))

    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            running=False
            pygame.quit()
            quit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            # Set the x, y postions of the mouse click
            x, y = event.pos
            if foodimg.get_rect().collidepoint(x, y):
                foodrect.center=(random.randint(5,1060),random.randint(5,700))
                print "Hi",
                continue 
    gameDisplay.blit(foodimg,foodrect)
    pygame.display.flip()

This is the food image.




Aucun commentaire:

Enregistrer un commentaire