Created
July 11, 2024 07:45
-
-
Save Mossuru777/8c5ea3207d50d3a5e3b9d51396a79942 to your computer and use it in GitHub Desktop.
じゃんけん
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try: | |
from ctypes import windll, wintypes, byref | |
def enableEscapeSequence(): | |
INVALID_HANDLE_VALUE = -1 | |
STD_INPUT_HANDLE = -10 | |
STD_OUTPUT_HANDLE = -11 | |
STD_ERROR_HANDLE = -12 | |
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004 | |
ENABLE_LVB_GRID_WORLDWIDE = 0x0010 | |
hOut = windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE) | |
if hOut == INVALID_HANDLE_VALUE: | |
return False | |
dwMode = wintypes.DWORD() | |
if windll.kernel32.GetConsoleMode(hOut, byref(dwMode)) == 0: | |
return False | |
dwMode.value |= ENABLE_VIRTUAL_TERMINAL_PROCESSING | |
# dwMode.value |= ENABLE_LVB_GRID_WORLDWIDE | |
if windll.kernel32.SetConsoleMode(hOut, dwMode) == 0: | |
return False | |
return True | |
enableEscapeSequence() | |
except ImportError: | |
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import selectFromKeyboard | |
import lineClear | |
じゃんけんの回数 = 2 | |
出す手 = ["🖐", "✌", "👊"] | |
CPUが負ける手 = ["👊", "🖐", "✌"] | |
win = 0 | |
for i in range(じゃんけんの回数): | |
print(f"{i+1}/{じゃんけんの回数}回目のじゃんけんです。\n{win}勝 {i-win}敗") | |
あいこ = False | |
while True: | |
print("どの手を出しますか?") | |
プレイヤーの手 = selectFromKeyboard.selectFromKeyboard(出す手, True) | |
CPUの手 = random.choice(出す手) | |
if あいこ: | |
lineClear.lineClear(5) | |
あいこ = False | |
else: | |
lineClear.lineClear(1) | |
print(f"あなた {プレイヤーの手}\nCPU {CPUの手}") | |
if プレイヤーの手 == CPUの手: | |
print("*** あいこです ***\n") | |
あいこ = True | |
continue | |
if CPUの手 == CPUが負ける手[出す手.index(プレイヤーの手)]: | |
print("*** あなたの勝ちです ***\n") | |
win += 1 | |
else: | |
print("*** あなたの負けです ***\n") | |
break | |
print("************************") | |
print(f"** {win}勝 {じゃんけんの回数-win}敗 **") | |
if win == じゃんけんの回数 - win: | |
print("** 引き分けです!!!! **") | |
elif win > じゃんけんの回数 - win: | |
print("** あなたの勝ち!!!! **") | |
else: | |
print("** あなたの負け.... **") | |
print("************************") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import enableEscapeSequence | |
def lineClear(n, clearCurrentLine=True): | |
if n < 0 or (n == 0 and clearCurrentLine == False): | |
return | |
# \033[nG カーソルを現在の行のn桁目に移動 | |
print("\033[0G", end="") | |
if clearCurrentLine: | |
# \033[K カーソル位置から行末までをクリア | |
print("\033[K", end="") | |
for _ in range(n): | |
# \033[nA カーソルをn行上に移動 | |
print(f"\033[1A\033[K", end="") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import readchar | |
import lineClear | |
def selectFromKeyboard(選択肢のリスト, clearLinesAfterSelect=False): | |
# 現在の選択index | |
current = 0 | |
#選択が確定されるまで選択肢表示&キー読み取りループ | |
while True: | |
# 選択肢表示 | |
for i in range(len(選択肢のリスト)): | |
print(f"{"->" if i == current else " "} {選択肢のリスト[i]}") | |
# キー読み取りループ | |
while True: | |
c = readchar.readkey() | |
if c == readchar.key.ENTER: | |
# エンター | |
if clearLinesAfterSelect: | |
lineClear.lineClear(len(選択肢のリスト)) | |
return 選択肢のリスト[current] | |
if c == readchar.key.DOWN and current < len(選択肢のリスト) - 1: | |
# ↓のカーソル移動 | |
current += 1 | |
lineClear.lineClear(len(選択肢のリスト)) | |
break | |
if c == readchar.key.UP and current > 0: | |
# ↑のカーソル移動 | |
current -= 1 | |
lineClear.lineClear(len(選択肢のリスト)) | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment