Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Leaderboard, Sound Feedback, and Timer Functionality to Space Explorer Quiz Game #1064

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Game_Development/Space_quiz.py/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Space Explorer Quiz Game

Welcome to the Space Explorer Quiz Game! This is a fun and interactive quiz game built using Python and Pygame, where players can test their knowledge of astronomy through a series of questions.

## Features

- **Interactive Gameplay**: Answer multiple-choice questions related to space and astronomy.
- **Leaderboard**: Track your scores against others and see how you rank.
- **Sound Effects**: Enjoy sound feedback for correct and incorrect answers.
- **Timer**: Each question has a time limit to increase the challenge.

## Requirements

To run this game, you need:
- Python 3.x
- Pygame library

You can install Pygame using pip:


## Controls

Press 1, 2, 3, or 4 to select your answer.
Press R to retake the quiz.
Press L to view the leaderboard.
Press Q to quit the game.

```bash
pip install pygame



Binary file added Game_Development/Space_quiz.py/correct.wav
Binary file not shown.
Binary file added Game_Development/Space_quiz.py/incorrect.wav
Binary file not shown.
141 changes: 113 additions & 28 deletions Game_Development/Space_quiz.py/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0) # Color for correct answer
RED = (255, 0, 0) # Color for incorrect answer

# Load background image
background = pygame.image.load("jump3.jpg") # Change to your background image file
Expand All @@ -21,6 +23,10 @@
# Font
font = pygame.font.Font(None, 36)

# Load sound effects
correct_sound = pygame.mixer.Sound("correct.wav")
incorrect_sound = pygame.mixer.Sound("incorrect.wav")

# Questions and answers
questions = [
{
Expand Down Expand Up @@ -48,6 +54,8 @@
# Game state
current_question = 0
score = 0
time_left = 30 # 30 seconds for each question
leaderboard = [] # List to store scores

def display_question(question_data):
"""Displays the current question and answer options centered on the screen."""
Expand All @@ -60,44 +68,121 @@ def display_question(question_data):

# Display answer options centered
for i, option in enumerate(question_data["options"]):
option_text = font.render(f"{i + 1}. {option}", True, WHITE)
option_text = font.render(f"{i + 1}. {option}", True, WHITE) # Set text color to white
option_rect = option_text.get_rect(center=(WIDTH // 2, HEIGHT // 2 + i * 40))
screen.blit(option_text, option_rect)

# Display timer
timer_text = font.render(f"Time left: {time_left}", True, WHITE) # Set timer text color to white
timer_rect = timer_text.get_rect(topright=(WIDTH - 10, 10))
screen.blit(timer_text, timer_rect)

pygame.display.flip()

def show_feedback(correct):
"""Displays feedback on the selection."""
feedback_text = "Correct!" if correct else "Incorrect!"
feedback_color = GREEN if correct else RED # Set color based on correctness
feedback_surface = font.render(feedback_text, True, feedback_color) # Set feedback text color
feedback_rect = feedback_surface.get_rect(center=(WIDTH // 2, HEIGHT // 2 + 100))
screen.blit(feedback_surface, feedback_rect)
pygame.display.flip()
pygame.time.wait(1000) # Show feedback for 1 second

def show_score():
"""Displays the final score centered on the screen."""
screen.blit(background, (0, 0))
score_text = font.render(f"Your Score: {score} / {len(questions)}", True, BLUE)
score_text = font.render(f"Your Score: {score} / {len(questions)}", True, WHITE) # Set score text color to white
score_rect = score_text.get_rect(center=(WIDTH // 2, HEIGHT // 2))
screen.blit(score_text, score_rect)
pygame.display.flip()
pygame.time.wait(3000) # Wait for 3 seconds

# Main game loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

if event.type == pygame.KEYDOWN:
if event.key in [pygame.K_1, pygame.K_2, pygame.K_3, pygame.K_4]:
selected_option = event.unicode
selected_index = int(selected_option) - 1

if selected_index >= 0 and selected_index < len(questions[current_question]["options"]):
if questions[current_question]["options"][selected_index] == questions[current_question]["answer"]:
score += 1
current_question += 1

if current_question >= len(questions):
show_score()
pygame.quit()
sys.exit()

if current_question < len(questions):
display_question(questions[current_question])
else:
show_score()
def show_leaderboard():
"""Displays the leaderboard."""
screen.blit(background, (0, 0))
leaderboard_text = font.render("Leaderboard", True, WHITE) # Set leaderboard title color to white
leaderboard_rect = leaderboard_text.get_rect(center=(WIDTH // 2, 50))
screen.blit(leaderboard_text, leaderboard_rect)

for i, score in enumerate(leaderboard):
score_text = font.render(f"{i + 1}. Score: {score}", True, WHITE) # Set score text color to white
score_rect = score_text.get_rect(center=(WIDTH // 2, 100 + i * 30))
screen.blit(score_text, score_rect)

pygame.display.flip()
pygame.time.wait(5000) # Wait for 5 seconds before returning to main menu

def show_final_screen():
"""Displays the final screen with options to retake the quiz or quit."""
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r: # Restart quiz
main()
elif event.key == pygame.K_l: # Show leaderboard
show_leaderboard()
elif event.key == pygame.K_q: # Quit
pygame.quit()
sys.exit()

# Display final options
screen.blit(background, (0, 0))
final_text = font.render("Press 'R' to retake, 'L' for leaderboard or 'Q' to quit.", True, WHITE) # Set final options text color to white
final_rect = final_text.get_rect(center=(WIDTH // 2, HEIGHT // 2))
screen.blit(final_text, final_rect)
pygame.display.flip()

def main():
global current_question, score, time_left
current_question = 0
score = 0
time_left = 30 # Reset timer for the new game

# Main game loop
pygame.time.set_timer(pygame.USEREVENT + 1, 1000) # Timer event every second
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

if event.type == pygame.USEREVENT + 1: # Timer event
time_left -= 1
if time_left <= 0:
current_question += 1
time_left = 30 # Reset timer for the next question

if event.type == pygame.KEYDOWN:
if event.key in [pygame.K_1, pygame.K_2, pygame.K_3, pygame.K_4]:
selected_option = event.unicode
selected_index = int(selected_option) - 1

if selected_index >= 0 and selected_index < len(questions[current_question]["options"]):
correct = questions[current_question]["options"][selected_index] == questions[current_question]["answer"]
if correct:
score += 1
correct_sound.play() # Play correct sound
else:
incorrect_sound.play() # Play incorrect sound

show_feedback(correct)
current_question += 1

if current_question >= len(questions):
leaderboard.append(score) # Add score to leaderboard
show_score()
show_final_screen() # Show the final screen

if current_question < len(questions):
display_question(questions[current_question])
else:
show_score()

if __name__ == "__main__":
main()

Loading