Created
August 6, 2024 21:43
-
-
Save mhadaniya/f4c861040f64d0d65631a3b4c1e7375b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pygame | |
| import sys | |
| pygame.init() | |
| # Definindo as cores | |
| dark_gray = (50, 50, 50) | |
| orange = (255, 165, 0) | |
| yellow = (255, 255, 0) | |
| # Configurando a tela | |
| screen = pygame.display.set_mode((800, 600)) | |
| pygame.display.set_caption("Movimento do Retângulo e Bola") | |
| # Definindo as propriedades do retângulo | |
| rect_x = 10 | |
| rect_y = 250 | |
| rect_width = 50 | |
| rect_height = 100 | |
| rect_speed = 2 | |
| # Definindo as propriedades da bola | |
| ball_x = 400 | |
| ball_y = 300 | |
| ball_radius = 15 | |
| ball_speed_x = 1 | |
| ball_speed_y = 1 | |
| # Pontuação | |
| 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_UP]: | |
| rect_y -= rect_speed | |
| if keys[pygame.K_DOWN]: | |
| rect_y += rect_speed | |
| # Movimento da bola | |
| ball_x += ball_speed_x | |
| ball_y += ball_speed_y | |
| # Rebatimento da bola nas bordas superior e inferior | |
| if ball_y - ball_radius < 0 or ball_y + ball_radius > 600: | |
| ball_speed_y = -ball_speed_y | |
| # Verificação das bordas esquerda e direita | |
| if ball_x - ball_radius < 0 or ball_x + ball_radius > 800: | |
| score += 1 | |
| ball_x, ball_y = 400, 300 # Reposicionar a bola no centro | |
| ball_speed_x, ball_speed_y = -ball_speed_x, ball_speed_y # Inverter a direção da bola | |
| # Verificação da colisão com o retângulo | |
| rect = pygame.Rect(rect_x, rect_y, rect_width, rect_height) | |
| ball = pygame.Rect(ball_x - ball_radius, ball_y - ball_radius, ball_radius * 2, ball_radius * 2) | |
| if rect.colliderect(ball): | |
| ball_speed_x = -ball_speed_x | |
| screen.fill(dark_gray) | |
| pygame.draw.rect(screen, orange, (rect_x, rect_y, rect_width, rect_height)) | |
| pygame.draw.circle(screen, yellow, (ball_x, ball_y), ball_radius) | |
| # Exibindo a pontuação | |
| font = pygame.font.Font(None, 74) | |
| text = font.render(str(score), 1, yellow) | |
| screen.blit(text, (10, 10)) | |
| pygame.display.flip() | |
| pygame.quit() | |
| sys.exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment