TANK GAME
- 3 Devlogs
- 13 Total hours
TANK GAME
TANK GAME
This is the part where the game actually starts doing things. Wraps up arena generation, then jumps into shooting, movement, input, the bot’s brain, powerups, and bullets.
begin_level and start_match are just glue, reset the tanks, build the level, flip the state to playing. start_match additionally zeroes out score and level since that only happens when a whole new match starts, not every round.
fire_from is where powerups actually change what comes out of the barrel. Big shells make the bullet bigger and slower but harder hitting. Lock-On just flags the bullet as homing. Twin Cannons is the odd one out since it fires two bullets at a slight angle instead of changing one bullet’s stats. Bullets spawn a little in front of the tank’s nose too, otherwise they’d spawn inside their own tank and immediately register a hit on the guy who just fired.
try_move_tank is the one function basically everything routes through, player and bot both. Clamp the position, check it against every obstacle, only actually move if nothing’s blocking, unless you’ve got the Dozer Blade and you’re hitting a crate specifically, then the crate just dies instead.
handle_player_input is the “instant facing” control scheme. Read whichever movement keys are held, build a direction, normalize it so diagonals aren’t faster than straight lines, then just snap the tank’s angle straight to that direction. No gradual turning for humans, you press a direction and you’re already facing it.
update_bot is the biggest one here and honestly the most fun to write. Every frame it loads its difficulty stats, decides whether to chase the player or detour for a nearby powerup, figures out how far off its aim is, probes a little bit ahead of itself to avoid walking into walls, turns gradually toward its goal since bots don’t get the instant snap the player gets, picks a speed based on distance to target, and only actually shoots if its aim is tight enough and the target’s in range.
maybe_spawn_powerup and check_powerup_pickup are pretty simple, try random spots until one doesn’t overlap anything or spawn too close to a tank, and check distance for pickup.
update_bullets is the long one. Homing bullets get nudged toward their target a little each frame instead of snapping straight at it, so they’re curveable and dodgeable, not laser guided. Then every bullet gets checked against the arena bounds, every obstacle, and both tanks, in that order, and whichever one it hits first is what actually happens to it.
update_particles is just the tiny cosmetic bit, dots that drift, slow down over time, and fade out. Purely for hit sparks and crate destruction, doesn’t affect gameplay at all.
Devlogs might be ragebait… Lines 1 to 217 are basically just setup, nothing’s actually running yet.
Top of the file is the instruction manual docstring, how to install and run it. Then imports, math for angles and distances, random for scattering stuff and giving the bot some unpredictability, pygame doing the actual window and drawing and input work. After that’s a big constants block, basically a settings panel. Window size, tank stats, bullet speed, all the colors. The two fun ones are POWER_TYPES, which holds each powerup’s color and name in one place, and DIFFICULTIES, which is basically the bot’s stat sheet for easy medium and hard. Then MAPS, the four arenas, built so they’re mirror symmetric so nobody spawns with worse cover. Three small helper functions come next. clamp keeps a number in range. ang_diff figures out the short way to turn instead of spinning the long way around. circle_rect_hit checks if a circle touches a rectangle, and honestly this one function quietly handles every collision in the whole game.
Then the entity classes, Obstacle, Powerup, Bullet, Tank. All pretty dumb on purpose, they just hold data like position and health, they don’t do anything themselves. Last bit is Game.init setting up empty lists and the score, and reset_tanks, which puts player one at the bottom facing up and player two or the bot at the top facing down. That’s it, that’s everything before the game logic actually kicks in.
Making the base for the tank game and creating the maps, difficulty modes, colors, and upgrades.