Skip to content
The Chat Archive
grok-3-preview-02-24· xAI LMArena Human Preference 140K

write me a python game for a Pro evolution soccer type of game in pygame make it very…

  1. User

    write me a python game for a Pro evolution soccer type of game in pygame make it very advanced just like the original without images or sounds

  2. Assistant

    Creating a full-fledged soccer game like Pro Evolution Soccer (PES) without images or sounds in Pygame is a massive undertaking, but I can provide you with a solid foundation for an advanced soccer game. This example will include core mechanics such as player movement, ball physics, AI opponents, scoring, and basic game states. Since we're not using images or sounds, we'll rely on Pygame's drawing functions to represent players, the ball, and the field.

    Below is a detailed implementation of an advanced soccer game in Pygame. Note that this is a simplified version of a full PES game, but it includes many advanced features. You can build upon this foundation to add more complexity, such as additional AI behaviors, formations, or multiplayer support.

    import pygame
    import math
    import random
    
    # Initialize Pygame
    pygame.init()
    
    # Screen dimensions
    SCREEN_WIDTH = 1280
    SCREEN_HEIGHT = 720
    screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    pygame.display.set_caption("Advanced Soccer Game")
    
    # Colors
    WHITE = (255, 255, 255)
    BLACK = (0, 0, 0)
    GREEN = (0, 128, 0)
    RED = (255, 0, 0)
    BLUE = (0, 0, 255)
    YELLOW = (255, 255, 0)
    
    # Game constants
    FPS = 60
    PLAYER_RADIUS = 10
    BALL_RADIUS = 5
    GOAL_WIDTH = 200
    GOAL_HEIGHT = 100
    PLAYER_SPEED = 5
    BALL_SPEED = 7
    AI_SPEED = 4
    FRICTION = 0.98  # Ball friction to simulate slowing down
    
    # Game states
    STATE_MENU = 0
    STATE_PLAYING = 1
    STATE_GOAL = 2
    STATE_END = 3
    
    # Initialize game state
    game_state = STATE_MENU
    player_score = 0
    ai_score = 0
    clock = pygame.time.Clock()
    
    # Font for text
    font = pygame.font.SysFont(None, 48)
    
    # Define the field boundaries
    FIELD_RECT = pygame.Rect(50, 50, SCREEN_WIDTH - 100, SCREEN_HEIGHT - 100)
    GOAL_LEFT = pygame.Rect(50, SCREEN_HEIGHT // 2 - GOAL_HEIGHT // 2, 10, GOAL_HEIGHT)
    GOAL_RIGHT = pygame.Rect(SCREEN_WIDTH - 60, SCREEN_HEIGHT // 2 - GOAL_HEIGHT // 2, 10, GOAL_HEIGHT)
    
    class Player:
        def __init__(self, x, y, color):
            self.x = x
            self.y = y
            self.color = color
            self.radius = PLAYER_RADIUS
            self.speed = PLAYER_SPEED
            self.has_ball = False
    
        def move(self, dx, dy):
            self.x += dx * self.speed
            self.y += dy * self.speed
            # Keep player within field boundaries
            self.x = max(FIELD_RECT.left + self.radius, min(FIELD_RECT.right - self.radius, self.x))
            self.y = max(FIELD_RECT.top + self.radius, min(FIELD_RECT.bottom - self.radius, self.y))
    
        def draw(self, surface):
            pygame.draw.circle(surface, self.color, (int(self.x), int(self.y)), self.radius)
            if self.has_ball:
                pygame.draw.circle(surface, YELLOW, (int(self.x), int(self.y)), self.radius + 5, 2)
    
    class Ball:
        def __init__(self, x, y):
            self.x = x
            self.y = y
            self.radius = BALL_RADIUS
            self.vx = 0
            self.vy = 0
            self.owner = None
    
        def move(self):
            if self.owner:
                self.x = self.owner.x
                self.y = self.owner.y
            else:
                self.x += self.vx
                self.y += self.vy
                # Apply friction
                self.vx *= FRICTION
                self.vy *= FRICTION
                # Keep ball within field boundaries
                if self.x - self.radius < FIELD_RECT.left:
                    self.x = FIELD_RECT.left + self.radius
                    self.vx = -self.vx
                elif self.x + self.radius > FIELD_RECT.right:
                    self.x = FIELD_RECT.right - self.radius
                    self.vx = -self.vx
                if self.y - self.radius < FIELD_RECT.top:
                    self.y = FIELD_RECT.top + self.radius
                    self.vy = -self.vy
                elif self.y + self.radius > FIELD_RECT.bottom:
                    self.y = FIELD_RECT.bottom - self.radius
                    self.vy = -self.vy
    
        def kick(self, target_x, target_y):
            if self.owner:
                self.owner.has_ball = False
                self.owner = None
                angle = math.atan2(target_y - self.y, target_x - self.x)
                self.vx = BALL_SPEED * math.cos(angle)
                self.vy = BALL_SPEED * math.sin(angle)
    
        def draw(self, surface):
            pygame.draw.circle(surface, WHITE, (int(self.x), int(self.y)), self.radius)
    
    class AIPlayer(Player):
        def __init__(self, x, y, color):
            super().__init__(x, y, color)
            self.speed = AI_SPEED
    
        def update(self, ball, player):
            if ball.owner == self:
                # If AI has the ball, move towards the player's goal
                target_x = GOAL_LEFT.centerx
                target_y = GOAL_LEFT.centery
                if random.random() < 0.05:  # Random chance to kick towards goal
                    ball.kick(target_x, target_y)
            elif ball.owner is None:
                # Chase the ball if it's free
                target_x = ball.x
                target_y = ball.y
            else:
                # Move to a defensive position
                target_x = (player.x + GOAL_RIGHT.centerx) / 2
                target_y = (player.y + GOAL_RIGHT.centery) / 2
    
            # Move towards target
            dx = target_x - self.x
            dy = target_y - self.y
            dist = math.hypot(dx, dy)
            if dist > 0:
                dx /= dist
                dy /= dist
                self.move(dx, dy)
    
    def check_collision(player, ball):
        dist = math.hypot(player.x - ball.x, player.y - ball.y)
        return dist < player.radius + ball.radius
    
    def reset_game():
        global player, ai, ball, game_state
        player = Player(SCREEN_WIDTH // 4, SCREEN_HEIGHT // 2, BLUE)
        ai = AIPlayer(3 * SCREEN_WIDTH // 4, SCREEN_HEIGHT // 2, RED)
        ball = Ball(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)
        game_state = STATE_PLAYING
    
    def draw_field(surface):
        surface.fill(BLACK)
        pygame.draw.rect(surface, GREEN, FIELD_RECT)
        pygame.draw.rect(surface, WHITE, GOAL_LEFT)
        pygame.draw.rect(surface, WHITE, GOAL_RIGHT)
        pygame.draw.line(surface, WHITE, (SCREEN_WIDTH // 2, FIELD_RECT.top), (SCREEN_WIDTH // 2, FIELD_RECT.bottom), 2)
        pygame.draw.circle(surface, WHITE, (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2), 50, 2)
    
    def draw_menu(surface):
        title_text = font.render("Advanced Soccer Game", True, WHITE)
        start_text = font.render("Press SPACE to Start", True, WHITE)
        surface.blit(title_text, (SCREEN_WIDTH // 2 - title_text.get_width() // 2, SCREEN_HEIGHT // 3))
        surface.blit(start_text, (SCREEN_WIDTH // 2 - start_text.get_width() // 2, SCREEN_HEIGHT // 2))
    
    def draw_goal(surface):
        goal_text = font.render("GOAL!", True, WHITE)
        surface.blit(goal_text, (SCREEN_WIDTH // 2 - goal_text.get_width() // 2, SCREEN_HEIGHT // 3))
        continue_text = font.render("Press SPACE to Continue", True, WHITE)
        surface.blit(continue_text, (SCREEN_WIDTH // 2 - continue_text.get_width() // 2, SCREEN_HEIGHT // 2))
    
    def draw_end(surface):
        end_text = font.render(f"Game Over! Player: {player_score} AI: {ai_score}", True, WHITE)
        restart_text = font.render("Press SPACE to Restart", True, WHITE)
        surface.blit(end_text, (SCREEN_WIDTH // 2 - end_text.get_width() // 2, SCREEN_HEIGHT // 3))
        surface.blit(restart_text, (SCREEN_WIDTH // 2 - restart_text.get_width() // 2, SCREEN_HEIGHT // 2))
    
    # Initialize game objects
    reset_game()
    
    # Game loop
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    if game_state == STATE_MENU:
                        reset_game()
                    elif game_state == STATE_GOAL:
                        reset_game()
                    elif game_state == STATE_END:
                        player_score = 0
                        ai_score = 0
                        reset_game()
    
        if game_state == STATE_MENU:
            draw_field(screen)
            draw_menu(screen)
        elif game_state == STATE_PLAYING:
            # Player movement
            keys = pygame.key.get_pressed()
            dx, dy = 0, 0
            if keys[pygame.K_LEFT]:
                dx = -1
            if keys[pygame.K_RIGHT]:
                dx = 1
            if keys[pygame.K_UP]:
                dy = -1
            if keys[pygame.K_DOWN]:
                dy = 1
            if dx != 0 or dy != 0:
                dist = math.hypot(dx, dy)
                dx /= dist
                dy /= dist
            player.move(dx, dy)
    
            # Ball possession
            if check_collision(player, ball) and ball.owner is None:
                player.has_ball = True
                ball.owner = player
            if check_collision(ai, ball) and ball.owner is None:
                ai.has_ball = True
                ball.owner = ai
    
            # Kick the ball
            if keys[pygame.K_SPACE] and player.has_ball:
                mouse_x, mouse_y = pygame.mouse.get_pos()
                ball.kick(mouse_x, mouse_y)
    
            # AI behavior
            ai.update(ball, player)
    
            # Ball movement
            ball.move()
    
            # Check for goals
            if GOAL_LEFT.collidepoint(ball.x, ball.y) and ball.owner is None:
                ai_score += 1
                game_state = STATE_GOAL
            elif GOAL_RIGHT.collidepoint(ball.x, ball.y) and ball.owner is None:
                player_score += 1
                game_state = STATE_GOAL
    
            # Check for end of game (e.g., first to 5 goals)
            if player_score >= 5 or ai_score >= 5:
                game_state = STATE_END
    
            # Draw everything
            draw_field(screen)
            player.draw(screen)
            ai.draw(screen)
            ball.draw(screen)
    
            # Draw score
            score_text = font.render(f"Player: {player_score} AI: {ai_score}", True, WHITE)
            screen.blit(score_text, (10, 10))
        elif game_state == STATE_GOAL:
            draw_field(screen)
            player.draw(screen)
            ai.draw(screen)
            ball.draw(screen)
            draw_goal(screen)
        elif game_state == STATE_END:
            draw_field(screen)
            draw_end(screen)
    
        pygame.display.flip()
        clock.tick(FPS)
    
    pygame.quit()
    

    Explanation of the Code

    1. Game Setup:

      • The game uses Pygame to create a window and handle input.
      • Constants are defined for screen dimensions, colors, player and ball properties, and game states.
    2. Classes:

      • Player: Represents a player with movement, ball possession, and drawing capabilities.
      • Ball: Represents the soccer ball with physics-based movement (velocity, friction, and collision with boundaries).
      • AIPlayer: Inherits from Player and implements basic AI behavior, such as chasing the ball, moving towards the goal, or defending.
    3. Game States:

      • STATE_MENU: Displays a start menu.
      • STATE_PLAYING: The main gameplay state where players move, kick the ball, and score goals.
      • STATE_GOAL: Displays a "GOAL!" message and waits for the player to continue.
      • STATE_END: Displays the final score and allows restarting the game.
    4. Game Mechanics:

      • Player Movement: Controlled using arrow keys, with normalization to ensure consistent speed in all directions.
      • Ball Possession: Players can pick up the ball by colliding with it, and kick it towards the mouse cursor using the spacebar.
      • AI Behavior: The AI chases the ball if it's free, moves towards the goal if it has the ball, or defends if the player has the ball.
      • Goals: Scored when the ball enters the opponent's goal area, with a simple scoring system.
      • Physics: The ball slows down due to friction and bounces off field boundaries.
    5. Drawing:

      • The field, goals, players, and ball are drawn using Pygame's drawing functions (rectangles, circles, and lines).
      • Text is rendered using Pygame's font system for menus, scores, and game messages.

    How to Play

    • Use the arrow keys to move your player (blue circle).
    • Press the spacebar to kick the ball towards the mouse cursor when you have possession.
    • Score goals by getting the ball into the opponent's goal (right side for the player, left side for the AI).
    • The game ends when either the player or AI reaches 5 goals.
    • Press the spacebar to start, continue after a goal, or restart at the end.

    Enhancements You Can Add

    To make this game even more advanced, consider the following improvements:

    1. Teams: Add multiple players per team with formations and roles (e.g., defenders, midfielders, forwards).
    2. Advanced AI: Implement more sophisticated AI behaviors, such as passing, tackling, and strategic positioning.
    3. Stamina System: Add a stamina mechanic to limit player speed over time.
    4. Fouls and Referee: Implement fouls, free kicks, and penalties.
    5. Multiplayer: Add support for two human players using different key bindings.
    6. Graphics and Sound: Integrate sprites for players and the ball, and add sound effects for kicks, goals, and crowd noise.
    7. Game Modes: Add different game modes, such as tournament or practice mode.

    This code provides a solid foundation for a soccer game, and with further development, it can become even more complex and realistic.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.