Samsung Galaxy Mobile Animation using Python (Pygame)
Are you looking for a fun Python project that combines creativity with programming? In this tutorial, we will explore how to create a Samsung Galaxy style mobile animation using the Pygame library. This project is perfect for beginners and intermediate Python developers who want to practice graphics, animation, and effects like twinkling stars, glowing galaxies, and smooth text animations.
Why Use Pygame for Animations?
Pygame is one of the most popular Python libraries for developing games and 2D animations. It provides simple functions to handle graphics, colors, and animations, making it an excellent choice for creative coding projects. With just a few lines of code, you can design interactive applications, mini-games, and animations like this Samsung Galaxy themed mobile screen effect.
Features of This Project
- Starry Background: 100 randomly generated stars that twinkle with sinusoidal brightness.
- Galaxy Effect: A smooth rotating spiral galaxy created with polygons and glowing blue lines.
- SAMSUNG Text Animation: The text fades in and pulses for a dynamic effect.
- Mobile-Friendly Resolution: The canvas is set to 360×640, which matches common smartphone display sizes.
Step-by-Step Code Explanation
1. Screen Setup
We initialize the Pygame window with dimensions 360×640
, which is similar to a Samsung Galaxy
mobile screen size.
2. Stars Creation
A list of stars is generated randomly with x
, y
, radius
, and brightness
.
Each star twinkles using math.sin()
and slightly moves vertically for a natural space effect.
3. Galaxy Spiral Effect
Using trigonometric functions, an elliptical spiral is created around the center. The galaxy expands gradually with rotation to simulate a glowing spiral.
4. Text Animation
The "SAMSUNG" text fades in as the galaxy grows and pulses in brightness using a sine wave function. This creates a modern startup-screen effect.
🌐 Samsung Mobile Animation
import pygame
import sys
import math
import random
def samsung_galaxy_mobile():
# Screen size
width, height = 360, 640
pygame.init()
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Samsung Mobile Animation")
clock = pygame.time.Clock()
# Colors
BLACK = (0, 0, 0)
BLUE = (0, 100, 255)
WHITE = (255, 255, 255)
# Stars data: x, y, size, base brightness
stars = []
for _ in range(100):
stars.append([
random.uniform(0, width),
random.uniform(0, height),
random.uniform(0.5, 2), # star radius
random.randint(150, 255) # base brightness
])
angle = 0
radius = 0
max_radius = 150
running = True
frame_count = 0
# Font for "SAMSUNG"
font = pygame.font.SysFont('Arial', 40, bold=True)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(BLACK)
# Animate stars with twinkling & slight vertical movement
for star in stars:
# twinkle brightness oscillates sinusoidally
twinkle = star[3] + 50 * math.sin(frame_count * 0.1 + star[0])
twinkle = max(150, min(255, twinkle))
# slight vertical oscillation
star[1] += 0.05 * math.sin(frame_count * 0.05 + star[0])
if star[1] > height:
star[1] = 0
elif star[1] < 0:
star[1] = height
color = (int(twinkle), int(twinkle), int(twinkle))
pygame.draw.circle(screen, color, (int(star[0]), int(star[1])), int(star[2]))
# Grow the galaxy radius slowly
if radius < max_radius:
radius += 1.5 # slower growth for smoothness
angle += 0.03 # rotation speed
points = []
for i in range(0, 360, 10):
rad = math.radians(i + angle * 30)
# Elliptical spiral for galaxy shape
x = width // 2 + math.cos(rad) * radius * 0.85
y = height // 2 + math.sin(rad) * radius * 0.55
points.append((x, y))
# Draw multiple concentric glowing polygons for galaxy effect
for glow in range(3, 0, -1):
alpha = max(0, 50 * glow * (radius / max_radius)) # fading glow
glow_color = (0, 100, 255, int(alpha)) # semi-transparent blue
# Pygame doesn't support alpha directly on lines, so draw thicker lines with fading color
pygame.draw.polygon(screen, (0, 100, 255), points, width=glow)
# Text fade-in and pulse effect
pulse = (math.sin(frame_count * 0.1) + 1) / 2 # from 0 to 1
alpha = int(255 * min(1, radius / max_radius)) # fade in with radius growth
pulse_alpha = int(alpha * (0.7 + 0.3 * pulse)) # pulse between 70%-100% opacity
# Render text with pulse alpha on a separate surface
text_surface = font.render("SAMSUNG", True, WHITE)
text_surface.set_alpha(pulse_alpha)
text_rect = text_surface.get_rect(center=(width // 2, height // 2))
screen.blit(text_surface, text_rect)
pygame.display.flip()
clock.tick(60)
frame_count += 1
if radius >= max_radius and frame_count > 300:
running = False
pygame.quit()
sys.exit()
samsung_galaxy_mobile()
🔍 Output Preview
Applications of This Project
- Create custom mobile startup animations for fun projects.
- Learn how to use Pygame for animation effects.
- Understand sinusoidal animations for twinkling, pulsing, and fading effects.
- Enhance Python coding skills while working on creative visual projects.
Final Thoughts
This project demonstrates how you can combine Python programming with creative animations to build visually appealing effects. By using Pygame, we created a Samsung Galaxy style mobile startup animation featuring stars, glowing spirals, and pulsing text. If you are learning Python and want to add a cool project to your portfolio, this animation is a great choice.