()
| 166 | |
| 167 | |
| 168 | def main(): |
| 169 | # Initialize pygame |
| 170 | screen = pygame.display.set_mode((WIDTH, HEIGHT)) |
| 171 | pygame.display.set_caption("Tetris") |
| 172 | # Create a clock object |
| 173 | clock = pygame.time.Clock() |
| 174 | # Create a Tetris object |
| 175 | game = Tetris(WIDTH // GRID_SIZE, HEIGHT // GRID_SIZE) |
| 176 | fall_time = 0 |
| 177 | fall_speed = 50 # You can adjust this value to change the falling speed, it's in milliseconds |
| 178 | while True: |
| 179 | # Fill the screen with black |
| 180 | screen.fill(BLACK) |
| 181 | for event in pygame.event.get(): |
| 182 | # Check for the QUIT event |
| 183 | if event.type == pygame.QUIT: |
| 184 | pygame.quit() |
| 185 | sys.exit() |
| 186 | # Check for the KEYDOWN event |
| 187 | if event.type == pygame.KEYDOWN: |
| 188 | if event.key == pygame.K_LEFT: |
| 189 | if game.valid_move(game.current_piece, -1, 0, 0): |
| 190 | game.current_piece.x -= 1 # Move the piece to the left |
| 191 | if event.key == pygame.K_RIGHT: |
| 192 | if game.valid_move(game.current_piece, 1, 0, 0): |
| 193 | game.current_piece.x += 1 # Move the piece to the right |
| 194 | if event.key == pygame.K_DOWN: |
| 195 | if game.valid_move(game.current_piece, 0, 1, 0): |
| 196 | game.current_piece.y += 1 # Move the piece down |
| 197 | if event.key == pygame.K_UP: |
| 198 | if game.valid_move(game.current_piece, 0, 0, 1): |
| 199 | game.current_piece.rotation += 1 # Rotate the piece |
| 200 | if event.key == pygame.K_SPACE: |
| 201 | while game.valid_move(game.current_piece, 0, 1, 0): |
| 202 | game.current_piece.y += ( |
| 203 | 1 # Move the piece down until it hits the bottom |
| 204 | ) |
| 205 | game.lock_piece(game.current_piece) # Lock the piece in place |
| 206 | # Get the number of milliseconds since the last frame |
| 207 | delta_time = clock.get_rawtime() |
| 208 | # Add the delta time to the fall time |
| 209 | fall_time += delta_time |
| 210 | if fall_time >= fall_speed: |
| 211 | # Move the piece down |
| 212 | game.update() |
| 213 | # Reset the fall time |
| 214 | fall_time = 0 |
| 215 | # Draw the score on the screen |
| 216 | draw_score(screen, game.score, 10, 10) |
| 217 | # Draw the grid and the current piece |
| 218 | game.draw(screen) |
| 219 | if game.game_over: |
| 220 | # Draw the "Game Over" message |
| 221 | draw_game_over( |
| 222 | screen, WIDTH // 2 - 100, HEIGHT // 2 - 30 |
| 223 | ) # Draw the "Game Over" message |
| 224 | # You can add a "Press any key to restart" message here |
| 225 | # Check for the KEYDOWN event |
no test coverage detected