diff --git a/Python/2048_Stack/README.md b/Python/2048_Stack/README.md new file mode 100644 index 00000000..10eb87e4 --- /dev/null +++ b/Python/2048_Stack/README.md @@ -0,0 +1,22 @@ +## Requirements + +Install the required python libraries by following the instructions below: + +1. Open command line +2. Change into the directory where the requirement file is located +3. Run pip install -r requirements.txt (Python 2) or pip3 install -r requirements.txt (Python 3) + +## Usage instructions + +1. Run main.py using python main.py +2. Click on start button +3. Click on the tile you want to choose +4. Click on the stack you want to push the tile in + +Start Page: + +![image](https://user-images.githubusercontent.com/69297997/139248477-d1e0904f-495e-4fd1-8743-a5e6c1398e46.png) + +Game Page: + +![image](https://user-images.githubusercontent.com/69297997/139248964-709f059e-c2f6-46d0-845d-7c850b64f07f.png) diff --git a/Python/2048_Stack/images/Logo_2.jpg b/Python/2048_Stack/images/Logo_2.jpg new file mode 100644 index 00000000..f558c4c5 Binary files /dev/null and b/Python/2048_Stack/images/Logo_2.jpg differ diff --git a/Python/2048_Stack/images/Startbt.jpg b/Python/2048_Stack/images/Startbt.jpg new file mode 100644 index 00000000..00434f93 Binary files /dev/null and b/Python/2048_Stack/images/Startbt.jpg differ diff --git a/Python/2048_Stack/images/eximt.jpg b/Python/2048_Stack/images/eximt.jpg new file mode 100644 index 00000000..dfce18da Binary files /dev/null and b/Python/2048_Stack/images/eximt.jpg differ diff --git a/Python/2048_Stack/images/fullbg.jpg b/Python/2048_Stack/images/fullbg.jpg new file mode 100644 index 00000000..54aee469 Binary files /dev/null and b/Python/2048_Stack/images/fullbg.jpg differ diff --git a/Python/2048_Stack/main.py b/Python/2048_Stack/main.py new file mode 100644 index 00000000..1ce15194 --- /dev/null +++ b/Python/2048_Stack/main.py @@ -0,0 +1,267 @@ +from tkinter import * +from tkinter import messagebox +import tkinter.font as font +from PIL import ImageTk +from PIL import Image +import time +import random +import json + + +class Stack2048: + + def __init__(self, canvas, main2,main1): + self.c = canvas + self.root = main2 + self.main1=main1 + self.pegwidth = 50 + self.pegheight = 260 #height of each stack + self.pegdist = 100 #distance between stacks + self.pegstate = [[], [], [], []] #current state of stacks + self.peg_wise_blocks = [[], [], [], []] #store shape of blocks in each stacks + self.block = [0,0] #block picker state 0 means vacant + self.block_picker = [0,0] #stores shapes block in block picker peg + self.score=0 + string= "SCORE : "+str(self.score) + self.sc=self.c.create_text(600,10, fill='#000000', text=string , font=('Noto Sans CJK JP Black',16)) + self.color = {2:"#ff0000",4:"#00ff00",8:"#62a254",16:"#c0c620",32:"#36a8d5",64:"#ff00ff",128:"#800000",256:"#008080",512:"#008000",1024:"#000080",2048:"#808000"} + + with open("score.json") as f: + data=json.load(f) + + string= "MAX SCORE : "+str(max(data)) + self.mxsc=self.c.create_text(100,10, fill='#000000', text=string , font=('Noto Sans CJK JP Black',16)) + + + + + + def round_rectangle(self,x1,y1,x2,y2,r,**kwargs): + points = (x1+r, y1, x1+r, y1, x2-r, y1, x2-r, y1, x2, y1, x2, y1+r, x2, y1+r, x2, y2-r, x2, y2-r, x2, y2, x2-r, y2, x2-r, y2, x1+r, y2, x1+r, y2, x1, y2, x1, y2-r, x1, y2-r, x1, y1+r, x1, y1+r, x1, y1) + return self.c.create_polygon(points, **kwargs, outline="#000000", width=1, smooth=True) + + + #function to merge the blocks + def merge_blocks(self,i): + l = len(self.pegstate[i]) + + #check weather top to blocks are same or not if yes merge these two blocks + if l!=1 and self.pegstate[i][l-1]==self.pegstate[i][l-2]: + b1 = self.peg_wise_blocks[i].pop() + b2 = self.peg_wise_blocks[i].pop() + cor = self.c.coords(b2[0]) + x = cor[0]-30 + y = cor[1] + self.c.delete(b1[0]) + self.c.delete(b1[1]) + self.c.delete(b2[0]) + self.c.delete(b2[1]) + n = self.pegstate[i].pop() + n += self.pegstate[i].pop() + self.pegstate[i].append(n) + c=self.color.get(n) + p = self.round_rectangle(x, y, x+50, y+50, 30, fill=c) + t = self.c.create_text(x+25,y+25, fill='#ffffff', text=n, font=('Noto Sans CJK JP Black',16)) + self.score+=n + string="SCORE : "+str(self.score) + self.c.itemconfig(self.sc,text=string) + self.peg_wise_blocks[i].append([p,t]) + self.c.update() + time.sleep(0.5) + + if n == 2048 : + messagebox.showinfo("Popup Window","You Win") + raiseframe(main1) + self.merge_blocks(i) + + + #function to move block from block picker to selected stack + def onclickPeg(self, event, i): + #if block is selected and stack contain less than 5 block then only move the block + if self.block[0]!=0: + cor_peg = self.c.coords(self.pegs[i]) + p,t = self.block[0],self.block[1] + self.block[0]=0 + cor_block = self.c.coords(p) + x = cor_peg[0]-cor_block[0]+15 + y=-95-len(self.pegstate[i])*50-50 + + self.c.move(p,x,y) + + self.c.tag_unbind(p,'') + n=self.c.itemcget(t,'text') + self.c.delete(t) + + cor_block = self.c.coords(p) + t = self.c.create_text(cor_block[0]-7.5,cor_block[1]+25, fill='white', text=n, font=('Noto Sans CJK JP Black',16)) + self.pegstate[i].append(int(n)) + self.peg_wise_blocks[i].append([p,t]) + self.c.update() + time.sleep(0.5) + self.merge_blocks(i) + self.c.update() + time.sleep(0.5) + + #vacant the space in block picker and create block + if self.block_picker[0]==p: + self.block_picker[0]=0 + else: + self.block_picker[1]=0 + + self.create_block() + + #if block is not selected show error + elif self.block[0] == 0: + messagebox.showerror("error","Select Block") + + + #if stack is full the end the game + if len(self.pegstate[0])==5 or len(self.pegstate[1])==5 or len(self.pegstate[2])==5 or len(self.pegstate[3])==5: + messagebox.showwarning("End","GAME OVER") + + with open("score.json") as f: + data=json.load(f) + if(max(data)', lambda event: self.onclickPeg(event,0)) + self.pegs.append(p) + + x1, x2 = x1+self.pegdist, x2+self.pegdist + p = self.round_rectangle(x1, y1, x2, y2, 15, fill='#b6915e') + self.c.tag_bind(p, '', lambda event: self.onclickPeg(event,1)) + self.pegs.append(p) + + x1, x2 = x1+self.pegdist, x2+self.pegdist + p = self.round_rectangle(x1, y1, x2, y2, 15, fill='#b6915e') + self.c.tag_bind(p, '', lambda event: self.onclickPeg(event,2)) + self.pegs.append(p) + + x1, x2 = x1+self.pegdist, x2+self.pegdist + p = self.round_rectangle(x1, y1, x2, y2, 15, fill="#b6915e") + self.c.tag_bind(p, '', lambda event: self.onclickPeg(event,3)) + self.pegs.append(p) + + p = self.round_rectangle(275, 405, 420, 455, 15,fill='#b6915e') + self.pegs.append(p) + self.root.update() + + + #function to create block in block picker + def create_block(self): + x1, y1 = 275,405 + f = 0 + if self.block_picker[0]!=0: + x1 = x1+95 + self.block_picker[1]=1 + else: + self.block_picker[0]=1 + + x2, y2 = x1+50, y1+50 + a= [] + for i in range (0,4): + if len(self.pegstate[i])==0: + a.append(0) + else: + a.append(max(self.pegstate[i])) + + i = max(a) + j = 0 + while 2**j', lambda event: self.onclickBlock(event,p,t)) + self.c.tag_bind(t, '', lambda event: self.onclickBlock(event,p,t)) + if self.block_picker[0] == 1: + self.block_picker[0]=p + else : + self.block_picker[1]=p + + +#start game +def play(main2,main1): + main2.tkraise() + canvas = Canvas(main2,width=700,height=500,bd=0, highlightthickness=0,bg='#fffaf1') + canvas.place(x=0,y=150) + s = Stack2048(canvas,main2,main1) + s.create_pegs() + s.create_block() + s.create_block() + +def raiseframe(frame): + frame.tkraise() + + +if __name__ == "__main__": + + tk = Tk() + tk.title('2048') + tk.geometry('700x700') + tk.resizable(0,0) + main1 = Frame(tk,height=700,width=700,bg='#fffaf1') #main window + main2 = Frame(tk,height=700,width=700,bg='#fffaf1') #game window + main1.place(x=0,y=0) + main2.place(x=0,y=0) + + #create main window + my_img1=ImageTk.PhotoImage(Image.open("images/fullbg.jpg").resize((700,700))) + mylabel=Label(main1 , image=my_img1) + mylabel.place(x=0,y=0,relheight=1,relwidth=1) + + #start game button + stimg=ImageTk.PhotoImage(Image.open("images/Startbt.jpg").resize((200,70))) + startb = Button(main1, text='START GAME',relief=FLAT,image=stimg, command = lambda : play(main2,main1)) + startb.place(x=75,y=400) + + #exit game button + eximg=ImageTk.PhotoImage(Image.open("images/eximt.jpg").resize((200,70))) + exit = Button(main1, text='EXIT',image=eximg, relief=FLAT, command=tk.destroy) + exit.place(x=425,y=400) + + #create game window + eximg1=ImageTk.PhotoImage(Image.open("images/eximt.jpg").resize((150,50))) + logo=ImageTk.PhotoImage(Image.open("images/Logo_2.jpg").resize((200,100))) + mylabel2=Label(main2 , image=logo,bd=0, highlightthickness=0) + mylabel2.place(x=300,y=10) + back = Button(main2, text='Back',relief=FLAT, image=eximg1, command = lambda : raiseframe(main1)) + back.place(x=50,y=20) + + main1.tkraise() + + + tk.mainloop() + + diff --git a/Python/2048_Stack/requirements.txt b/Python/2048_Stack/requirements.txt new file mode 100644 index 00000000..b93e102b --- /dev/null +++ b/Python/2048_Stack/requirements.txt @@ -0,0 +1,3 @@ +jsonschema==3.2.0 +Pillow==8.2.0 +tk==0.1.0 \ No newline at end of file diff --git a/Python/2048_Stack/score.json b/Python/2048_Stack/score.json new file mode 100644 index 00000000..0637a088 --- /dev/null +++ b/Python/2048_Stack/score.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/Python/README.md b/Python/README.md index 12bdcb6c..b25c738c 100644 --- a/Python/README.md +++ b/Python/README.md @@ -74,3 +74,4 @@ | 73 | [scrap Github profile](/Python/GithubScraper) | A python script that grabs all details of github user | [sai sumanth kumar](github.com/saisumanthkumar) | | 74 | [Dinosaur Game](/Python/dinosaur_game) | The famous Chrome dinosaur game when there is no Internet | [Ian Yy](https://github.com/Ian-Yy) | | 75 | [Speech to Text Converter](/Python/speech-to-text) | Converts speech from a WAVE file to text. | [Dishant Vyas](https://github.com/dishantvyas15) | +| 76 | [2048 Game](/Python/2048_Stack) | The 2048 game using a stack | [Yash Brid](https://github.com/bridyash13)