Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Left, right and up arrow keys and README updated #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 50 additions & 13 deletions 2048_.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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()
main()
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.