From e7dbc40767829e0ed8cae25732111cff40d67345 Mon Sep 17 00:00:00 2001 From: MIGHTY1o1 Date: Wed, 6 Nov 2024 04:10:39 +0530 Subject: [PATCH] add movie game --- Web_app/pages/GuessMovie.py | 68 ++++++++++++++++++++++++ Web_app/pages/bollywood_trivia_data.json | 35 ++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 Web_app/pages/GuessMovie.py create mode 100644 Web_app/pages/bollywood_trivia_data.json diff --git a/Web_app/pages/GuessMovie.py b/Web_app/pages/GuessMovie.py new file mode 100644 index 00000000..bf475c81 --- /dev/null +++ b/Web_app/pages/GuessMovie.py @@ -0,0 +1,68 @@ +import streamlit as st +import json +import random +import os + + +def load_trivia_data(): + base_path = os.path.dirname(__file__) # Get the directory of the current script + json_path = os.path.join(base_path, "bollywood_trivia_data.json") + with open(json_path, "r") as file: + data = json.load(file) + return data["questions"] + + +# Initialize session state for score and current question +if "score" not in st.session_state: + st.session_state.score = 0 +if "question_index" not in st.session_state: + st.session_state.question_index = 0 +if "questions" not in st.session_state: + st.session_state.questions = load_trivia_data() + random.shuffle(st.session_state.questions) # Shuffle questions for randomness + + +# Display the question and options +def display_question(): + question = st.session_state.questions[st.session_state.question_index] + st.write( + f"**Question {st.session_state.question_index + 1}:** {question['question']}" + ) + + # Create buttons for options + for option in question["options"]: + if st.button(option): + check_answer(option, question["answer"]) + + +# Check if the answer is correct +def check_answer(selected_option, correct_answer): + if selected_option == correct_answer: + st.session_state.score += 1 + st.success("Correct!") + else: + st.error(f"Wrong! The correct answer is: {correct_answer}") + + # Move to the next question + if st.session_state.question_index < len(st.session_state.questions) - 1: + st.session_state.question_index += 1 + else: + st.write( + f"Game Over! Your final score is {st.session_state.score}/{len(st.session_state.questions)}" + ) + reset_game() + + +# Reset the game +def reset_game(): + st.session_state.score = 0 + st.session_state.question_index = 0 + st.session_state.questions = load_trivia_data() + random.shuffle(st.session_state.questions) + + +# Set up the Streamlit app +st.set_page_config(page_title="Bollywood Movie Trivia Game", page_icon="🎬") + +st.title("Bollywood Movie Trivia Game") +display_question() diff --git a/Web_app/pages/bollywood_trivia_data.json b/Web_app/pages/bollywood_trivia_data.json new file mode 100644 index 00000000..efcb443a --- /dev/null +++ b/Web_app/pages/bollywood_trivia_data.json @@ -0,0 +1,35 @@ +{ + "questions": [ + { + "id": 1, + "question": "Which Bollywood movie features the song 'Kal Ho Naa Ho'?", + "options": ["Kabhi Khushi Kabhie Gham", "Dilwale Dulhania Le Jayenge", "Kal Ho Naa Ho", "Kabir Singh"], + "answer": "Kal Ho Naa Ho" + }, + { + "id": 2, + "question": "Who directed the movie '3 Idiots'?", + "options": ["Rajkumar Hirani", "Karan Johar", "Raju Hirani", "Sanjay Leela Bhansali"], + "answer": "Rajkumar Hirani" + }, + { + "id": 3, + "question": "In which movie did Shah Rukh Khan play a double role as twin brothers?", + "options": ["Chennai Express", "Karan Arjun", "Dilwale Dulhania Le Jayenge", "Kabhi Khushi Kabhie Gham"], + "answer": "Karan Arjun" + }, + { + "id": 4, + "question": "Which Bollywood film is based on the life of the Indian cricketer Mahendra Singh Dhoni?", + "options": ["Lagaan", "MS Dhoni: The Untold Story", "Chakdey India", "Sachin: A Billion Dreams"], + "answer": "MS Dhoni: The Untold Story" + }, + { + "id": 5, + "question": "What is the highest-grossing Bollywood film of all time (as of 2023)?", + "options": ["Dangal", "Baahubali: The Conclusion", "Bajrangi Bhaijaan", "PK"], + "answer": "Dangal" + } + ] + } + \ No newline at end of file