-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpsls_game.py
More file actions
123 lines (96 loc) · 3.41 KB
/
rpsls_game.py
File metadata and controls
123 lines (96 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#rpsls_game.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
Rock–Paper–Scissors–Lizard–Spock (RPSLS) game
The interface displays the player’s choice, the computer’s randomly selected move,
the result of each round, and a running score.
'''
# Módulos
import tkinter as tk
from tkinter import ttk
import random
# ================================
# CONSTANTES
# ================================
WINDOW_WIDTH = 450
WINDOW_HEIGHT = 350
LABEL_PADY = 5
FRAME_PADY = 10
BUTTON_PADX = 5
BUTTON_WIDTH = 12
MOVES = ["Piedra", "Papel", "Tijeras", "Lagarto", "Spock"]
RULES = {
"Piedra": ["Tijeras", "Lagarto"],
"Papel": ["Piedra", "Spock"],
"Tijeras": ["Papel", "Lagarto"],
"Lagarto": ["Spock", "Papel"],
"Spock": ["Tijeras", "Piedra"]
}
# ================================
# LÓGICA DEL JUEGO
# ================================
class GameEngine:
def __init__(self, rules):
self.rules = rules
def play_round(self, user_choice):
cpu_choice = random.choice(MOVES)
if user_choice == cpu_choice:
result = "Empate"
elif cpu_choice in self.rules[user_choice]:
result = "Ganaste"
else:
result = "Perdiste"
return user_choice, cpu_choice, result
# ================================
# INTERFAZ GRÁFICA
# ================================
class RPSLSGame(tk.Tk):
def __init__(self):
super().__init__()
self.title("Piedra, Papel, Tijeras, Lagarto y Spock")
self.geometry(f"{WINDOW_WIDTH}x{WINDOW_HEIGHT}")
self.resizable(False, False)
# Motor del juego separado
self.engine = GameEngine(RULES)
self.user_score = 0
self.cpu_score = 0
# Etiquetas
self.label_user = ttk.Label(self, text="Tú elegiste: -", anchor="center")
self.label_cpu = ttk.Label(self, text="CPU eligió: -", anchor="center")
self.label_result = ttk.Label(self, text="Resultado: -", anchor="center")
self.label_score = ttk.Label(self, text="Marcador — Tú: 0 | CPU: 0", anchor="center")
self.label_user.pack(pady=LABEL_PADY)
self.label_cpu.pack(pady=LABEL_PADY)
self.label_result.pack(pady=LABEL_PADY)
self.label_score.pack(pady=LABEL_PADY)
# Botones ordenados alfabéticamente
frame = ttk.Frame(self)
frame.pack(pady=FRAME_PADY)
for move in sorted(MOVES):
btn = ttk.Button(
frame,
text=move,
width=BUTTON_WIDTH,
command=lambda m=move: self.play(m)
)
btn.pack(side="left", padx=BUTTON_PADX)
# ================================
# LÓGICA DE INTERFAZ
# ================================
def play(self, user_choice):
user, cpu, result = self.engine.play_round(user_choice)
self.label_user.config(text=f"Tú elegiste: {user}")
self.label_cpu.config(text=f"CPU eligió: {cpu}")
self.label_result.config(text=f"Resultado: {result}")
if result == "Ganaste":
self.user_score += 1
elif result == "Perdiste":
self.cpu_score += 1
self.label_score.config(
text=f"Marcador — Tú: {self.user_score} | CPU: {self.cpu_score}"
)
# Main
if __name__ == "__main__":
app = RPSLSGame()
app.mainloop()