Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pong take one #87

Open
Sausagewater69 opened this issue Dec 9, 2023 · 0 comments
Open

Pong take one #87

Sausagewater69 opened this issue Dec 9, 2023 · 0 comments

Comments

@Sausagewater69
Copy link

import pygame
import sys

Initialize Pygame

pygame.init()

Constants

WIDTH, HEIGHT = 600, 400
BALL_SPEED = 5
PADDLE_SPEED = 8

Colors

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

Create the screen

screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pong Game")

Create paddles and ball

player_paddle = pygame.Rect(50, HEIGHT // 2 - 25, 15, 50)
opponent_paddle = pygame.Rect(WIDTH - 65, HEIGHT // 2 - 25, 15, 50)
ball = pygame.Rect(WIDTH // 2 - 10, HEIGHT // 2 - 10, 20, 20)

Set initial ball direction

ball_direction = [1, 1]

Game loop

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

# Move paddles
keys = pygame.key.get_pressed()
if keys[pygame.K_w] and player_paddle.top > 0:
    player_paddle.y -= PADDLE_SPEED
if keys[pygame.K_s] and player_paddle.bottom < HEIGHT:
    player_paddle.y += PADDLE_SPEED

# Update ball position
ball.x += BALL_SPEED * ball_direction[0]
ball.y += BALL_SPEED * ball_direction[1]

# Ball collision with walls
if ball.top <= 0 or ball.bottom >= HEIGHT:
    ball_direction[1] *= -1

# Ball collision with paddles
if ball.colliderect(player_paddle) or ball.colliderect(opponent_paddle):
    ball_direction[0] *= -1

# Draw everything on the screen
screen.fill(BLACK)
pygame.draw.rect(screen, WHITE, player_paddle)
pygame.draw.rect(screen, WHITE, opponent_paddle)
pygame.draw.ellipse(screen, WHITE, ball)

# Update the display
pygame.display.flip()

# Cap the frame rate
pygame.time.Clock().tick(60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant