This content originally appeared on DEV Community and was authored by Karol
(ENG below)
Od kiedy zacząłem powtarzać Pythona, w mojej głowie pojawił się pomysł stworzenia jakiegoś mini projektu z wykorzystaniem Pygame. Zastanawiałem się jednak co mogę stworzyć, aż do przedwczoraj kiedy to moim oczom ukazała się reklama na YouTube. Postanowiłem ją odtworzyć we własnym zakresie.
Tak powstał projekt Ball Bounces – gra/symulator, w której piłki odbijają się wewnątrz okręgu, rysują linie, przejmują je od siebie nawzajem, a zwycięża ostatnia żywa piłka lub ta, która pierwsza zdobędzie 100 punktów.
Od pomysłu do kodu
Podstawą gry jest okrąg narysowany w Pythonie
def draw_arena(surface):
pygame.draw.circle(surface, (240, 240, 240), CENTER, ARENA_R, 3)
Piłki to obiekty klasy Ball, która przechowuje swoją pozycję, prędkość, listę zdobytych “anchorów” oraz liczbę punktów.
class Ball:
def __init__(self, color):
self.color = color
self.pos = CENTER.copy()
self.vel = pygame.Vector2(random.uniform(-1,1), random.uniform(-1,1)) * 200
self.hit_points = []
self.score = 0
Kolizje, były dość problematyczne ponieważ mamy ich kilka:
- piłka – ściana: klasyczne odbicie wektora prędkości,
- piłka – piłka: sprężyste zderzenie równych mas,
- piłka – linia: sub-stepping i test odległości punkt-odcinek, aby uniknąć gubienia przejęć przy większej prędkości.
Przejęcia lini to jeden z ciekawszych elementów – założenie było takie aby piłka która przecina linię przeciwnika, dostawała nowy anchor i punkt, a przeciwnik je tracił. W przypadku gdy był to ostatni anchor przeciwnika – kulka “umiera” i jest usuwana z gry.
Sterowanie i tryby
Grę można uruchomić w trybie 2,3 lub 4 graczy.
Sterowanie jest banalne:
- S – start,
- R – restart,
- 2 / 3 / 4 – wybór liczby graczy.
Efekt końcowy
Gra dość szybko staje się chaotyczna – linie krzyżują się, piłki przejmują nawzajem swoje punkty, a plansza zamienia się w sieć kolorowych linii. Dokładnie takiego efektu oczekiwałem. Dodatkowo można obstawiać, która kulka wygra.
Kod źródłowy
Cały projekt do obejrzenia na moim Githubie:
https://github.com/Karol-Polak/Ball_Bounces/tree/main
Podsumowanie
“Ball Bounces” to świetny przykład, jak ciekawe efekty można uzyskać korzystając z Pygame. Mam już pomysły na kolejne update’y, które mogłyby znaleźć się w grze.
Do usłyszenia
Since I started revising Python, I had the idea of building a small project with Pygame. I was wondering what I could build, until the day before yesterday when I saw an ad on Youtube. I decided to recreate it on my own.
That’s how the project Ball Bounces was created – a game/simulator where balls bounce inside a circle, draw lines, steal them from each other, and the winner is either the last ball alive or the one that first reaches 100 points.
From idea to code
The foundation of the game is a circle drawn in Python.
def draw_arena(surface):
pygame.draw.circle(surface, (240, 240, 240), CENTER, ARENA_R, 3)
The balls are objects of the Ball class, which stores their position, velocity, the list of collected “anchors”, and the score.
class Ball:
def __init__(self, color):
self.color = color
self.pos = CENTER.copy()
self.vel = pygame.Vector2(random.uniform(-1,1), random.uniform(-1,1)) * 200
self.hit_points = []
self.score = 0
Collisions turned out to be quite tricky because there are several types:
- ball – wall: classic velocity vector reflection,
- ball – ball: elastic collision of equal masses,
- ball – line: sub-stepping and point-to-segment distance test to avoid missing captures at higher speeds.
Linę capturing is one of the most interesting elements – the assumption was that if a ball crosses an opponent’s line, it receives a new anchor and a point, while the opponent loses them. If it was the opponent’s last anchor, their ball “dies” and is removed from the game.
Contols and modes
The game can be launched in 2,3 or 4 player mode.
Contols are simple:
- S – start,
- R – restart,
- 2 / 3 / 4 – choose the number of players.
Final effect
The game quickly becomes chaotic – lines cross, balls steal each other’s points, and the board turns into a web colorful lines. That’s exactly the effect I was hoping for. Additionally, you can even bet on which ball will win.
Source Code
The entire project is available on my Github:
https://github.com/Karol-Polak/Ball_Bounces/tree/main
Summary
“Ball Bounces” is a great example of how interesting effects can be achieved with Pygame. I already have ideas for future updates that could make their way into the game.
See you
This content originally appeared on DEV Community and was authored by Karol