mirror of
https://github.com/hastagAB/Awesome-Python-Scripts.git
synced 2025-02-07 09:00:54 +00:00
* add yts_torrents project (#116) * add yts_torrents project * Added Python Script HacktoberFest 2019 (#128) * Added whatsapp-message Script * Update README.md * Script: Importance Checker (Updated Project List) (#132) * Add files to fork * Update README.md * Fixed ToDoBot link (#133) To Do bot link was using "-" instead of %20 Changed path to folders Removed full repo path to folders to make project robust to cloning as discussed on issue #139 on [AzureNotebooks](https://github.com/Microsoft/AzureNotebooks/issues/193). Fixed clean up photo directory name Fixed file organizer link Script: Importance Checker (Updated Project List) (#132) * Add files to fork * Update README.md Squash Squash Fixed ToDoBot link To Do bot link was using "-" instead of %20 parent 7c58b564104cdd1990492276292f04ec19009e57 author MatTerra <mateus.b.s.terra@gmail.com> 1574710004 -0300 committer MatTerra <mateus.b.s.terra@gmail.com> 1574710650 -0300 Squash Fixed ToDoBot link To Do bot link was using "-" instead of %20 Squash Changed path to folders Removed full repo path to folders to make project robust to cloning as discussed on issue #139 on [AzureNotebooks](https://github.com/Microsoft/AzureNotebooks/issues/193). Fixed ToDoBot link To Do bot link was using "-" instead of %20 Script: Importance Checker (Updated Project List) (#132) * Add files to fork * Update README.md * All changes made : Script added (#130) * Added whatsapp-message Script * Update README.md Co-authored-by: Ayush Bhardwaj <classicayush@gmail.com> * Revert "All changes made : Script added (#130)" (#135) This reverts commit74de9279f7
. * Add MIT license for project (#122) Signed-off-by: Kaushlendra Pratap <kaushlendrapratap.9837@gmail.com> * Add files via upload (#120) * Bump requests from 2.18.4 to 2.20.0 in /Tweets_Tool (#137) Bumps [requests](https://github.com/psf/requests) from 2.18.4 to 2.20.0. - [Release notes](https://github.com/psf/requests/releases) - [Changelog](https://github.com/psf/requests/blob/master/HISTORY.md) - [Commits](https://github.com/psf/requests/compare/v2.18.4...v2.20.0) Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Bump urllib3 from 1.22 to 1.24.2 in /Tweets_Tool (#136) Bumps [urllib3](https://github.com/urllib3/urllib3) from 1.22 to 1.24.2. - [Release notes](https://github.com/urllib3/urllib3/releases) - [Changelog](https://github.com/urllib3/urllib3/blob/master/CHANGES.rst) - [Commits](https://github.com/urllib3/urllib3/compare/1.22...1.24.2) Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Ayush Bhardwaj <classicayush@gmail.com> * Added Image Recognition Script (#109) * Added face recognition script * Updated README Co-authored-by: Ayush Bhardwaj <classicayush@gmail.com> * Instagram Video Downloader (#94) * Instagram Video Downloader downloads all the videos from instagram post * Update README.md Co-authored-by: Ayush Bhardwaj <classicayush@gmail.com> * Wikipedia-Search (#87) * Create pywikisearch.py * Create README.md * Update README.md * Update README.md * Update README.md * Update pywikisearch.py * Create requirements.txt (#1) Co-authored-by: Ayush Bhardwaj <classicayush@gmail.com> * Added PX to REM, REM to Px as command line tools (#82) Co-authored-by: Atthaphon Urairat <aurairat@inetasia.com> Co-authored-by: Ayush Bhardwaj <classicayush@gmail.com> * Added an SSH host adder script, which allows you to add hosts to your ssh config file. (#76) Co-authored-by: NinoCosmic <nikola.dokoski@cosmicdevelopment.com> Co-authored-by: Ayush Bhardwaj <classicayush@gmail.com> * Added RSA Algorithm used in cryptography (#61) * Added RSA Algorithm * Update README.md * Create README.md * Update README.md * Add files via upload * Update README.md * Added RSA Algorithm Co-authored-by: Ayush Bhardwaj <classicayush@gmail.com> * fix table allignment * python script which creates a map in html showing realtime covid stat… (#138) * python script which creates a map in html showing realtime covid status in india * remove extra space; fix allignment Co-authored-by: Ayush Bhardwaj <classicayush@gmail.com> * fix to covid visualiser, sometimes deaths are null (#139) * Update README.md * Added a Random_Email_Generator to the list of projects! (#141) * Added Random Email Generator Python Script * Create README.md * Update README.md * Update Random_email_generator.py * Update Random_email_generator.py * Update README.md * Added Random Email Generator Python Script Co-authored-by: shub-garg <shubhamsgcool@gmail.com> * Ansh's Dictionary (#142) Added a project of English Theasauras * sudoku-solver (#144) * Update file-organizer.py * Update README.md * Add greeting.yml file * Add PR template for the project * update greetings.yml * update greetings.yml * Rename pull_request_template.md to PULL_REQUEST_TEMPLATE.md * Update greetings.yml * Delete greetings.yml * Added Py_Cleaner (#162) * Update README.md * Added Py_Cleaner * Toonify Scripts (#154) * Toonify Script Added * Update README.md * Update toonify-API-2.py * Added git automation to repo (#171) Added gitAutomation to README.md refactoring * Added Fibonacci_Sequence_Generator and updated main README.md accordingly (#173) * Tambola tickets generator (#169) * Adds utility to generate Tambola tickets * Adds utility to generate Tambola tickets * Added script to send messages to sqs in parallel (#165) * Added script to send messages to sqs in parallel * Update README.md * Added Codeforces Checker Python Script (#176) * python script to create github repo (#178) * Added python script to remove duplicate files from a directory (#179) * create script * create README.md * updated README.md * updated README.md * added batch image watermarker (#184) * added batch image watermarker * Create README.md * Update README.md to include project * add script for convert pdf to text (#183) Co-authored-by: pham.huu.quang <SUN-ASTERISK\pham.huu.quang@b121686-lt.sun-asterisk.com> * Update README.md * url_shortener files were added (#191) url_shortener.py skel was created the script has completed README.md description and requirements.txt were added The project was added to README.md * Python script to view wifi password. (#163) * Added wifi.py * Delete main.py * Create wifi_password.py * Create requirements.txt * Create README.md * Rename wifi_password.py to main.py * Update README.md * Update README.md Co-authored-by: Ayush Bhardwaj <classicayush@gmail.com> * Added Python made News Generator HackerNews (#174) * IMDB list querier project by Burak Bekci (#187) Co-authored-by: Ayush Bhardwaj <classicayush@gmail.com> * Added a Folder Manger Script. (#188) * Added Folder_Manager * Added Name to README.md Co-authored-by: Ayush Bhardwaj <classicayush@gmail.com> * Update README.md * Simple 2048 GUI tkinter game in Python3 (#194) * Simple 2048 GUI tkinter game in Python3 * added contributor to readme file * added elastic-snapshot script (#193) * added elastic-snapshot script * added project to the projects list * updated requirements.txt * Download page as pdf (#196) * Download page as PDF. * Contributor name. * Pudim page typo. * Added spotify downloader (#195) Co-authored-by: Ayush Bhardwaj <classicayush@gmail.com> * Github CLI tool added. (#198) * GithubBot added * add new project * Update README.md * Update README.md * Added Independent RSA Communication Algorithm (#199) * Added an indepedent RSA library Added an indepedent RSA library with no depedences (adapted to communication) * Created README.md Created README.md * Update README.md * Added project to README.md Added project "Independent RSA Communication Algorithm" to README.md Co-authored-by: Ayush Bhardwaj <classicayush@gmail.com> * Bump cryptography from 2.7 to 3.2 in /file-encrypt-decrypt (#201) Bumps [cryptography](https://github.com/pyca/cryptography) from 2.7 to 3.2. - [Release notes](https://github.com/pyca/cryptography/releases) - [Changelog](https://github.com/pyca/cryptography/blob/master/CHANGELOG.rst) - [Commits](https://github.com/pyca/cryptography/compare/2.7...3.2) Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Add TranslateCLI (#200) * Added automation for converting JSON to YAML format. (#197) * Added automation for converting json to yaml format * Create Readme.md * Deletd the extra files * Added requirements.txt * Added name and project in project list Co-authored-by: Ayush Bhardwaj <classicayush@gmail.com> * Rock-Paper-Scissor Game (#202) * Rock-Paper-Scissor-Game * updated README.md * Add instagram link * remove quora link * add portfolio * update: portfolio (#208) * fix(README): remove msg from last commit * New Project (#206) * Added folder locker and hider * Update README.md * Bump py from 1.9.0 to 1.10.0 in /file-encrypt-decrypt (#209) Bumps [py](https://github.com/pytest-dev/py) from 1.9.0 to 1.10.0. - [Release notes](https://github.com/pytest-dev/py/releases) - [Changelog](https://github.com/pytest-dev/py/blob/master/CHANGELOG.rst) - [Commits](https://github.com/pytest-dev/py/compare/1.9.0...1.10.0) Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Bump cryptography from 3.2 to 3.3.2 in /file-encrypt-decrypt (#210) Bumps [cryptography](https://github.com/pyca/cryptography) from 3.2 to 3.3.2. - [Release notes](https://github.com/pyca/cryptography/releases) - [Changelog](https://github.com/pyca/cryptography/blob/main/CHANGELOG.rst) - [Commits](https://github.com/pyca/cryptography/compare/3.2...3.3.2) Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Bump urllib3 from 1.24.2 to 1.26.5 in /Tweets_Tool (#212) Bumps [urllib3](https://github.com/urllib3/urllib3) from 1.24.2 to 1.26.5. - [Release notes](https://github.com/urllib3/urllib3/releases) - [Changelog](https://github.com/urllib3/urllib3/blob/main/CHANGES.rst) - [Commits](https://github.com/urllib3/urllib3/compare/1.24.2...1.26.5) --- updated-dependencies: - dependency-name: urllib3 dependency-type: direct:production ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Bump urllib3 from 1.25.11 to 1.26.5 in /Github_Bot (#213) Bumps [urllib3](https://github.com/urllib3/urllib3) from 1.25.11 to 1.26.5. - [Release notes](https://github.com/urllib3/urllib3/releases) - [Changelog](https://github.com/urllib3/urllib3/blob/main/CHANGES.rst) - [Commits](https://github.com/urllib3/urllib3/compare/1.25.11...1.26.5) --- updated-dependencies: - dependency-name: urllib3 dependency-type: direct:production ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Added Image Compressor Script (#211) * Added Readme.md Gave a description of the project * Added Image_Compressor Added image compressor script and a sample image to compress the image file to a reduced size. * Updated README.md Added my name in Contributors list. * Created a Speed checking script in python 3 named TestMyInternetSpeed (#214) * speedtest using python * main python script for checking speeds * Add files via upload * Delete TestMySpeed.py * Delete requirements.txt * Delete README.md * Update README.md * Update README.md * Revert "Delete README.md" This reverts commit8d14c3b3e6
. reverted the accidentally commited readme file commit * added the readme file i deleted accidentally * Update README.md Co-authored-by: Dhruv Bansal <dhruvbansal@Dhruvs-MacBook-Air.local> * Bump requests from 2.18.4 to 2.20.0 in /Tweets_Tool (#215) Bumps [requests](https://github.com/psf/requests) from 2.18.4 to 2.20.0. - [Release notes](https://github.com/psf/requests/releases) - [Changelog](https://github.com/psf/requests/blob/master/HISTORY.md) - [Commits](https://github.com/psf/requests/compare/v2.18.4...v2.20.0) --- updated-dependencies: - dependency-name: requests dependency-type: direct:production ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Bump urllib3 from 1.24.2 to 1.26.5 in /Tweets_Tool (#216) Bumps [urllib3](https://github.com/urllib3/urllib3) from 1.24.2 to 1.26.5. - [Release notes](https://github.com/urllib3/urllib3/releases) - [Changelog](https://github.com/urllib3/urllib3/blob/main/CHANGES.rst) - [Commits](https://github.com/urllib3/urllib3/compare/1.24.2...1.26.5) --- updated-dependencies: - dependency-name: urllib3 dependency-type: direct:production ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Ayush Bhardwaj <classicayush@gmail.com> * Bump requests from 2.18.4 to 2.20.0 in /Tweets_Tool (#217) Bumps [requests](https://github.com/psf/requests) from 2.18.4 to 2.20.0. - [Release notes](https://github.com/psf/requests/releases) - [Changelog](https://github.com/psf/requests/blob/master/HISTORY.md) - [Commits](https://github.com/psf/requests/compare/v2.18.4...v2.20.0) --- updated-dependencies: - dependency-name: requests dependency-type: direct:production ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Add my script PLAGIARISM_DETECTOR to the repo (#218) * AI simulation for guessing the number (#219) * Add files via upload * Create README.md * Create Requirements * Update README.md * Added CSV to Excel writer (#222) * Added Cryptocurrency prices (#223) * Added Cryptocurrency prices * Update README.md * Add the word frequency counter (#225) * Battery notification (#227) * Create Battery_notification.py * Create README.md * Update README.md * Update README.md * Update README.md * Update README.md * add Harry Potter Cloak script using Open-CV (#228) * add Harry Potter Cloak script using Open-CV * add Indian Flag using Turtle * Delete README.md * Delete main.py * Update README.md * Update README.md (#146) * Csv_to_json (#224) * Added csv_to_json script * added requirements.txt file * added images * Added README.md * Added my details * Add the website URL detector (#230) * README fix (#231) * Updated README * Images fix * Link fix * fix broken hit counter button (#232) * add awesome repo button (#233) * Added Steg_Tool Script (#234) Co-authored-by: Shankar JP <shankarjp12@outlook.com> Co-authored-by: Mayank Nader <34679965+makkoncept@users.noreply.github.com> Co-authored-by: mukeshprasad <44356982+mukeshprasad@users.noreply.github.com> Co-authored-by: natesabrown <45186473+natesabrown@users.noreply.github.com> Co-authored-by: Mateus Berardo de Souza Terra <mateus.b.s.terra@gmail.com> Co-authored-by: Kaushlendra Pratap <41343571+Kaushl2208@users.noreply.github.com> Co-authored-by: Sarah Floris <sdf94@uw.edu> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: LOKESH KHURANA <35563030+theluvvkhurana@users.noreply.github.com> Co-authored-by: Shobhit Bhosure <38807205+shobhit99@users.noreply.github.com> Co-authored-by: Nissaar <34180849+Nissaar@users.noreply.github.com> Co-authored-by: Atthaphon Urairat <u.atthaphon@gmail.com> Co-authored-by: Atthaphon Urairat <aurairat@inetasia.com> Co-authored-by: Nikola Dokoski (Nino) <ninodokoskiot@hotmail.com> Co-authored-by: NinoCosmic <nikola.dokoski@cosmicdevelopment.com> Co-authored-by: Chinmay Rane <chinmayrane16@gmail.com> Co-authored-by: tushar5526 <30565750+tushar5526@users.noreply.github.com> Co-authored-by: joec237 <37048612+joec237@users.noreply.github.com> Co-authored-by: SHUBHAM GARG <52582943+shub-garg@users.noreply.github.com> Co-authored-by: shub-garg <shubhamsgcool@gmail.com> Co-authored-by: Ansh Dhingra <56676059+anshdhinhgra47@users.noreply.github.com> Co-authored-by: ayedaemon <39592651+ayedaemon@users.noreply.github.com> Co-authored-by: Abhishek <55457501+Abhishek-Dobliyal@users.noreply.github.com> Co-authored-by: Chetan Pandey <chetanpandey1266@gmail.com> Co-authored-by: loge1998 <38487437+loge1998@users.noreply.github.com> Co-authored-by: JohnWesleyK <59068112+JohnWesleyK@users.noreply.github.com> Co-authored-by: Synster <amandeepkalra.kalra@gmail.com> Co-authored-by: Jinam Shah <40169508+jinamshah@users.noreply.github.com> Co-authored-by: Jinesh Parakh <50925060+jineshparakh@users.noreply.github.com> Co-authored-by: HARISH KUMAR TIWARI <harishtiwary46@gmail.com> Co-authored-by: Aayushi Varma <59158445+aayuv17@users.noreply.github.com> Co-authored-by: Remco Halman <remco.halman@gmail.com> Co-authored-by: QuangPH <55747283+quangph-1686a@users.noreply.github.com> Co-authored-by: pham.huu.quang <SUN-ASTERISK\pham.huu.quang@b121686-lt.sun-asterisk.com> Co-authored-by: Sam Ebison <ebsa491@gmail.com> Co-authored-by: sagar627 <55939746+sagar627@users.noreply.github.com> Co-authored-by: Atharva Ashok Patil <39415890+Attupatil@users.noreply.github.com> Co-authored-by: Burak Bekci <bekciburak97@gmail.com> Co-authored-by: Harsh Raj <rafftar98@gmail.com> Co-authored-by: gitkp <47073116+gitkp11@users.noreply.github.com> Co-authored-by: Joe <joe@clanryan.us> Co-authored-by: Jeremias Moreira Gomes <j3r3miasmg@gmail.com> Co-authored-by: Abhilasha Sinha <43816262+Abhilasha06@users.noreply.github.com> Co-authored-by: dec0 <35975131+wi6n3l@users.noreply.github.com> Co-authored-by: Rodrigo Oliveira <rodrigo.redcode@gmail.com> Co-authored-by: stark117 <55136108+saksham117@users.noreply.github.com> Co-authored-by: Punit Sakre <punitsakre786@gmail.com> Co-authored-by: Rashmi Singh <72220428+rashmiprabhat567@users.noreply.github.com> Co-authored-by: Prajjwal Pathak <71623767+pyGuru123@users.noreply.github.com> Co-authored-by: Prathima Kadari <kadariprathima4@gmail.com> Co-authored-by: Dhruv Bansal <dhruvbansal237@gmail.com> Co-authored-by: Dhruv Bansal <dhruvbansal@Dhruvs-MacBook-Air.local> Co-authored-by: Akshita Singhal <56424809+akshitasinghal4444@users.noreply.github.com> Co-authored-by: ShadowHunter15 <72161983+ShadowHunter15@users.noreply.github.com> Co-authored-by: Muhammed Ali Dilek <xemeds@protonmail.com> Co-authored-by: Sonya Nikiforova <son.nik@mail.ru> Co-authored-by: Krishna Sharma <krishnasharma1386@gmail.com> Co-authored-by: TanmayNagdeve <55934827+TanmayNagdeve@users.noreply.github.com> Co-authored-by: MoiZ <techboyy6@gmail.com> Co-authored-by: shreyas-lab <68059145+shreyas-lab@users.noreply.github.com> Co-authored-by: Shankar Jayaprakash <79354504+shankarjp@users.noreply.github.com> Co-authored-by: Shankar JP <shankarjp12@outlook.com>
256 lines
7.6 KiB
Python
256 lines
7.6 KiB
Python
import tkinter as tk
|
|
import random
|
|
import colors as c
|
|
|
|
|
|
class Game(tk.Frame):
|
|
def __init__(self):
|
|
tk.Frame.__init__(self)
|
|
self.grid()
|
|
self.master.title('2048')
|
|
|
|
self.main_grid = tk.Frame(
|
|
self, bg=c.GRID_COLOR, bd=3, width=400, height=400)
|
|
self.main_grid.grid(pady=(100, 0))
|
|
|
|
self.make_GUI()
|
|
self.start_game()
|
|
|
|
self.master.bind("<Left>", self.left)
|
|
self.master.bind("<Right>", self.right)
|
|
self.master.bind("<Up>", self.up)
|
|
self.master.bind("<Down>", self.down)
|
|
|
|
self.mainloop()
|
|
|
|
|
|
def make_GUI(self):
|
|
# make grid
|
|
self.cells = []
|
|
for i in range(4):
|
|
row = []
|
|
for j in range(4):
|
|
cell_frame = tk.Frame(
|
|
self.main_grid,
|
|
bg=c.EMPTY_CELL_COLOR,
|
|
width=100,
|
|
height=100)
|
|
cell_frame.grid(row=i, column=j, padx=5, pady=5)
|
|
cell_number = tk.Label(self.main_grid, bg=c.EMPTY_CELL_COLOR)
|
|
cell_number.grid(row=i, column=j)
|
|
cell_data = {"frame": cell_frame, "number": cell_number}
|
|
row.append(cell_data)
|
|
self.cells.append(row)
|
|
|
|
# make score header
|
|
score_frame = tk.Frame(self)
|
|
score_frame.place(relx=0.5, y=40, anchor="center")
|
|
tk.Label(
|
|
score_frame,
|
|
text="Score",
|
|
font=c.SCORE_LABEL_FONT).grid(row=0)
|
|
|
|
self.score_label = tk.Label(score_frame, text="0", font=c.SCORE_FONT)
|
|
self.score_label.grid(row=1)
|
|
|
|
|
|
def start_game(self):
|
|
# create matrix of zeroes
|
|
self.matrix = [[0] * 4 for _ in range(4)]
|
|
|
|
# fill 2 random cells with 2s
|
|
row = random.randint(0, 3)
|
|
col = random.randint(0, 3)
|
|
|
|
self.matrix[row][col] = 2
|
|
self.cells[row][col]["frame"].configure(bg=c.CELL_COLORS[2])
|
|
self.cells[row][col]["number"].configure(
|
|
bg=c.CELL_COLORS[2],
|
|
fg=c.CELL_NUMBER_COLORS[2],
|
|
font=c.CELL_NUMBER_FONTS[2],
|
|
text="2"
|
|
)
|
|
|
|
while(self.matrix[row][col] != 0):
|
|
row = random.randint(0, 3)
|
|
col = random.randint(0, 3)
|
|
|
|
self.matrix[row][col] = 2
|
|
self.cells[row][col]["frame"].configure(bg=c.CELL_COLORS[2])
|
|
self.cells[row][col]["number"].configure(
|
|
bg=c.CELL_COLORS[2],
|
|
fg=c.CELL_NUMBER_COLORS[2],
|
|
font=c.CELL_NUMBER_FONTS[2],
|
|
text="2"
|
|
)
|
|
|
|
self.score = 0
|
|
|
|
|
|
# Matrix Manipulation Functions
|
|
|
|
def stack(self):
|
|
new_matrix = [[0] * 4 for _ in range(4)]
|
|
for i in range(4):
|
|
fill_position = 0
|
|
for j in range(4):
|
|
if self.matrix[i][j] != 0:
|
|
new_matrix[i][fill_position] = self.matrix[i][j]
|
|
fill_position += 1
|
|
self.matrix = new_matrix
|
|
|
|
|
|
def combine(self):
|
|
for i in range(4):
|
|
for j in range(3):
|
|
if self.matrix[i][j] != 0 and self.matrix[i][j] == self.matrix[i][j + 1]:
|
|
self.matrix[i][j] *= 2
|
|
self.matrix[i][j + 1] = 0
|
|
self.score += self.matrix[i][j]
|
|
|
|
|
|
def reverse(self):
|
|
new_matrix = []
|
|
for i in range(4):
|
|
new_matrix.append([])
|
|
for j in range(4):
|
|
new_matrix[i].append(self.matrix[i][3 - j])
|
|
self.matrix = new_matrix
|
|
|
|
|
|
def transpose(self):
|
|
new_matrix = [[0] * 4 for _ in range(4)]
|
|
for i in range(4):
|
|
for j in range(4):
|
|
new_matrix[i][j] = self.matrix[j][i]
|
|
self.matrix = new_matrix
|
|
|
|
|
|
# Add a new 2 or 4 tile randomly to an empty cell
|
|
|
|
def add_new_tile(self):
|
|
row = random.randint(0, 3)
|
|
col = random.randint(0, 3)
|
|
while(self.matrix[row][col] != 0):
|
|
row = random.randint(0, 3)
|
|
col = random.randint(0, 3)
|
|
self.matrix[row][col] = random.choice([2, 4]) # create new tile of 2 or 4 at any random row & col
|
|
|
|
|
|
# Update the GUI to match the matrix
|
|
|
|
def update_GUI(self):
|
|
for i in range(4):
|
|
for j in range(4):
|
|
cell_value = self.matrix[i][j]
|
|
if cell_value == 0:
|
|
self.cells[i][j]["frame"].configure(bg=c.EMPTY_CELL_COLOR) # give bgcolor to empty cell's frame
|
|
self.cells[i][j]["number"].configure( # give bgcolor to empty cell's number
|
|
bg=c.EMPTY_CELL_COLOR, text="")
|
|
else:
|
|
self.cells[i][j]["frame"].configure( # give cell colour's value to filled cell's frame
|
|
bg=c.CELL_COLORS[cell_value])
|
|
|
|
self.cells[i][j]["number"].configure(
|
|
bg=c.CELL_COLORS[cell_value],
|
|
fg=c.CELL_NUMBER_COLORS[cell_value],
|
|
font=c.CELL_NUMBER_FONTS[cell_value],
|
|
text=str(cell_value))
|
|
self.score_label.configure(text=self.score)
|
|
self.update_idletasks()
|
|
|
|
|
|
# Arrow-Press Functions
|
|
|
|
def left(self, event):
|
|
self.stack()
|
|
self.combine()
|
|
self.stack()
|
|
self.add_new_tile()
|
|
self.update_GUI()
|
|
self.game_over()
|
|
|
|
|
|
def right(self, event):
|
|
self.reverse()
|
|
self.stack()
|
|
self.combine()
|
|
self.stack()
|
|
self.reverse()
|
|
self.add_new_tile()
|
|
self.update_GUI()
|
|
self.game_over()
|
|
|
|
|
|
def up(self, event):
|
|
self.transpose()
|
|
self.stack()
|
|
self.combine()
|
|
self.stack()
|
|
self.transpose()
|
|
self.add_new_tile()
|
|
self.update_GUI()
|
|
self.game_over()
|
|
|
|
|
|
def down(self, event):
|
|
self.transpose()
|
|
self.reverse()
|
|
self.stack()
|
|
self.combine()
|
|
self.stack()
|
|
self.reverse()
|
|
self.transpose()
|
|
self.add_new_tile()
|
|
self.update_GUI()
|
|
self.game_over()
|
|
|
|
|
|
# Check if any moves are possible
|
|
|
|
def horizontal_move_exists(self):
|
|
for i in range(4):
|
|
for j in range(3):
|
|
if self.matrix[i][j] == self.matrix[i][j + 1]:
|
|
return True
|
|
return False
|
|
|
|
|
|
def vertical_move_exists(self):
|
|
for i in range(3):
|
|
for j in range(4):
|
|
if self.matrix[i][j] == self.matrix[i + 1][j]:
|
|
return True
|
|
return False
|
|
|
|
|
|
# Check if Game is Over (Win/Lose)
|
|
|
|
def game_over(self):
|
|
if any(2048 in row for row in self.matrix):
|
|
game_over_frame = tk.Frame(self.main_grid, borderwidth=2)
|
|
game_over_frame.place(relx=0.5, rely=0.5, anchor="center")
|
|
tk.Label(
|
|
game_over_frame,
|
|
text="You win!",
|
|
bg=c.WINNER_BG,
|
|
fg=c.GAME_OVER_FONT_COLOR,
|
|
font=c.GAME_OVER_FONT).pack()
|
|
elif not any(0 in row for row in self.matrix) and not self.horizontal_move_exists()\
|
|
and not self.vertical_move_exists():
|
|
game_over_frame = tk.Frame(self.main_grid, borderwidth=2)
|
|
game_over_frame.place(relx=0.5, rely=0.5, anchor="center")
|
|
tk.Label(
|
|
game_over_frame,
|
|
text="Game over!",
|
|
bg=c.LOSER_BG,
|
|
fg=c.GAME_OVER_FONT_COLOR,
|
|
font=c.GAME_OVER_FONT).pack()
|
|
|
|
|
|
def main():
|
|
Game()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |