March 13, 2026

Language game in python

In the history of technology many attempts were made to communicate with robots in natural language. Instead of a common belief, the key to success isn't located in a certain hardware or a certain software algorithm but the symbol grounding problem is at foremost a language game.

Its up the programmer to invent such a game from scratch. An easy to follow example is given here. The game engine shows a random card with a geometric object and the human user has to enter the correct word for the picture. This allows the human user to increase its score.

OF course, the game is very easy to play. The main objective is to see the game is a practical demonstration for grounded language. Its an easy to implement and easy to understand example about natural language.

# language game, version 1.0

import pygame
import random

# --- Configuration & Colors ---
WIDTH, HEIGHT = 800, 600
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
COLORS = {
    "red": (255, 0, 0),
    "blue": (0, 0, 255),
    "green": (0, 255, 0),
    "yellow": (255, 255, 0),
    "purple": (128, 0, 128)
}
SHAPES = ["circle", "square", "rectangle"]

class LanguageGame:
    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
        pygame.display.set_caption("Shape & Color Language Game")
        self.font = pygame.font.SysFont("Arial", 32)
        self.clock = pygame.time.Clock()
        
        self.score = 0
        self.user_text = ""
        self.feedback_msg = ""
        self.feedback_color = BLACK
        self.new_card()

    def new_card(self):
        """Generates a new random shape and color combination."""
        self.current_color_name = random.choice(list(COLORS.keys()))
        self.current_shape = random.choice(SHAPES)
        self.target_text = f"{self.current_color_name} {self.current_shape}"
        self.user_text = ""

    def draw_shape(self):
        """Draws the geometric object based on current selection."""
        color = COLORS[self.current_color_name]
        center = (WIDTH // 2, HEIGHT // 2 - 50)
        
        if self.current_shape == "circle":
            pygame.draw.circle(self.screen, color, center, 80)
        elif self.current_shape == "square":
            rect = pygame.Rect(0, 0, 150, 150)
            rect.center = center
            pygame.draw.rect(self.screen, color, rect)
        elif self.current_shape == "rectangle":
            rect = pygame.Rect(0, 0, 200, 100)
            rect.center = center
            pygame.draw.rect(self.screen, color, rect)

    def run(self):
        running = True
        while running:
            self.screen.fill(WHITE)
            
            # 1. Event Handling
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    running = False
                
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_RETURN:
                        # Check answer
                        if self.user_text.lower().strip() == self.target_text:
                            self.score += 10
                            self.feedback_msg = "Correct! +10"
                            self.feedback_color = (0, 150, 0)
                            self.new_card()
                        else:
                            self.feedback_msg = "Try again!"
                            self.feedback_color = (200, 0, 0)
                            self.user_text = ""
                    elif event.key == pygame.K_BACKSPACE:
                        self.user_text = self.user_text[:-1]
                    else:
                        self.user_text += event.unicode

            # 2. Drawing UI
            self.draw_shape()
            
            # Render Score
            score_surf = self.font.render(f"Score: {self.score}", True, BLACK)
            self.screen.blit(score_surf, (20, 20))
            
            # Render Input Prompt
            prompt_surf = self.font.render("Type the color and shape:", True, BLACK)
            self.screen.blit(prompt_surf, (WIDTH // 2 - 150, HEIGHT - 180))
            
            # Render User Typing
            input_box = pygame.Rect(WIDTH // 2 - 150, HEIGHT - 130, 300, 50)
            pygame.draw.rect(self.screen, BLACK, input_box, 2)
            text_surf = self.font.render(self.user_text, True, BLACK)
            self.screen.blit(text_surf, (input_box.x + 10, input_box.y + 5))
            
            # Render Feedback
            feed_surf = self.font.render(self.feedback_msg, True, self.feedback_color)
            self.screen.blit(feed_surf, (WIDTH // 2 - 50, HEIGHT - 60))

            pygame.display.flip()
            self.clock.tick(30)

        pygame.quit()

if __name__ == "__main__":
    game = LanguageGame()
    game.run()
 

No comments:

Post a Comment