Untitled

 avatar
unknown
plain_text
9 months ago
1.7 kB
9
Indexable
import pygame, random, sys
pygame.init()

WIDTH, HEIGHT = 500, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Rhythm Game 🎵")
clock = pygame.time.Clock()

WHITE = (255,255,255)
BLACK = (0,0,0)
LANE_X = [80,180,320,420]
LANE_KEYS = [pygame.K_d, pygame.K_f, pygame.K_j, pygame.K_k]
font = pygame.font.Font(None, 36)

notes = []
score = 0
spawn_timer = 0

def spawn_note():
    col = random.randint(0,3)
    notes.append({'x': LANE_X[col], 'y': -20, 'col': col})

running = True
while running:
    screen.fill(BLACK)
    for event in pygame.event.get():
        if event.type == pygame.QUIT: running = False
        if event.type == pygame.KEYDOWN:
            for note in notes[:]:
                if 500 < note['y'] < 540:  # hit window
                    if event.key == LANE_KEYS[note['col']]:
                        notes.remove(note)
                        score += 10

    # Draw lanes
    for x in LANE_X:
        pygame.draw.rect(screen, (30,30,30), (x-40,0,80,HEIGHT))
    # Draw hit bar
    pygame.draw.rect(screen, (50,50,50), (0,520,WIDTH,20))
    # Draw keys
    for i, key in enumerate("DFJK"):
        screen.blit(font.render(key, True, WHITE), (LANE_X[i]-10,550))

    # Spawn notes
    spawn_timer += 1
    if spawn_timer > 20:
        spawn_note()
        spawn_timer = 0

    # Move & draw notes
    for note in notes:
        note['y'] += 6
        pygame.draw.rect(screen, (255,100,100), (note['x']-20, note['y'], 40, 20))
        if note['y'] > HEIGHT:
            notes.remove(note)

    # Score
    s_text = font.render(f"Score: {score}", True, WHITE)
    screen.blit(s_text, (10,10))

    pygame.display.flip()
    clock.tick(60)

pygame.quit()
sys.exit()
Editor is loading...
Leave a Comment