0

I am testing to move different objects on the screen but independently from each other, the problem is that when i move one object on top of another the last one disappears although i am bliting each of them constantly on the screen. Why is this happening and how can i do this better?

import pygame
from pygame.locals import *
from random import randrange


class Tst(object):

    def __init__(self):
        self.img=pygame.Surface((20,20))
        self.img.fill((100,200,0))
        self.rect=self.img.get_rect(center=(randrange(780),randrange(480)))
        #pygame.draw.rect(TV,(0,0,255),self.rect,1)
        TV.blit(self.img,self.rect)
    def move(self):
        mouse_pos=pygame.mouse.get_pos()
        if pygame.mouse.get_pressed()[0]:
            if self.rect.collidepoint(mouse_pos):
                TV.fill((0,0,0),self.rect)
                self.rect=pygame.Rect(mouse_pos[0]-10,mouse_pos[1]-10,20,20)
        TV.blit(self.img,self.rect)


pygame.init()
TV=pygame.display.set_mode((800,500))

tstList=[]
for _ in range(10):
    tstList.append(Tst())

while 1:
    for e in pygame.event.get():
        if e.type==QUIT:
        pygame.quit()

    for tst in tstList:
        tst.move()

    pygame.display.flip()

1 Answers1

1

The problem is with this line:

self.rect=pygame.Rect(mouse_pos[0]-10,mouse_pos[1]-10,20,20)

Basically, they are all at the mouse position. If you give each square a name and print the name and rect to the console, you can see this. When the mouse is over a square you set are setting it to the mouse pos, and eventually all the squares are on top of one another at the mouse pos. I am not sure exactly what you are trying to do so am unable to say what you should do to change this, as it depends on what you are trying to achieve. Let me know if you need any more advice and I'll try to help.

Here is the code which prints them out:

import pygame
from pygame.locals import *
from random import randrange


class Tst(object):

    def __init__(self, name):
        self.name = name
        self.img=pygame.Surface((20,20))
        self.img.fill((100,200,0))
        self.rect=self.img.get_rect(center=(randrange(780),randrange(480)))
        #print self.rect
        #pygame.draw.rect(TV,(0,0,255),self.rect,1)
        TV.blit(self.img,self.rect)
    def move(self):
        mouse_pos=pygame.mouse.get_pos()
        if pygame.mouse.get_pressed()[0]:
            if self.rect.collidepoint(mouse_pos):
                TV.fill((0,0,0),self.rect)
                self.rect=pygame.Rect(mouse_pos[0]-10,mouse_pos[1]-10,20,20)
                print self.name, 
                print self.rect
        TV.blit(self.img,self.rect)


pygame.init()
TV=pygame.display.set_mode((800,500))

tstList=[]
for x in range(10):
    print x
    letters = "abcdefghij"
    tstList.append(Tst(letters[x]))

while 1:
    for e in pygame.event.get():
        if e.type==QUIT:
            pygame.quit()

    for tst in tstList:
        tst.move()
        #print tst.img.get_rect()
    pygame.display.flip()
marienbad
  • 1,461
  • 1
  • 9
  • 19