|
| 1 | +import pgzrun |
| 2 | +import random |
| 3 | +import math |
| 4 | + |
| 5 | +TITLE = "Stranger Python" |
| 6 | +FONT_COLOR = (255, 255, 255) |
| 7 | +WIDTH = 800 |
| 8 | +HEIGHT = 600 |
| 9 | +CENTRO_X = WIDTH / 2 |
| 10 | +CENTRO_Y = HEIGHT / 2 |
| 11 | +CENTRO = (CENTRO_X, CENTRO_Y) |
| 12 | + |
| 13 | +LIVELLO_FINALE = 8 |
| 14 | + |
| 15 | +PERSONAGGI = ["dustin", "lucas", "mike", "undici", "will"] |
| 16 | + |
| 17 | +game_over = False |
| 18 | +game_completato = False |
| 19 | +livello_corrente = 1 |
| 20 | +selezione_iniziale = True |
| 21 | + |
| 22 | +personaggio_target = None |
| 23 | +personaggi_scelta = [] |
| 24 | +stelle = [] |
| 25 | + |
| 26 | + |
| 27 | +def draw(): |
| 28 | + screen.clear() |
| 29 | + screen.blit("sfondo", (-150, -50)) |
| 30 | + |
| 31 | + if game_over: |
| 32 | + mostra_messaggio("GAME OVER", "Prova di nuovo...") |
| 33 | + return |
| 34 | + |
| 35 | + if game_completato: |
| 36 | + mostra_messaggio("HAI VINTO!", "Complimenti!") |
| 37 | + return |
| 38 | + |
| 39 | + if selezione_iniziale: |
| 40 | + screen.draw.text( |
| 41 | + "Scegli il tuo personaggio", |
| 42 | + center=(CENTRO_X, 100), |
| 43 | + fontsize=40, |
| 44 | + color="white", |
| 45 | + ) |
| 46 | + for p in personaggi_scelta: |
| 47 | + p.draw() |
| 48 | + return |
| 49 | + |
| 50 | + for s in stelle: |
| 51 | + s.draw() |
| 52 | + |
| 53 | + |
| 54 | +def update(): |
| 55 | + global stelle, selezione_iniziale, game_over |
| 56 | + |
| 57 | + if selezione_iniziale: |
| 58 | + return |
| 59 | + |
| 60 | + if len(stelle) == 0: |
| 61 | + stelle = genera_stelle(livello_corrente) |
| 62 | + return |
| 63 | + |
| 64 | + for s in stelle: |
| 65 | + # Movimento verticale |
| 66 | + s.y += s.vy |
| 67 | + |
| 68 | + # Movimento orizzontale + oscillazione sinusoidale |
| 69 | + oscillazione = math.sin(s.timer * 0.1) * s.osc_amp |
| 70 | + s.x += s.vx + oscillazione |
| 71 | + |
| 72 | + s.timer += 1 |
| 73 | + |
| 74 | + # Rimbalzo ai bordi |
| 75 | + if s.x < 0: |
| 76 | + s.x = 0 |
| 77 | + s.vx = -s.vx |
| 78 | + if s.x > WIDTH: |
| 79 | + s.x = WIDTH |
| 80 | + s.vx = -s.vx |
| 81 | + |
| 82 | + # Cambio direzione casuale ogni tanto |
| 83 | + if random.random() < 0.01: |
| 84 | + s.vx = random.randint(-3, 3) |
| 85 | + |
| 86 | + # Se tocca il fondo → game over |
| 87 | + if s.y > HEIGHT: |
| 88 | + gestisci_game_over() |
| 89 | + |
| 90 | + |
| 91 | +def genera_stelle(numero_extra): |
| 92 | + # lista: 1 target + numero_extra distrazioni |
| 93 | + lista = ottieni_personaggi_da_creare(numero_extra) |
| 94 | + nuove = [] |
| 95 | + |
| 96 | + for nome in lista: |
| 97 | + a = Actor(nome) |
| 98 | + a.x = random.randint(100, WIDTH - 100) |
| 99 | + a.y = -50 |
| 100 | + |
| 101 | + # velocità verticale |
| 102 | + a.vy = random.uniform(livello_corrente * 0.3, livello_corrente * 0.6) |
| 103 | + |
| 104 | + # velocità orizzontale |
| 105 | + a.vx = random.randint(-3, 3) |
| 106 | + |
| 107 | + # parametri oscillazione |
| 108 | + a.osc_amp = random.uniform(1, 4) |
| 109 | + a.timer = random.randint(0, 1000) |
| 110 | + |
| 111 | + nuove.append(a) |
| 112 | + |
| 113 | + return nuove |
| 114 | + |
| 115 | + |
| 116 | +def ottieni_personaggi_da_creare(num_extra): |
| 117 | + lista = [personaggio_target] |
| 118 | + altri = [p for p in PERSONAGGI if p != personaggio_target] |
| 119 | + |
| 120 | + for _ in range(num_extra): |
| 121 | + lista.append(random.choice(altri)) |
| 122 | + |
| 123 | + return lista |
| 124 | + |
| 125 | + |
| 126 | +def on_mouse_down(pos): |
| 127 | + global selezione_iniziale, personaggio_target, livello_corrente, stelle |
| 128 | + |
| 129 | + if selezione_iniziale: |
| 130 | + for p in personaggi_scelta: |
| 131 | + if p.collidepoint(pos): |
| 132 | + personaggio_target = p.image |
| 133 | + selezione_iniziale = False |
| 134 | + return |
| 135 | + |
| 136 | + for s in stelle: |
| 137 | + if s.collidepoint(pos): |
| 138 | + if s.image == personaggio_target: |
| 139 | + click_su_target() |
| 140 | + else: |
| 141 | + gestisci_game_over() |
| 142 | + |
| 143 | + |
| 144 | +def click_su_target(): |
| 145 | + global livello_corrente, stelle, game_completato |
| 146 | + |
| 147 | + if livello_corrente == LIVELLO_FINALE: |
| 148 | + game_completato = True |
| 149 | + else: |
| 150 | + livello_corrente += 1 |
| 151 | + stelle = [] |
| 152 | + |
| 153 | + |
| 154 | +def gestisci_game_over(): |
| 155 | + global game_over |
| 156 | + game_over = True |
| 157 | + |
| 158 | + |
| 159 | +def mostra_messaggio(titolo, sottotitolo): |
| 160 | + screen.draw.text(titolo, fontsize=60, center=CENTRO, color=FONT_COLOR) |
| 161 | + screen.draw.text( |
| 162 | + sottotitolo, fontsize=30, center=(CENTRO_X, CENTRO_Y + 30), color=FONT_COLOR |
| 163 | + ) |
| 164 | + |
| 165 | + |
| 166 | +def mostra_schermata_scelta(): |
| 167 | + global personaggi_scelta |
| 168 | + personaggi_scelta = [] |
| 169 | + distanza = WIDTH / (len(PERSONAGGI) + 1) |
| 170 | + |
| 171 | + for i, nome in enumerate(PERSONAGGI): |
| 172 | + a = Actor(nome) |
| 173 | + a.x = (i + 1) * distanza |
| 174 | + a.y = HEIGHT / 2 |
| 175 | + personaggi_scelta.append(a) |
| 176 | + |
| 177 | + |
| 178 | +mostra_schermata_scelta() |
| 179 | +pgzrun.go() |
0 commit comments