doulikepeaches
doulikepeaches

Reputation: 69

How to update the positional coordinates of my rect?

I'm creating a memory card game, but having difficulty updating each boxes' rect starting positions. First for loop loads each image and the second get's the starting position for each one. When I do a collision test, it only answers to <0,125,0,175> as my rect position's aren't updating. How can I update this?

import pygame, sys
from pygame.locals import *

screen_height = 800
screen_width = 800
card_x_size = 125
card_y_size = 175
marginx = 45
marginy = 25

class Box():
    def __init__(self,image):
        self.image = image
        self.rect = image.get_rect()

    def draw(self,screen):
        screen.blit(self.image, self.rect)

def play_game():
    pygame.init()
    screen = pygame.display.set_mode((screen_width, screen_height))

    back_images = ['back.jpg']*16
    back_image_list = []
    for back_image in back_images:
        back_pic = pygame.image.load(back_image)
        back_pic = pygame.transform.scale(back_pic, (card_x_size,card_y_size))
        back_image_list.append(back_pic)
        rect = back_pic.get_rect()

    boxes = [Box(img) for img in back_image_list]


    for j, box in enumerate(boxes):
        pos_x = marginx + j % 4 * available_spacex
        pos_y = marginy + j // 4 * available_spacey
        box.rect.topleft = (pos_x, pos_y)
        print(pos_x,pos_y)


    while True:
        mouse_clicked = False
        clicked_card = False
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == MOUSEMOTION:
                mousex, mousey = event.pos
            elif event.type == MOUSEBUTTONUP:
                mouse_clicked = True
                print(mousex, mousey)
                if rect.collidepoint(mousex,mousey):
                    clicked_card = True
                    print("hit")

        for b in boxes:
            b.draw(screen)

        pygame.display.update()

play_game()

Upvotes: 2

Views: 128

Answers (1)

Rabbid76
Rabbid76

Reputation: 210878

You have to evaluate whether the mouse click is on one of the box objects:

for box in boxes:
    if box.rect.collidepoint(mousex, mousey):
        clicked_card = True
        print("hit")

function play_game:

def play_game():
    # [...]

    while True:
        mouse_clicked = False
        clicked_card = False
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == MOUSEMOTION:
                mousex, mousey = event.pos
            elif event.type == MOUSEBUTTONUP:
                mousex, mousey = event.pos
                mouse_clicked = True
                print(mousex, mousey)
                for box in boxes:
                    if box.rect.collidepoint(mousex, mousey):
                        clicked_card = True
                        print("hit")

        for b in boxes:
            b.draw(screen)

        pygame.display.update()

Upvotes: 2

Related Questions