Skip to content

Instantly share code, notes, and snippets.

@bemxio
Last active May 24, 2024 12:20
Show Gist options
  • Save bemxio/0f201c9e2d3a42c84e60be5b7b589df2 to your computer and use it in GitHub Desktop.
Save bemxio/0f201c9e2d3a42c84e60be5b7b589df2 to your computer and use it in GitHub Desktop.
A simple demo made in Python using Pygame, displaying balls of various color and size
#!/usr/bin/python3
"""
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org>
"""
from typing import Tuple, List
from random import randint, choice
import pygame
# constants
SURFACE_WIDTH = 500
SURFACE_HEIGHT = 500
SURFACE_DELAY = 5
BALL_AMOUNT = 10
BALL_SIZE = 10
BALL_SPEED = 1
DIRECTIONS = [-1, 1]
class Ball:
def __init__(self, x: int, y: int, radius: int = 10, color: Tuple[int, int, int] = (255, 255, 255), direction: List[int] = [1, 1], speed: int = 1):
self.x, self.y = x, y
self.radius = radius
self.color = color
self.direction = direction
self.speed = speed
def draw(self, surface: pygame.Surface):
pygame.draw.circle(surface, self.color, (self.x, self.y), self.radius)
def move(self):
self.x += self.speed * self.direction[0]
self.y += self.speed * self.direction[1]
pygame.init()
surface = pygame.display.set_mode((500, 500))
flag = True
balls = [Ball(
x=randint(0, SURFACE_WIDTH), y=randint(0, SURFACE_WIDTH),
radius=BALL_SIZE,
color=[randint(0, 255) for _ in range(3)],
direction=[choice(DIRECTIONS), choice(DIRECTIONS)],
speed=BALL_SPEED
) for _ in range(BALL_AMOUNT)]
while flag:
pygame.time.delay(SURFACE_DELAY)
for event in pygame.event.get():
if event.type == pygame.QUIT:
flag = False
surface.fill((0, 0, 0))
for ball in balls:
ball.move()
if ball.x - ball.radius <= 0:
ball.direction[0] = 1
elif ball.x + ball.radius >= SURFACE_WIDTH:
ball.direction[0] = -1
if ball.y - ball.radius <= 0:
ball.direction[1] = 1
elif ball.y + ball.radius >= SURFACE_HEIGHT:
ball.direction[1] = -1
ball.draw(surface)
pygame.display.update()
pygame.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment