diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..26d33521 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/Python-Projects.iml b/.idea/Python-Projects.iml new file mode 100644 index 00000000..07abf202 --- /dev/null +++ b/.idea/Python-Projects.iml @@ -0,0 +1,12 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 00000000..dc64fad4 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,26 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 00000000..105ce2da --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..db8786c0 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..c1b104bb --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Car Racing Game/background_music.wav b/Car Racing Game/background_music.wav new file mode 100644 index 00000000..85ac3c91 Binary files /dev/null and b/Car Racing Game/background_music.wav differ diff --git a/Car Racing Game/car_nfs.png b/Car Racing Game/car_nfs.png new file mode 100644 index 00000000..6c1e9f4c Binary files /dev/null and b/Car Racing Game/car_nfs.png differ diff --git a/Car Racing Game/main.py b/Car Racing Game/main.py new file mode 100644 index 00000000..6623e2d7 --- /dev/null +++ b/Car Racing Game/main.py @@ -0,0 +1,158 @@ +import pygame +import random + +# Constants +SCREEN_WIDTH = 1000 +SCREEN_HEIGHT = 700 +CAR_WIDTH = 80 +CAR_HEIGHT = 160 +ROAD_WIDTH = 600 +FPS = 60 +WHITE = (255, 255, 255) + +class Car: + def __init__(self): + self.x = SCREEN_WIDTH // 2 - CAR_WIDTH // 2 + self.y = SCREEN_HEIGHT - CAR_HEIGHT - 10 + self.speed_x = 0 + self.speed_y = 0 + self.image = pygame.transform.scale(pygame.image.load('car_nfs.png'), (CAR_WIDTH, CAR_HEIGHT)) + + def move_left(self): + if self.x > SCREEN_WIDTH // 4 - CAR_WIDTH: + self.speed_x = -5 + + def move_right(self): + if self.x < SCREEN_WIDTH // 2: + self.speed_x = 5 + + def move_forward(self): + self.speed_y = -5 + + def move_backward(self): + self.speed_y = 5 + + def stop_x(self): + self.speed_x = 0 + + def stop_y(self): + self.speed_y = 0 + + def update_position(self): + self.x += self.speed_x + self.y += self.speed_y + + def draw(self): + screen.blit(self.image, (self.x, self.y)) + +class Road: + def __init__(self): + self.y = 0 + self.speed = 5 + self.image = pygame.image.load('road_nfs.jpg') + self.image = pygame.transform.scale(self.image, (ROAD_WIDTH, SCREEN_HEIGHT)) + + def move(self): + self.y += self.speed + if self.y > SCREEN_HEIGHT: + self.y = 0 + + def draw(self): + screen.blit(self.image, (SCREEN_WIDTH // 4, self.y)) + screen.blit(self.image, (SCREEN_WIDTH // 4, self.y - SCREEN_HEIGHT)) + +class HurdleManager: + def __init__(self): + self.hurdles = [] + self.speed = 5 + + def create_hurdle(self): + size = random.randint(20, 50) + x = random.randint(SCREEN_WIDTH // 4, SCREEN_WIDTH // 2 - size) + y = -size + self.hurdles.append(pygame.Rect(x, y, size, size)) + + def move_hurdles(self): + for hurdle in self.hurdles: + hurdle.y += self.speed + if hurdle.y > SCREEN_HEIGHT: + self.hurdles.remove(hurdle) + +class Game: + def __init__(self): + self.car = Car() + self.road = Road() + self.hurdle_manager = HurdleManager() + self.score = 0 + self.play_background_music() + + def handle_events(self): + for event in pygame.event.get(): + if event.type == pygame.QUIT: + pygame.quit() + quit() + + keys = pygame.key.get_pressed() + if keys[pygame.K_LEFT]: + self.car.move_left() + if keys[pygame.K_RIGHT]: + self.car.move_right() + if keys[pygame.K_UP]: + self.car.move_forward() + if keys[pygame.K_DOWN]: + self.car.move_backward() + + if not keys[pygame.K_LEFT] and not keys[pygame.K_RIGHT]: + self.car.stop_x() + if not keys[pygame.K_UP] and not keys[pygame.K_DOWN]: + self.car.stop_y() + + def update_score(self): + self.score += 1 + + def display_score(self): + font = pygame.font.Font(None, 36) + score_text = font.render("Score: " + str(self.score), True, WHITE) + screen.blit(score_text, (10, 10)) + + def run(self): + clock = pygame.time.Clock() + + while True: + self.handle_events() + + self.road.move() + + screen.fill((0, 0, 0)) + self.road.draw() + + self.hurdle_manager.create_hurdle() + self.hurdle_manager.move_hurdles() + self.car.update_position() + self.car.draw() + self.display_score() + + self.car.speed_x = self.road.speed * 2 + self.car.speed_y = self.road.speed * 2 + self.hurdle_manager.speed = self.road.speed * 1.5 + + self.update_score() + + pygame.display.update() + clock.tick(FPS) + + def play_background_music(self): + pygame.mixer.init() + pygame.mixer.music.load('background_music.wav') + pygame.mixer.music.play(-1) # -1 makes the music loop indefinitely + +# Initialize Pygame +pygame.init() + +# Create the game window +screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) +pygame.display.set_caption("Need for Speed - Car Racing Game") + +# Run the game +game = Game() +game.run() diff --git a/Car Racing Game/road_nfs.jpg b/Car Racing Game/road_nfs.jpg new file mode 100644 index 00000000..e687318a Binary files /dev/null and b/Car Racing Game/road_nfs.jpg differ diff --git a/Language Detector/Readme.md b/Language Detector/Readme.md new file mode 100644 index 00000000..ba80a44c --- /dev/null +++ b/Language Detector/Readme.md @@ -0,0 +1,15 @@ +# Language Detector + +This is a simple Python project that uses the `langdetect` library to detect the language of a given text. + +## Setup + +1. Clone the repository. +2. Install the dependencies: + + ```bash + pip install -r requirements.txt +3. Run the main script: + + ```bash + python main.py diff --git a/Language Detector/main.py b/Language Detector/main.py new file mode 100644 index 00000000..bff53e01 --- /dev/null +++ b/Language Detector/main.py @@ -0,0 +1,18 @@ +# Language Detector +from langdetect import detect + +# Detecting languages from input text +language1 = detect('¿Qué te gusta hacer') +language2 = detect('Nyasae no omuya') + +# Mapping of language codes to human-readable names +language_mapping = { + 'es': 'Spanish', + 'en': 'English', + 'sw': 'Swahili', + 'ek': 'Kisii' +} + +# Print the detected language in a more user-friendly format +print(f'This is the {language_mapping.get(language1, language1)} language shortened by: {language1}') +print(f'This is the {language_mapping.get(language2, language2)} language shortened by: {language2}') diff --git a/Language Detector/requirements.txt b/Language Detector/requirements.txt new file mode 100644 index 00000000..084b89b2 --- /dev/null +++ b/Language Detector/requirements.txt @@ -0,0 +1 @@ +langdetect==1.0.9 diff --git a/Networking-Tools/README.md b/Networking-Tools/README.md new file mode 100644 index 00000000..563e03e8 --- /dev/null +++ b/Networking-Tools/README.md @@ -0,0 +1,11 @@ +# Networking Tools +A set of simple networking tools created in Python + +## How To Use +⚠️ **Please consider using port scanner for educational purposes only. Using it to scan websites or IP addresses without permission can be seen as a crime.** + +Install the requirements using Git: + +`pip install -r requirements.txt` + +Run `tools_main.py` \ No newline at end of file diff --git a/Networking-Tools/requirements.txt b/Networking-Tools/requirements.txt new file mode 100644 index 00000000..999704a3 --- /dev/null +++ b/Networking-Tools/requirements.txt @@ -0,0 +1,2 @@ +requests +pyfiglet \ No newline at end of file diff --git a/Networking-Tools/tools.py b/Networking-Tools/tools.py new file mode 100644 index 00000000..fc92bb44 --- /dev/null +++ b/Networking-Tools/tools.py @@ -0,0 +1,213 @@ +""" +This module contains tools such as IP Locator, IP Finder, +Pinging and Port Scanner. +DISCLAIMER: Please consider using Port Scanner in educational purposes only. + + +## TOOLS DESCRIPTION +### IP Locater +Takes the IP address and with the help of an [API](https://ip-api.com/), extracts data about the IP address. +Then writes the extracted data in two different files with the same name "ip_data". One of the files is a JSON file. The other one is +a text file. These files will be saved on the system's desktop. + +### IP Finder +Takes the domain name and extracts the IPs that are relevant with the domain name. +It uses windows `nslookup` command to get IPs. + +### Pinging +Takes the domain name or IP address and then pings it. +It uses `ping` command to ping the domain name or IP address. + +### Port Scanner +Takes the domain name or IP address. After that, it gets the range of the ports to scan. +Then it starts scanning to look for open ports. The Port Scanner will only look for open ports. +So if a port is not open, it will not show it on the screen. +""" + + +# Import necessary modules +import subprocess +import platform +import requests +import pyfiglet +import datetime +import socket +import json +import os +import re + + +# Make the foreground color of the terminal green. +subprocess.call("color A", shell=True) + +# A pattern to validate an IP address. +ip_pattern = r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$" + + +# Tools +def locate_ip(): + """ + Locate an IP address using an API. + """ + # Clear up the terminal. + subprocess.call("cls", shell=True) + # Banner of the tool. + banner = pyfiglet.figlet_format("IP LOCATER") + print(banner) + # Get the IP address. Check the input whether it is a valid IP address. + # This while loop will run until it gets a valid IP address that matches the IP pattern. + while True: + IP_address = input("\nEnter a valid IP address: ") + # Check if the given input whether it is 'q'. + # If it is, then it will abort operation and quit program. + if IP_address.lower() == 'q': + quit() + # Validating the given IP address. + if not re.match(ip_pattern, IP_address): + print("Invalid IP address") + continue + else: + break + # Change current working directory to system's desktop + os.chdir(rf"C:\Users\{os.getlogin()}\Desktop") + # File names containing data of the IP address. + file_name_json = "ip_data.json" + file_name_text = "ip_data.txt" + # The files containing IP data will have these fields. + fields = "status,message,continent,continentCode,country,countryCode,region,regionName,city,district,zip,lat,lon,timezone,currency,isp,org,as,mobile,proxy,hosting,query" + # Sending a request to extract data. + url = f"http://ip-api.com/json/{IP_address}?fields={fields}" + response = requests.get(url) + # Write the extracted data in the files. + # Write on both JSON file and text file. + with open(file_name_json, 'w') as ip_data_file_json: + json.dump(response.json(), ip_data_file_json, indent=4) + with open(file_name_text, 'w') as ip_data_file_text: + ip_data_file_text.write(response.text) + # Let the user know that files created on the system's desktop. + print("You got the files containing data about the given IP address.") + print("Please check your system desktop.") + input("\nPress any key to continue...") + + +def get_ip(): + """ + Get the IP address of a certain domain name. + """ + subprocess.call("cls", shell=True) + # Banner of the tool. + banner = pyfiglet.figlet_format("IP FINDER") + print(banner) + # Get the domain name. + # Check if the given input whether it is 'q'. + # If it is, then it will abort operation and quit program. + domain_name = input("\nEnter a valid domain name: ") + if domain_name.lower() == 'q': + quit() + # Get the IP of the domain name. + command = f"nslookup {domain_name}" + subprocess.call(command) == 0 + input("\nPress any key to continue...") + + +def ping(): + """ + Ping a domain or website or IP address. + """ + subprocess.call("cls", shell=True) + # Banner of the tool. + banner = pyfiglet.figlet_format("PING") + print(banner) + # Get the host to ping. + # Host can be a domain name or an IP address. + # Check the given input whether it is 'q'. + # If it is, then abort operation and quit program. + host = input("Enter a valid domain name or IP address: ") + if host.lower() == 'q': + quit() + # If the os is Windows, then the parameter is "-n". + # If not, the parameter is "-c" + if platform.system().lower() == "windows": + parameter = "-n" + else: + parameter = "-c" + # It will send 5 packages to ping the host. + command = f"ping {parameter} 5 {host}" + subprocess.call(command) == 0 + input("\nPress any key to continue...") + + +def port_scanner(): + """ + Scan ports on a certain host. + """ + subprocess.call("cls", shell=True) + # Banner of the tool. + banner = pyfiglet.figlet_format("PORT SCANNER") + print(banner) + print("For scanning using a domain, enter .\nFor scanning using an IP, enter ") + # All this terrifying while loop does is to check the given input at "scan_type". + # If it is "q", then it will abort operation and quit program. + # If it is "ip", then it will validate the IP address. If it is valid, then it will break the loop. + # If it is "domain", then it will get the domain name and break the loop. + # If it is none of the above, it will let the user know that the input is not valid. + while True: + scan_type = input(">>> ") + if scan_type.lower() == 'q': + quit() + if scan_type.lower() == 'ip': + while True: + host = input("Enter IP for scanning:\n>>> ") + if host.lower() == 'q': + quit() + if not re.match(ip_pattern, host): + print("Invalid IP address.\n") + continue + else: + break + break + elif scan_type.lower() == 'domain': + host = input("Enter domain for scanning:\n>>> ") + if host.lower() == 'q': + quit() + break + else: + print("Invalid input.\n") + continue + # Get the IP address of the domain name if a domain name is given. + hostIP = socket.gethostbyname(host) + # Get the port range. + port_range = input("Enter port range in format - (ex: 20-80):\n>>> ") + # Split the starting port and the ending port. + port_range = port_range.split("-") + # Make a neat banner again containing IP address and the time that scanning started. + print("_"*60) + print("Scanning ports on host: ", hostIP) + start_time = datetime.datetime.now() + print("Scan started at ", start_time) + print("_"*60) + print("\nPort\t\t\tStatus\n") + # Start scanning ports + for port in range(int(port_range[0]), int(port_range[1])+1): + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + # Timeout time for scanning each port is half a second. + s.settimeout(0.5) + connection = s.connect_ex((hostIP, port)) + # Check whether the port is open or not. + # If it is open, print it. + # If it is not open, pass the closed port. + if connection == 0: + print(f"{port}\t----------\tOpen") + else: + pass + s.close() + # Scan ending time. + end_time = datetime.datetime.now() + # Time taken to scan. + time_taken = end_time - start_time + # Make a neat banner again containing the time that scanning ended and the time taken to scan. + print("_"*60) + print("Scan ended at ", end_time) + print("Time taken: ", time_taken) + print("_"*60) + input("\nPress any key to continue...") diff --git a/Networking-Tools/tools_main.py b/Networking-Tools/tools_main.py new file mode 100644 index 00000000..b9ffd1b1 --- /dev/null +++ b/Networking-Tools/tools_main.py @@ -0,0 +1,125 @@ +# Import necessary modules +import subprocess +import tools + + +# Function for displaying menu +def Display_Menu(): + header = """ + ___________________________ + | | + | Basic Networking Tools | + |_____________ __________| + \ \ + ^_ _^ + (o o)\_________ + (_ _)\ )/\/ + U ||----W|| + || || + + ### Available Tools: + [1] Locate an IP address + [2] Get the IP address of a domain + [3] Ping a host or IP address + [4] Port Scanner + [5] Exit + + * Type to clear previous commands + * Type or to abort operation of a tool + + """ + print(header) + + +# Functionality of every option +# Each option operates the tool related to the option number. +def option_1(): + """ + Option 1 --> IP Locater + """ + tools.locate_ip() + Display_Menu() + Home() + + +def option_2(): + """ + Option 2 --> IP Finder + """ + tools.get_ip() + Display_Menu() + Home() + + +def option_3(): + """ + Option 3 --> Pinging + """ + tools.ping() + Display_Menu() + Home() + + +def option_4(): + """ + Option 4 --> Port Scanner + """ + tools.port_scanner() + Display_Menu() + Home() + + +def option_5(): + """ + Option 5 --> Exit + """ + quit() + + +# Function for selecting options +def Home(): + """ + Home of the program is selecting an option from available options. + """ + # Available options' numbers as shown in the menu + available_options = (1, 2, 3, 4, 5) + # Get the option's number. + # If it is wrong, this while loop will run unless it gets + # a proper number that is related with one of the available options. + while True: + try: + selected_option = input("\nEnter your option\n>>> ") + # In order to clear the mess created by the previous commands, + # we define 'clear' command to clear up the screen from previous commands. + if selected_option == 'clear': + subprocess.call('cls', shell=True) + Display_Menu() + continue + else: + selected_option = int(selected_option) + except ValueError: + print("Please enter the number of the option") + continue + else: + # If the given option number is not available, try to get an available option number. + if selected_option not in available_options: + print("The option is not available.\nTry another one.") + continue + else: + break + # Operate the option related to the given number + if selected_option == 1: + option_1() + elif selected_option == 2: + option_2() + elif selected_option == 3: + option_3() + elif selected_option == 4: + option_4() + elif selected_option == 5: + option_5() + + +if __name__ == "__main__": + Display_Menu() + Home() diff --git a/Number Guessing Game/main.py b/Number Guessing Game/main.py index 00af59d2..6901e1cb 100644 --- a/Number Guessing Game/main.py +++ b/Number Guessing Game/main.py @@ -1,43 +1,76 @@ import random -""" - Funtion start() create the magic number, number of attemps, and ask user for input -""" def start(): - global magic_number - magic_number = random.randint(1, 100) - - global attempts + """ + Creates the magic number, number of attempts, and asks the user for input. + """ + global magic_number, attempts + magic_number = choose_difficulty() attempts = 0 - print('---------------------------') - print('Guess a number between 1 and 100') + print('Guess the magic number!') + print('Try to guess the number in as few attempts as possible.') + +def choose_difficulty(): + """ + Allows the user to select the difficulty level and returns the corresponding range for the magic number. + """ + print("Choose difficulty level:") + print("1. Easy (1-50)") + print("2. Medium (1-100)") + print("3. Hard (1-200)") + + while True: + level = input("Enter 1, 2, or 3: ") + if level == '1': + return random.randint(1, 50) + elif level == '2': + return random.randint(1, 100) + elif level == '3': + return random.randint(1, 200) + else: + print("Invalid choice. Please enter 1, 2, or 3.") -# Function that checks if player won, if it won, returns True def check_win(player_guess): + """ + Checks if the player’s guess is correct, too high, or too low. + """ if player_guess > magic_number: - print('Too big...') + print('Too high! Try a smaller number.') elif player_guess < magic_number: - print('Too small') + print('Too low! Try a larger number.') elif player_guess == magic_number: return True -start() - -# Game loop -while True: - # Take the player input - guess = int(input()) - attempts += 1 - - if check_win(guess): - print('You won! - Number of attempts: ' + str(attempts)) - - keep_playing = input('Keep playing?(y\\n)') - # If player want to keep the game, reset the number of attempts - if keep_playing == 'y': - attempts = 0 - start() - # If player don't want to keep playing, quit the game - elif keep_playing == 'n': - quit() +def play_game(): + """ + Main game loop that starts the game, processes user input, and checks for win conditions. + """ + global attempts + start() + + while True: + try: + guess = int(input('Enter your guess: ')) + if guess < 1 or guess > 200: + print("Please enter a number between 1 and 200.") + continue + except ValueError: + print("Invalid input. Please enter a valid number.") + continue + + attempts += 1 + + if check_win(guess): + print(f'Congratulations! You guessed the number in {attempts} attempts.') + keep_playing = input('Would you like to play again? (y/n): ') + if keep_playing.lower() == 'y': + play_game() + elif keep_playing.lower() == 'n': + print("Thanks for playing! Have a great day!") + break + else: + print("Invalid input. Exiting the game.") + break + +play_game()