From eb5bd4e274724916f201de490a7d9fa82a68ec0f Mon Sep 17 00:00:00 2001 From: nuclode Date: Mon, 4 Dec 2017 22:05:15 +0530 Subject: [PATCH] Left, right and up arrow keys and README updated --- 2048_.py | 63 +++++++++++++++++++++++++++++++++++++++++++------------ README.md | 12 +++++------ 2 files changed, 56 insertions(+), 19 deletions(-) diff --git a/2048_.py b/2048_.py index f0b5920..55be244 100644 --- a/2048_.py +++ b/2048_.py @@ -9,7 +9,7 @@ boxsize = min(WINDOWWIDTH,WINDOWHEIGHT)//4; margin = 5 thickness = 0 -#defining the RGB for various colours used +#defining the RGB for various colours used WHITE= (255, 255, 255) BLACK= ( 0, 0, 0) RED = (255, 0, 0) @@ -79,7 +79,7 @@ def showStartScreen(): #the start screen titleFont = pygame.font.Font('freesansbold.ttf', 100) titleSurf1 = titleFont.render('2048', True, WHITE, ORANGE) - drawPressKeyMsg() + drawPressKeyMsg() while True: screen.fill(BGCOLOR) @@ -135,7 +135,7 @@ def checkForKeyPress(): def show(TABLE): #showing the table screen.fill(colorback) - myfont = pygame.font.SysFont("Arial", 100, bold=True) + myfont = pygame.font.SysFont("Arial", 60, bold=True) for i in range(4): for j in range(4): pygame.draw.rect(screen, dictcolor1[TABLE[i][j]], (j*boxsize+margin, @@ -145,7 +145,8 @@ def show(TABLE): thickness) if TABLE[i][j] != 0: label = myfont.render("%4s" %(TABLE[i][j]), 1, dictcolor2[TABLE[i][j]] ) - screen.blit(label, (j*boxsize+4*margin, i*boxsize+5*margin)) + screen.blit(label, (j*boxsize+2*margin, i*boxsize+9*margin)) + pygame.display.update() def runGame(TABLE): @@ -174,7 +175,7 @@ def runGame(TABLE): if new_table != TABLE: TABLE=randomfill(new_table) show(TABLE) - + def key(DIRECTION,TABLE): if DIRECTION =='w': for pi in range(1,4): @@ -208,15 +209,51 @@ def movedown(pi,pj,T): justcomb=True return T -# def moveleft(pi,pj,T): - #code for leftwards arrow key -# def moveright(pi,pj,T): - #code for rightwards arrow key -# def moveup(pi,pj,T): - #code for upwards arrow key - +def moveleft(pi,pj,T): + justcomb=False + while pj > 0 and (T[pi][pj-1]==0 or (T[pi][pj-1] == T[pi][pj] and not justcomb)): + if T[pi][pj-1] == 0: + T[pi][pj-1] = T[pi][pj] + T[pi][pj] = 0 + pj-=1 + elif T[pi][pj-1]==T[pi][pj]: + T[pi][pj-1] += T[pi][pj] + T[pi][pj] = 0 + pj-=1 + justcomb = True + return T + +def moveright(pi,pj,T): + justcomb=False + while pj < 3 and (T[pi][pj+1] == 0 or (T[pi][pj+1] == T[pi][pj] and not justcomb)): + if T[pi][pj+1] == 0: + T[pi][pj+1] = T[pi][pj] + T[pi][pj] = 0 + pj+=1 + elif T[pi][pj+1]==T[pi][pj]: + T[pi][pj+1] += T[pi][pj] + T[pi][pj] = 0 + pj+=1 + justcomb=True + return T + +def moveup(pi,pj,T): + justcomb=False + while pi > 0 and (T[pi-1][pj] == 0 or (T[pi-1][pj] == T[pi][pj] and not justcomb)): + if T[pi-1][pj] == 0: + T[pi-1][pj] = T[pi][pj] + T[pi][pj] = 0 + pi -= 1 + elif T[pi-1][pj] == T[pi][pj]: + T[pi-1][pj] += T[pi][pj] + T[pi][pj] = 0 + pi -= 1 + justcomb = True + return T + + def terminate(): pygame.quit() sys.exit() -main() \ No newline at end of file +main() diff --git a/README.md b/README.md index 62da49f..9290f27 100644 --- a/README.md +++ b/README.md @@ -12,30 +12,30 @@ A pygame implementation of the game 2048 Here is a brief summary to get you started: -* `showStartScreen()` function basically shows the first screen on executing the code. +* `showStartScreen()` function basically shows the first screen on executing the code. * `randomfill(TABLE)` function randomly fills the table in empty spaces. * `drawPressKeyMsg()` function shows the press key message on the start screen at the bottom right corner. * `checkForKeyPress()` function checks for an event(*key press in our case*) and returns the same. * `show()` function displays the game screen after each key press and also matches the defined colours with the respective cells. * `runGame` is the function that controls the overall game. It is called in an infinite loop in the `main` function. It calls the `randomfill` function and even checks for the pressed key by calling the `key` function. It stores the copy of the previous table and matches with the present state. This is the function controlling the whole state of the game. * `moveDown` function is defined for the actions to be undertaken when the downward key is pressed. +* `moveUp` function is defined for the actions to be undertaken when the upward key is pressed +* `moveRight` function is defined for the actions to be undertaken when the right key is pressed +* `moveLeft` function is defined for the actions to be undertaken when the left key is pressed ## Screenshots [![Screenshot_from_2017-12-01_12_43_59.png](https://s2.postimg.org/5tyl7niux/Screenshot_from_2017-12-01_12_43_59.png)](https://postimg.org/image/qe3f64ylx/) -[![Screenshot_from_2017-12-01_12_43_14.png](https://s2.postimg.org/s4mg7pnvt/Screenshot_from_2017-12-01_12_43_14.png)](https://postimg.org/image/a1tdghs11/) +[![Screenshot_from_2017-12-04_19-39-27.png](https://s7.postimg.org/sea0arqsr/Screenshot_from_2017-12-04_19-39-27.png)](https://postimg.org/image/e7u9fjfxj/) ## Issues -* Complete the code for other moving options `moveup`, `moveright` and `moveleft`. * Make the start screen look more attractive with options for different levels and leaderboard. * Make a leaderboard for storing high scores calculated on basis of *Points earned* and *Time taken*. * It will be great to implement AI techniques which can provide the player with hints for the next step. ## Contributing -Any contributions are more than welcome. Just **fork** the repo and start contributing. - - +Any contributions are more than welcome. Just **fork** the repo and start contributing.