4

I am beginner in python. Recently i am doing a pygame poject. I want to make a game where the screen will show an image in a random position, and after 0.3 seconds, the image will move to another random position again. The player will repeatedly click on the position-changed image with the mouse and the score will increase. Even if I do everything, clicking with the mouse does not increase the score.

Here is my code:

pygame.init()
width = 500
height = 500
score = 0
display = pygame.display.set_mode((width, height))
pygame.display.set_caption("Tapping")
image = pygame.image.load('spaceship.png').convert()
sides = ['Top', 'Botom', 'left', 'Right']
weights = [width, width, height, height]
posx = random.randint(50, 450)
posy = random.randint(20, 460)
tsp = 1.2
Mousex = 0
Mousey = 0

def image_view(x, y):
    display.blit(image, (x, y))

run = True
while run:
    display.fill((153, 255, 187))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            Mousex, Mousey = event.pos
            if image.get_rect().collidepoint(posx, posy):
                score += 1

    side = random.choices(sides, weights)[0]

    if side == 'Top':
        posx = random.randrange(100, 300)
        posy = random.randrange(20, 100)
        time.sleep(tsp)
    elif side == 'Botom':
        posx = random.randrange(350, 430)
        posy = random.randrange(250, 450)
        time.sleep(tsp)
    elif side == 'left':
        posx = random.randrange(20, 250)
        posy = random.randrange(20, 250)
        time.sleep(tsp)
    elif side == 'Right':
        posx = random.randrange(280, 450)
        posy = random.randrange(280, 450)
        time.sleep(tsp)

    print(score)
    image_view(posx, posy)
    pygame.display.update()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174

1 Answers1

4

You have to evaluate if the mouse is on the image. Note, a pygame.Surface has no position. It is blit at a position. Hence the position of the pygame.Rect object, which is returned by get_rect() is (0, 0).
You have to set the position by a keyword argument (e.g. image.get_rect(topleft = (posx, posy))). Finally you can use collidepoint() to evaluate if the mouse cursor (Mousex, Mousey) is on the region of the display, where the image is currently placed:

if event.type == pygame.MOUSEBUTTONDOWN:
    Mousex, Mousey = event.pos
    image_rect = image.get_rect(topleft = (posx, posy))
    if image_rect.collidepoint(Mousex, Mousey):
        score += 1

Further more, time.sleep(tsp) prevents the system from responding. Never delay the main application loop.
Use pygame.time.get_ticks() to get get the time in milliseconds. Add a variable next_choice_time. The time indicates when the position of the image has to be changed. Set a new time when the position of the image is changed:

next_choice_time = 0
while run:
    current_time = pygame.time.get_ticks()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            Mousex, Mousey = event.pos
            image_rect = image.get_rect(topleft = (posx, posy))
            if image_rect.collidepoint(Mousex, Mousey):
                score += 1
                next_choice_time = current_time

    if current_time >= next_choice_time:
        next_choice_time = current_time + 300 # 300 milliseconds == 0.3 seconds
        side = random.choices(sides, weights)[0]
        # [...]
    

See the example:

import pygame
import random

pygame.init()
width = 500
height = 500
score = 0
display = pygame.display.set_mode((width, height))
pygame.display.set_caption("Tapping")
image = pygame.image.load('spaceship.png').convert()
sides = ['Top', 'Botom', 'left', 'Right']
weights = [width, width, height, height]
posx = random.randint(50, 450)
posy = random.randint(20, 460)
tsp = 1.2
Mousex = 0
Mousey = 0

def image_view(x, y):
    display.blit(image, (x, y))

run = True

next_choice_time = 0
while run:
    current_time = pygame.time.get_ticks()
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            Mousex, Mousey = event.pos
            image_rect = image.get_rect(topleft = (posx, posy))
            if image_rect.collidepoint(Mousex, Mousey):
                score += 1
                next_choice_time = current_time
                print(score)

    if current_time >= next_choice_time:
        next_choice_time = current_time + 300 # 300 milliseconds == 0.3 seconds
        side = random.choices(sides, weights)[0]
        if side == 'Top':
            posx = random.randrange(100, 300)
            posy = random.randrange(20, 100)
        elif side == 'Botom':
            posx = random.randrange(350, 430)
            posy = random.randrange(250, 450)
        elif side == 'left':
            posx = random.randrange(20, 250)
            posy = random.randrange(20, 250)
        elif side == 'Right':
            posx = random.randrange(280, 450)
            posy = random.randrange(280, 450)

    display.fill((153, 255, 187))
    image_view(posx, posy)
    pygame.display.update()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174