State of War Win11,Initialize Pygame

State of War for Windows 11: A Turn-Based Strategy Game

A grid-based strategy game where players command units to destroy the enemy base. Features include unit movement, combat, AI opponents, and a win condition.

State of War Win11,Initialize Pygame


Game Mechanics

  1. Grid Map

    State of War Win11,Initialize Pygame

    • 10x10 tile grid.
    • Bases placed at opposite corners (Player 1: top-left, Player 2: bottom-right).
    • Units start adjacent to their base.
  2. Units

    State of War Win11,Initialize Pygame

    • Health: 100 HP.
    • Movement: 1 tile per turn (adjacent tiles, including diagonals).
    • Attack: Deals 20 damage to adjacent enemies.
    • Actions: Move OR attack per turn.
  3. Bases

    • Health: 200 HP.
    • Defense: Cannot move.
    • Destruction: Ends the game.
  4. Turns

    • Player 1 (Human): Click unit → Click destination (move) or enemy (attack).
    • Player 2 (AI): Randomly moves/attacks.

Controls

  • Mouse Click:
    • Select unit (highlighted in blue).
    • Move to empty tile (green highlight).
    • Attack adjacent enemy (red highlight).
  • ESC: End game.

Win Condition

Destroy the enemy base to win.


Python Implementation

import pygame
import sys
import random
pygame.init()
# Constants
GRID_SIZE = 10
TILE_SIZE = 60
WINDOW_WIDTH = GRID_SIZE * TILE_SIZE
WINDOW_HEIGHT = GRID_SIZE * TILE_SIZE
FPS = 60
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
GRAY = (128, 128, 128)
YELLOW = (255, 255, 0)
class Tile:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.unit = None
        self.base = None
class Unit:
    def __init__(self, x, y, owner):
        self.x = x
        self.y = y
        self.owner = owner
        self.health = 100
class Base:
    def __init__(self, x, y, owner):
        self.x = x
        self.y = y
        self.owner = owner
        self.health = 200
class Game:
    def __init__(self):
        self.screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
        pygame.display.set_caption("State of War - Win11")
        self.clock = pygame.time.Clock()
        self.grid = [[Tile(x, y) for y in range(GRID_SIZE)] for x in range(GRID_SIZE)]
        self.selected_unit = None
        self.current_player = 1
        self.game_over = False
        self.winner = None
        self.init_game()
    def init_game(self):
        # Create bases
        self.grid[0][0].base = Base(0, 0, 1)
        self.grid[GRID_SIZE-1][GRID_SIZE-1].base = Base(GRID_SIZE-1, GRID_SIZE-1, 2)
        # Create units
        for i in range(3):
            self.grid[i+1][0].unit = Unit(i+1, 0, 1)
            self.grid[GRID_SIZE-2-i][GRID_SIZE-1].unit = Unit(GRID_SIZE-2-i, GRID_SIZE-1, 2)
    def draw(self):
        self.screen.fill(BLACK)
        for x in range(GRID_SIZE):
            for y in range(GRID_SIZE):
                tile = self.grid[x][y]
                rect = pygame.Rect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE)
                pygame.draw.rect(self.screen, GRAY, rect, 1)
                # Draw base
                if tile.base:
                    color = BLUE if tile.base.owner == 1 else RED
                    pygame.draw.rect(self.screen, color, rect)
                    font = pygame.font.SysFont(None, 24)
                    text = font.render(f"{tile.base.health}", True, WHITE)
                    self.screen.blit(text, (rect.x + 10, rect.y + 10))
                # Draw unit
                if tile.unit:
                    color = BLUE if tile.unit.owner == 1 else RED
                    pygame.draw.circle(self.screen, color, (rect.centerx, rect.centery), 20)
                    font = pygame.font.SysFont(None, 20)
                    text = font.render(f"{tile.unit.health}", True, WHITE)
                    self.screen.blit(text, (rect.centerx - 10, rect.centery - 10))
        # Highlight selected unit
        if self.selected_unit:
            rect = pygame.Rect(self.selected_unit.x * TILE_SIZE, self.selected_unit.y * TILE_SIZE, TILE_SIZE, TILE_SIZE)
            pygame.draw.rect(self.screen, YELLOW, rect, 3)
        # Game over message
        if self.game_over:
            font = pygame.font.SysFont(None, 72)
            text = font.render(f"Player {self.winner} Wins!", True, WHITE)
            self.screen.blit(text, (WINDOW_WIDTH//2 - 150, WINDOW_HEIGHT//2))
        pygame.display.flip()
    def get_tile(self, pos):
        x, y = pos[0] // TILE_SIZE, pos[1] // TILE_SIZE
        if 0 <= x < GRID_SIZE and 0 <= y < GRID_SIZE:
            return self.grid[x][y]
        return None
    def handle_click(self, pos):
        if self.game_over or self.current_player != 1:
            return
        tile = self.get_tile(pos)
        if not tile:
            return
        # Select unit
        if tile.unit and tile.unit.owner == 1:
            self.selected_unit = tile.unit
            return
        # Move or attack
        if self.selected_unit:
            dx = abs(tile.x - self.selected_unit.x)
            dy = abs(tile.y - self.selected_unit.y)
            distance = max(dx, dy)
            # Move to empty tile
            if distance == 1 and not tile.unit and not tile.base:
                self.grid[self.selected_unit.x][self.selected_unit.y].unit = None
                self.selected_unit.x = tile.x
                self.selected_unit.y = tile.y
                tile.unit = self.selected_unit
                self.end_turn()
            # Attack adjacent enemy
            elif distance ==

转载请说明出处
蓝警之家 » State of War Win11,Initialize Pygame

发表评论

欢迎 访客 发表评论

蓝色警戒主题网站!

信誉保障 联系客服