Menu
CoddyTech

Tiri angolati

Lezione 36 di 40 del corso Crea giochi con Pygame di Coddy.

A Pong, il punto in cui la pallina colpisce la racchetta determina dove andrà. Vicino all'estremità superiore vola verso l'alto, vicino a quella inferiore verso il basso e al centro procede dritta.

Misura la distanza tra il centro della pallina e il centro della racchetta e imposta il nuovo vy con quel valore:

if ball.colliderect(right):
    vx = -abs(vx)
    vy = (ball.centery - right.centery) // 6

Sopra il centro la differenza è negativa, quindi la pallina va verso l'alto; sotto è positiva. Dividendo per 6 eviti che vada troppo veloce: un colpo all'estremità dà un vy di circa 7.

Così Pong diventa un gioco di abilità: prendi la mira con la racchetta.

challenge icon

Sfida

Difficile

In entrambe le collisioni con le racchette, dopo aver impostato vx, imposta vy sul centro della pallina meno il centro della racchetta, diviso per 6 con //.

Provalo tu

import pygame

pygame.init()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Pong")
clock = pygame.time.Clock()
font = pygame.font.Font(None, 36)

ball = pygame.Rect(293, 193, 14, 14)
vx, vy = 5, 4
left = pygame.Rect(20, 160, 12, 80)
right = pygame.Rect(568, 160, 12, 80)
left_score = 0
right_score = 0
waiting = False

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
            waiting = False

    keys = pygame.key.get_pressed()
    if keys[pygame.K_w]:
        left.y -= 6
    if keys[pygame.K_s]:
        left.y += 6
    if keys[pygame.K_UP]:
        right.y -= 6
    if keys[pygame.K_DOWN]:
        right.y += 6
    left.clamp_ip(screen.get_rect())
    right.clamp_ip(screen.get_rect())

    if not waiting:
        ball.x += vx
        ball.y += vy
        if ball.top <= 0 or ball.bottom >= 400:
            vy = -vy
        if ball.colliderect(left):
            vx = abs(vx)
        if ball.colliderect(right):
            vx = -abs(vx)
        if ball.left > 600:
            left_score += 1
            ball.center = (300, 200)
            vx = -vx
            waiting = True
        if ball.right < 0:
            right_score += 1
            ball.center = (300, 200)
            vx = -vx
            waiting = True

    screen.fill((30, 30, 46))
    pygame.draw.rect(screen, (80, 160, 255), left)
    pygame.draw.rect(screen, (235, 80, 80), right)
    pygame.draw.rect(screen, (255, 196, 0), ball)
    score = font.render(f"{left_score} : {right_score}", True, (240, 240, 240))
    screen.blit(score, score.get_rect(center=(300, 30)))
    if waiting:
        message = font.render("Press Space to serve", True, (240, 240, 240))
        screen.blit(message, message.get_rect(center=(300, 360)))
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
quiz iconMettiti alla prova

Questa lezione include un breve quiz. Inizia la lezione per rispondere e tenere traccia dei tuoi progressi.

Tutte le lezioni di Crea giochi con Pygame

Esercitati da solo: Playground Pygame