A simple 8-bit style tank battle game using Pygame
This is a simple 8-bit style tank battle game where two players control tanks and try to shoot each other. The game features:
Here's the complete Python code for the game. You'll need to have Pygame installed (pip install pygame).
import pygame
import math
import random
# Initialize pygame
pygame.init()
# Screen dimensions
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("8-bit Tank Battle")
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
GRAY = (100, 100, 100)
# Game variables
clock = pygame.time.Clock()
FPS = 60
font = pygame.font.SysFont('Arial', 24)
# Tank class
class Tank:
def __init__(self, x, y, color, controls):
self.x = x
self.y = y
self.color = color
self.width = 40
self.height = 30
self.angle = 0
self.speed = 3
self.controls = controls # [up, down, left, right, fire]
self.cooldown = 0
self.health = 100
self.score = 0
def draw(self, screen):
# Tank body (rectangle)
tank_rect = pygame.Rect(self.x - self.width//2, self.y - self.height//2,
self.width, self.height)
pygame.draw.rect(screen, self.color, tank_rect)
# Tank barrel (line)
barrel_length = 25
end_x = self.x + barrel_length * math.cos(math.radians(self.angle))
end_y = self.y - barrel_length * math.sin(math.radians(self.angle))
pygame.draw.line(screen, BLACK, (self.x, self.y), (end_x, end_y), 5)
# Draw health bar
health_width = 40
health_height = 5
health_x = self.x - health_width//2
health_y = self.y - self.height//2 - 10
pygame.draw.rect(screen, RED, (health_x, health_y, health_width, health_height))
pygame.draw.rect(screen, GREEN, (health_x, health_y, health_width * (self.health/100), health_height))
def move(self, keys):
if keys[self.controls[0]]: # Up
self.y -= self.speed
self.angle = 90
if keys[self.controls[1]]: # Down
self.y += self.speed
self.angle = 270
if keys[self.controls[2]]: # Left
self.x -= self.speed
self.angle = 180
if keys[self.controls[3]]: # Right
self.x += self.speed
self.angle = 0
# Keep tank on screen
self.x = max(self.width//2, min(WIDTH - self.width//2, self.x))
self.y = max(self.height//2, min(HEIGHT - self.height//2, self.y))
# Cooldown timer
if self.cooldown > 0:
self.cooldown -= 1
def fire(self):
if self.cooldown == 0:
self.cooldown = 30 # 0.5 second cooldown at 60 FPS
return Bullet(self.x, self.y, self.angle, self.color)
return None
# Bullet class
class Bullet:
def __init__(self, x, y, angle, color):
self.x = x
self.y = y
self.angle = angle
self.color = color
self.speed = 7
self.radius = 5
self.lifetime = 120 # 2 seconds at 60 FPS
def update(self):
# Move bullet based on angle
self.x += self.speed * math.cos(math.radians(self.angle))
self.y -= self.speed * math.sin(math.radians(self.angle))
self.lifetime -= 1
# Return True if bullet is out of bounds or expired
return (self.x < 0 or self.x > WIDTH or
self.y < 0 or self.y > HEIGHT or
self.lifetime <= 0)
def draw(self, screen):
pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.radius)
def check_collision(self, tank):
# Simple distance-based collision detection
distance = math.sqrt((self.x - tank.x)**2 + (self.y - tank.y)**2)
return distance < (self.radius + tank.width//2)
# Obstacle class
class Obstacle:
def __init__(self, x, y, width, height):
self.rect = pygame.Rect(x, y, width, height)
self.color = GRAY
def draw(self, screen):
pygame.draw.rect(screen, self.color, self.rect)
# Create tanks
player1 = Tank(100, HEIGHT//2, BLUE, [pygame.K_w, pygame.K_s, pygame.K_a, pygame.K_d, pygame.K_SPACE])
player2 = Tank(WIDTH-100, HEIGHT//2, RED, [pygame.K_UP, pygame.K_DOWN, pygame.K_LEFT, pygame.K_RIGHT, pygame.K_RETURN])
# Create obstacles
obstacles = [
Obstacle(WIDTH//2 - 50, HEIGHT//2 - 50, 100, 100),
Obstacle(WIDTH//4, HEIGHT//4, 50, 200),
Obstacle(3*WIDTH//4, 3*HEIGHT//4, 50, 200)
]
# Game loop
bullets = []
running = True
game_over = False
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN and game_over:
if event.key == pygame.K_r:
# Reset game
player1 = Tank(100, HEIGHT//2, BLUE, [pygame.K_w, pygame.K_s, pygame.K_a, pygame.K_d, pygame.K_SPACE])
player2 = Tank(WIDTH-100, HEIGHT//2, RED, [pygame.K_UP, pygame.K_DOWN, pygame.K_LEFT, pygame.K_RIGHT, pygame.K_RETURN])
bullets = []
game_over = False
if not game_over:
# Get keyboard state
keys = pygame.key.get_pressed()
# Move tanks
player1.move(keys)
player2.move(keys)
# Fire bullets
if keys[player1.controls[4]]:
bullet = player1.fire()
if bullet:
bullets.append(bullet)
if keys[player2.controls[4]]:
bullet = player2.fire()
if bullet:
bullets.append(bullet)
# Update bullets
for bullet in bullets[:]:
if bullet.update():
bullets.remove(bullet)
else:
# Check collisions with tanks
if bullet.color == BLUE and bullet.check_collision(player2):
player2.health -= 10
bullets.remove(bullet)
if player2.health <= 0:
player1.score += 1
player2.health = 100
elif bullet.color == RED and bullet.check_collision(player1):
player1.health -= 10
bullets.remove(bullet)
if player1.health <= 0:
player2.score += 1
player1.health = 100
# Check for game over (first to 3 points)
if player1.score >= 3 or player2.score >= 3:
game_over = True
# Draw everything
screen.fill(BLACK)
# Draw obstacles
for obstacle in obstacles:
obstacle.draw(screen)
# Draw tanks
player1.draw(screen)
player2.draw(screen)
# Draw bullets
for bullet in bullets:
bullet.draw(screen)
# Draw scores
score_text = font.render(f"Blue: {player1.score} Red: {player2.score}", True, WHITE)
screen.blit(score_text, (WIDTH//2 - score_text.get_width()//2, 10))
# Draw game over message
if game_over:
winner = "Blue Player" if player1.score >= 3 else "Red Player"
game_over_text = font.render(f"{winner} Wins! Press R to restart", True, YELLOW)
screen.blit(game_over_text, (WIDTH//2 - game_over_text.get_width()//2, HEIGHT//2))
pygame.display.flip()
clock.tick(FPS)
pygame.quit()