def start(): global grid # create the game grid grid = GameGrid(grid_h, grid_w) # create the first tetromino to enter the game grid # by using the create_tetromino function defined below current_tetromino = create_tetromino(grid_h, grid_w) # print("next tetromino:") next_tetromino = create_tetromino(grid_h, grid_w) grid.current_tetromino = current_tetromino grid.next_tetromino = next_tetromino stddraw.clearKeysTyped() pause = False # main game loop (keyboard interaction for moving the tetromino) while True: if not pause: mx, my = stddraw.getPosition() tileX = grid.current_tetromino.bottom_left_corner.x ax = int(mx / 42.35) - 1 # print(ax, tileX) if ax > tileX: for i in range(ax - tileX): grid.current_tetromino.move("right", grid) elif ax < tileX: for i in range(tileX - ax): grid.current_tetromino.move("left", grid) # check user interactions via the keyboard if stddraw.hasNextKeyTyped(): key_typed = stddraw.nextKeyTyped() # Pause if key_typed == 'p': print("Pause") if pause: pause = False else: pause = True elif not pause: # if the left arrow key has been pressed if key_typed == "left": # move the tetromino left by one # print("Left Typed") current_tetromino.move(key_typed, grid) # if the right arrow key has been pressed elif key_typed == "right": # print("Right Typed") # move the tetromino right by one current_tetromino.move(key_typed, grid) # if the down arrow key has been pressed elif key_typed == "down": # move the tetromino down by one # (causes the tetromino to fall down faster) current_tetromino.move(key_typed, grid) # piece drop elif key_typed == 'space': for i in range(grid_h): current_tetromino.move('down', grid) # Speed Increase elif key_typed == 'w': if grid.delta_time > 50: grid.delta_time -= 40 # Speed Decrease elif key_typed == 's': if grid.delta_time < 500: grid.delta_time += 40 elif key_typed == 'e': current_tetromino.rotate(grid) elif key_typed == 'q': current_tetromino.rotate_ccw(grid) if key_typed == 'r': print("restart") start() # clear the queue that stores all the keys pressed/typed stddraw.clearKeysTyped() # move (drop) the tetromino down by 1 at each iteration if not pause: success = current_tetromino.move("down", grid) # place the tetromino on the game grid when it cannot go down anymore if not success and not pause: # get the tile matrix of the tetromino tiles_to_place = current_tetromino.tile_matrix # update the game grid by adding the tiles of the tetromino game_over = grid.update_grid(tiles_to_place) # end the main game loop if the game is over if game_over: if display_game_over(grid_h, grid_w + 5): pause = True start() # create the next tetromino to enter the game grid # by using the create_tetromino function defined below current_tetromino = next_tetromino grid.current_tetromino = current_tetromino print("next tetromino:") next_tetromino = create_tetromino(grid_h, grid_w) grid.next_tetromino = next_tetromino next_tetromino.draw_dummy() # display the game grid and as well the current tetromino grid.display(pause) print("Game over")
def start(): # set the dimensions of the game grid grid_h, grid_w = 20, 12 # set the size of the drawing canvas canvas_h, canvas_w = 40 * grid_h, 40 * grid_w stddraw.setCanvasSize(canvas_w, canvas_h) # set the scale of the coordinate system stddraw.setXscale(-0.5, grid_w - 0.5) stddraw.setYscale(-0.5, grid_h - 0.5) # create the game grid grid = GameGrid(grid_h, grid_w) # create the first tetromino to enter the game grid # by using the create_tetromino function defined below current_tetromino = create_tetromino(grid_h, grid_w) next_tetromino = create_tetromino(grid_h, grid_w) grid.current_tetromino = current_tetromino # display a simple menu before opening the game display_game_menu(grid_h, grid_w) # main game loop (keyboard interaction for moving the tetromino) while True: # check user interactions via the keyboard if stddraw.hasNextKeyTyped(): key_typed = stddraw.nextKeyTyped() # if the left arrow key has been pressed if key_typed == "left": # move the tetromino left by one current_tetromino.move(key_typed, grid) # if the right arrow key has been pressed elif key_typed == "right": # move the tetromino right by one current_tetromino.move(key_typed, grid) # if the down arrow key has been pressed elif key_typed == "down": # move the tetromino down by one # (causes the tetromino to fall down faster) current_tetromino.move(key_typed, grid) # clear the queue that stores all the keys pressed/typed elif key_typed == "up": current_tetromino.rotateTetromino() elif key_typed == "space": temp = current_tetromino.move("down", grid) while (temp): temp = current_tetromino.move("down", grid) stddraw.clearKeysTyped() # move (drop) the tetromino down by 1 at each iteration success = current_tetromino.move("down", grid) # place the tetromino on the game grid when it cannot go down anymore if not success: # get the tile matrix of the tetromino tiles_to_place = current_tetromino.tile_matrix # update the game grid by adding the tiles of the tetromino game_over = grid.update_grid(tiles_to_place) rowSet = rowsToCheck(tiles_to_place) grid.rowCheck(rowSet) columnSet = columnsToCheck(tiles_to_place) grid.sumCheck(columnSet, current_tetromino) # end the main game loop if the game is over if game_over: break # create the next tetromino to enter the game grid # by using the create_tetromino function defined below current_tetromino = next_tetromino grid.current_tetromino = current_tetromino next_tetromino = create_tetromino(grid_h, grid_w) print("Score = " + str(grid.score)) print("Next tetromino type is: " + next_tetromino.type) # display the game grid and as well the current tetromino grid.display() print("Game over")
def start(): # set the dimensions of the game grid grid_h, grid_w = 17, 12 # set the size of the drawing canvas canvas_h, canvas_w = 40 * grid_h, 40 * grid_w + 100 stddraw.setCanvasSize(canvas_w, canvas_h) # set the scale of the coordinate system stddraw.setXscale(-0.5, grid_w + 3) stddraw.setYscale(-0.5, grid_h - 0.5) # create the game grid grid = GameGrid(grid_h, grid_w) # create the first tetromino to enter the game grid # by using the create_tetromino function defined below current_tetromino = create_tetromino(grid_h, grid_w) grid.current_tetromino = current_tetromino # display a simple menu before opening the game display_game_menu(grid_h, grid_w) # initial score score = 0 speed = 250 #initial speed # main game loop (keyboard interaction for moving the tetromino) while True: # check user interactions via the keyboard if stddraw.hasNextKeyTyped(): key_typed = stddraw.nextKeyTyped() # if the left arrow key has been pressed if key_typed == "left": # move the tetromino left by one current_tetromino.move(key_typed, grid) # if the right arrow key has been pressed elif key_typed == "right": # move the tetromino right by one current_tetromino.move(key_typed, grid) # if the down arrow key has been pressed elif key_typed == "down": # move the tetromino down by one # (causes the tetromino to fall down faster) current_tetromino.move(key_typed, grid) elif key_typed == "up": # rotate the tetromino 90 degree clock-wise current_tetromino.rotate(grid) elif key_typed == "space": # drop the tetromino for i in range(grid_h): current_tetromino.move("down", grid) # clear the queue that stores all the keys pressed/typed stddraw.clearKeysTyped() # move (drop) the tetromino down by 1 at each iteration success = current_tetromino.move("down", grid) grid.connected_4() # place the tetromino on the game grid when it cannot go down anymore if not success: # get the tile matrix of the tetromino tiles_to_place = current_tetromino.tile_matrix # update the game grid by adding the tiles of the tetromino game_over = grid.update_grid(tiles_to_place) indv_score = 0 # starting value for a full row's score ind_score = 0 # starting value for a merged tiles score # check is_row_full for all rows for i in range(grid_h): grid.check_2048(grid.tile_matrix) # score from merged tiles ind_score = grid.update_score(grid.tile_num2) if grid.is_row_full(i, grid.tile_matrix): # score from deleted full rows indv_score = grid.update_score(grid.tile_num) grid.tile_num2 = np.zeros(100) # for merged score score_val = ind_score + indv_score score += int(score_val) print(score) # end the main game loop if the game is over if game_over: break # increasing difficulty by increasing speed as the game process if score > 450: speed = 10 elif score > 250: speed = 50 elif score > 150: speed = 100 if score > 10000: break # create the next tetromino to enter the game grid # by using the create_tetromino function defined below current_tetromino = create_tetromino(grid_h, grid_w) grid.current_tetromino = current_tetromino # display the game grid and as well the current tetromino grid.display(score, speed) # finish the game and display game over finish_game(grid_h, grid_w) print("Game over")