Thursday, July 9, 2026

Tahsin's Python Table Tennis Game App

import tkinter as tk

import random

import os

import platform


# --- Sound function ---

def play_background_sound():

    try:

        if platform.system() == "Windows":

            import winsound

            winsound.PlaySound("SystemExit", winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP)

        else:

            os.system("afplay /System/Library/Sounds/Hero.aiff &")

    except Exception:

        pass


# --- Game class ---

class TableTennisGame:

    def __init__(self, root):

        self.root = root

        self.root.title("Virtual Table Tennis")

        self.root.resizable(False, False)


        self.canvas = tk.Canvas(root, width=800, height=400, bg="green")

        self.canvas.pack()


        # Ball

        self.ball = self.canvas.create_oval(390, 190, 410, 210, fill="white")

        self.ball_dx = 3

        self.ball_dy = 3


        # Player racket

        self.player_racket = self.draw_racket(30, 150)

        # Opponent racket

        self.opponent_racket = self.draw_racket(760, 150)


        # Scores

        self.player_score = 0

        self.opponent_score = 0

        self.score_text = self.canvas.create_text(400, 20, text="Player: 0   Opponent: 0",

                                                  font=("Arial", 16), fill="white")


        # Bind controls

        self.root.bind("<Up>", lambda e: self.move_racket(self.player_racket, -20))

        self.root.bind("<Down>", lambda e: self.move_racket(self.player_racket, 20))


        self.update_game()


    def draw_racket(self, x, y):

        handle = self.canvas.create_rectangle(x, y+40, x+10, y+100, fill="brown")

        head = self.canvas.create_oval(x-20, y-20, x+30, y+30, fill="red")

        return (handle, head)


    def move_racket(self, racket, dy):

        for part in racket:

            self.canvas.move(part, 0, dy)


    def get_racket_coords(self, racket):

        return self.canvas.coords(racket[1])


    def update_score(self):

        self.canvas.itemconfig(self.score_text,

                               text=f"Player: {self.player_score}   Opponent: {self.opponent_score}")


    def update_game(self):

        self.canvas.move(self.ball, self.ball_dx, self.ball_dy)

        bx1, by1, bx2, by2 = self.canvas.coords(self.ball)


        # Bounce top/bottom

        if by1 <= 0 or by2 >= 400:

            self.ball_dy *= -1


        # Player collision

        px1, py1, px2, py2 = self.get_racket_coords(self.player_racket)

        if bx1 <= px2 and by2 >= py1 and by1 <= py2:

            self.ball_dx = abs(self.ball_dx)


        # Opponent AI

        ox1, oy1, ox2, oy2 = self.get_racket_coords(self.opponent_racket)

        racket_center = (oy1 + oy2) / 2

        ball_center = (by1 + by2) / 2

        if racket_center < ball_center:

            self.move_racket(self.opponent_racket, 5)

        else:

            self.move_racket(self.opponent_racket, -5)


        # Opponent collision

        if bx2 >= ox1 and by2 >= oy1 and by1 <= oy2:

            self.ball_dx = -abs(self.ball_dx)


        # Scoring

        if bx1 <= 0:  # Opponent scores

            self.opponent_score += 1

            self.update_score()

            self.reset_ball()

        elif bx2 >= 800:  # Player scores

            self.player_score += 1

            self.update_score()

            self.reset_ball()


        self.root.after(20, self.update_game)


    def reset_ball(self):

        self.canvas.coords(self.ball, 390, 190, 410, 210)

        self.ball_dx = random.choice([-3, 3])

        self.ball_dy = random.choice([-3, 3])


# --- Run game ---

if __name__ == "__main__":

    play_background_sound()

    root = tk.Tk()

    game = TableTennisGame(root)

    root.mainloop()

Tahsin's Tiny Virtual Operating System (written in Python; Features: Text Editor, Calculator, File Explorer and Terminal)

import tkinter as tk

from tkinter import ttk, filedialog, messagebox

import os


class VirtualOS:

    def __init__(self, root):

        self.root = root

        self.root.title("Virtual OS")

        self.root.geometry("800x600")


        # Desktop background

        self.desktop = tk.Frame(self.root, bg="lightblue")

        self.desktop.pack(fill="both", expand=True)


        # Taskbar

        self.taskbar = tk.Frame(self.root, bg="gray", height=30)

        self.taskbar.pack(side="bottom", fill="x")


        # Taskbar buttons

        tk.Button(self.taskbar, text="Text Editor", command=self.open_text_editor).pack(side="left")

        tk.Button(self.taskbar, text="Calculator", command=self.open_calculator).pack(side="left")

        tk.Button(self.taskbar, text="File Explorer", command=self.open_file_explorer).pack(side="left")

        tk.Button(self.taskbar, text="Terminal", command=self.open_terminal).pack(side="left")


    # --- Applications ---

    def open_text_editor(self):

        win = tk.Toplevel(self.root)

        win.title("Text Editor")

        text = tk.Text(win, wrap="word")

        text.pack(fill="both", expand=True)


        def save_file():

            f = filedialog.asksaveasfilename(defaultextension=".txt")

            if f:

                with open(f, "w") as file:

                    file.write(text.get("1.0", "end-1c"))


        tk.Button(win, text="Save", command=save_file).pack()


    def open_calculator(self):

        win = tk.Toplevel(self.root)

        win.title("Calculator")


        entry = tk.Entry(win, width=20)

        entry.pack()


        def calculate():

            try:

                result = eval(entry.get())

                messagebox.showinfo("Result", f"Answer: {result}")

            except Exception as e:

                messagebox.showerror("Error", str(e))


        tk.Button(win, text="Calculate", command=calculate).pack()


    def open_file_explorer(self):

        win = tk.Toplevel(self.root)

        win.title("File Explorer")


        tree = ttk.Treeview(win)

        tree.pack(fill="both", expand=True)


        path = os.getcwd()

        tree.insert("", "end", text=path, open=True)


        for item in os.listdir(path):

            tree.insert("", "end", text=item)


    def open_terminal(self):

        win = tk.Toplevel(self.root)

        win.title("Terminal")


        output = tk.Text(win, height=15, bg="black", fg="white")

        output.pack(fill="both", expand=True)


        entry = tk.Entry(win)

        entry.pack(fill="x")


        def run_command(event=None):

            cmd = entry.get()

            entry.delete(0, "end")

            if cmd == "ls":

                output.insert("end", "\n".join(os.listdir(os.getcwd())) + "\n")

            elif cmd.startswith("cd "):

                try:

                    os.chdir(cmd.split(" ", 1)[1])

                    output.insert("end", f"Changed directory to {os.getcwd()}\n")

                except Exception as e:

                    output.insert("end", f"Error: {e}\n")

            elif cmd == "pwd":

                output.insert("end", os.getcwd() + "\n")

            else:

                output.insert("end", f"Unknown command: {cmd}\n")


        entry.bind("<Return>", run_command)


# --- Run Virtual OS ---

if __name__ == "__main__":

    root = tk.Tk()

    os_system = VirtualOS(root)

    root.mainloop()


Tahsin's Python Table Tennis Game App

import tkinter as tk import random import os import platform # --- Sound function --- def play_background_sound():     try:         if platf...