达到最大目标数后,如何停止pygame中的事件循环?

时间:2019-02-10 16:17:49

标签: python python-3.x pygame

我正在尝试结束事件循环,其中已单击的目标数量已与变量NoOfTargets相同(在此示例中为10)。

我认为,由于事件循环处理所有pygame事件,因此我决定进行验证以查看count是否与NoOfTargets相同。 count最初设置为0。单击新目标时,它将为计数增加+1。

            import pygame
            import random
            import math
            pygame.init()

            display_width = 800
            display_height = 600
            NoOfTargets = 10
            Target_radius = 10
            Frames_Per_Second = 60

            black = (0,0,0)
            white = (255,255,255)
            red = (255,0,0)

            def Target(Targetx, Targety , Targetr, colour):
                pygame.draw.circle(gameDisplay, colour, [Targetx, Targety], Targetr)
                pygame.display.update()

            def Pythagoras_Calculator(Target_x, Target_y, Mouse_X, Mouse_Y, Radius):
                Side = pow((Target_x-Mouse_X), 2) + pow((Target_y-Mouse_Y), 2)
                Distance = math.sqrt(Side)
                if Distance > Radius:
                    return False
                else:
                    return True

            def Get_Event_Time(GameTime):
                ListOfClickTime = []
                ListOfClickTime.append(GameTime)
                for x in range(len(ListOfClickTime)):
                    print(ListOfClickTime[x])

            ##    ListOfTime = []
            ##    TimeAtEventClick = pygame.time.get_ticks()
            ##    print("Time at event click", TimeAtEventClick)
            ##    print("Game Time:", GameTime)
            ##    TimeBetweenEvent = TimeAtEventClick-GameTime
            ##    print("Time between event", TimeBetweenEvent)
            ##    ListOfTime.append(int(TimeBetweenNewEvent))

            def Test(GameTime):
                print("Time Between EVENTS:",GameTime)

            gameDisplay = pygame.display.set_mode((display_width, display_height))
            pygame.display.set_caption("XD machine")
            clock = pygame.time.Clock()
            x = (display_width * 0.45)
            y = (display_height * 0.8)


            gameDisplay.fill(white)
            pygame.display.update()

            Random_x = random.randrange(0, display_width-Target_radius)
            Random_y = random.randrange(0, display_height-Target_radius)

            GameEnded = False
            while not GameEnded:
                count = 0
                Target(Random_x, Random_y, Target_radius, red)

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

                    elif count == NoOfTargets:
                        GameEnded = False
                        pygame.quit()

                    elif event.type == pygame.MOUSEBUTTONDOWN:
                        Mouse_X, Mouse_Y = event.pos
                        #GameTime = pygame.time.get_ticks()#Gets the time in milliseconds.
                        #Test(GameTime)
                        OnTarget = Pythagoras_Calculator(Random_x, Random_y, Mouse_X, Mouse_Y, Target_radius)
                        print(Mouse_X, Mouse_Y, OnTarget)
                        print(Random_x, Random_y)
                        if OnTarget == True:
                            count =+ 1
                            Random_x = random.randrange(0, display_width)
                            Random_y = random.randrange(0, display_height)
                            gameDisplay.fill(white)
                            pygame.display.update()
                            Target(Random_x, Random_y, Target_radius, red)
                            GameTime = pygame.time.get_ticks()
                            Get_Event_Time(GameTime)



            """if __name__ == "__Main__":
                pygame.init()
                Main()
                pygame.quit()
                quit()"""

我想要它,以便在计数时(它记录了已单击的目标数)与NoOfTargets相同,以便eventloop中断并且游戏停止。请帮助我成为pygame的新手。

1 个答案:

答案 0 :(得分:1)

在循环count内连续设置为0。count = 0必须在主循环之前而不是在循环内部完成:

count = 0
while not GameEnded:
    Target(Random_x, Random_y, Target_radius, red)

count =+ 1并没有达到您的期望。 = + 1将为count分配1。注意=是赋值,+只是该值的正号。增量分配是+=而不是=+

if OnTarget == True:
    count += 1

最好评估count是否超出限制,最好在事件循环之外进行。请注意,如果仅发生事件,则将执行事件循环。
如果游戏应该结束,则必须将GameEnded而不是True设置为False

if count == NoOfTargets:
    GameEnded = True

此外,不要在事件循环中进行任何绘制:

GameEnded = False
count = 0
while not GameEnded:
    Target(Random_x, Random_y, Target_radius, red)

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

        elif event.type == pygame.MOUSEBUTTONDOWN:
            Mouse_X, Mouse_Y = event.pos
            OnTarget = Pythagoras_Calculator(Random_x, Random_y, Mouse_X, Mouse_Y, Target_radius)
            print(Mouse_X, Mouse_Y, OnTarget)
            print(Random_x, Random_y)
            if OnTarget == True:
                count += 1
                Random_x = random.randrange(0, display_width)
                Random_y = random.randrange(0, display_height)
                GameTime = pygame.time.get_ticks()
                Get_Event_Time(GameTime)

    if count == NoOfTargets:
        GameEnded = True

    gameDisplay.fill(white)
    Target(Random_x, Random_y, Target_radius, red)
    pygame.display.update()