0

我需要手动检查一张图像是否撞到另一张图像。我似乎无法得到结果......拜托这让我发疯了!我已经尝试了2天,它不会工作。

我没有包含其他一些代码,例如 Menu().main(),但它运行良好。我不明白为什么当僵尸在玩家身上时它不会返回 true。

class Game():

    def setup(self):
        global tiles
        for files in os.listdir(os.curdir):
            if files.endswith(".png") and files != "Player.png" and files != "Zombie.png":
                tilees.append(files)
                numtil = tilees.count(files)
                print("a " + str(numtil))

        fli = pygame.image.load(files).convert_alpha()

        i = 0

        while i <= 1000:
            tiles.append(fli)
            i = i + 1

        global TileLen
        TileLen = tiles.count(fli)

        Game().Game()

    def Game(self):
        global zomx, zomy, zombie, MMS, step, zombieRect
        zombieRect = []
        zomx = []
        zomy = []
        zombie = []
        step = 0
        HP = 15
        Map = pygame.image.load("BG.png").convert()
        Map = pygame.transform.scale(Map, (pygame.display.list_modes()[0]))

        person = pygame.image.load("Player.png").convert()
        person = pygame.transform.scale(person, (30, 30))

        dagger = pygame.image.load("Dagger.png").convert_alpha()
        dagger = pygame.transform.scale(dagger, (13, 13))

        screen.blit(Map, (0, 0))
        perx = 800
        pery = 700

        ai().zombieadd(MMS)

        screen.blit(person, (perx, pery))
        screen.blit(dagger, (perx+20, pery-5))

        global thething

        thething = 0
        for crap in zombie:

            screen.blit(zombie[thething], (zomx[thething], zomy[thething]))
            print(thething)
            thething = thething + 1

        pygame.display.flip()

        awesome = 1
        fttt = 0
        ff = 0
        ofx = 0
        ofy = 0

        while 1:
            print(perx, pery)
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()
                elif event.type == KEYDOWN:
                    if event.key == K_w:
                        pery = pery - 5
                        if fttt != 1:
                            if awesome == 4:
                                dagger = pygame.transform.rotate(dagger, 270)
                                person = pygame.transform.rotate(person, 270)
                            elif awesome == 3:
                                dagger = pygame.transform.rotate(dagger, 90)
                                person = pygame.transform.rotate(person, 90)
                            elif awesome == 2:
                                dagger = pygame.transform.rotate(dagger, 180)
                                person = pygame.transform.rotate(person, 180)
                            person = pygame.transform.rotate(person, 0)
                            dagger = pygame.transform.rotate(dagger, 0)
                        awesome = 1
                        fttt = 1

                    elif event.key == K_s:
                        pery = pery + 5
                        if fttt != 2:
                            if awesome == 4:
                                dagger = pygame.transform.rotate(dagger, 270)
                                person = pygame.transform.rotate(person, 270)
                            elif awesome == 3:
                                dagger = pygame.transform.rotate(dagger, 90)
                                person = pygame.transform.rotate(person, 90)
                            person = pygame.transform.rotate(person, 180)
                            dagger = pygame.transform.rotate(dagger, 180)
                        awesome = 2
                        fttt = 2

                    elif event.key == K_a:
                        perx = perx - 5
                        if fttt != 3:
                            if awesome == 2:
                                dagger = pygame.transform.rotate(dagger, 180)
                                person = pygame.transform.rotate(person, 180)
                            elif awesome == 4:
                                dagger = pygame.transform.rotate(dagger, 270)
                                person = pygame.transform.rotate(person, 270)
                            person = pygame.transform.rotate(person, 270)
                            dagger = pygame.transform.rotate(dagger, 270)
                        awesome = 3
                        fttt = 3

                    elif event.key == K_d:
                        perx = perx + 5
                        if fttt != 4:
                            if awesome == 2:
                                dagger = pygame.transform.rotate(dagger, 180)
                                person = pygame.transform.rotate(person, 180)
                            elif awesome == 3:
                                dagger = pygame.transform.rotate(dagger, 90)
                                person = pygame.transform.rotate(person, 90)
                            person = pygame.transform.rotate(person, 90)
                            dagger = pygame.transform.rotate(dagger, 90)
                        awesome = 4
                        fttt = 4

                    elif event.key == K_e:
                        if awesome == 1:
                            while ofx >= -6:
                                ofx = ofx - 1
                                screen.blit(dagger, (perx+20, pery-5+ofx))
                                pygame.time.delay(1)
                                pygame.display.update()
                            if ofx <= -6:
                                ofx = 0
                        elif awesome == 2:
                            while ofx >= -6:
                                ofx = ofx - 1
                                screen.blit(dagger, (perx-5, pery+25-ofx))
                                pygame.time.delay(1)
                                pygame.display.update()
                            if ofx <= -6:
                                ofx = 0
                        if awesome == 3:
                            while ofx >= -6:
                                ofx = ofx - 1
                                screen.blit(dagger, (perx-5+ofx, pery-5))
                                pygame.time.delay(1)
                                pygame.display.update()
                            if ofx <= -6:
                                ofx = 0
                        if awesome == 4:
                            while ofx >= -6:
                                ofx = ofx - 1
                                screen.blit(dagger, (perx+20-ofx, pery+25))
                                pygame.time.delay(1)
                                pygame.display.update()
                            if ofx <= -6:
                                ofx = 0



            screen.blit(Map, (0, 0))
            screen.blit(person, (perx, pery))
            if awesome == 1:
                screen.blit(dagger, (perx+20, pery-5))
            elif awesome == 2:
                screen.blit(dagger, (perx-5, pery+25))
            elif awesome == 3:
                screen.blit(dagger, (perx-5, pery-5))
            else:
                screen.blit(dagger, (perx+20, pery+25))

            text = "Health: "+str(HP)
            font = pygame.font.Font(None, 32)
            ttex = font.render(text, True, (255, 0, 0))
            screen.blit(ttex, (30, 30))


            thecrap = 0
            while thecrap < thething:
                print(thecrap)
                screen.blit(zombie[thecrap], (zomx[thecrap], zomy[thecrap]))
                if perx >= zomx[thecrap]:
                    loop = 0
                    while loop <= 5:
                        zomx[thecrap] = zomx[thecrap] + .09
                        loop = loop + 1

                if pery >= zomy[thecrap]:
                    loop = 0
                    while loop <= 5:
                        zomy[thecrap] = zomy[thecrap] + .09
                        loop = loop + 1
                if perx <= zomx[thecrap]:
                    loop = 0
                    while loop <= 5:
                        zomx[thecrap] = zomx[thecrap] - .09
                        loop = loop + 1
                if pery <= zomy[thecrap]:
                    loop = 0
                    while loop <= 5:
                        zomy[thecrap] = zomy[thecrap] - .09
                        loop = loop + 1
                if person.get_rect().contains(pygame.Rect(zomx[thecrap], zomy[thecrap], 30, 30)):
                    HP = HP - .5
                thecrap = thecrap + 1
            thecrap = 0
            pygame.display.update()

            if HP <= 0:
                print("Game over! Sorry!")
                while 1:
                    abss = font.render("Sorry, but you lost! Press any key to continue...", True, (255, 255, 255))
                    pygame.display.update()
                    screen.blit(abss, (90, 90))

                    for event in pygame.event.get():
                        Menu().main()

class ai():

    def zombieadd(self, Num):
        global zomx, zomy, zombie, zombieRect
        zombieRect = []
        zombie = []
        global step

        step = 0
        print(Num)

        while Num > step:
            global zomx, zomy, zombie
            print(step)
            zomx.append(random.randint(0, 600))
            zomy.append(random.randint(0, 600))
            zombieI = pygame.image.load("Zombie.png").convert()
            zombie.append(zombieI)
            zombie[step] = pygame.transform.scale(zombie[step], (30, 30))
            print(zomx[step], zomy[step])
            zombieRect.append(zombie[step].get_rect())

            step = step + 1

        Num = 0

        return 0


Menu().main()

class Game():

    def setup(self):
        global tiles
        for files in os.listdir(os.curdir):
            if files.endswith(".png") and files != "Player.png" and files != "Zombie.png":
                tilees.append(files)
                numtil = tilees.count(files)
                print("a " + str(numtil))

        fli = pygame.image.load(files).convert_alpha()

        i = 0

        while i <= 1000:
            tiles.append(fli)
            i = i + 1

        global TileLen
        TileLen = tiles.count(fli)

        Game().Game()

    def Game(self):
        global zomx, zomy, zombie, MMS, step, zombieRect
        zombieRect = []
        zomx = []
        zomy = []
        zombie = []
        step = 0
        HP = 15
        Map = pygame.image.load("BG.png").convert()
        Map = pygame.transform.scale(Map, (pygame.display.list_modes()[0]))

        person = pygame.image.load("Player.png").convert()
        person = pygame.transform.scale(person, (30, 30))

        dagger = pygame.image.load("Dagger.png").convert_alpha()
        dagger = pygame.transform.scale(dagger, (13, 13))

        screen.blit(Map, (0, 0))
        perx = 800
        pery = 700

        ai().zombieadd(MMS)

        screen.blit(person, (perx, pery))
        screen.blit(dagger, (perx+20, pery-5))

        global thething

        thething = 0
        for crap in zombie:

            screen.blit(zombie[thething], (zomx[thething], zomy[thething]))
            print(thething)
            thething = thething + 1

        pygame.display.flip()

        awesome = 1
        fttt = 0
        ff = 0
        ofx = 0
        ofy = 0

        while 1:
            print(perx, pery)
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()
                elif event.type == KEYDOWN:
                    if event.key == K_w:
                        pery = pery - 5
                        if fttt != 1:
                            if awesome == 4:
                                dagger = pygame.transform.rotate(dagger, 270)
                                person = pygame.transform.rotate(person, 270)
                            elif awesome == 3:
                                dagger = pygame.transform.rotate(dagger, 90)
                                person = pygame.transform.rotate(person, 90)
                            elif awesome == 2:
                                dagger = pygame.transform.rotate(dagger, 180)
                                person = pygame.transform.rotate(person, 180)
                            person = pygame.transform.rotate(person, 0)
                            dagger = pygame.transform.rotate(dagger, 0)
                        awesome = 1
                        fttt = 1

                    elif event.key == K_s:
                        pery = pery + 5
                        if fttt != 2:
                            if awesome == 4:
                                dagger = pygame.transform.rotate(dagger, 270)
                                person = pygame.transform.rotate(person, 270)
                            elif awesome == 3:
                                dagger = pygame.transform.rotate(dagger, 90)
                                person = pygame.transform.rotate(person, 90)
                            person = pygame.transform.rotate(person, 180)
                            dagger = pygame.transform.rotate(dagger, 180)
                        awesome = 2
                        fttt = 2

                    elif event.key == K_a:
                        perx = perx - 5
                        if fttt != 3:
                            if awesome == 2:
                                dagger = pygame.transform.rotate(dagger, 180)
                                person = pygame.transform.rotate(person, 180)
                            elif awesome == 4:
                                dagger = pygame.transform.rotate(dagger, 270)
                                person = pygame.transform.rotate(person, 270)
                            person = pygame.transform.rotate(person, 270)
                            dagger = pygame.transform.rotate(dagger, 270)
                        awesome = 3
                        fttt = 3

                    elif event.key == K_d:
                        perx = perx + 5
                        if fttt != 4:
                            if awesome == 2:
                                dagger = pygame.transform.rotate(dagger, 180)
                                person = pygame.transform.rotate(person, 180)
                            elif awesome == 3:
                                dagger = pygame.transform.rotate(dagger, 90)
                                person = pygame.transform.rotate(person, 90)
                            person = pygame.transform.rotate(person, 90)
                            dagger = pygame.transform.rotate(dagger, 90)
                        awesome = 4
                        fttt = 4

                    elif event.key == K_e:
                        if awesome == 1:
                            while ofx >= -6:
                                ofx = ofx - 1
                                screen.blit(dagger, (perx+20, pery-5+ofx))
                                pygame.time.delay(1)
                                pygame.display.update()
                            if ofx <= -6:
                                ofx = 0
                        elif awesome == 2:
                            while ofx >= -6:
                                ofx = ofx - 1
                                screen.blit(dagger, (perx-5, pery+25-ofx))
                                pygame.time.delay(1)
                                pygame.display.update()
                            if ofx <= -6:
                                ofx = 0
                        if awesome == 3:
                            while ofx >= -6:
                                ofx = ofx - 1
                                screen.blit(dagger, (perx-5+ofx, pery-5))
                                pygame.time.delay(1)
                                pygame.display.update()
                            if ofx <= -6:
                                ofx = 0
                        if awesome == 4:
                            while ofx >= -6:
                                ofx = ofx - 1
                                screen.blit(dagger, (perx+20-ofx, pery+25))
                                pygame.time.delay(1)
                                pygame.display.update()
                            if ofx <= -6:
                                ofx = 0



            screen.blit(Map, (0, 0))
            screen.blit(person, (perx, pery))
            if awesome == 1:
                screen.blit(dagger, (perx+20, pery-5))
            elif awesome == 2:
                screen.blit(dagger, (perx-5, pery+25))
            elif awesome == 3:
                screen.blit(dagger, (perx-5, pery-5))
            else:
                screen.blit(dagger, (perx+20, pery+25))

            text = "Health: "+str(HP)
            font = pygame.font.Font(None, 32)
            ttex = font.render(text, True, (255, 0, 0))
            screen.blit(ttex, (30, 30))


            thecrap = 0
            while thecrap < thething:
                print(thecrap)
                screen.blit(zombie[thecrap], (zomx[thecrap], zomy[thecrap]))
                if perx >= zomx[thecrap]:
                    loop = 0
                    while loop <= 5:
                        zomx[thecrap] = zomx[thecrap] + .09
                        loop = loop + 1

                if pery >= zomy[thecrap]:
                    loop = 0
                    while loop <= 5:
                        zomy[thecrap] = zomy[thecrap] + .09
                        loop = loop + 1
                if perx <= zomx[thecrap]:
                    loop = 0
                    while loop <= 5:
                        zomx[thecrap] = zomx[thecrap] - .09
                        loop = loop + 1
                if pery <= zomy[thecrap]:
                    loop = 0
                    while loop <= 5:
                        zomy[thecrap] = zomy[thecrap] - .09
                        loop = loop + 1
                if person.get_rect().contains(zombieRect)):
                    HP = HP - .5
                thecrap = thecrap + 1
            thecrap = 0
            pygame.display.update()

            if HP <= 0:
                print("Game over! Sorry!")
                while 1:
                    abss = font.render("Sorry, but you lost! Press any key to continue...", True, (255, 255, 255))
                    pygame.display.update()
                    screen.blit(abss, (90, 90))

                    for event in pygame.event.get():
                        Menu().main()

class ai():

    def zombieadd(self, Num):
        global zomx, zomy, zombie, zombieRect
        zombieRect = []
        zombie = []
        global step

        step = 0
        print(Num)

        while Num > step:
            global zomx, zomy, zombie
            print(step)
            zomx.append(random.randint(0, 600))
            zomy.append(random.randint(0, 600))
            zombieI = pygame.image.load("Zombie.png").convert()
            zombie.append(zombieI)
            zombie[step] = pygame.transform.scale(zombie[step], (30, 30))
            print(zomx[step], zomy[step])
            zombieRect.append(zombie[step].get_rect())

            step = step + 1

        Num = 0

        return 0


Menu().main()
4

1 回答 1

0

你在很多事情上都走了很长一段路。

PyGame 的 Sprite 类针对各种游戏内功能进行了优化,您似乎没有使用它。

但是,如果您只想处理 Rects,则必须考虑正在比较哪些 Rects。图像是 pygame.Surface 对象 -get_rect调用后,您将获得它们的尺寸但不一定是它们的坐标。您从任何 Surface 对象的 get_rect() 调用返回的 Rect 将在 (0, 0) 处包含其 rect.x 和 rect.y。

幸运的是—— get_rect() 调用允许您传入参数。因此,无论您使用什么方法来跟踪角色的 x 和 y 坐标,您都可以在 get_rect() 调用期间使用关键字将它们传入,以获取pygame.Rect正确反映其坐标的对象。

例如——这不是直接基于你的代码,所以如果这在第一遍就爆炸了,不是我的错...... :)

zombieRect = zombie_image.get_rect(topleft=(zombieX, zombieY))

...然后 THIS 用于与 Player rect 进行比较,或者您想要将其撞到的任何 Rect - 使用类似的get_rect(topleft=(x, y))技术 - 您可能会开始看到结果。

说了这么多,我强烈建议你用 PyGame 对象将事物合并到类中。PyGame 的大部分内容是用 C 编写的,如果我没记错的话,它的一部分是用 FORTRAN 和 ASM 编写的(请不要让我对这两个语言都抱有误解,我可能记错了)所以只要把东西放在一个适当的类下,你会让你的生活轻松很多。

于 2014-05-17T15:22:19.587 回答