Pages

duminică, 16 iunie 2024

PyGame : Game development in PyGame: making a basic map.

Example with PyGame with 3D features ...
The project can be found on the GitHub repo - stage 11 ...

sâmbătă, 30 martie 2024

PyGame : ... antialiased filled circle !

The pygame python module does not implement an antialiased filled circle and this is the scope of this tutorial.
The pygame module for drawing shapes are:
  • pygame.draw.rect - to draw a rectangle
  • pygame.draw.polygon - to draw a polygon
  • pygame.draw.circle - to draw a circle
  • pygame.draw.ellipse - to draw an ellipse
  • pygame.draw.arc - to draw an elliptical arc
  • pygame.draw.line - to draw a straight line
  • pygame.draw.lines - to draw multiple contiguous straight line segments
  • pygame.draw.aaline - to draw a straight antialiased line
  • pygame.draw.aalines - to draw multiple contiguous straight antialiased line segments.
Let's install pygame python module.
pip install pygame
Collecting pygame
  Downloading pygame-2.5.2-cp312-cp312-win_amd64.whl.metadata (13 kB)
...
Installing collected packages: pygame
Successfully installed pygame-2.5.2
Let's see the source code:
import pygame
import pygame.gfxdraw

TARGET_SIZE = 200
BG_ALPHA_COLOR = (0, 0, 0, 100)

class Target(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.Surface((TARGET_SIZE, TARGET_SIZE), pygame.SRCALPHA)
        self.rect = self.image.get_rect()
        self.color = (255, 0, 0)
        self.filled = False
        self.width = 1

    def DrawTarget(self):
        pygame.gfxdraw.aacircle(self.image, int(self.rect.width/2), int(self.rect.height/2),\
                int(self.rect.width/2 - 1), self.color)
        
        pygame.gfxdraw.filled_ellipse(self.image, int(self.rect.width/2), \
            int(self.rect.height/2), int(self.rect.width/2 - 1), int(self.rect.width/2 - 1), self.color)
        
        temp = pygame.Surface((TARGET_SIZE, TARGET_SIZE), pygame.SRCALPHA)
        
        if not self.filled:
            pygame.gfxdraw.filled_ellipse(temp, int(self.rect.width/2), int(self.rect.height/2), \
                int(self.rect.width/2 - self.width), int(self.rect.width/2 - self.width), BG_ALPHA_COLOR)
            pygame.gfxdraw.aacircle(temp, int(self.rect.width/2), int(self.rect.height/2), \
                int(self.rect.width/2 - self.width), BG_ALPHA_COLOR)
        
        self.image.blit(temp, (0, 0), None, pygame.BLEND_ADD)
pygame.init()
screen = pygame.display.set_mode((400, 400))

target = Target()
target.DrawTarget()

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill((255, 255, 255))
    screen.blit(target.image, (100, 100))
    pygame.display.flip()

pygame.quit()

luni, 10 iulie 2023

News : About my work and one of my websites.

I would like to bring to the attention of those who follow my activity on my websites and appreciate the inability to continue with one of the websites: free-tutorials.org. It is currently hosted on a free host, but I cannot import it 100%, which has led me not to complete it with new posts. The continuation of the activities there, considering the limited time, will be carried out on my blogs with the defined theme with which I started: Linux - Fedora, Graphics, Python, Pygame.
In the meantime, because the host is expensive and until now someone has helped me to host it on his server, it is possible to sell the domain: free-tutorials.org - I receive purchase offers at my personal Yahoo email address catafest@yahoo.com.
Minimum starting price 250 euros, because the domain is old from 2018.

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()