fork download
  1. from collections import namedtuple
  2. from random import shuffle
  3. import tkinter as tk
  4.  
  5. Carta = namedtuple('Carta', ['numero', 'naipe'])
  6.  
  7.  
  8. class Observadores(object):
  9.  
  10. def __init__(self):
  11. self.observadores = set()
  12.  
  13. def adiciona(self, observador):
  14. self.observadores.add(observador)
  15.  
  16. def notifica(self):
  17. for observador in self.observadores:
  18. observador.evento()
  19.  
  20.  
  21. class EstourouPontos(Exception):
  22.  
  23. def __str__(self):
  24. return 'Perdeu !'
  25.  
  26.  
  27. class Baralho(object):
  28.  
  29. numeros = '2 3 4 5 6 7 8 9 10 J Q K A'.split()
  30. naipes = '♥ ♦ ♣ ♠'.split()
  31.  
  32. def __init__(self):
  33. self.baralho = [Carta(numero, naipe) for numero in Baralho.numeros for naipe in Baralho.naipes]
  34. self.embaralha()
  35.  
  36. def __len__(self):
  37. return len(self.baralho)
  38.  
  39. def __getitem__(self, indice):
  40. return self.baralho[indice]
  41.  
  42. def embaralha(self):
  43. shuffle(self.baralho)
  44.  
  45. def retornaCarta(self):
  46. return self.baralho.pop()
  47.  
  48.  
  49. class Jogador(object):
  50.  
  51. def __init__(self):
  52. self.cartas = []
  53. self.pontos = 0
  54.  
  55. def recebeCarta(self, carta):
  56. self.cartas.append(carta)
  57. self.atualizaPontos(carta)
  58.  
  59. def atualizaPontos(self, carta):
  60. self.pontos += self.verificaCarta(carta)
  61. if self.pontos >= 21:
  62. raise EstourouPontos()
  63.  
  64. def verificaCarta(self, carta):
  65. if carta.numero == 'A':
  66. return 1
  67. elif carta.numero in ('J', 'Q', 'K'):
  68. return 10
  69. else:
  70. return int(carta.numero)
  71.  
  72.  
  73. class Tela(Observadores):
  74.  
  75. def __init__(self):
  76. super().__init__()
  77. self.root = tk.Tk()
  78. self.root.title('Blackjack')
  79. self.root.geometry('+100+100')
  80. self.root.resizable(False, False)
  81. self.fonte = ('Comic Sans MS', 12, 'bold')
  82. self.frame1 = tk.Frame(self.root)
  83. self.frame2 = tk.Frame(self.root)
  84. tk.Label(self.root, text = 'BLACKJACK', font = self.fonte, fg = 'red').pack()
  85. self.botaoIniciar = tk.Button(self.frame1, text = 'Iniciar', fg = 'green', font = self.fonte, width = 12)
  86. self.botaoIniciar['command'] = self.telaJogo
  87. self.botaoSair = tk.Button(self.frame1, text = 'Sair', fg = 'red', font = self.fonte, width = 12)
  88. self.botaoSair['command'] = self.sairDoJogo
  89. self.botaoPedeCarta = tk.Button(self.frame2, text = 'Mais uma carta', fg = 'blue', font = self.fonte, width = 12)
  90. self.botaoPedeCarta['command'] = self.pedeCarta
  91. self.mostraCarta = tk.Label(self.frame2, text = '--', font = self.fonte)
  92. self.mostraPontos = tk.Label(self.frame2, text = '00 Pontos', fg = 'green', font = self.fonte)
  93. self.botaoIniciar.pack(side = tk.LEFT)
  94. self.botaoSair.pack(side = tk.LEFT)
  95. self.mostraPontos.pack(side = tk.TOP)
  96. self.mostraCarta.pack(side = tk.TOP)
  97. self.botaoPedeCarta.pack(side = tk.BOTTOM)
  98. self.telaInicial()
  99.  
  100. def telaInicial(self):
  101. self.frame2.pack_forget()
  102. self.frame1.pack(side = tk.BOTTOM)
  103.  
  104. def telaJogo(self):
  105. self.frame1.pack_forget()
  106. self.frame2.pack(side = tk.BOTTOM)
  107.  
  108. def sairDoJogo(self):
  109. self.root.destroy()
  110.  
  111. def pedeCarta(self):
  112. self.notifica()
  113.  
  114. def message(self, texto, cor = 'red'):
  115. self.mostraPontos['text'] = texto
  116. self.mostraPontos['fg'] = cor
  117.  
  118. def atualizaDados(self, carta, jogador):
  119. self.mostraCarta['text'] = str(carta.numero) + carta.naipe
  120. self.mostraPontos['text'] = str(jogador.pontos) + ' Pontos'
  121.  
  122. def mainloop(self):
  123. self.root.mainloop()
  124.  
  125.  
  126. class Blackjack(object):
  127.  
  128. def __init__(self):
  129. self.tela = Tela()
  130. self.jogador = Jogador()
  131. self.baralho = Baralho()
  132. self.tela.adiciona(self)
  133. self.tela.mainloop()
  134.  
  135. def evento(self):
  136. try:
  137. carta = self.PegaCarta()
  138. self.jogador.recebeCarta(carta)
  139. self.tela.atualizaDados(carta, self.jogador)
  140. except EstourouPontos as erro:
  141. self.tela.message(erro)
  142.  
  143. def PegaCarta(self):
  144. return self.baralho.retornaCarta()
  145.  
  146.  
  147. if __name__ == '__main__':
  148. blackjack = Blackjack()
Runtime error #stdin #stdout #stderr 0.06s 17824KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 148, in <module>
  File "./prog.py", line 129, in __init__
  File "./prog.py", line 77, in __init__
  File "/usr/lib/python3.5/tkinter/__init__.py", line 1880, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable