본문 바로가기

Chat

시간 죽이기: 슬롯 머신 코드 _by python

색상 코드 요약:
\033[91m: 밝은 빨간색,   \033[92m: 밝은 초록색
\033[93m: 밝은 노란색,   \033[94m: 밝은 파란색
\033[95m: 밝은 보라색,   \033[96m: 밝은 청록색
\033[97m: 밝은 흰색

 

슬릇머신 효과음 연결한 소스: https://cs-essay.tistory.com/83

  

Slot machine(슬롯머신)

import random
import keyboard
import time

total_score = 0   # 총 점수 초기화

def slot_machine():
    global total_score
    symbols = ['★', '●', '◆', '♠', '♥', '♣', '㉿', '®', '㈜']
    
    # 슬롯 머신 결과 출력 전 돌아가는 애니메이션 효과
    for _ in range(10):  # 10회 반복
        result = random.choices(symbols, k=3)
        # 기호를 파란색으로 출력
        blue_result = [f"\033[94m{symbol}\033[0m" for symbol in result]
        print("Slot machine result: ", ' | '.join(blue_result))
        time.sleep(0.1)  # 0.1초 대기
        # 이전 result를 지우고 새로운 result를 출력
        print("\033[F\033[K", end='')  # 커서를 위로 이동하고 줄을 지움

    # 마지막 result 출력
    result = random.choices(symbols, k=3)
    blue_result = [f"\033[94m{symbol}\033[0m" for symbol in result]
    print("Slot machine result: ", ' | '.join(blue_result))
    
    # score 계산
    score = 0
    if result[0] == result[1] == result[2]:  # 잭팟
        score = 100
        
        for _ in range(3):  # 메시지를 3번 반복 출력
            print("")
            time.sleep(0.5)  # 0.5초 대기
            print("\033[F\033[K", end='')  # 이전 출력 지우기
            print("\033[91mCongratulations! Jackpot! Score: 100\033[0m")
            time.sleep(0.5)  # 잠시 대기
            print("\033[F\033[K", end='')  # 이전 출력 지우기
    elif result[0] == result[1] or result[1] == result[2] or result[0] == result[2]:  # 두 개가 같은 경우
        score = 10
        for _ in range(3):  # 메시지를 3번 반복 출력
            print("")
            time.sleep(0.5)  # 0.5초 대기
            print("\033[F\033[K", end='')  # 이전 출력 지우기
            print("\033[34mTwo symbols are the same! Score: 10\033[0m")  # 빨간색 출력                  
            time.sleep(0.5)  # 잠시 대기
            print("\033[F\033[K", end='')  # 이전 출력 지우기
    else:
        print("Try again! Score: 0\n")

    # 총 score 합산
    total_score += score

    # 현재 총 score 출력 (노란색)
    print(f"\033[93mCurrent total score: {total_score} points\033[0m\n")  # \033[93m은 노란색 시작, \033[0m은 색상 초기화

print("Press the space bar to start the Slot machine game.")
print("Press 'q' to quit.")

while True:
    if keyboard.is_pressed('space'):
        slot_machine()
        # 입력 후 잠시 대기
        while keyboard.is_pressed('space'):
            pass  # 스페이스바가 더 이상 눌리지 않을 때까지 대기
    elif keyboard.is_pressed('q'):
        print("Exiting the game.")
        break