如何在pygame中使用光标移动移动Sprite图像?

时间:2017-02-24 16:39:40

标签: python python-3.x pygame pygame-surface pygame-tick

现在低于我所做的只是创建一个精灵如图所示.. 进一步做一些有趣的事情,想到线程我添加了一个线程,它将检查光标位置,因此更新全局x& y导致由display.update触发的精灵位置的变化 我是新手所以我可能在很多方面都错了...所以请耐心等待.... 非常感谢提前

    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)

***以下是我的最新代码----------根据答案更新***请检查

    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        

1 个答案:

答案 0 :(得分:0)

线程只会使事情复杂化。摆脱它,并从self.rect.move_ip(x,y)中取出display.update()__init__。在名为update()的类中创建一个函数。这个函数只会通过说明来移动矩形。 self.rect.center = pygame.mouse.get_pos()。然后在主游戏循环中,将sprite1.update()放在那里或者使用组名更改它(就像你对spritegrp.update()所做的那样),然后在那里调用display.update()而不是在函数中

其他事项:

  • super().__init__()不需要任何参数
  • ifwhile循环不需要括号
  • 你不需要导入main(我可能错了,但我不这么认为)
  • from module import *一般来说是一种不好的做法,但是如果你打算这样做(我不推荐),你就不能把module.method放在任何地方。你做了from pygame import *,但仍然放了pygame.mouse和其他类似的人。也许你的意思是from pygame.locals import *
  • 冒号在它们和单词之间没有空格,即for evt in event.get() :不好
  • 缩进也应该是制表符的长度,或四个空格。 (你使用的是什么IDE?大多数是自动完成的。)
  • 变量赋值应包含空格:x = False
相关问题