Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import *
- board = []
- score = 0
- #This creates a 4x4 grid without the need of typing it out
- for x in range(4):
- board.append([0] * 4)
- #Starting by generating 2 random 2's
- starting_row_1 = randint(0,3)
- starting_col_1 = randint(0,3)
- starting_row_2 = randint(0,3)
- starting_col_2 = randint(0,3)
- #This checks if they are in the same position and changes them if so
- if starting_row_1 == starting_row_2 and starting_col_1 == starting_col_2:
- if starting_row_1 == 3:
- starting_row_1 = starting_row_1 - 1
- else:
- starting_row_1 = starting_row_1 + 1
- board[starting_row_1][starting_col_1] = 2
- board[starting_row_2][starting_col_2] = 2
- from york_graphics import *
- #This function draws and adds the numbers to the graphics board and the interface
- openWindow(width=800, height=400, title='2048 Game')
- def draw_board(board2):
- colour = getRGBColour(255,255,255)
- setCanvasColour(colour)
- drawLine(0,400)
- moveTo(100,0)
- drawLine(0,400)
- moveTo(200,0)
- drawLine(0,400)
- moveTo(300,0)
- drawLine(0,400)
- moveTo(400,0)
- drawLine(0,400)
- moveTo(0,0)
- drawLine(400 ,0)
- moveTo(0,100)
- drawLine(400 ,0)
- moveTo(0,200)
- drawLine(400 ,0)
- moveTo(0,300)
- drawLine(400 ,0)
- moveTo(0,400)
- drawLine(400 ,0)
- moveTo(50,50)
- drawText(str(board2[0][0]))
- moveTo(50,150)
- drawText(str(board2[0][1]))
- moveTo(50,250)
- drawText(str(board2[0][2]))
- moveTo(50,350)
- drawText(str(board2[0][3]))
- moveTo(150,50)
- drawText(str(board2[1][0]))
- moveTo(150,150)
- drawText(str(board2[1][1]))
- moveTo(150,250)
- drawText(str(board2[1][2]))
- moveTo(150,350)
- drawText(str(board2[1][3]))
- moveTo(250,50)
- drawText(str(board2[2][0]))
- moveTo(250,150)
- drawText(str(board2[2][1]))
- moveTo(250,250)
- drawText(str(board2[2][2]))
- moveTo(250,350)
- drawText(str(board2[2][3]))
- moveTo(350,50)
- drawText(str(board2[3][0]))
- moveTo(350,150)
- drawText(str(board2[3][1]))
- moveTo(350,250)
- drawText(str(board2[3][2]))
- moveTo(350,350)
- drawText(str(board2[3][3]))
- moveTo(600,50)
- drawText("Welcome to Sam Morgan's 2048 game!")
- moveTo(600,100)
- drawText("The controls for the game are simple:")
- moveTo(600,150)
- drawText("Up = W")
- moveTo(600,180)
- drawText("Left = A")
- moveTo(600,210)
- drawText("Down = S")
- moveTo(600,240)
- drawText("Right = D")
- moveTo(600,300)
- drawText("Enjoy and don't get too addicted!")
- moveTo(0,0)
- updateCanvas()
- #generate new column is the maths of the game, it checks if they are equal and adds them if so
- def generate_new_column(column):
- if column[0] == column[1]:
- score += 2*column[0]
- column[0] += column[1] + 1
- column[1] = column[2]
- column[2] = column[3]
- column[3] = 0
- if column[1] == column[2]:
- score += 2*column[1]
- column[1] += column[2] + 1
- column[2] = column[3]
- column[3] = 0
- if column[2] == column[3]:
- score += 2*column[2]
- column[2] += column[3] + 1
- column[3] = 0
- return [2*(int(number/2)) for number in column]
- #Adding the n variable allowed to rotate by a selected and inputed amount of times
- def rotate(board,n):
- newBoard = board
- for i in range(n):
- newBoard = transpose(newBoard)
- newBoard = flip_vert(newBoard)
- return newBoard
- def move(board):
- newBoard = []
- for colNum, column in enumerate(board):
- newColumn = []
- for row in column:
- if row != 0:
- newColumn.append(row)
- for x in range(4-len(newColumn)):
- newColumn.append(0)
- newBoard.append(generate_new_column(newColumn))
- return newBoard
- def transpose(board):
- newList_1 = []
- for x in range(len(board)):
- newList_2 = []
- for row in board:
- newList_2.append(row[x])
- newList_1.append(newList_2)
- return newList_1
- def flip_vert(board):
- board = board[::-1]
- return board
- #This checks for 0's in the board and selects one randomly to add a 4 or 2
- def placeNewTile(board):
- emptySqaures = 0
- for row in board:
- for column in row:
- if column == 0:
- emptySqaures += 1
- newSpot = randint(1,emptySqaures)
- i = 0
- for column_index, column in enumerate(board): #enumerate allows to check the position of the cells
- for row_index, cell in enumerate(column):
- if cell == 0:
- i += 1
- if i == newSpot:
- if randint(1,3) == 3:
- board[column_index][row_index] = 4
- else:
- board[column_index][row_index] = 2
- return board
- x = 1
- while x == 1:
- clearCanvas()
- draw_board(board)
- user_choice = waitForKeyPress()
- if user_choice.lower() == 'w':
- new_board = move(board)
- elif user_choice.lower() == 's':
- new_board = rotate(board, 2)
- new_board = move(new_board)
- new_board = rotate(new_board, 2)
- elif user_choice.lower() == 'd':
- new_board = rotate(board, 3)
- new_board = move(new_board)
- new_board = rotate(new_board, 1)
- elif user_choice.lower() == 'a':
- new_board = rotate(board, 1)
- new_board = move(new_board)
- new_board = rotate(new_board, 3)
- else:
- continue
- board = placeNewTile(new_board)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement