Не знаю почему, но моя игра PyGame зависает, когда запускается функция game()
.
Я пытался удалить строку за строкой в функции, которая вызывает у меня проблему, и во всех попытках игра все равно зависала.
Функция, которая вызывает у меня проблему:
def game():
global Bone1_pos
global Bone2_pos
global Bone1_y
global Bone2_y
Bone1_x = 500
Bone2_x = 1100
clock.tick(FPS)
#Pos 1
if Bone1_pos == 1:
Bone1_y = -80
if Bone2_pos == 1:
Bone2_y = -80
#Pos 2
if Bone1_pos == 2:
Bone1_y = -40
if Bone2_pos == 2:
Bone2_y = -40
#Pos 3
if Bone1_pos == 3:
Bone1_y = 25
if Bone2_pos == 3:
Bone2 = 25
pygame.event.get()
BoneUp1.rect = pygame.Rect(Bone1_x, Bone1_y, 75, 173)
BoneDown1.rect = pygame.Rect(Bone1_x, Bone1_y + 450, 75, 173)
BoneUp2.rect = pygame.Rect(Bone2_x, Bone2_y, 75, 173)
BoneDown2.rect = pygame.Rect(Bone2_x, Bone2_y + 450, 75, 173)
#Sprite blit
screen.blit(background, (0, 0))
screen.blit(BoneDown1.image, (Bone1_x, Bone1_y + 400))
screen.blit(BoneUp1.image, (Bone1_x, Bone1_y))
screen.blit(BoneDown2.image, (Bone2_x, Bone2_y + 400))
screen.blit(BoneUp2.image, (Bone2_x, Bone2_y))
screen.blit(spikeUp0.image, (0, 0))
screen.blit(spikeDown0.image, (0, 436))
screen.blit(Dog.image, (Dog.rect.topleft))
if pygame.key.get_pressed()[K_UP]: # Up
Dog.rect.y -= 1
pygame.time.delay(2)
else: # Down
Dog.rect.y += 1
pygame.time.delay(2)
if pygame.key.get_pressed() [K_LEFT]: # Left
Dog.rect.x -= 1
pygame.time.delay(2)
if pygame.key.get_pressed() [K_RIGHT]: # Right
Dog.rect.x += 1
pygame.time.delay(2)
# Barrier
if Dog.rect.x < 0:
Dog.rect.x = 0
elif Dog.rect.x > 822:
Dog.rect.x = 822
# Collison
for s in [spikeUp0, spikeDown0, BoneUp1, BoneDown1, BoneUp2, BoneDown2]:
if pygame.sprite.collide_rect(Dog, s):
pygame.quit()
sys.exit()
pygame.display.update()
Bone1_x -= 1
Bone2_x -= 1
if Bone1_x <= -40 :
Bone1_pos = random.randint(1,3)
Bone1_x = 1100
if Bone2_x <= -40:
Bone2_pos = random.randint(1,3)
Bone2_x = 1100
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.update()