pygame类结构

时间:2011-12-22 17:17:01

标签: python refactoring pygame

我的目标是创建一个模块,在pygame画布上制作网格,并允许您通过x和y坐标突出显示框。

这是一个简单的示例用法。

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()

这是我到目前为止的代码。问题是,我需要一个事件循环来保持窗口打开并使关闭按钮起作用,但我还需要允许其他功能绘制到屏幕上。

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)

当我运行第一个样本时,代码将卡在循环中,不允许我运行highlightBox函数,直到循环完成(按下退出按钮)。

3 个答案:

答案 0 :(得分:1)

对于初学者,我不会把游戏循环放在初始化函数中;找到一些其他的地方。要解决这个问题,只需将要执行的代码放在游戏循环中,旁边是处理事件的代码:

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

    # Print your screen in here
    # Also do any other stuff that you consider appropriate

答案 1 :(得分:1)

我认为你需要的是断开Grid类与它的显示。你可以让它生成表面,它将通过主游戏循环打印到屏幕Surface。您的 init ,highlight_cell和clear_grid方法可以返回Surfaces,或者创建一个每个游戏循环调用一次的get_surface方法

这将提供更大的灵活性

答案 2 :(得分:0)

我使用multiprocessing库和管道获得了一个工作版本。它似乎有点不合理,但它适用于这个项目。

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