0

I am relatively new to pygame, but have messed with python for a few years. I am trying to return the positional value of a cube while it is being dragged, and can't find a way to do this. I don't fully understand how get_rect() works and don't know if I can apply it to the class. Can anyone help me out? Below is what I have so far.

import pygame
import random

pygame.init()
w, h = 800, 500
screen = pygame.display.set_mode((w, h))

TAN = 255, 229, 204
bg = 0, 51, 102
WHITE = 255, 255, 255
BLACK = 0, 0, 0
RED = 235, 0, 0
BLUE = 0, 90, 255
GREEN = 0, 204, 0
FPS = 100
font = pygame.font.Font("freesansbold.ttf", 22)
redCubes = []



class Cube:
    def __init__(self, x, y, w, h, number, color):
        self.rect = pygame.rect.Rect(x, y, w, h)
        self.x, self.y, self.w, self.h = x, y, w, h
        self.number = number
        self.clicked = -1
        self.color = color

    def update(self):
        if self.clicked > 0:
            self.rect.center = pygame.mouse.get_pos()

    def cubePos(self):

        print( Cube.__init__().x)



for i in range(6):
    cube = random.choice(["0"]+["1"]+["2"]+["3"]+["4"]+["5"])
    redCubes.append(cube)


screen = pygame.display.set_mode((w, h))
all_rectangles = []
rectangle_1 = Cube(120, 220, 50, 50, redCubes[0], RED)
rectangle_2 = Cube(180, 220, 50, 50, redCubes[1], RED)
rectangle_3 = Cube(240, 220, 50, 50, redCubes[2], RED)
rectangle_4 = Cube(300, 220, 50, 50, redCubes[3], RED)
rectangle_5 = Cube(360, 220, 50, 50, redCubes[4], RED)
rectangle_6 = Cube(420, 220, 50, 50, redCubes[5], RED)


all_rectangles.append(rectangle_1)
all_rectangles.append(rectangle_2)
all_rectangles.append(rectangle_3)
all_rectangles.append(rectangle_4)
all_rectangles.append(rectangle_5)
all_rectangles.append(rectangle_6)

clock = pygame.time.Clock()


forbText = font.render("Forbidden", True, BLACK)
permText = font.render("Permitted", True, BLACK)
reqText = font.render("Required", True, BLACK)
textRect = forbText.get_rect()


uselessBool = 1
dragValue = False
def dragging():
    global all_rectangles, uselessBool, dragValue
    for dragRect in all_rectangles:

        if event.type == pygame.MOUSEBUTTONDOWN:

            if event.button == 1:

                if uselessBool == 1:
                    if dragRect.rect.collidepoint(event.pos):
                        if event.type == pygame.MOUSEMOTION:
                            dragRect.clicked = dragRect.clicked * -1
                            dragValue = dragRect.clicked
                            uselessBool = 0
                            dragRect.clicked

                        else:
                            dragRect.clicked = dragRect.clicked * -1
                            uselessBool = 0
                            dragValue = dragRect.clicked
                        Cube.cubePos




    if event.type == pygame.MOUSEBUTTONUP:
        uselessBool = 1

running = True
while running:
    screen.fill((TAN))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    screen.fill(WHITE)

    dragging()


    for r in all_rectangles:
        r.update()
        rect = pygame.draw.rect(screen, BLACK, (r.rect.x, r.rect.y, 50, 50))
        inRect = pygame.draw.rect(screen, r.color, (r.rect.x + 2, r.rect.y + 2, 46, 46))

        numberTxt = font.render(r.number, True, WHITE)
        screen.blit(numberTxt, (r.rect.x + 18, r.rect.y + 13))

    clock.tick(FPS)
    pygame.display.flip()
    pygame.display.update()

pygame.quit()

0 Answers0