Untitled
unknown
plain_text
2 years ago
1.5 kB
1
Indexable
Never
import pygame import random # Define constants SCREEN_WIDTH = 500 SCREEN_HEIGHT = 800 WHITE = (255, 255, 255) BLACK = (0, 0, 0) BLUE = (0, 0, 255) GREEN = (0, 255, 0) RED = (255, 0, 0) FPS = 120 # Initialize pygame pygame.init() # Create screen screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Piano Tiles") # Create clock clock = pygame.time.Clock() # Create tile class class Tile(pygame.sprite.Sprite): def __init__(self, color, x, y): super().__init__() self.color = color self.image = pygame.Surface((SCREEN_WIDTH//4, SCREEN_HEIGHT//10)) self.image.fill(color) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y def update(self): self.rect.y += 10 # Create tiles group tiles_group = pygame.sprite.Group() # Create tiles for i in range(4): color = random.choice([BLACK, BLUE, GREEN, RED]) tile = Tile(color, i*SCREEN_WIDTH//4, -SCREEN_HEIGHT//10) tiles_group.add(tile) # Create game loop running = True while running: # Handle events for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Update tiles tiles_group.update() # Draw screen screen.fill(WHITE) tiles_group.draw(screen) # Update screen pygame.display.flip() # Set FPS clock.tick(FPS) # Quit game pygame.quit()