So I'm trying to create a restaurant menu that a user can choose an appetizer and an entree from through pygame rects and collisions. I have four if statements for four rectangles that will return a value that will then be used as an index to pull from a list. However, the program only functions with the first if statement and always returns a value of 0, even if I don't click the box and my mouse clicked on a different button. I've tried isolating the other if statements and also found that they did not return a value individually, and only the app_box_one functioned correctly when clicked. Clicking on app_box_two or the other boxes did not do anything and kept the value of a = None. I also read about return ending the function, but I'm not sure if that is the problem here as all the other boxes (even though they are the exact same, just a lower y coordinate) other than app_box_one don't do anything. Sorry this is a long question, any help is appreciated.
def main():
a = None
e = None
run = True
FPS = 60
while run:
window.fill(WHITE)
window.blit(menu, (0, 0))
clock = pygame.time.Clock()
clock.tick(FPS)
app_box = pygame.Rect(WIDTH / 2 - 215, HEIGHT / 2 - 200, 200, 75)
entree_box = pygame.Rect(WIDTH / 2 + 30, HEIGHT / 2 - 200, 200, 75)
pos = pygame.mouse.get_pos()
if app_box.collidepoint(pos):
if clicking:
a = screen(1, 0)
print(a)
if entree_box.collidepoint(pos):
if clicking:
e = screen(0, 1)
pygame.draw.rect(window, NUTMEG, app_box)
pygame.draw.rect(window, NUTMEG, entree_box)
draw_win()
clicking = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
clicking = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
if a != None and e != None:
meal(a, e)
pygame.display.update()
def screen(app, ent):
mx, my = pygame.mouse.get_pos()
if app == 1:
running = True
while running:
window.fill(WHITE)
window.blit(menu, (0, 0))
app_box_one = pygame.Rect(WIDTH/3 - 100, HEIGHT/5, 400, 100)
pygame.draw.rect(window, NUTMEG, app_box_one)
app_box_two = pygame.Rect(WIDTH/3 - 100, HEIGHT/3 + 45, 400, 100)
pygame.draw.rect(window, NUTMEG, app_box_two)
app_box_three = pygame.Rect(WIDTH/3 - 100, HEIGHT - 350, 400, 100)
pygame.draw.rect(window, NUTMEG, app_box_three)
app_box_four = pygame.Rect(WIDTH/3 - 100, HEIGHT - 200, 400, 100)
pygame.draw.rect(window, NUTMEG, app_box_four)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.MOUSEBUTTONDOWN:
if app_box_one.collidepoint(mx, my):
app_ix = 0
return app_ix
if app_box_two.collidepoint(mx, my):
app_ix = 1
return app_ix
if app_box_two.collidepoint(mx, my):
app_ix = 2
return app_ix
if app_box_two.collidepoint(mx, my):
app_ix = 3
return app_ix
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
pygame.display.update()
main_clock.tick(60)
if ent == 1:
running = True
while running:
window.fill(WHITE)
window.blit(menu, (0, 0))
option_box_one = pygame.Rect(WIDTH/3 - 100, HEIGHT/5, 400, 100)
pygame.draw.rect(window, NUTMEG, option_box_one)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.MOUSEBUTTONDOWN:
if option_box_one.collidepoint(mx, my):
entree_ix = 0
return entree_ix
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
pygame.display.update()
main_clock.tick(60)
def meal(app_index, entree_index):
window.fill(WHITE)
window.blit(menu, (0, 0))
meal_font_one = pygame.font.SysFont('corbel', 35, True, True)
meal_font_two = pygame.font.SysFont('corbel', 35, True, True)
meal_message_one = app_list[app_index]
meal_message_two = entree_list[entree_index]
final_meal_message_one = meal_font_one.render(meal_message_one, True, BLACK)
window.blit(final_meal_message_one, (WIDTH/4 - 120, HEIGHT/2))
final_meal_message_two = meal_font_two.render(meal_message_two, True, BLACK)
window.blit(final_meal_message_two, (WIDTH / 4 - 120, HEIGHT / 2 - 50))
pygame.display.update
main()
Aucun commentaire:
Enregistrer un commentaire