Pages

Se afișează postările cu eticheta python 3.11. Afișați toate postările
Se afișează postările cu eticheta python 3.11. Afișați toate postările

sâmbătă, 8 aprilie 2023

PyGame : ovoid with a random pattern.

Here's how to create an ovoid with a random pattern. Run the script several times to see the differences:
import pygame
import random
pygame.init()

# Set up the display window
screen_size = (400, 400)
screen = pygame.display.set_mode(screen_size)
# Set window title
pygame.display.set_caption("Ovoid with Random Pattern")
# Define the ovoid
ovoid_pos = (150, 100)
ovoid_size = (100, 200)

# Create the ovoid surface
ovoid_surface = pygame.Surface(ovoid_size, pygame.SRCALPHA)

# Define the pattern
pattern_size = (random.randint(1, 9), random.randint(1, 9))
pattern_surface = pygame.Surface(pattern_size)
pattern_surface.fill((255, 255, 255))
pygame.draw.line(pattern_surface, (0, 0, 0), (0, 0), pattern_size)

# Create the mask surface
mask_surface = pygame.Surface(ovoid_size, pygame.SRCALPHA)
pygame.draw.ellipse(mask_surface, (255, 255, 255), mask_surface.get_rect(), 0)

# Apply the pattern to the ovoid surface
for x in range(0, ovoid_size[0], pattern_size[0]):
    for y in range(0, ovoid_size[1], pattern_size[1]):
        ovoid_surface.blit(pattern_surface, (x, y))

# Apply the mask to the ovoid surface
ovoid_surface.blit(mask_surface, (0, 0), special_flags=pygame.BLEND_RGBA_MULT)

# Draw the ovoid to the screen
screen.blit(ovoid_surface, ovoid_pos)

# Update the display
pygame.display.flip()

# Wait for the user to close the window
done = False
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

# Quit pygame properly
pygame.quit()

vineri, 3 martie 2023

PyGame : simple web camera !

In this simple tutorial, I'll show you how to use pygame to use it with a webcam.
Let's install the pygame with the pip tool:
C:\PythonProjects\pygamecamera001>pip install pygame --user
Collecting pygame
  Downloading pygame-2.2.0-cp311-cp311-win_amd64.whl (10.4 MB)
     ---------------------------------------- 10.4/10.4 MB 9.0 MB/s eta 0:00:00
Installing collected packages: pygame
Successfully installed pygame-2.2.0
This is the source code for web camera:
import pygame.camera
import pygame.image
import sys

pygame.camera.init()

cameras = pygame.camera.list_cameras()

webcam = pygame.camera.Camera(cameras[0])

webcam.start()

img = webcam.get_image()

WIDTH = img.get_width()
HEIGHT = img.get_height()

screen = pygame.display.set_mode( ( WIDTH, HEIGHT ) )
pygame.display.set_caption("pyGame webcam")

while True :
    for e in pygame.event.get() :
        if e.type == pygame.QUIT :
            sys.exit()
    screen.blit(img, (0,0))
    pygame.display.flip()
    img = webcam.get_image()