Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6984489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:33:21+00:00 2026-05-27T18:33:21+00:00

My goal is to make a module that will make a grid on a

  • 0

My goal is to make a module that will make a grid on a pygame canvas and allow you to highlight boxes by their x and y coords.

Here is a simple example usage.

from grid import Grid

g = Grid(100, 100, 10) # width and height in cells, cell width in pixels
g.highlightBox(2, 2, (0, 255, 0)) # cell x and y, rgb color tuple
g.clearGrid()

Here is the code I have so far. The problem is, I need to have an event loop to keep the window open and make the close button functional, but I also need to allow the other functions to draw to the screen.

import pygame
import sys

class Grid:
    colors = {"blue":(0, 0, 255), "red":(255, 0, 0), "green":(0, 255, 0), "black":(0, 0, 0), "white":(255, 255, 255)}

    def __init__(self, width, height, cellSize, borderWidth=1):
        self.cellSize = cellSize
        self.borderWidth = borderWidth
        self.width = width * (cellSize + borderWidth)
        self.height = height * (cellSize + borderWidth)
        self.screen = pygame.display.set_mode((self.width, self.height))

        running = True
        while running:
            event = pygame.event.poll()
            if event.type == pygame.QUIT:
                running = False

    def clearGrid(self):
        pass

    def highlightBox(self, x, y, color):
        xx = x * (self.cellSize + self.borderWidth)
        yy = y * (self.cellSize + self.borderWidth)
        pygame.draw.rect(self.screen, color, (xx, yy, self.cellSize, self.cellSize), 0)

When I run the first sample, the code will be stuck in the loop, not allowing me to run the highlightBox function until the loop is done(the exit button is pressed).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:33:22+00:00Added an answer on May 27, 2026 at 6:33 pm

    I got a working version with the multiprocessing library and pipes. It seems kinda unpythonic but it will work for this project.

    import pygame
    import sys
    from multiprocessing import Process, Pipe
    
    class Grid:
        colors = {"blue":(0, 0, 255), "red":(255, 0, 0), "green":(0, 255, 0), "black":(0, 0, 0), "white":(255, 255, 255)}
    
        def __init__(self, width, height, cellSize, borderWidth=1):
            self.cellSize = cellSize
            self.borderWidth = borderWidth
            self.width = width * (cellSize + borderWidth)
            self.height = height * (cellSize + borderWidth)
    
            #pygame.draw.rect(self.screen, todo[1], (todo[2], todo[3], todo[4], todo[5]), 0)
            self.parent_conn, self.child_conn = Pipe()
            self.p = Process(target=self.mainLoop, args=(self.child_conn, self.width, self.height,))
            self.p.start()
    
        def close():
            self.p.join()
    
        def clearGrid(self):
            pass
    
        def highlightBox(self, x, y, color):
            xx = x * (self.cellSize + self.borderWidth)
            yy = y * (self.cellSize + self.borderWidth)
            self.parent_conn.send(["box", color, xx, yy, self.cellSize, self.cellSize])
    
        def mainLoop(self, conn, width, height):
            #make window
            screen = pygame.display.set_mode((self.width, self.height))
    
            running = True
            while running:
                # is there data to read
                if conn.poll():
                    #read all data
                    todo = conn.recv()
                    print("Recived " + str(todo))
    
                #do the drawing
                if todo[0] == "box":
                    print("drawing box")
                    pygame.draw.rect(screen, todo[1], (todo[2], todo[3], todo[4], todo[5]), 0) #color, x, y, width, height
                    todo = ["none"]
    
                #draw to screen
                pygame.display.flip()
    
                #get events
                event = pygame.event.poll()
                if event.type == pygame.QUIT:
                    running = False
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My goal is to make a Button that has two Content values. Imagine a
My goal is to make a layout that is 200% width and height, with
My goal here is to make a button. I want the text to sit
My goal is to make a simple TCP proxy application for my test lab.
My goal is to make a flash/as3 program that would pull multiple sets of
My goal is to make an HTML page that is similar to a photo
My goal is to make a simple GUI which almost look like this attached
My goal is to make the title of every external link equal to its
I'm re-writing alibrary with a mandate to make it totally allocation free. The goal
Goal: Make it possible to decorate class methods. When a class method gets decorated,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.