Menu
CoddyTech
flag Pt iconPortuguêsdown icon

Saque

Lição 35 de 40 do curso Crie jogos com Pygame da Coddy.

Depois de um ponto, a bola deve esperar no meio até que um jogador esteja pronto. Uma flag indica se ela está esperando:

waiting = False

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
    # as raquetes se movem sempre
    if not waiting:
        # a bola se move, rebate e marca pontos; um ponto define waiting = True

As raquetes ficam fora do if, para que os jogadores possam se preparar enquanto a bola espera. Espaço é uma ação que ocorre uma vez por pressionamento, então ela vem de um evento KEYDOWN.

Enquanto a bola espera, mostre Press Space to serve perto da parte inferior, centralizado em (300, 360).

challenge icon

Desafio

Médio
  1. Antes do loop, defina waiting = False.
  2. Coloque o movimento da bola, as rebatidas e a pontuação dentro de if not waiting: e defina waiting = True após cada ponto.
  3. Um evento KEYDOWN para pygame.K_SPACE define waiting = False.
  4. Enquanto estiver aguardando, exiba Press Space to serve centralizada em (300, 360).

Experimente você mesmo

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

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = 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())

    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
    if ball.right < 0:
        right_score += 1
        ball.center = (300, 200)
        vx = -vx

    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)))
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
quiz iconTeste seus conhecimentos

Esta lição inclui um quiz rápido. Comece a lição para respondê-lo e acompanhar seu progresso.

Todas as lições de Crie jogos com Pygame

Pratique por conta própria: Playground de Pygame