top of page
Beach

Projects

XCOIN

This code is a bitcoin simulation. It can be improved and even made real. The currency is Turkish Lira for now, you can change it.
 

import tkinter as tk

import random

import json

import os

 

class PriceSimulatorApp:

    def __init__(self, root):

        self.root = root

        self.root.title("Xcoin")

        self.root.attributes("-fullscreen", True)

        self.root.bind("<Escape>", self.exit_fullscreen)

 

        self.data_file = 'xcoin_data.json'

        self.load_data()

 

        self.price = 1.0  

        self.prices = [self.price]  

        self.price_label = tk.Label(root, text=f"Price: {self.price:.2f} TL", font=("Helvetica", 32))

        self.price_label.pack(pady=20)

 

        self.balance_label = tk.Label(root, text=f"Balance: {self.balance:.2f} TL", font=("Helvetica", 24))

        self.balance_label.pack(pady=20)

 

        self.owned_label = tk.Label(root, text=f"Owned Xcoin: {self.owned} units", font=("Helvetica", 24))

        self.owned_label.pack(pady=20)

 

        self.buy_button = tk.Button(root, text="Buy", font=("Helvetica", 24), command=self.buy)

        self.buy_button.pack(side=tk.LEFT, padx=20)

 

        self.sell_button = tk.Button(root, text="Sell", font=("Helvetica", 24), command=self.sell)

        self.sell_button.pack(side=tk.RIGHT, padx=20)

 

        self.canvas = tk.Canvas(root, width=800, height=400, bg='white')

        self.canvas.pack(fill=tk.BOTH, expand=True)

 

        self.update_price()

 

    def load_data(self):

        if os.path.exists(self.data_file):

            with open(self.data_file, 'r') as file:

                data = json.load(file)

                self.balance = data.get('balance', 10.0)

                self.owned = data.get('owned', 0)

        else:

            self.balance = 10.0  

            self.owned = 0  

    def save_data(self):

        data = {

            'balance': self.balance,

            'owned': self.owned

        }

        with open(self.data_file, 'w') as file:

            json.dump(data, file)

 

    def update_price(self):

        change = random.uniform(-0.20, 0.20)

        new_price = max(1.0, self.price * (1 + change))

        self.price = new_price

        self.prices.append(self.price)

        if len(self.prices) > 30:

            self.prices.pop(0)

 

        self.price_label.config(text=f"Price: {self.price:.2f} TL")

        self.update_graph()

 

        self.root.after(1000, self.update_price)

 

    def update_graph(self):

        self.canvas.delete("all")

        max_price = max(self.prices)

        min_price = min(self.prices)

        canvas_height = int(self.canvas['height'])

        canvas_width = int(self.canvas['width'])

        bar_width = canvas_width / len(self.prices)

       

        for i, price in enumerate(self.prices):

            x0 = i * bar_width

            y0 = canvas_height - (price / max_price * canvas_height)

            x1 = (i + 1) * bar_width

            y1 = canvas_height

            self.canvas.create_rectangle(x0, y0, x1, y1, fill="blue", outline="black")

            self.canvas.create_text(x0 + bar_width / 2, y0, anchor=tk.S, text=f"{price:.2f}")

 

    def buy(self):

        if self.balance >= self.price:

            self.balance -= self.price

            self.owned += 1

            self.update_labels()

            self.save_data()

 

    def sell(self):

        if self.owned > 0:

            self.balance += self.price

            self.owned -= 1

            self.update_labels()

            self.save_data()

 

    def update_labels(self):

        self.balance_label.config(text=f"Balance: {self.balance:.2f} TL")

        self.owned_label.config(text=f"Owned Xcoin: {self.owned} units")

 

    def exit_fullscreen(self, event=None):

        self.root.attributes("-fullscreen", False)

 

root = tk.Tk()

app = PriceSimulatorApp(root)

root.mainloop()

bottom of page