목표는 파이 게임 캔버스에 그리드를 만들고 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
기능을 실행할 수있게되지 않을 것입니다 (종료 버튼을 누르면).
사실, init 함수에서 루프가 발생하는 것이 가장 좋지 않을 수 있습니다. 결코 적게는,이 여전히 외부 함수에서 화면에 쓰기의 문제를 해결하지 않습니다. – giodamelio
@giodamelio 귀하는 귀하의 문제가 무엇인지 명확하게 밝히지 않았습니다. 전화해야하는 다른 기능이 있다면 잘 (루프에서) 호출하십시오. 아니면 특정 이벤트가 발생할 때만 호출되도록하고 싶다고 말하는 것입니까? 문제가 무엇인지 구체적으로 설명하십시오. –