0

Now below what i did is simply created a sprite as shown.. futher to do something interesting,thought of threading i added a thread which will check the cursor position, hence update global x & y resulting in change of sprite position that is trigered by display.update I am newbie so i might me wrong in many ways ...so please bear me.... a lot thanks in advance

    from pygame import *
    from main import *
    import threading
    import sys
    #lets add our sprites

    (x,y) = (0, 0)

    class threading1(threading.Thread):


        def run(self):
            global x,y
            clockobj1 = pygame.time.Clock()
            while (True):

                clockobj1.tick(6)
                (x,y)=pygame.mouse.get_pos()
                display.update()


    class sprites(pygame.sprite.Sprite ):

        def __init__(self,color= redd, width=120, height=120):
            super(sprites,self).__init__()
            self.image = pygame.Surface((width,height))
            self.image.fill(color)

            self.rect=self.image.get_rect()
            self.rect.move_ip(x,y)
            display.update()



    if __name__ == '__main__':
        clockobj = pygame.time.Clock()


        init()
        mainwin = pygame.display.set_mode((720,640))

        sprite1 =  sprites()


        spritegrp =  pygame.sprite.Group()
        spritegrp.add(sprite1)
        spritegrp.update()
        mainwin.fill(blue)
        spritegrp.draw(mainwin)
        threadingobj = threading1()
        threadingobj.start()


        x = True
        while(x):
            display.update()

            for evt in event.get() :
             if (evt.type == QUIT) :
                 quit()
                 x=False
             clockobj.tick(40)

***BELOW IS MY LATEST CODE----------UPDATED AS PER ANSWERS***PLEASE CHECK

    import pygame
    from main import *
    import threading
    import sys

    #  lets add our sprites


    class Sprites(pygame.sprite.Sprite ):
        def __init__(self, color=redd, width=120, height=120):
            super().__init__()
            self.image = pygame.Surface((width, height))
            self.image.fill(color)
            self.rect = self.image.get_rect()

        def updaterect(self):
            print("i m in updatereact")
            print(pygame.mouse.get_pos())
            self.rect.center= pygame.mouse.get_pos()
            pygame.display.update()
    if __name__ == '__main__':
        clockobj = pygame.time.Clock()
        pygame.init()
        mainwin = pygame.display.set_mode((720,640))
        sprite1 = Sprites()
        sprite1.updaterect()
        spritegrp = pygame.sprite.Group()
        spritegrp.add(sprite1)
        spritegrp.update()
        mainwin.fill(blue)
        spritegrp.draw(mainwin)
        x = True
        while x:
            sprite1.updaterect()
            pygame.display.update()
            for evt in pygame.event.get() :
             if evt.type == pygame.QUIT :
                 quit()
                 x=False        
  • I don't use sprites in Pygame nor classes in Python so I can't fix your code, but try teleporting the sprite position to your cursor position every loop. Make their x and y positions the same every loop. – Douglas Feb 24 '17 at 20:00
  • please check my update – Kunal Sikri Feb 25 '17 at 06:20

1 Answers1

1

Threading will only just complicate things. Get rid of it, and take self.rect.move_ip(x,y) and display.update() out of the __init__ for the class. Make a function in the class called update(). This function will move the rect just by saying. self.rect.center = pygame.mouse.get_pos(). Then in the main game loop, put sprite1.update() in there or update it by using the group name instead (as you did with spritegrp.update() ) and then call display.update() there too instead of in the function.

Other things:

  • super().__init__() doesn't need any args
  • if and while loops don't need parentheses
  • you don't need to import main (I may be wrong about this but I don't think so)
  • from module import * is a bad practice in general, but if you are going to do it (I don't recommend it), you can't put module.method anywhere. You did from pygame import *, but still put pygame.mouse and others like that. Maybe you meant from pygame.locals import * ?
  • colons don't have a space in between them and the word, i.e. for evt in event.get() : is bad
  • Indentation should also be the length of a tab, or four spaces. (What IDE are you using? Most do it automatically.)
  • variables assignments should have spaces: x = False
makeworld
  • 1,266
  • 12
  • 17
  • Sir please check my update, problem that i am facing now is that it is not updating that is at starting it moves to cursor position thats all..it is doing – Kunal Sikri Feb 25 '17 at 06:19
  • @KunalSikri The code is not runnable, could you please update with runnable code, and also change the things I said above. – makeworld Feb 25 '17 at 15:55
  • I have Reupdated the code as per checking it on pycharm, it runs fine without any errors however the motive of code is still not acheived.. thanks – Kunal Sikri Feb 27 '17 at 06:04
  • @KunalSikri Try changing `updatereact` to this: `updatereact(self, pos):` and then change this line: `self.rect.center= pygame.mouse.get_pos()` to `self.rect.center = pos`. Then pass `pygame.mouse.get_pos()` when calling the method. – makeworld Feb 27 '17 at 23:40
  • Thank you so much...@Cole128... but finally i have found answer that is i just needed to draw the group again i.e spritegrp.draw(mainwin) that solves the prob.. – Kunal Sikri Mar 01 '17 at 03:55
  • Sorry I couldn't help you fully, @KunalSikri , but glad you fixed it. – makeworld Mar 01 '17 at 23:32