Skip to content

Instantly share code, notes, and snippets.

@pathristikon
Created November 12, 2019 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pathristikon/4e3bc675ff97f9f0a1692f80fe94f6a7 to your computer and use it in GitHub Desktop.
Save pathristikon/4e3bc675ff97f9f0a1692f80fe94f6a7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
from tkinter import Tk, Button, Toplevel, Message, Frame, Label
from enum import Enum
import random
class Colors(Enum):
"""
Enum class for colors
"""
RED = 'red'
BLUE = 'blue'
GREEN = 'green'
class Bomber:
"""
Main game
"""
_app = None
_frame = None
_top_frame = None
_score_widget = None
_score = 0
_buttons = {}
_success_buttons = 0
_number_of_buttons = 10
_game_size = "370x350+500+200"
_notification_popup_size = "220x100+570+300"
_game_difficulty = 250
def __init__(self):
self._app = Tk()
self._top_frame = Frame(self._app, bg="yellow", pady=10, width=100, height=40)
self._top_frame.pack()
self._score_widget = Label(self._top_frame, text="Score: 0", bg="yellow")
self._score_widget.place(relx=0.5, rely=0.5, anchor="center")
self._frame = Frame(self._app)
self._frame.pack()
self._app.geometry(self._game_size)
self._app.resizable(False, False)
self._app.title('Bomber')
self.create_game()
def create_game(self):
"""
Create game
:return:
"""
count = 0
for x in range(0, self._number_of_buttons):
for y in range(0, self._number_of_buttons):
params = {
"text": " ",
"relief": "raised",
"height": 1,
"width": 1,
"bd": 0,
"borderwidth": 1,
"command": lambda c=count: self.button_action(c)
}
btn = Button(self._frame, **params)
self._buttons[count] = btn
btn.grid(row=x, column=y)
count = count + 1
self._frame.mainloop()
def button_action(self, c: str):
"""
What we do on pressing key
:param c:
:return:
"""
def check_if_player_won():
self._success_buttons = self._success_buttons + 1
if self._success_buttons == 100:
self.notification('You won!')
btn = self._buttons[c]
if random.randint(0, self._game_difficulty) == 10:
btn['text'] = 'X'
btn['state'] = 'disabled'
self.disable_all_buttons()
self.notification()
return
points = random.randint(0, 9)
self.update_score(points)
btn['text'] = points
btn['state'] = 'disabled'
colors = list(map(lambda color: color.value, Colors))
btn['highlightbackground'] = random.choice(colors)
check_if_player_won()
def notification(self, text="Unfortunately your game is over!"):
"""
Game over notification
:return:
"""
top = Toplevel()
top.title("Game over!")
top.geometry(self._notification_popup_size)
top.resizable(False, False)
msg = Message(top, text=text, aspect="250")
button = Button(top, text="Close", command=lambda: self._app.destroy())
msg.pack()
button.pack()
def disable_all_buttons(self):
"""
Disable all buttons after gameover
:return:
"""
for button in self._buttons.values():
if random.getrandbits(1) and button['state'] != 'disabled':
button['text'] = "*"
button['state'] = "disabled"
def update_score(self, points: int):
"""
Update the score text after every press
:param points:
:return:
"""
self._score = self._score + points
self._score_widget['text'] = "Score: {}".format(self._score)
if __name__ == "__main__":
Bomber()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment