वह की वह j
import pygame
import sys
# Initialize Pygame
pygame.init()
# Constants
WIDTH, HEIGHT = 800, 600
BALL_RADIUS = 20
GRAVITY = 1
# Colors
WHITE = (255, 255, 255)
RED = (255, 0, 0)
# Initialize screen
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Gravity Ball Game")
# Ball properties
ball_x = WIDTH // 2
ball_y = 50
ball_speed = 0
# Finish area
finish_area_rect = pygame.Rect(WIDTH - 50, HEIGHT - 50, 50, 50)
# Game loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Apply gravity
ball_speed += GRAVITY
ball_y += ball_speed
# Ball-wall collision
if ball_y + BALL_RADIUS > HEIGHT:
ball_y = HEIGHT - BALL_RADIUS
ball_speed = 0
# Ball-finish area collision
if finish_area_rect.colliderect(pygame.Rect(ball_x - BALL_RADIUS, ball_y - BALL_RADIUS, BALL_RADIUS * 2, BALL_RADIUS * 2)):
print("You Win!")
pygame.quit()
sys.exit()
# Draw everything
screen.fill(WHITE)
pygame.draw.circle(screen, RED, (ball_x, int(ball_y)), BALL_RADIUS)
pygame.draw.rect(screen, RED, finish_area_rect)
pygame.display.flip()
# Control the frame rate
pygame.time.Clock().tick(30)
टिप्पणियाँ
एक टिप्पणी भेजें