gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 08:21:30 24 4
gpt4 key购买 nike

我试图结束事件循环,其中已单击的目标数量已达到与变量 NoOfTargets 相同的数量(在本例中为 10)。

我认为由于事件循环处理所有 pygame 事件,我决定验证一下 count 是否与 NoOfTargets 相同。 count 最初设置为 0。当点击新目标时,count 会加 +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()"""

我希望当 count(记录已点击的目标数量)与 NoOfTargets 相同时,事件循环将中断并且游戏停止。请帮助我,我是 pygame 新手。

最佳答案

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

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

count =+ 1 没有执行您期望的操作。 =+ 1 会将 1 分配给 count。注意 = 是赋值,+ 只是值的正号。增量赋值是+=而不是=+:

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()

关于python - 在达到最大目标数量后,如何停止 pygame 中的事件循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54618401/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com