import pygame
import random
# Initialize Pygame
pygame.init()
# Set up the screen
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("2D Basketball Game")
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
# Player variables
player_size = 50
player_speed = 5
player_x = screen_width // 2 - player_size // 2
player_y = screen_height - 2 * player_size
# Ball variables
ball_size = 25
ball_speed = 4
ball_x = random.randint(ball_size, screen_width - ball_size)
ball_y = ball_size
ball_dx = ball_speed * random.choice([-1, 1])
ball_dy = ball_speed
# Main game loop
running = True
while running:
screen.fill(BLACK)
# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Player movement
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player_x -= player_speed
if keys[pygame.K_RIGHT]:
player_x += player_speed
# Boundary checking for player
if player_x < 0:
player_x = 0
elif player_x > screen_width - player_size:
player_x = screen_width - player_size
# Ball movement
ball_x += ball_dx
ball_y += ball_dy
# Boundary checking for ball
if ball_x <= 0 or ball_x >= screen_width - ball_size:
ball_dx *= -1
if ball_y <= 0:
ball_dy *= -1
# Collision detection between ball and player
if player_y <= ball_y + ball_size <= player_y + player_size and player_x <= ball_x <= player_x + player_size:
ball_dy *= -1
# Draw player
pygame.draw.rect(screen, WHITE, (player_x, player_y, player_size, player_size))
# Draw ball
pygame.draw.circle(screen, GREEN, (ball_x, ball_y), ball_size)
# Update the display
pygame.display.update()
# Cap the frame rate
pygame.time.Clock().tick(60)
# Quit Pygame
pygame.quit()
import pygame
import random
# Initialize Pygame
pygame.init()
# Set up the screen
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("2D Basketball Game")
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
# Player variables
player_size = 50
player_speed = 5
player_x = screen_width // 2 - player_size // 2
player_y = screen_height - 2 * player_size
# Ball variables
ball_size = 25
ball_speed = 4
ball_x = random.randint(ball_size, screen_width - ball_size)
ball_y = ball_size
ball_dx = ball_speed * random.choice([-1, 1])
ball_dy = ball_speed
# Main game loop
running = True
while running:
screen.fill(BLACK)
# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Player movement
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player_x -= player_speed
if keys[pygame.K_RIGHT]:
player_x += player_speed
if keys[pygame.K_UP]:
player_y -= player_speed
if keys[pygame.K_DOWN]:
player_y += player_speed
# Boundary checking for player
if player_x < 0:
player_x = 0
elif player_x > screen_width - player_size:
player_x = screen_width - player_size
if player_y < 0:
player_y = 0
elif player_y > screen_height - player_size:
player_y = screen_height - player_size
# Ball movement
ball_x += ball_dx
ball_y += ball
import pygame
import random
# Initialize Pygame
pygame.init()
# Set up the screen
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("2D Basketball Game")
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
# Player variables
player_size = 50
player_speed = 5
player_x = screen_width // 2 - player_size // 2
player_y = screen_height - 2 * player_size
# Ball variables
ball_size = 25
ball_speed = 4
ball_x = random.randint(ball_size, screen_width - ball_size)
ball_y = ball_size
ball_dx = ball_speed * random.choice([-1, 1])
ball_dy = ball_speed
# Main game loop
running = True
while running:
screen.fill(BLACK)
# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Player movement
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player_x -= player_speed
if keys[pygame.K_RIGHT]:
player_x += player_speed
if keys[pygame.K_UP]:
player_y -= player_speed
if keys[pygame.K_DOWN]:
player_y += player_speed
# Boundary checking for player
if player_x < 0:
player_x = 0
elif player_x > screen_width - player_size:
player_x = screen_width - player_size
if player_y < 0:
player_y = 0
elif player_y > screen_height - player_size:
player_y = screen_height - player_size
# Ball movement
ball_x += ball_dx
ball_y += ball_dy
# Boundary checking for ball
if ball_x <= 0 or ball_x >= screen_width - ball_size:
ball_dx *= -1
if ball_y <= 0:
ball_dy *= -1
# Collision detection between ball and player
if player_y <= ball_y + ball_size <= player_y + player_size and player_x <= ball_x <= player_x + player_size:
ball_dy *= -1
# Draw player
pygame.draw.rect(screen, WHITE, (player_x, player_y, player_size, player_size))
# Draw ball
pygame.draw.circle(screen, GREEN, (ball_x, ball_y), ball_size)
# Update the display
pygame.display.update()
# Cap the frame rate
pygame.time.Clock().tick(60)
# Quit Pygame
pygame.quit()