Created
December 21, 2022 17:14
-
-
Save dorkitude/58788cecff85594c42bb9ced5e4a5dc9 to your computer and use it in GitHub Desktop.
basic game on pygame
This file contains 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 | |
# Initialize Pygame | |
pygame.init() | |
# Set the window size and title | |
screen_size = (600, 400) | |
screen = pygame.display.set_mode(screen_size) | |
pygame.display.set_caption("Bouncing Ball") | |
# Set the ball properties | |
ball_color = (255, 0, 0) # Red | |
ball_radius = 20 | |
ball_pos = [300, 200] # Initial position (middle of the screen) | |
ball_vel = [5, 5] # Initial velocity (move right and down) | |
# Set the frame rate | |
frame_rate = 60 | |
# Main game loop | |
running = True | |
while running: | |
# Handle events | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
running = False | |
# Update the ball position | |
ball_pos[0] += ball_vel[0] | |
ball_pos[1] += ball_vel[1] | |
# Check for ball collision with the walls | |
if ball_pos[0] - ball_radius < 0 or ball_pos[0] + ball_radius > screen_size[0]: | |
ball_vel[0] *= -1 # Reverse the horizontal velocity | |
if ball_pos[1] - ball_radius < 0 or ball_pos[1] + ball_radius > screen_size[1]: | |
ball_vel[1] *= -1 # Reverse the vertical velocity | |
# Draw the ball | |
pygame.draw.circle(screen, ball_color, ball_pos, ball_radius) | |
# Update the display | |
pygame.display.flip() | |
# Wait for the next frame | |
pygame.time.delay(int(1000 / frame_rate)) | |
# Quit Pygame | |
pygame.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment