Почему в моем коде в функции mehanika2 не вызывается функция menu1?
В моем коде сначало запускается функция menu1 и у меня есть еще функция mehanika она нужна для сканирования нажатий клавиш(mehanika2 тоже) потом при нажатии на кнопку запускается функция options и в ней уже включается функция mehanika2 потом при нажатии должен заканчиваться цикл startop и он заканчиваеться и еще должна включатся функция menu1 но она уже не включается, пару часов сидел пытался разобраться не получилось помогите пожалуйста.
Код
import pygame import os os.chdir("D:/Computer Science/denis") bg_path = os.path.abspath("bg.jpg") ball_path = os.path.abspath("ball.png") nlo1_path = os.path.abspath("nlo1.png") nlo2_path = os.path.abspath("nlo2.png") button_path = os.path.abspath("knopa.png") strelka_path = os.path.abspath("strelka.png") font_path = os.path.abspath("GravitasOne-Regular.ttf") bg = pygame.image.load(os.path.basename(bg_path)) ball = pygame.image.load(os.path.basename(ball_path)) nlo1 = pygame.image.load(os.path.basename(nlo1_path)) nlo2 = pygame.image.load(os.path.basename(nlo2_path)) button1 = pygame.image.load(os.path.basename(button_path)) button2 = pygame.image.load(os.path.basename(button_path)) button3 = pygame.image.load(os.path.basename(button_path)) strelka = pygame.image.load(os.path.basename(strelka_path)) pygame.init() font = pygame.font.Font(font_path, 70) party = 1.5 taburet = 0.8 divan = 1.0 help = False nlo1_collided = False nlo2_collided = False x = True y = True y2 = True y3 = True startgame = True startmenu = True startop = True window = pygame.display.set_mode((1920, 1080)) screen = pygame.Surface((1920, 1080)) pygame.display.set_caption("1,2,3,4,5,6") ball_x = 922 ball_y = 502 nlo1_y = 405 nlo2_y = 405 button_width = 552 button_height = 143 strelka_width = 80 strelka_height = 60 strelka_x = 10 strelka_y = 10 exitbutton_x = 684 exitbutton_y = 705 exit_text = font.render("EXIT", True, (255, 255, 255)) playbutton_x = 684 playbutton_y = 305 play_text = font.render("PLAY", True, (255, 255, 255)) optionbutton_x = 684 optionbutton_y = 505 option_text = font.render("OPTIONS", True, (255, 255, 255)) def options(): global menu1 global startop while startop: mehanika2() pygame.display.update() for menu in pygame.event.get(): if menu.type == pygame.QUIT: startop = False window.blit(screen, (0, 0)) screen.blit(bg, (0, 0)) screen.blit(pygame.transform.scale(strelka, (strelka_width, strelka_height)), (strelka_x, strelka_y)) def game(): global startgame, x, y, y2, y3, taburet, divan, party, help, nlo1_collided, nlo2_collided, ball_x, ball_y, nlo1_y, nlo2_y while startgame: pygame.display.update() if y3: ball_y -= divan else: ball_y += divan if ball_y < 0: y3 = False if ball_y > 1005: y3 = True if x: ball_x += taburet else: ball_x -= taburet if ball_x > 1920 or ball_x < -75: startgame = False if y: nlo1_y -= party bogdan = True else: nlo1_y += party bogdan = False if nlo1_y < 0: y = False if nlo1_y > 810: y = True if y2: nlo2_y -= party bogdan2 = True else: nlo2_y += party bogdan2 = False if nlo2_y < 0: y2 = False if nlo2_y > 810: y2 = True for event in pygame.event.get(): if event.type == pygame.QUIT: startgame = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_RETURN and bogdan: y = False if event.key == pygame.K_RETURN and not bogdan: y = True if event.key == pygame.K_SPACE and bogdan2: y2 = False if event.key == pygame.K_SPACE and not bogdan2: y2 = True nlo1_rect = pygame.Rect(1550, nlo1_y, 25, 270) nlo2_rect = pygame.Rect(345, nlo2_y, 25, 270) ball_rect = pygame.Rect(ball_x, ball_y, 75, 75) if not ball_rect.colliderect(nlo1_rect) and not nlo1_collided: nlo1_collided = True help = False nlo2_collided = False if not ball_rect.colliderect(nlo2_rect) and not nlo2_collided: nlo2_collided = True help = False nlo1_collided = False if ball_rect.colliderect(nlo1_rect) and not help: help = True x = False taburet = taburet + 0.1 divan = divan + 0.1 party = party + 0.05 if ball_rect.colliderect(nlo2_rect) and not help: help = True x = True taburet = taburet + 0.1 divan = divan + 0.1 party = party + 0.05 window.blit(screen, (0, 0)) screen.blit(bg, (0, 0)) screen.blit(nlo1, (1550, nlo1_y)) screen.blit(nlo2, (100, nlo2_y)) screen.blit(ball, (ball_x, ball_y)) def menu1(): global startmenu global mouse_pos global menu1 while startmenu: mehanika() pygame.display.update() for menu in pygame.event.get(): if menu.type == pygame.QUIT: startmenu = False window.blit(screen, (0, 0)) screen.blit(bg, (0, 0)) screen.blit(pygame.transform.scale(button1, (button_width, button_height)), (exitbutton_x, exitbutton_y)) screen.blit(pygame.transform.scale(button2, (button_width, button_height)), (playbutton_x, playbutton_y)) screen.blit(pygame.transform.scale(button3, (button_width, button_height)), (optionbutton_x, optionbutton_y)) screen.blit(option_text, (optionbutton_x + 60, optionbutton_y + 40)) screen.blit(play_text, (playbutton_x + 150, playbutton_y + 40)) screen.blit(exit_text, (exitbutton_x + 150, exitbutton_y + 40)) def mehanika(): global startmenu global mouse_pos global startop mouse_pos = pygame.mouse.get_pos() if exitbutton_x < mouse_pos[0] < exitbutton_x + button_width and exitbutton_y < mouse_pos[1] < exitbutton_y + button_height: for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: startmenu = False if playbutton_x < mouse_pos[0] < playbutton_x + button_width and playbutton_y < mouse_pos[1] < playbutton_y + button_height: for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: startmenu = False game() if optionbutton_x < mouse_pos[0] < optionbutton_x + button_width and optionbutton_y < mouse_pos[1] < optionbutton_y + button_height: for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: startmenu = False options() def mehanika2(): global startop mouse_pos2 = pygame.mouse.get_pos() if strelka_x < mouse_pos2[0] < strelka_x + strelka_width and strelka_y < mouse_pos2[1] < strelka_y + strelka_height: for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: startop = False menu1() menu1() |
import pygame import os os.chdir("D:/Computer Science/denis") bg_path = os.path.abspath("bg.jpg") ball_path = os.path.abspath("ball.png") nlo1_path = os.path.abspath("nlo1.png") nlo2_path = os.path.abspath("nlo2.png") button_path = os.path.abspath("knopa.png") strelka_path = os.path.abspath("strelka.png") font_path = os.path.abspath("GravitasOne-Regular.ttf") bg = pygame.image.load(os.path.basename(bg_path)) ball = pygame.image.load(os.path.basename(ball_path)) nlo1 = pygame.image.load(os.path.basename(nlo1_path)) nlo2 = pygame.image.load(os.path.basename(nlo2_path)) button1 = pygame.image.load(os.path.basename(button_path)) button2 = pygame.image.load(os.path.basename(button_path)) button3 = pygame.image.load(os.path.basename(button_path)) strelka = pygame.image.load(os.path.basename(strelka_path)) pygame.init() font = pygame.font.Font(font_path, 70) party = 1.5 taburet = 0.8 divan = 1.0 help = False nlo1_collided = False nlo2_collided = False x = True y = True y2 = True y3 = True startgame = True startmenu = True startop = True window = pygame.display.set_mode((1920, 1080)) screen = pygame.Surface((1920, 1080)) pygame.display.set_caption("1,2,3,4,5,6") ball_x = 922 ball_y = 502 nlo1_y = 405 nlo2_y = 405 button_width = 552 button_height = 143 strelka_width = 80 strelka_height = 60 strelka_x = 10 strelka_y = 10 exitbutton_x = 684 exitbutton_y = 705 exit_text = font.render("EXIT", True, (255, 255, 255)) playbutton_x = 684 playbutton_y = 305 play_text = font.render("PLAY", True, (255, 255, 255)) optionbutton_x = 684 optionbutton_y = 505 option_text = font.render("OPTIONS", True, (255, 255, 255)) def options(): global menu1 global startop while startop: mehanika2() pygame.display.update() for menu in pygame.event.get(): if menu.type == pygame.QUIT: startop = False window.blit(screen, (0, 0)) screen.blit(bg, (0, 0)) screen.blit(pygame.transform.scale(strelka, (strelka_width, strelka_height)), (strelka_x, strelka_y)) def game(): global startgame, x, y, y2, y3, taburet, divan, party, help, nlo1_collided, nlo2_collided, ball_x, ball_y, nlo1_y, nlo2_y while startgame: pygame.display.update() if y3: ball_y -= divan else: ball_y += divan if ball_y < 0: y3 = False if ball_y > 1005: y3 = True if x: ball_x += taburet else: ball_x -= taburet if ball_x > 1920 or ball_x < -75: startgame = False if y: nlo1_y -= party bogdan = True else: nlo1_y += party bogdan = False if nlo1_y < 0: y = False if nlo1_y > 810: y = True if y2: nlo2_y -= party bogdan2 = True else: nlo2_y += party bogdan2 = False if nlo2_y < 0: y2 = False if nlo2_y > 810: y2 = True for event in pygame.event.get(): if event.type == pygame.QUIT: startgame = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_RETURN and bogdan: y = False if event.key == pygame.K_RETURN and not bogdan: y = True if event.key == pygame.K_SPACE and bogdan2: y2 = False if event.key == pygame.K_SPACE and not bogdan2: y2 = True nlo1_rect = pygame.Rect(1550, nlo1_y, 25, 270) nlo2_rect = pygame.Rect(345, nlo2_y, 25, 270) ball_rect = pygame.Rect(ball_x, ball_y, 75, 75) if not ball_rect.colliderect(nlo1_rect) and not nlo1_collided: nlo1_collided = True help = False nlo2_collided = False if not ball_rect.colliderect(nlo2_rect) and not nlo2_collided: nlo2_collided = True help = False nlo1_collided = False if ball_rect.colliderect(nlo1_rect) and not help: help = True x = False taburet = taburet + 0.1 divan = divan + 0.1 party = party + 0.05 if ball_rect.colliderect(nlo2_rect) and not help: help = True x = True taburet = taburet + 0.1 divan = divan + 0.1 party = party + 0.05 window.blit(screen, (0, 0)) screen.blit(bg, (0, 0)) screen.blit(nlo1, (1550, nlo1_y)) screen.blit(nlo2, (100, nlo2_y)) screen.blit(ball, (ball_x, ball_y)) def menu1(): global startmenu global mouse_pos global menu1 while startmenu: mehanika() pygame.display.update() for menu in pygame.event.get(): if menu.type == pygame.QUIT: startmenu = False window.blit(screen, (0, 0)) screen.blit(bg, (0, 0)) screen.blit(pygame.transform.scale(button1, (button_width, button_height)), (exitbutton_x, exitbutton_y)) screen.blit(pygame.transform.scale(button2, (button_width, button_height)), (playbutton_x, playbutton_y)) screen.blit(pygame.transform.scale(button3, (button_width, button_height)), (optionbutton_x, optionbutton_y)) screen.blit(option_text, (optionbutton_x + 60, optionbutton_y + 40)) screen.blit(play_text, (playbutton_x + 150, playbutton_y + 40)) screen.blit(exit_text, (exitbutton_x + 150, exitbutton_y + 40)) def mehanika(): global startmenu global mouse_pos global startop mouse_pos = pygame.mouse.get_pos() if exitbutton_x < mouse_pos[0] < exitbutton_x + button_width and exitbutton_y < mouse_pos[1] < exitbutton_y + button_height: for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: startmenu = False if playbutton_x < mouse_pos[0] < playbutton_x + button_width and playbutton_y < mouse_pos[1] < playbutton_y + button_height: for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: startmenu = False game() if optionbutton_x < mouse_pos[0] < optionbutton_x + button_width and optionbutton_y < mouse_pos[1] < optionbutton_y + button_height: for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: startmenu = False options() def mehanika2(): global startop mouse_pos2 = pygame.mouse.get_pos() if strelka_x < mouse_pos2[0] < strelka_x + strelka_width and strelka_y < mouse_pos2[1] < strelka_y + strelka_height: for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: startop = False menu1() menu1()
Дополнительно:
Ответы:
Вероятно, никогда не выполняются условия if-ов.
Попробуй сделать типо:
menu = menu1() menu |
menu = menu1() menu
И можешь добавить в if print("!") ну проверить выполняются условия if или нет
Предложу еще до выполнения if проверить чему равно event.type с помощью print() (если получится)
def mehanika2(): global startop mouse_pos2 = pygame.mouse.get_pos() if strelka_x < mouse_pos2[0] < strelka_x + strelka_width and strelka_y < mouse_pos2[1] < strelka_y + strelka_height: for event in pygame.event.get(): print(event.type) if event.type == pygame.MOUSEBUTTONDOWN: print("!") startop = False menu1() |
def mehanika2(): global startop mouse_pos2 = pygame.mouse.get_pos() if strelka_x < mouse_pos2[0] < strelka_x + strelka_width and strelka_y < mouse_pos2[1] < strelka_y + strelka_height: for event in pygame.event.get(): print(event.type) if event.type == pygame.MOUSEBUTTONDOWN: print("!") startop = False menu1()
Опишите проблему, и специалист поможет с настройкой, исправлением ошибки или доработкой сайта. Подберём понятный план работ без лишней переписки.
Пока нет других ответов. Будьте первым, кто поможет автору.
Ответить на вопрос
Для того чтобы понять, почему функция menu1 не вызывается в функции mehanika2, нужно провести анализ вашего кода.
Есть несколько возможных причин, почему функция menu1 не вызывается в функции mehanika2:
1. Проверьте, правильно ли вы вызываете функцию menu1 внутри функции mehanika2. Убедитесь, что вы используете правильное имя функции и правильный синтаксис вызова функции. Например, вызов функции должен быть написан как menu1();.
2. Убедитесь, что функция menu1 определена до вызова функции mehanika2. Если функция menu1 определена после функции mehanika2, то она не будет доступна для вызова внутри функции mehanika2.
3. Проверьте, нет ли ошибок в функции menu1, которые могут привести к ее некорректной работе. Если функция menu1 содержит ошибку, то она может не вызываться в функции mehanika2.
4. Проверьте, нет ли условий или других логических операций в вашем коде, которые могут предотвращать вызов функции menu1 в функции mehanika2. Например, если у вас есть условие, которое блокирует вызов функции menu1, то она не будет вызвана.
Для более точного анализа проблемы, пожалуйста, предоставьте фрагмент вашего кода, где определены функции menu1 и mehanika2. Это поможет нам более точно определить причину проблемы и предложить наиболее подходящее решение.