• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Jun 12th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import pygame, random
2. from pygame.locals import *
3.
4. pygame.init()
5.
6.
7. # TETRIS GAME FUNCTIONS #######################################
8. def makeGrid():
9.     # set up a row list
10.     row = []
11.     # set up an array for entire grid
12.     grid = []
13.     for x in range(0, 20):
14.         for y in range(0, 10):
15.             # add 10 items to the row
16.             row.append(0)
17.         # add the row 20 times to the grid
18.         grid.append(row)
19.         row = []
20.     return grid  # get a 10 x 20 grid
21.
22.
23. def placePiece(activeGrid, activeBlock, startY, startX):
24.     for x in range(0, len(activeBlock)):
25.         # for the number of vertical rows in the block grid
26.         for y in range(0, len(activeBlock[1])):
27.             # copy the block data to the corresponding position on the grid,
28.             # with the starting value being at the top center of the grid
29.             if activeBlock[x][y] != 0:
30.                 activeGrid[x + startX][startY + y] = activeBlock[x][y]
31.
32.     return activeGrid
33.
34.
35. # use the current active grid, vertMove (only 1 or 0), horizMove (1 or -1 or 0)
36. def movePiece(activeGrid, mainGrid, vertMove, horizMove=0):
37.     # create a new grid to copy the movement to
38.     newGrid = makeGrid()
39.     # iterates through every point on the grid
40.     for x in range(0, len(activeGrid)):
41.         for y in range(0, len(activeGrid[1])):
42.             # once a grid space is found that is not empty -
43.             if activeGrid[x][y] != 0:
44.                 # if a vertical or horizontal move of the piece would place it out of bounds, don't move the piece
45.                 if (x + vertMove) > 19 or (y + horizMove > 9 or y + horizMove < 0):
46.                     return activeGrid
47.                 # copy the space in the old grid to the corresponding place on the new grid (including the movement)
48.                 newGrid[x + vertMove][y + horizMove] = activeGrid[x][y]
49.     # if the new grid will collide with other blocks, do not move the piece
50.     if collision(newGrid, mainGrid):
51.         return activeGrid
52.     else:
53.         return newGrid
54.
55.
56. def rotatePiece(activeBlock):
57.
58.     # rotate the piece - turn the rows into the columns and vice versa
59.     rotatedBlock = zip(*activeBlock[::-1])
60.     # since zip turns it into a list of tuples, turn it back into a list of lists
61.     rotatedBlock = [list(elem) for elem in rotatedBlock]
62.
63.
64.     return rotatedBlock
65.
66.
67. def collision(activeGrid, mainGrid):
68.     # iterate through the entire active grid
69.     for x in range(0, len(activeGrid)):
70.         for y in range(0, len(activeGrid[1])):
71.             # if the position on the active grid is not empty
72.             if activeGrid[x][y] != 0:
73.                 # if the corresponding position on the main grid is also not empty, there is a collision
74.                 if mainGrid[x][y] != 0:
75.                     return True
76.     # if the entire loop is run without a collision, return false
77.     return False
78.
79.
81.     # once the piece is at its final spot, it has to be added to the main grid
82.
83.     # iterate through the entire active grid
84.     for x in range(0, len(activeGrid)):
85.         for y in range(0, len(activeGrid[1])):
86.             # if the position on the active grid is not empty
87.             if activeGrid[x][y] != 0:
88.                 # replace the main grid spaces with the active grid spaces
89.                 mainGrid[x][y] = activeGrid[x][y]
90.
91.     return mainGrid
92.
93.
94. def drawActive():
95.     for x in range(0, 20):
96.         for y in range(0, 10):
97.             if activeGrid[x][y] != 0:
98.                 # first, get the colour of the block
99.                 colour = colours[activeGrid[x][y] - 1]
100.                 colourAlt = colourAlts[activeGrid[x][y] - 1]
101.                 # draw the rectangle
102.                 pygame.draw.rect(screen, colour, [(y * 40), (x * 40), 40, 40])
103.                 pygame.draw.rect(screen, colourAlt, [(y * 40), (x * 40), 40, 40], 3)
104.
105.
106. def drawMain():
107.     # loop through the main grid. Every time there is a non 0 space, draw a 40x40 square
108.
109.     for x in range(0, 20):
110.         for y in range(0, 10):
111.             if mainGrid[x][y] != 0:
112.                 # first, get the colour of the block
113.                 colour = colours[mainGrid[x][y] - 1]
114.                 colourAlt = colourAlts[mainGrid[x][y] - 1]
115.                 # draw the rectangle
116.                 pygame.draw.rect(screen, colour, [(y * 40), (x * 40), 40, 40])
117.                 pygame.draw.rect(screen, colourAlt, [(y * 40), (x * 40), 40, 40], 3)
118.             else:
119.                 # draw the rest of the grid
120.                 pygame.draw.rect(screen, (40, 40, 40), [(y * 40), (x * 40), 40, 40], 3)
121.
122.
123. def lineCheck(mainGrid):
124.     # loop through the mainGrid
125.     for x in range(0, len(mainGrid)):
126.         # start a line counter each time a new line is started
127.         lineCounter = 0
128.         for y in range(0, len(mainGrid[1])):
129.             # if the position on the line is not empty, add 1 to the line counter
130.             if mainGrid[x][y] != 0:
131.                 lineCounter += 1
132.         # if the line counter hits 10 (entire line is filled)
133.         if lineCounter == 10:
134.             # remove that line from the grid, and append a new, blank line at the beginning
135.             mainGrid.remove(mainGrid[x])
136.             mainGrid.insert(0, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
137.     return mainGrid
138.
139. def refreshScreen():
140.     screen.fill(BLACK)
141.     drawMain()
142.     drawActive()
143.
144. ###############################################################
145.
146. # PYGAME VARIABLES ############################################
147. WIDTH = 600
148. HEIGHT = 800
149.
150. ###############################################################
151.
152. # COLOURS USED FOR GAME #######################################
153. BLACK = (0, 0, 0)
154. WHITE = (255, 255, 255)
155. LIGHT_BLUE = (108, 237, 239)
156. DARK_BlUE = (0, 30, 231)
157. ORANGE = (230, 164, 57)
158. YELLOW = (241, 239, 79)
159. GREEN = (110, 235, 71)
160. PURPLE = (147, 44, 231)
161. RED = (221, 47, 33)
162. LIGHT_BLUE_ALT = (0, 120, 120)
163. DARK_BLUE_ALT = (0, 0, 120)
164. ORANGE_ALT = (120, 80, 0)
165. YELLOW_ALT = (120, 120, 0)
166. GREEN_ALT = (0, 120, 0)
167. PURPLE_ALT = (80, 0, 120)
168. RED_ALT = (120, 0, 0)
169.
170. # append colours of blocks to a list
171. colours = [YELLOW, LIGHT_BLUE, GREEN, RED, ORANGE, DARK_BlUE, PURPLE]
172.
173. # append all colour alts to a list
174. colourAlts = [YELLOW_ALT, LIGHT_BLUE_ALT, GREEN_ALT, RED_ALT, ORANGE_ALT, DARK_BLUE_ALT, PURPLE_ALT]
175.
176. ###############################################################
177.
178. # BLOCK SHAPES - number used to make shape determines colour ##
179. oBlock = [[1, 1],
180.           [1, 1]]
181.
182. iBlock = [[0, 0, 0, 0],
183.           [2, 2, 2, 2],
184.           [0, 0, 0, 0],
185.           [0, 0, 0, 0]]
186.
187. sBlock = [[0, 3, 3],
188.           [3, 3, 0],
189.           [0, 0, 0]]
190.
191. zBlock = [[4, 4, 0],
192.           [0, 4, 4],
193.           [0, 0, 0]]
194.
195. lBlock = [[5, 5, 5],
196.           [5, 0, 0],
197.           [0, 0, 0]]
198.
199. jBlock = [[6, 6, 6],
200.           [0, 0, 6],
201.           [0, 0, 0]]
202.
203. tBlock = [[7, 7, 7],
204.           [0, 7, 0],
205.           [0, 0, 0]]
206.
207. ###############################################################
208.
209. # INITIAL SETUP ###############################################
210.
211. # set the screen & caption
212. screen = pygame.display.set_mode((WIDTH, HEIGHT))
213. pygame.display.set_caption('TETRIS')
214.
215. # append all blocks to a single list
216. blocks = [oBlock, iBlock, sBlock, zBlock, lBlock, jBlock, tBlock]
217.
218. # append colours of blocks to a list
219. colours = [YELLOW, LIGHT_BLUE, GREEN, RED, ORANGE, DARK_BlUE, PURPLE]
220.
221. # draw main grid - used to hold all of the blocks that are no longer in play (have been dropped)
222. mainGrid = makeGrid()
223.
224. # initialize activeBlock as an empty list to begin
225. activeBlock = []
226.
227. # coords for where the active block currently is
228.
229.
230. # start a clock to control tick rate
231. clock = pygame.time.Clock()
232. drawMain()
233. ###############################################################
234.
235. # GAME LOOP ###################################################
236.
237. gameOver = False
238. while gameOver is False:
239.     pygame.display.flip()
240.     # if there is no active block, create one
241.     if not activeBlock:
242.         activeBlock = blocks[random.randint(0, 6)]
243.         # draw the grid for the piece in play
244.         activeGrid = makeGrid()
245.         # add the game piece to the grid
246.         # the code piece for startX determines the starting position for the block by taking the length
247.         # and dividing by 2, which is then subtracted from half of the board width
248.         # for the number of horizontal rows in the block grid
249.         activeGrid = placePiece(activeGrid, activeBlock, int(5 - len(activeBlock) // 2), 0)
250.
251.
252.     clock.tick(5)
253.     # draw the main grid on the bottom. the active grid which
254.     # holds the moving piece will be layered on top of that
255.     screen.fill(BLACK)
256.
257.     tempGrid = movePiece(activeGrid, mainGrid, 1)
258.     # if the block has not moved
259.     if tempGrid == activeGrid:
260.         if collision(tempGrid, mainGrid):
261.             gameOver = True
262.         else:
264.             activeBlock = []
265.             activeGrid = makeGrid()
266.     else:
267.         activeGrid = tempGrid
268.
269.
270.     refreshScreen()
271.
272.     # CONTROLS
273.     for event in pygame.event.get():
274.         if event.type == pygame.KEYDOWN:
275.             # if they press the right arrow key
276.             if event.key == pygame.K_RIGHT:
277.                 # move the piece one space right
278.                 activeGrid = movePiece(activeGrid, mainGrid, 0, 1)
279.                 # refresh screen
280.                 refreshScreen()
281.             elif event.key == pygame.K_LEFT:
282.                 # if they press the left arrow key
283.                 activeGrid = movePiece(activeGrid, mainGrid, 0, -1)
284.                 refreshScreen()
285.
286.             elif event.key == pygame.K_UP:
287.                 activeBlock = rotatePiece(activeBlock)
288.                 activeGrid = makeGrid()
289.                 activeGrid = placePiece(activeGrid, activeBlock, 4, 0)
290.                 refreshScreen()
291.
292.             elif event.key == pygame.K_DOWN:
293.                 activeGrid = movePiece(activeGrid, mainGrid, 1)
294.                 refreshScreen()
295.
296.
297.     mainGrid = lineCheck(mainGrid)
298.
299.     for event in pygame.event.get():
300.         if event.type == QUIT:
301.             gameOver = True
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top