From bea7447a4d23bb426d5068393f0bea324ecd1db0 Mon Sep 17 00:00:00 2001 From: HostGrady <59182478+HostGrady@users.noreply.github.com> Date: Thu, 25 Jun 2020 23:00:31 -0400 Subject: [PATCH 01/11] Update README.md --- README.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 768aa5c..dc7172d 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,14 @@ -2048-python +2048-python-patches =========== - -Based on the popular game [2048](https://github.com/gabrielecirulli/2048) by Gabriele Cirulli. The game's objective is to slide numbered tiles on a grid to combine them to create a tile with the number 2048. Here is a Python version that uses TKinter! +A fork of [2048-python](https://github.com/yangshun/2048-python) by yangshun. The game uses the original 2048-python code as a base however I have altered the code to make it a more enjoyable experience. ![screenshot](img/screenshot.png) +Required: +TKinter + +Note: +The game has only been tested on Python 3.8 and on Linux. To start the game, run: $ python3 puzzle.py From 6ee8a10c373797c4fbb3ae06e99acb851b002a1c Mon Sep 17 00:00:00 2001 From: HostGrady <59182478+HostGrady@users.noreply.github.com> Date: Thu, 25 Jun 2020 23:59:25 -0400 Subject: [PATCH 02/11] Removed console messages --- logic.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/logic.py b/logic.py index 7b6fb1e..80c3024 100644 --- a/logic.py +++ b/logic.py @@ -163,7 +163,6 @@ def merge(mat): def up(game): - print("up") # return matrix after shifting up game = transpose(game) game, done = cover_up(game) @@ -176,7 +175,6 @@ def up(game): def down(game): - print("down") game = reverse(transpose(game)) game, done = cover_up(game) temp = merge(game) @@ -188,7 +186,6 @@ def down(game): def left(game): - print("left") # return matrix after shifting left game, done = cover_up(game) temp = merge(game) @@ -199,7 +196,6 @@ def left(game): def right(game): - print("right") # return matrix after shifting right game = reverse(game) game, done = cover_up(game) From 3100d09ec8dd77940a261e93b9966604a8764d35 Mon Sep 17 00:00:00 2001 From: HostGrady <59182478+HostGrady@users.noreply.github.com> Date: Fri, 26 Jun 2020 00:14:05 -0400 Subject: [PATCH 03/11] new win/loss screens --- puzzle.py | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/puzzle.py b/puzzle.py index 46773a5..3f92d0f 100644 --- a/puzzle.py +++ b/puzzle.py @@ -1,10 +1,10 @@ import random -from tkinter import Frame, Label, CENTER +import tkinter as tk +from tkinter import Frame, Label, CENTER, messagebox import logic import constants as c - class GameGrid(Frame): def __init__(self): Frame.__init__(self) @@ -16,8 +16,8 @@ def __init__(self): # self.gamelogic = gamelogic self.commands = {c.KEY_UP: logic.up, c.KEY_DOWN: logic.down, c.KEY_LEFT: logic.left, c.KEY_RIGHT: logic.right, - c.KEY_UP_ALT: logic.up, c.KEY_DOWN_ALT: logic.down, - c.KEY_LEFT_ALT: logic.left, c.KEY_RIGHT_ALT: logic.right, + # c.KEY_UP_ALT: logic.up, c.KEY_DOWN_ALT: logic.down, + # c.KEY_LEFT_ALT: logic.left, c.KEY_RIGHT_ALT: logic.right, c.KEY_H: logic.left, c.KEY_L: logic.right, c.KEY_K: logic.up, c.KEY_J: logic.down} @@ -25,7 +25,7 @@ def __init__(self): self.init_grid() self.init_matrix() self.update_grid_cells() - + self.mainloop() def init_grid(self): @@ -70,7 +70,6 @@ def update_grid_cells(self): new_number), bg=c.BACKGROUND_COLOR_DICT[new_number], fg=c.CELL_COLOR_DICT[new_number]) self.update_idletasks() - def key_down(self, event): key = repr(event.char) if key == c.KEY_BACK and len(self.history_matrixs) > 1: @@ -85,17 +84,14 @@ def key_down(self, event): self.history_matrixs.append(self.matrix) self.update_grid_cells() done = False - if logic.game_state(self.matrix) == 'win': - self.grid_cells[1][1].configure( - text="You", bg=c.BACKGROUND_COLOR_CELL_EMPTY) - self.grid_cells[1][2].configure( - text="Win!", bg=c.BACKGROUND_COLOR_CELL_EMPTY) if logic.game_state(self.matrix) == 'lose': - self.grid_cells[1][1].configure( - text="You", bg=c.BACKGROUND_COLOR_CELL_EMPTY) - self.grid_cells[1][2].configure( - text="Lose!", bg=c.BACKGROUND_COLOR_CELL_EMPTY) - + retry = messagebox.askyesno("You Lost!", "Do you want to reset the game?") + if retry == True: + print('New Game!') + self.destroy() + GameGrid() + if retry == False: + exit() def generate_next(self): index = (self.gen(), self.gen()) while self.matrix[index[0]][index[1]] != 0: From 51dcfb6159bb5dede85725842bcc6d5f512d14fb Mon Sep 17 00:00:00 2001 From: HostGrady <59182478+HostGrady@users.noreply.github.com> Date: Wed, 1 Jul 2020 02:45:59 -0400 Subject: [PATCH 04/11] Update LICENSE --- LICENSE | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 89ad3fb..8730db8 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,7 @@ The MIT License (MIT) Copyright (c) 2014 Tay Yang Shun +Copyright (c) 2020 HostGrady Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -18,4 +19,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. \ No newline at end of file +SOFTWARE. From e8c14f3fb5de7d3fc6e0ac6e559a70bd4db0b9fb Mon Sep 17 00:00:00 2001 From: HostGrady <59182478+HostGrady@users.noreply.github.com> Date: Wed, 1 Jul 2020 02:52:31 -0400 Subject: [PATCH 05/11] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index dc7172d..cb29f17 100644 --- a/README.md +++ b/README.md @@ -19,3 +19,5 @@ Contributors: - [Tay Yang Shun](http://github.com/yangshun) - [Emmanuel Goh](http://github.com/emman27) +- [Emmanuel Goh](http://github.com/HostGrady) + From fba9ee898cfa54d3104a04a62cdcb02195d88257 Mon Sep 17 00:00:00 2001 From: HostGrady <59182478+HostGrady@users.noreply.github.com> Date: Wed, 1 Jul 2020 02:54:08 -0400 Subject: [PATCH 06/11] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index cb29f17..d4bfb33 100644 --- a/README.md +++ b/README.md @@ -19,5 +19,5 @@ Contributors: - [Tay Yang Shun](http://github.com/yangshun) - [Emmanuel Goh](http://github.com/emman27) -- [Emmanuel Goh](http://github.com/HostGrady) +- [HostGrady](http://github.com/HostGrady) From 0d78423a0b71d05383f2d74a6fc36e600b3a0186 Mon Sep 17 00:00:00 2001 From: HostGrady <59182478+HostGrady@users.noreply.github.com> Date: Sat, 4 Jul 2020 22:15:20 -0400 Subject: [PATCH 07/11] Added settings/configuration Changed how the controls worked, can be changed via the config file. --- config.py | 5 +++++ constants.py | 55 ++++++++++++++++++++++++++++++++++++++-------------- puzzle.py | 7 ++----- 3 files changed, 47 insertions(+), 20 deletions(-) create mode 100644 config.py diff --git a/config.py b/config.py new file mode 100644 index 0000000..3c33852 --- /dev/null +++ b/config.py @@ -0,0 +1,5 @@ +def controls(): + return 'default' # edit return value with controls you want +# controls options are: default (wasd), vim (kjhl), azerty (zqsd) +# other control options must be added manually +# edit the constants file for new control options diff --git a/constants.py b/constants.py index 86b550f..c21a616 100644 --- a/constants.py +++ b/constants.py @@ -1,3 +1,4 @@ +import config SIZE = 400 GRID_LEN = 4 GRID_PADDING = 10 @@ -22,19 +23,43 @@ 32768: "#776e65", 65536: "#f9f6f2", } FONT = ("Verdana", 40, "bold") +# default controls +if config.controls() == 'default': + KEY_UP = "'w'" + KEY_DOWN = "'s'" + KEY_LEFT = "'a'" + KEY_RIGHT = "'d'" + KEY_BACK = "'b'" +elif config.controls() == 'azerty': + KEY_UP = "'z'" + KEY_DOWN = "'s'" + KEY_LEFT = "'q'" + KEY_RIGHT = "'d'" + KEY_BACK = "'b'" +# vim controls +elif config.controls() == 'vim': + KEY_UP = "'k'" + KEY_DOWN = "'j'" + KEY_LEFT = "'h'" + KEY_RIGHT = "'l'" + KEY_BACK = "'b'" +# why +elif config.controls() == 'why': + KEY_UP = "'1'" + KEY_DOWN = "'.'" + KEY_LEFT = "'='" + KEY_RIGHT = "'7'" + KEY_BACK = "'b'" +# elif config.controls() == 'your control name here' + # KEY_UP = "'key1'" + # KEY_UP = "'key2'" + # KEY_UP = "'key3'" + # KEY_UP = "'key4'" +# will use default if nothing is in the return value of controls +else: + KEY_UP = "'w'" + KEY_DOWN = "'s'" + KEY_LEFT = "'a'" + KEY_RIGHT = "'d'" + KEY_BACK = "'b'" -KEY_UP_ALT = "\'\\uf700\'" -KEY_DOWN_ALT = "\'\\uf701\'" -KEY_LEFT_ALT = "\'\\uf702\'" -KEY_RIGHT_ALT = "\'\\uf703\'" - -KEY_UP = "'w'" -KEY_DOWN = "'s'" -KEY_LEFT = "'a'" -KEY_RIGHT = "'d'" -KEY_BACK = "'b'" - -KEY_J = "'j'" -KEY_K = "'k'" -KEY_L = "'l'" -KEY_H = "'h'" diff --git a/puzzle.py b/puzzle.py index 3f92d0f..c130987 100644 --- a/puzzle.py +++ b/puzzle.py @@ -4,6 +4,7 @@ import logic import constants as c +import config class GameGrid(Frame): def __init__(self): @@ -15,11 +16,7 @@ def __init__(self): # self.gamelogic = gamelogic self.commands = {c.KEY_UP: logic.up, c.KEY_DOWN: logic.down, - c.KEY_LEFT: logic.left, c.KEY_RIGHT: logic.right, - # c.KEY_UP_ALT: logic.up, c.KEY_DOWN_ALT: logic.down, - # c.KEY_LEFT_ALT: logic.left, c.KEY_RIGHT_ALT: logic.right, - c.KEY_H: logic.left, c.KEY_L: logic.right, - c.KEY_K: logic.up, c.KEY_J: logic.down} + c.KEY_LEFT: logic.left, c.KEY_RIGHT: logic.right} self.grid_cells = [] self.init_grid() From 1a099bc0b88cadf7266725bd54af21689d6d5a78 Mon Sep 17 00:00:00 2001 From: HostGrady <59182478+HostGrady@users.noreply.github.com> Date: Sat, 4 Jul 2020 22:27:39 -0400 Subject: [PATCH 08/11] Update README.md Provides info on how to configure the controls --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index d4bfb33..ee11723 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,17 @@ To start the game, run: $ python3 puzzle.py +Configuration +== +You can configure the game by editing the config.py file. + +Editing config.py allows you to change the controls of the game. + +In order to edit the file, use the text editor of your choice. Replace "return 'default'" (line 2) with the prefered control scheme +(return 'vim' provides vim controls, for example) + +To add custom control schemes edit constants.py. Lines 53-57 provide an example of how to create a control scheme. Make sure to uncomment and edit the lines. + Contributors: == From 45faf05727db2ecbb95d06090ef34e533a251324 Mon Sep 17 00:00:00 2001 From: 5IGI0 <5IGI0@protonmail.com> Date: Wed, 8 Jul 2020 00:44:19 +0200 Subject: [PATCH 09/11] add config file/optimise code --- .gitignore | 5 ++++- config.py | 31 ++++++++++++++++++++++++----- constants.py | 55 +++++++++++++++++++++------------------------------- 3 files changed, 52 insertions(+), 39 deletions(-) diff --git a/.gitignore b/.gitignore index 8c8ba6a..430f6a9 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,7 @@ .vscode/settings.json # python -*.pyc \ No newline at end of file +*.pyc + +# config +config.json \ No newline at end of file diff --git a/config.py b/config.py index 3c33852..31bfed9 100644 --- a/config.py +++ b/config.py @@ -1,5 +1,26 @@ -def controls(): - return 'default' # edit return value with controls you want -# controls options are: default (wasd), vim (kjhl), azerty (zqsd) -# other control options must be added manually -# edit the constants file for new control options +import json + +DEFAULT_CONFIG = { + "keylayout": 'default' +} + +global config +try: + with open("./config.json") as fp: + config = json.load(fp) +except: + try: + with open("./config.json", "w") as fp: + json.dump(DEFAULT_CONFIG, fp) + except: + pass + config = DEFAULT_CONFIG + +def get_config_option(key): + if key in config.keys(): + return config[key] + else: + return None + +# controls options are: default (wasd), azerty (zqsd) +# other control options must be added manually in constants.py diff --git a/constants.py b/constants.py index c21a616..4b6834a 100644 --- a/constants.py +++ b/constants.py @@ -23,43 +23,32 @@ 32768: "#776e65", 65536: "#f9f6f2", } FONT = ("Verdana", 40, "bold") -# default controls -if config.controls() == 'default': - KEY_UP = "'w'" - KEY_DOWN = "'s'" - KEY_LEFT = "'a'" - KEY_RIGHT = "'d'" - KEY_BACK = "'b'" -elif config.controls() == 'azerty': - KEY_UP = "'z'" - KEY_DOWN = "'s'" - KEY_LEFT = "'q'" - KEY_RIGHT = "'d'" - KEY_BACK = "'b'" -# vim controls -elif config.controls() == 'vim': - KEY_UP = "'k'" - KEY_DOWN = "'j'" - KEY_LEFT = "'h'" - KEY_RIGHT = "'l'" - KEY_BACK = "'b'" -# why -elif config.controls() == 'why': - KEY_UP = "'1'" - KEY_DOWN = "'.'" - KEY_LEFT = "'='" - KEY_RIGHT = "'7'" + +# controls +presets = { + "default": "wasd", + "azerty": "zqsd", + "qwerty": "wasd", + "vim": "khjl" +} + +keylayout = config.get_config_option("keylayout") + +if keylayout in presets.keys(): + KEY_UP = "'"+presets[keylayout][0]+"'" + KEY_DOWN = "'"+presets[keylayout][2]+"'" + KEY_LEFT = "'"+presets[keylayout][1]+"'" + KEY_RIGHT = "'"+presets[keylayout][3]+"'" +elif len(keylayout) == 4: + KEY_UP = "'"+keylayout[0]+"'" + KEY_DOWN = "'"+keylayout[2]+"'" + KEY_LEFT = "'"+keylayout[1]+"'" + KEY_RIGHT = "'"+keylayout[3]+"'" KEY_BACK = "'b'" -# elif config.controls() == 'your control name here' - # KEY_UP = "'key1'" - # KEY_UP = "'key2'" - # KEY_UP = "'key3'" - # KEY_UP = "'key4'" -# will use default if nothing is in the return value of controls else: KEY_UP = "'w'" KEY_DOWN = "'s'" KEY_LEFT = "'a'" KEY_RIGHT = "'d'" - KEY_BACK = "'b'" +KEY_BACK = "'b'" \ No newline at end of file From 3675aad9924ee2a860094657240460af275399e4 Mon Sep 17 00:00:00 2001 From: HostGrady <59182478+HostGrady@users.noreply.github.com> Date: Tue, 7 Jul 2020 18:52:59 -0400 Subject: [PATCH 10/11] original license and readme --- LICENSE | 3 +-- README.md | 27 ++++++--------------------- 2 files changed, 7 insertions(+), 23 deletions(-) diff --git a/LICENSE b/LICENSE index 8730db8..89ad3fb 100644 --- a/LICENSE +++ b/LICENSE @@ -1,7 +1,6 @@ The MIT License (MIT) Copyright (c) 2014 Tay Yang Shun -Copyright (c) 2020 HostGrady Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -19,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md index ee11723..48224c7 100644 --- a/README.md +++ b/README.md @@ -1,34 +1,19 @@ -2048-python-patches +2048-python =========== -A fork of [2048-python](https://github.com/yangshun/2048-python) by yangshun. The game uses the original 2048-python code as a base however I have altered the code to make it a more enjoyable experience. -![screenshot](img/screenshot.png) +[![Run on Repl.it](https://repl.it/badge/github/yangshun/2048-python)](https://repl.it/github/yangshun/2048-python) + +Based on the popular game [2048](https://github.com/gabrielecirulli/2048) by Gabriele Cirulli. The game's objective is to slide numbered tiles on a grid to combine them to create a tile with the number 2048. Here is a Python version that uses TKinter! -Required: -TKinter +![screenshot](img/screenshot.png) -Note: -The game has only been tested on Python 3.8 and on Linux. To start the game, run: $ python3 puzzle.py -Configuration -== -You can configure the game by editing the config.py file. - -Editing config.py allows you to change the controls of the game. - -In order to edit the file, use the text editor of your choice. Replace "return 'default'" (line 2) with the prefered control scheme -(return 'vim' provides vim controls, for example) - -To add custom control schemes edit constants.py. Lines 53-57 provide an example of how to create a control scheme. Make sure to uncomment and edit the lines. - Contributors: == -- [Tay Yang Shun](http://github.com/yangshun) +- [Yanghun Tay](http://github.com/yangshun) - [Emmanuel Goh](http://github.com/emman27) -- [HostGrady](http://github.com/HostGrady) - From 2332a111b58fd81ebc6fbefadb2e2b9179c4e81a Mon Sep 17 00:00:00 2001 From: HostGrady <59182478+HostGrady@users.noreply.github.com> Date: Tue, 7 Jul 2020 19:07:25 -0400 Subject: [PATCH 11/11] Update README.md --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 48224c7..f322382 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,15 @@ To start the game, run: $ python3 puzzle.py +How to configure 2048-Python +== +After running puzzle.py, a json file called config.json will be created, in this file you will be able to configure 2048-Python. + +You can change the controls by changing "default" in line 1 of config.json to your prefered control scheme. + +Current control schemes: default (wasd), qwerty (wasd), azerty (zqsd), vim (khjl) + +To add a new control scheme edit constants.py and go to lines 28-32. Format is "name":"controls". Contributors: ==