# Leo Salazar # 3/1/25 # Stats for Block Blaster import pygame import random pygame.init() # basic setup screen = pygame.display.set_mode((600, 600)) clock = pygame.time.Clock() running = True dt = 0 # to make a block off screen def spawn_block(blocks): size = random.randint(50, 100) x = random.randint(0, screen.get_width() - size) blocks.append(pygame.Rect(x, 0 - size, size, size)) return blocks # required stats class class Stats: def __init__(self): self.destroyed = 0 self.missed = 0 self.shots = 0 def shot_accuracy(self): if self.shots == 0: return float(0) return (self.destroyed / self.shots) * 100 def display_info(self, screen): font = pygame.font.Font(None, 40) pygame.Surface.blit(screen, font.render(f'Score: {self.destroyed}', True, 'black'), (0,0)) pygame.Surface.blit(screen, font.render(f'Missed: {self.missed}', True, 'black'), (0,40)) pygame.Surface.blit(screen, font.render(f'Shots: {self.shots}', True, 'black'), (0,80)) pygame.Surface.blit(screen, font.render(f'Accuracy: {self.shot_accuracy():.2f}%', True, 'black'), (0,120)) stats = Stats() # block stuff blocks = [] blocks = spawn_block(blocks) block_vel = 200 count = 0 spawn_timer = 60 # the player player = pygame.Rect(300, 570, 30, 30) player_speed = 300 alive = True # the bullets bullets = [] bullet_speed = 400 # font setup font = pygame.font.Font(None, 45) end = pygame.font.Font(None, 60) score = 0 lives = 3 # game loop while running: for event in pygame.event.get(): # to quit the game if event.type == pygame.QUIT: running = False # to shoot a single bullet on each space bar press if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE and alive: bullets.append(pygame.Rect(player.x, player.y, 10, 10)) stats.shots += 1 # fills the screen with white (should always go first) screen.fill('white') # lose when out of lives if lives < 1: alive = False # while the player is still alive if alive: # the score and lives pygame.Surface.blit(screen, font.render(f"Score: {score}", True, 'black'), (0, 0)) pygame.Surface.blit(screen, font.render(f"Lives: {lives}", True, "black"), (0, 50)) # draw the player pygame.draw.rect(screen, 'blue', player) # keep them on the screen if player.left < 0: player.left = 0 if player.right > screen.get_width(): player.right = screen.get_width() # spawn blocks periodically count += 1 if count > spawn_timer: blocks = spawn_block(blocks) count = 0 # game over else: game_over = end.render("Game Over", False, "red") game_over_rect = game_over.get_rect() game_over_rect.center = (screen.get_width() // 2 , screen.get_height() // 2) pygame.Surface.blit(screen, game_over, game_over_rect) # calling stats display method stats.display_info(screen) # drawing/updating the bullets for bullet in bullets[:]: # removing offscreen bullets from list if bullet.bottom < 0: bullets.remove(bullet) # removing blocks/bullets that collide else: pygame.draw.rect(screen, 'purple', bullet) bullet.y -= bullet_speed * dt for block in blocks[:]: if bullet.colliderect(block): bullets.remove(bullet) blocks.remove(block) score += 1 stats.destroyed += 1 # drawing/updating the blocks for block in blocks[:]: pygame.draw.rect(screen, 'black', block) block.y += block_vel * dt # removing offscreen blocks if block.top > screen.get_height(): blocks.remove(block) if alive: stats.missed += 1 # player loses if collides with block if block.colliderect(player): alive = False # decrease life per missed block if alive: if block.top > screen.get_height(): lives -= 1 # player actions based on key presses keys = pygame.key.get_pressed() if player != None: if keys[pygame.K_a]: player.x -= player_speed * dt if keys[pygame.K_d]: player.x += player_speed * dt # publish our results pygame.display.flip() # time > fps dt = clock.tick(60) / 1000 # can't close the window without it pygame.quit()