Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 KB | None | 0 0
  1. from random import *
  2.  
  3. board = []
  4. score = 0
  5.  
  6. #This creates a 4x4 grid without the need of typing it out
  7. for x in range(4):
  8. board.append([0] * 4)
  9.  
  10. #Starting by generating 2 random 2's
  11. starting_row_1 = randint(0,3)
  12. starting_col_1 = randint(0,3)
  13. starting_row_2 = randint(0,3)
  14. starting_col_2 = randint(0,3)
  15.  
  16. #This checks if they are in the same position and changes them if so
  17. if starting_row_1 == starting_row_2 and starting_col_1 == starting_col_2:
  18. if starting_row_1 == 3:
  19. starting_row_1 = starting_row_1 - 1
  20. else:
  21. starting_row_1 = starting_row_1 + 1
  22.  
  23. board[starting_row_1][starting_col_1] = 2
  24. board[starting_row_2][starting_col_2] = 2
  25.  
  26. from york_graphics import *
  27.  
  28. #This function draws and adds the numbers to the graphics board and the interface
  29. openWindow(width=800, height=400, title='2048 Game')
  30. def draw_board(board2):
  31.  
  32.  
  33. colour = getRGBColour(255,255,255)
  34. setCanvasColour(colour)
  35. drawLine(0,400)
  36. moveTo(100,0)
  37. drawLine(0,400)
  38. moveTo(200,0)
  39. drawLine(0,400)
  40. moveTo(300,0)
  41. drawLine(0,400)
  42. moveTo(400,0)
  43. drawLine(0,400)
  44. moveTo(0,0)
  45. drawLine(400 ,0)
  46. moveTo(0,100)
  47. drawLine(400 ,0)
  48. moveTo(0,200)
  49. drawLine(400 ,0)
  50. moveTo(0,300)
  51. drawLine(400 ,0)
  52. moveTo(0,400)
  53. drawLine(400 ,0)
  54. moveTo(50,50)
  55. drawText(str(board2[0][0]))
  56. moveTo(50,150)
  57. drawText(str(board2[0][1]))
  58. moveTo(50,250)
  59. drawText(str(board2[0][2]))
  60. moveTo(50,350)
  61. drawText(str(board2[0][3]))
  62. moveTo(150,50)
  63. drawText(str(board2[1][0]))
  64. moveTo(150,150)
  65. drawText(str(board2[1][1]))
  66. moveTo(150,250)
  67. drawText(str(board2[1][2]))
  68. moveTo(150,350)
  69. drawText(str(board2[1][3]))
  70. moveTo(250,50)
  71. drawText(str(board2[2][0]))
  72. moveTo(250,150)
  73. drawText(str(board2[2][1]))
  74. moveTo(250,250)
  75. drawText(str(board2[2][2]))
  76. moveTo(250,350)
  77. drawText(str(board2[2][3]))
  78. moveTo(350,50)
  79. drawText(str(board2[3][0]))
  80. moveTo(350,150)
  81. drawText(str(board2[3][1]))
  82. moveTo(350,250)
  83. drawText(str(board2[3][2]))
  84. moveTo(350,350)
  85. drawText(str(board2[3][3]))
  86. moveTo(600,50)
  87. drawText("Welcome to Sam Morgan's 2048 game!")
  88. moveTo(600,100)
  89. drawText("The controls for the game are simple:")
  90. moveTo(600,150)
  91. drawText("Up = W")
  92. moveTo(600,180)
  93. drawText("Left = A")
  94. moveTo(600,210)
  95. drawText("Down = S")
  96. moveTo(600,240)
  97. drawText("Right = D")
  98. moveTo(600,300)
  99. drawText("Enjoy and don't get too addicted!")
  100. moveTo(0,0)
  101. updateCanvas()
  102.  
  103.  
  104. #generate new column is the maths of the game, it checks if they are equal and adds them if so
  105. def generate_new_column(column):
  106. if column[0] == column[1]:
  107. score += 2*column[0]
  108. column[0] += column[1] + 1
  109. column[1] = column[2]
  110. column[2] = column[3]
  111. column[3] = 0
  112. if column[1] == column[2]:
  113. score += 2*column[1]
  114. column[1] += column[2] + 1
  115. column[2] = column[3]
  116. column[3] = 0
  117. if column[2] == column[3]:
  118. score += 2*column[2]
  119. column[2] += column[3] + 1
  120. column[3] = 0
  121. return [2*(int(number/2)) for number in column]
  122.  
  123. #Adding the n variable allowed to rotate by a selected and inputed amount of times
  124. def rotate(board,n):
  125. newBoard = board
  126. for i in range(n):
  127. newBoard = transpose(newBoard)
  128. newBoard = flip_vert(newBoard)
  129. return newBoard
  130.  
  131. def move(board):
  132. newBoard = []
  133. for colNum, column in enumerate(board):
  134. newColumn = []
  135. for row in column:
  136. if row != 0:
  137. newColumn.append(row)
  138. for x in range(4-len(newColumn)):
  139. newColumn.append(0)
  140. newBoard.append(generate_new_column(newColumn))
  141. return newBoard
  142.  
  143. def transpose(board):
  144. newList_1 = []
  145. for x in range(len(board)):
  146. newList_2 = []
  147. for row in board:
  148. newList_2.append(row[x])
  149. newList_1.append(newList_2)
  150. return newList_1
  151.  
  152. def flip_vert(board):
  153. board = board[::-1]
  154. return board
  155.  
  156. #This checks for 0's in the board and selects one randomly to add a 4 or 2
  157. def placeNewTile(board):
  158. emptySqaures = 0
  159. for row in board:
  160. for column in row:
  161. if column == 0:
  162. emptySqaures += 1
  163. newSpot = randint(1,emptySqaures)
  164. i = 0
  165. for column_index, column in enumerate(board): #enumerate allows to check the position of the cells
  166. for row_index, cell in enumerate(column):
  167. if cell == 0:
  168. i += 1
  169. if i == newSpot:
  170. if randint(1,3) == 3:
  171. board[column_index][row_index] = 4
  172. else:
  173. board[column_index][row_index] = 2
  174. return board
  175.  
  176. x = 1
  177. while x == 1:
  178. clearCanvas()
  179. draw_board(board)
  180. user_choice = waitForKeyPress()
  181. if user_choice.lower() == 'w':
  182. new_board = move(board)
  183. elif user_choice.lower() == 's':
  184. new_board = rotate(board, 2)
  185. new_board = move(new_board)
  186. new_board = rotate(new_board, 2)
  187. elif user_choice.lower() == 'd':
  188. new_board = rotate(board, 3)
  189. new_board = move(new_board)
  190. new_board = rotate(new_board, 1)
  191. elif user_choice.lower() == 'a':
  192. new_board = rotate(board, 1)
  193. new_board = move(new_board)
  194. new_board = rotate(new_board, 3)
  195. else:
  196. continue
  197.  
  198. board = placeNewTile(new_board)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement