diff --git a/Python/Car-Racing.py b/Python/Car-Racing.py new file mode 100644 index 0000000..2853a8f --- /dev/null +++ b/Python/Car-Racing.py @@ -0,0 +1,113 @@ +import pygame +import random +import sys + +# Initialize pygame +pygame.init() + +# Screen dimensions +WIDTH, HEIGHT = 500, 700 +screen = pygame.display.set_mode((WIDTH, HEIGHT)) +pygame.display.set_caption("šŸŽļø Top-Down Car Racing") + +# Clock for controlling FPS +clock = pygame.time.Clock() +FPS = 60 + +# Colors +WHITE = (255, 255, 255) +BLACK = (0, 0, 0) +RED = (200, 0, 0) +GREEN = (0, 200, 0) + +# Load player car image +PLAYER_CAR = pygame.Surface((50, 100)) +PLAYER_CAR.fill(GREEN) + +# Load enemy car image +ENEMY_CAR = pygame.Surface((50, 100)) +ENEMY_CAR.fill(RED) + +# Player car position +player_x = WIDTH // 2 - 25 +player_y = HEIGHT - 120 +player_speed = 5 + +# Enemy cars +enemy_speed = 5 +enemy_list = [] + +def add_enemy(): + x_pos = random.randint(50, WIDTH - 100) + y_pos = -100 + enemy_list.append([x_pos, y_pos]) + +def draw_enemies(): + for enemy in enemy_list: + screen.blit(ENEMY_CAR, (enemy[0], enemy[1])) + +def move_enemies(): + global enemy_list + for enemy in enemy_list: + enemy[1] += enemy_speed + # Remove enemies that have left the screen + enemy_list = [enemy for enemy in enemy_list if enemy[1] < HEIGHT] + +def check_collision(): + for enemy in enemy_list: + if (player_y < enemy[1] + 100 and player_y + 100 > enemy[1]) and \ + (player_x < enemy[0] + 50 and player_x + 50 > enemy[0]): + return True + return False + +# Score +score = 0 +font = pygame.font.SysFont(None, 40) + +def show_score(): + text = font.render(f"Score: {score}", True, WHITE) + screen.blit(text, (10, 10)) + +# Main game loop +running = True +enemy_timer = 0 # Timer to add new enemies + +while running: + screen.fill(BLACK) + + for event in pygame.event.get(): + if event.type == pygame.QUIT: + pygame.quit() + sys.exit() + + # Key presses + keys = pygame.key.get_pressed() + if keys[pygame.K_LEFT] and player_x > 0: + player_x -= player_speed + if keys[pygame.K_RIGHT] and player_x < WIDTH - 50: + player_x += player_speed + + # Enemy logic + enemy_timer += 1 + if enemy_timer > 50: # add new enemy every ~50 frames + add_enemy() + enemy_timer = 0 + + move_enemies() + draw_enemies() + + # Draw player + screen.blit(PLAYER_CAR, (player_x, player_y)) + + # Collision check + if check_collision(): + print("šŸ’„ Game Over! Your score:", score) + pygame.quit() + sys.exit() + + # Update score + score += 1 + show_score() + + pygame.display.update() + clock.tick(FPS) diff --git a/Python/Flappy-Bird.py b/Python/Flappy-Bird.py new file mode 100644 index 0000000..37ad529 --- /dev/null +++ b/Python/Flappy-Bird.py @@ -0,0 +1,120 @@ +import pygame +import random +import sys + +# Initialize pygame +pygame.init() + +# Screen dimensions +WIDTH, HEIGHT = 400, 600 +screen = pygame.display.set_mode((WIDTH, HEIGHT)) +pygame.display.set_caption("🐤 Flappy Bird Clone") + +# Colors +WHITE = (255, 255, 255) +BLUE = (135, 206, 235) +GREEN = (0, 200, 0) +YELLOW = (255, 255, 0) + +# Clock for FPS +clock = pygame.time.Clock() +FPS = 60 + +# Game variables +gravity = 0.5 +bird_movement = 0 +game_active = True +score = 0 + +# Bird setup +bird = pygame.Rect(100, HEIGHT // 2, 34, 24) + +# Pipe setup +pipe_width = 70 +pipe_height = random.randint(150, 400) +pipe_gap = 150 +pipe_list = [] + +SPAWNPIPE = pygame.USEREVENT +pygame.time.set_timer(SPAWNPIPE, 1200) + +# Fonts +font = pygame.font.SysFont(None, 50) + +def draw_bird(): + pygame.draw.ellipse(screen, YELLOW, bird) + +def create_pipe(): + random_pipe_pos = random.randint(150, 400) + bottom_pipe = pygame.Rect(WIDTH, random_pipe_pos, pipe_width, HEIGHT - random_pipe_pos) + top_pipe = pygame.Rect(WIDTH, random_pipe_pos - pipe_gap - pipe_height, pipe_width, pipe_height) + return bottom_pipe, top_pipe + +def move_pipes(pipes): + for pipe in pipes: + pipe.centerx -= 4 + return [pipe for pipe in pipes if pipe.right > 0] + +def draw_pipes(pipes): + for pipe in pipes: + pygame.draw.rect(screen, GREEN, pipe) + +def check_collision(pipes): + for pipe in pipes: + if bird.colliderect(pipe): + return False + if bird.top <= 0 or bird.bottom >= HEIGHT: + return False + return True + +def show_score(current_score): + score_surface = font.render(f"Score: {current_score}", True, WHITE) + screen.blit(score_surface, (10, 10)) + +# 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 == pygame.K_SPACE and game_active: + bird_movement = 0 + bird_movement -= 8 # Bird jumps + if event.key == pygame.K_SPACE and not game_active: + game_active = True + pipe_list.clear() + bird.center = (100, HEIGHT // 2) + bird_movement = 0 + score = 0 + if event.type == SPAWNPIPE: + pipe_list.extend(create_pipe()) + + screen.fill(BLUE) + + if game_active: + # Bird + bird_movement += gravity + bird.centery += bird_movement + draw_bird() + + # Pipes + pipe_list = move_pipes(pipe_list) + draw_pipes(pipe_list) + + # Collision + game_active = check_collision(pipe_list) + + # Score (increases when pipes move past bird) + for pipe in pipe_list: + if pipe.centerx == bird.centerx: + score += 0.5 + show_score(int(score)) + else: + game_over_text = font.render("šŸ’„ Game Over!", True, WHITE) + restart_text = font.render("Press SPACE to Restart", True, WHITE) + screen.blit(game_over_text, (80, 250)) + screen.blit(restart_text, (30, 300)) + + pygame.display.update() + clock.tick(FPS) diff --git a/Python/Hangman-Python/Hangman.py b/Python/Hangman-Python/Hangman.py new file mode 100644 index 0000000..306d6a7 --- /dev/null +++ b/Python/Hangman-Python/Hangman.py @@ -0,0 +1,45 @@ +import random + +# List of words for the game +words = ["python", "developer", "hangman", "programming", "computer", "artificial", "intelligence", "keyboard", "science"] + +# Choose a random word +word = random.choice(words) +word_letters = set(word) # unique letters in the word +alphabet = set("abcdefghijklmnopqrstuvwxyz") +used_letters = set() # letters guessed by the user + +lives = 6 # total number of wrong guesses allowed + +print("šŸŽ® Welcome to Hangman!") +print("Guess the word, one letter at a time.") + +while len(word_letters) > 0 and lives > 0: + # show current progress + print("\nYou have", lives, "lives left and you have used these letters: ", " ".join(sorted(used_letters))) + + # show the word with guessed letters and underscores + word_display = [letter if letter in used_letters else "_" for letter in word] + print("Current word: ", " ".join(word_display)) + + # get user input + user_letter = input("Guess a letter: ").lower() + + if user_letter in alphabet - used_letters: + used_letters.add(user_letter) + if user_letter in word_letters: + word_letters.remove(user_letter) + print("āœ… Good guess!") + else: + lives -= 1 + print("āŒ Wrong guess! You lost a life.") + elif user_letter in used_letters: + print("āš ļø You already used that letter. Try again.") + else: + print("🚫 Invalid character. Please enter an alphabet letter.") + +# end of game messages +if lives == 0: + print("\nšŸ’€ You died! The word was:", word) +else: + print("\nšŸŽ‰ Congratulations! You guessed the word:", word) diff --git a/Python/Hangman-Python/README.md b/Python/Hangman-Python/README.md new file mode 100644 index 0000000..94903df --- /dev/null +++ b/Python/Hangman-Python/README.md @@ -0,0 +1,12 @@ +# Hangman Game + +## Game Details +- **Language:** Python +- **Description:** Hangman is a classic word-guessing game. The computer selects a secret word and the player tries to guess it one letter at a time. Each incorrect guess reduces the player's remaining +lives. The player wins by guessing all letters of the secret word before running out of lives. + +## How to Run +1. Make sure Python 3.x is installed. +2. Open a terminal and navigate to the folder: + ```bash + cd Python/Hangman-Python \ No newline at end of file diff --git a/Python/Hangman/Hangman.py b/Python/Hangman/Hangman.py new file mode 100644 index 0000000..306d6a7 --- /dev/null +++ b/Python/Hangman/Hangman.py @@ -0,0 +1,45 @@ +import random + +# List of words for the game +words = ["python", "developer", "hangman", "programming", "computer", "artificial", "intelligence", "keyboard", "science"] + +# Choose a random word +word = random.choice(words) +word_letters = set(word) # unique letters in the word +alphabet = set("abcdefghijklmnopqrstuvwxyz") +used_letters = set() # letters guessed by the user + +lives = 6 # total number of wrong guesses allowed + +print("šŸŽ® Welcome to Hangman!") +print("Guess the word, one letter at a time.") + +while len(word_letters) > 0 and lives > 0: + # show current progress + print("\nYou have", lives, "lives left and you have used these letters: ", " ".join(sorted(used_letters))) + + # show the word with guessed letters and underscores + word_display = [letter if letter in used_letters else "_" for letter in word] + print("Current word: ", " ".join(word_display)) + + # get user input + user_letter = input("Guess a letter: ").lower() + + if user_letter in alphabet - used_letters: + used_letters.add(user_letter) + if user_letter in word_letters: + word_letters.remove(user_letter) + print("āœ… Good guess!") + else: + lives -= 1 + print("āŒ Wrong guess! You lost a life.") + elif user_letter in used_letters: + print("āš ļø You already used that letter. Try again.") + else: + print("🚫 Invalid character. Please enter an alphabet letter.") + +# end of game messages +if lives == 0: + print("\nšŸ’€ You died! The word was:", word) +else: + print("\nšŸŽ‰ Congratulations! You guessed the word:", word) diff --git a/Python/Hangman/README.md b/Python/Hangman/README.md new file mode 100644 index 0000000..939593e --- /dev/null +++ b/Python/Hangman/README.md @@ -0,0 +1,11 @@ +# Hangman Game + +## Game Details +- **Language:** Python +- **Description:** Hangman is a classic word-guessing game where the computer randomly selects a secret word, and the player tries to guess it one letter at a time. + +## How to Run +1. Make sure Python 3.x is installed. +2. Open a terminal and navigate to the folder: + ```bash + cd Python/Hangman \ No newline at end of file diff --git a/Python/Memory-puzzle.py b/Python/Memory-puzzle.py new file mode 100644 index 0000000..b850a89 --- /dev/null +++ b/Python/Memory-puzzle.py @@ -0,0 +1,69 @@ +import tkinter as tk +import random +from functools import partial + +# Create main window +root = tk.Tk() +root.title("🧠 Memory Puzzle Game") +root.geometry("400x400") + +# Game variables +buttons = [] +first_choice = None +second_choice = None +pairs_found = 0 +moves = 0 + +# Symbols (8 pairs for 16 cards) +symbols = list("AABBCCDDEEFFGGHH") +random.shuffle(symbols) + +# Functions +def check_match(btn, symbol): + global first_choice, second_choice, pairs_found, moves + + btn.config(text=symbol, state="disabled", disabledforeground="black") + + if not first_choice: + first_choice = (btn, symbol) + elif not second_choice: + second_choice = (btn, symbol) + root.after(500, evaluate_match) + +def evaluate_match(): + global first_choice, second_choice, pairs_found, moves + + moves += 1 + btn1, sym1 = first_choice + btn2, sym2 = second_choice + + if sym1 != sym2: + btn1.config(text="", state="normal") + btn2.config(text="", state="normal") + else: + pairs_found += 1 + + first_choice = None + second_choice = None + + if pairs_found == 8: + win_label = tk.Label(root, text=f"šŸŽ‰ You won in {moves} moves!", font=("Arial", 16)) + win_label.pack(pady=20) + +# Create buttons (4x4 grid) +for i in range(4): + row = [] + for j in range(4): + index = i*4 + j + btn = tk.Button(root, text="", width=6, height=3, + command=partial(check_match, btn=None, symbol=symbols[index])) + btn.grid(row=i, column=j, padx=5, pady=5) + row.append(btn) + buttons.append(row) + +# Fix button references (needed for partial) +flat_buttons = [btn for row in buttons for btn in row] +for i, btn in enumerate(flat_buttons): + btn.config(command=partial(check_match, btn, symbols[i])) + +root.mainloop() diff --git a/Python/Platformer.py b/Python/Platformer.py new file mode 100644 index 0000000..16c1c13 --- /dev/null +++ b/Python/Platformer.py @@ -0,0 +1,84 @@ +import pygame +import sys + +# Initialize Pygame +pygame.init() + +# Screen dimensions +WIDTH, HEIGHT = 800, 600 +screen = pygame.display.set_mode((WIDTH, HEIGHT)) +pygame.display.set_caption("šŸƒ Platformer Game") + +# Clock for FPS +clock = pygame.time.Clock() +FPS = 60 + +# Colors +WHITE = (255, 255, 255) +BLUE = (135, 206, 250) +GREEN = (0, 200, 0) +RED = (200, 0, 0) + +# Player setup +player_width = 50 +player_height = 60 +player_x = 100 +player_y = HEIGHT - player_height - 50 +player_velocity_y = 0 +player_speed = 5 +jump_force = 15 +gravity = 0.8 +on_ground = False + +player_rect = pygame.Rect(player_x, player_y, player_width, player_height) + +# Platforms +platforms = [ + pygame.Rect(0, HEIGHT-40, WIDTH, 40), # ground + pygame.Rect(200, HEIGHT-150, 150, 20), + pygame.Rect(450, HEIGHT-250, 150, 20), + pygame.Rect(650, HEIGHT-350, 120, 20) +] + +# Game loop +running = True +while running: + clock.tick(FPS) + screen.fill(BLUE) + + # Event handling + for event in pygame.event.get(): + if event.type == pygame.QUIT: + pygame.quit() + sys.exit() + + # Key presses + keys = pygame.key.get_pressed() + if keys[pygame.K_LEFT]: + player_rect.x -= player_speed + if keys[pygame.K_RIGHT]: + player_rect.x += player_speed + if keys[pygame.K_SPACE] and on_ground: + player_velocity_y = -jump_force + on_ground = False + + # Apply gravity + player_velocity_y += gravity + player_rect.y += player_velocity_y + + # Collision with platforms + on_ground = False + for platform in platforms: + if player_rect.colliderect(platform) and player_velocity_y > 0: + player_rect.bottom = platform.top + player_velocity_y = 0 + on_ground = True + + # Draw platforms + for platform in platforms: + pygame.draw.rect(screen, GREEN, platform) + + # Draw player + pygame.draw.rect(screen, RED, player_rect) + + pygame.display.update() diff --git a/Python/Rock-paper.py b/Python/Rock-paper.py new file mode 100644 index 0000000..329a5dc --- /dev/null +++ b/Python/Rock-paper.py @@ -0,0 +1,35 @@ +import random + +# List of possible choices +choices = ["rock", "paper", "scissors"] + +print("šŸŽ® Welcome to Rock, Paper, Scissors!") +print("You will play against the computer.") +print("Type 'rock', 'paper', or 'scissors' to play. Type 'quit' to exit.") + +while True: + # User input + user_choice = input("\nEnter your choice: ").lower() + + if user_choice == "quit": + print("Thanks for playing! šŸ‘‹") + break + + if user_choice not in choices: + print("Invalid choice! Please choose rock, paper, or scissors.") + continue + + # Computer randomly chooses + computer_choice = random.choice(choices) + print(f"Computer chose: {computer_choice}") + + # Determine winner + if user_choice == computer_choice: + print("šŸ¤ It's a tie!") + elif (user_choice == "rock" and computer_choice == "scissors") or \ + (user_choice == "paper" and computer_choice == "rock") or \ + (user_choice == "scissors" and computer_choice == "paper"): + print("šŸŽ‰ You win!") + else: + print("šŸ’€ You lose!") +