-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #319 from shubhagarwal1/game
ADD Movie Trivia Game #317
- Loading branch information
Showing
2 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
] | ||
} | ||
|