This is a stock market, python game. To play, you have to make a profit. You start with $1000, buy or sell stock whenever you wish, and either make a profit or lose money. You have 60 seconds to make as much money as you can.
If you would like to view an example, please click HERE.
To play, copy and paste the code below into your Python file. After that, run the code and enjoy!
Copy the code for FREE!:
import pygame import random # Initialize Pygame pygame.init() # Constants WIDTH, HEIGHT = 800, 600 WHITE = (255, 255, 255) BLACK = (0, 0, 0) GREEN = (0, 255, 0) RED = (255, 0, 0) FONT_SIZE = 30 STOCK_CHANGE_INTERVAL = 2000 # 2 seconds GAME_DURATION = 60000 # 1 minute in milliseconds GOAL_BALANCE = 2000 # Screen setup screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Stock Market Game") # Fonts font = pygame.font.SysFont('arial', FONT_SIZE) large_font = pygame.font.SysFont('arial', 50) # Game variables balance = 1000 stocks_owned = 0 stock_price = 100 stock_price_history = [stock_price] # List to track stock price changes last_update_time = 0 start_time = pygame.time.get_ticks() game_over = False # Button class class Button: def __init__(self, text, x, y, width, height, callback): self.text = text self.rect = pygame.Rect(x, y, width, height) self.callback = callback def draw(self, screen): pygame.draw.rect(screen, BLACK, self.rect) text_surface = font.render(self.text, True, WHITE) text_rect = text_surface.get_rect(center=self.rect.center) screen.blit(text_surface, text_rect) def is_clicked(self, event): return event.type == pygame.MOUSEBUTTONDOWN and self.rect.collidepoint(event.pos) # Functions def draw_text(text, color, x, y, font): text_surface = font.render(text, True, color) screen.blit(text_surface, (x, y)) def draw_stock_price_history(): if len(stock_price_history) < 2: return max_price = max(stock_price_history) min_price = min(stock_price_history) for i in range(len(stock_price_history) - 1): x1 = WIDTH - 200 + i * 2 y1 = HEIGHT - (stock_price_history[i] - min_price) * (HEIGHT - 100) / (max_price - min_price + 1) x2 = WIDTH - 200 + (i + 1) * 2 y2 = HEIGHT - (stock_price_history[i + 1] - min_price) * (HEIGHT - 100) / (max_price - min_price + 1) pygame.draw.line(screen, GREEN, (x1, y1), (x2, y2), 2) def update_stock_price(): global stock_price stock_price += random.randint(-10, 10) stock_price = max(stock_price, 1) # Ensure the stock price doesn't drop below 1 # Random event that affects stock price significantly if random.random() < 0.1: # 10% chance of event stock_price += random.randint(-50, 50) stock_price = max(stock_price, 1) stock_price_history.append(stock_price) def buy_stock(): global balance, stocks_owned, stock_price if balance >= stock_price: balance -= stock_price stocks_owned += 1 def sell_stock(): global balance, stocks_owned, stock_price if stocks_owned > 0: balance += stock_price stocks_owned -= 1 def check_game_over(): global game_over current_time = pygame.time.get_ticks() if current_time - start_time >= GAME_DURATION or balance >= GOAL_BALANCE: game_over = True # Buttons buy_button = Button('Buy Stock', WIDTH - 200, HEIGHT - 100, 150, 50, buy_stock) sell_button = Button('Sell Stock', WIDTH - 400, HEIGHT - 100, 150, 50, sell_stock) # Game loop running = True while running: current_time = pygame.time.get_ticks() for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif buy_button.is_clicked(event): buy_button.callback() elif sell_button.is_clicked(event): sell_button.callback() if not game_over: # Update stock price every STOCK_CHANGE_INTERVAL milliseconds if current_time - last_update_time > STOCK_CHANGE_INTERVAL: update_stock_price() last_update_time = current_time check_game_over() # Drawing screen.fill(WHITE) # Clear the screen with white draw_text(f"Balance: ${balance}", BLACK, 20, 20, font) draw_text(f"Stocks Owned: {stocks_owned}", BLACK, 20, 60, font) draw_text(f"Stock Price: ${stock_price}", BLACK, 20, 100, font) draw_text("Stock Market Game", BLACK, 20, 150, large_font) draw_text("Stock Price History", BLACK, WIDTH - 200, HEIGHT - 200, font) # Adjusted position draw_stock_price_history() remaining_time = max(0, GAME_DURATION - (current_time - start_time)) // 1000 draw_text(f"Time Left: {remaining_time}s", BLACK, 20, 200, font) if game_over: if balance >= GOAL_BALANCE: draw_text("You Win!", GREEN, WIDTH // 2 - 100, HEIGHT // 2, large_font) else: draw_text("Game Over!", RED, WIDTH // 2 - 100, HEIGHT // 2, large_font) else: buy_button.draw(screen) sell_button.draw(screen) pygame.display.flip() pygame.quit()