|
| 1 | +import pygame |
| 2 | +import random |
| 3 | + |
| 4 | + |
| 5 | +pygame.init() |
| 6 | + |
| 7 | +#display |
| 8 | +WIDTH, HEIGHT = 800, 600 |
| 9 | +SCREEN = pygame.display.set_mode((WIDTH, HEIGHT)) |
| 10 | +pygame.display.set_caption("Blockfall Game") |
| 11 | + |
| 12 | +#colors |
| 13 | +WHITE = (255, 255, 255) |
| 14 | +BLACK = (0, 0, 0) |
| 15 | +RED = (255, 0, 0) |
| 16 | + |
| 17 | +#player blcok |
| 18 | +player_width, player_height = 50, 80 |
| 19 | +player_x = (WIDTH - player_width) // 2 |
| 20 | +player_y = HEIGHT - player_height - 20 |
| 21 | +player_speed = 5 |
| 22 | + |
| 23 | +#obstacles |
| 24 | +obstacle_width, obstacle_height = 50, 50 |
| 25 | +obstacles = [] |
| 26 | +obstacle_speed = 3 |
| 27 | +obstacle_frequency = 25 |
| 28 | +obstacle_counter = 0 |
| 29 | +block_size_increase = 0.1 |
| 30 | +block_size_color = (0, 255, 0) |
| 31 | + |
| 32 | +#game variables |
| 33 | +score = 0 |
| 34 | +font = pygame.font.Font(None, 36) |
| 35 | +clock = pygame.time.Clock() |
| 36 | +game_over = False |
| 37 | + |
| 38 | +#text on screen |
| 39 | +def draw_text(text, font, color, x, y): |
| 40 | + text_surface = font.render(text, True, color) |
| 41 | + text_rect = text_surface.get_rect() |
| 42 | + text_rect.topleft = (x, y) |
| 43 | + SCREEN.blit(text_surface, text_rect) |
| 44 | + |
| 45 | +#loop |
| 46 | +running = True |
| 47 | +while running: |
| 48 | + SCREEN.fill(BLACK) |
| 49 | + |
| 50 | + #events |
| 51 | + for event in pygame.event.get(): |
| 52 | + if event.type == pygame.QUIT: |
| 53 | + running = False |
| 54 | + |
| 55 | + if not game_over: |
| 56 | + #movement |
| 57 | + keys = pygame.key.get_pressed() |
| 58 | + if keys[pygame.K_LEFT]: |
| 59 | + player_x -= player_speed |
| 60 | + if keys[pygame.K_RIGHT]: |
| 61 | + player_x += player_speed |
| 62 | + |
| 63 | + #bounding player |
| 64 | + if player_x < 0: |
| 65 | + player_x = 0 |
| 66 | + if player_x > WIDTH - player_width: |
| 67 | + player_x = WIDTH - player_width |
| 68 | + |
| 69 | + |
| 70 | + if obstacle_counter % obstacle_frequency == 0: |
| 71 | + obstacle_x = random.randint(0, WIDTH - obstacle_width) |
| 72 | + obstacle_y = -obstacle_height |
| 73 | + obstacles.append(pygame.Rect(obstacle_x, obstacle_y, obstacle_width, obstacle_height)) |
| 74 | + obstacle_counter += 1 |
| 75 | + |
| 76 | + #move & color |
| 77 | + for obstacle in obstacles: |
| 78 | + obstacle.y += obstacle_speed |
| 79 | + pygame.draw.rect(SCREEN, RED, obstacle) |
| 80 | + |
| 81 | + #collison detection |
| 82 | + if obstacle.colliderect((player_x, player_y, player_width, player_height)): |
| 83 | + game_over = True |
| 84 | + |
| 85 | + #remove passed obsticles |
| 86 | + if obstacle.top > HEIGHT: |
| 87 | + obstacles.remove(obstacle) |
| 88 | + score += 1 |
| 89 | + if score % 50 == 0: |
| 90 | + #increase obstacle size |
| 91 | + obstacle_width = int(obstacle_width * (1 + block_size_increase)) |
| 92 | + obstacle_height = int(obstacle_height * (1 + block_size_increase)) |
| 93 | + if score % 100 == 0: |
| 94 | + obstacle_speed += 1 #increase obstacle speed |
| 95 | + |
| 96 | + #player block |
| 97 | + pygame.draw.rect(SCREEN, WHITE, (player_x, player_y, player_width, player_height)) |
| 98 | + |
| 99 | + #draw score |
| 100 | + draw_text(f"Score: {score}", font, WHITE, 10, 10) |
| 101 | + |
| 102 | + #draw obstacle speed |
| 103 | + draw_text(f"Speed: {obstacle_speed}", font, WHITE, WIDTH - 150, 10) |
| 104 | + else: |
| 105 | + #draw game over message |
| 106 | + draw_text("Game Over !", font, WHITE, WIDTH // 2 - 100, HEIGHT // 2 - 50) |
| 107 | + draw_text(f"Final Score: {score}", font, WHITE, WIDTH // 2 - 100, HEIGHT // 2) |
| 108 | + draw_text("Press 'R' to Restart", font, WHITE, WIDTH // 2 - 150, HEIGHT // 2 + 50) |
| 109 | + |
| 110 | + #'R' for restart |
| 111 | + keys = pygame.key.get_pressed() |
| 112 | + if keys[pygame.K_r]: |
| 113 | + game_over = False |
| 114 | + score = 0 |
| 115 | + player_x = (WIDTH - player_width) // 2 |
| 116 | + obstacles.clear() |
| 117 | + obstacle_speed = 3 |
| 118 | + |
| 119 | + pygame.display.flip() |
| 120 | + clock.tick(60) |
| 121 | + |
| 122 | +pygame.quit() |
0 commit comments