2011-11-21 3 views
3

안녕하세요, 저는 Python 및 그래픽 프로그래밍에 익숙하지 않습니다. 현재 저는 실습으로 그리드를 만드는 것을 망설이고 있습니다. 파이 게임이 표면 창 위에 객체를 놓는 데 문제가 있습니다.pygame 그래픽 그리드

다음은 내 코드입니다. 나는 문제가 blit 함수와 관련이 있을지 모르지만 나는 확신 할 수 없다. 어떤 도움을 주시면 감사하겠습니다. 또한 파이썬 쉘은 예외 나 오류를 강조 표시하지 않지만 프로그램이 실행되지 않습니다. 어떤 블록 전송이 완료되기 전에

import sys, random, pygame 
from pygame.locals import * 


def main(): 
    #settings 
    boardDims = (20,20) #number of cells on board 

    #pygame settings 
    cellDims = (20,20) #number of pixels on cells 
    framerate = 50 
    colours = {0:(0,0,0), 1:(255,255,255)}   

    #pygame 
    pygame.init() 
     #sets x and y pixels for the window 
    dims = (boardDims[0] * cellDims[0], 
      boardDims[1] * cellDims[1]) 
    #sets window, background, and framrate 
    screen = pygame.display.set_mode(dims) 
    background = screen.convert() 
    clock = pygame.time.Clock()   

    #create new board 
    board = {} 
     #iterates over x and y axis of the window 
    for x in range(boardDims[0]): 
     for y in range(boardDims[1]): 
      board[(x,y)] = 0 #sets whole board to value 0 
    board[(1,1)] = 1 #sets one square at co-ordinates x=1,y=1 to cell 
        # value 1 
    return board 


    running = 1 
    while running: 
     # 1 PYGAME 
     #get input 
     for event in pygame.event.get(): 
      if event.type == QUIT or \ 
       (event.type == KEYDOWN and event.key == K_ESCAPE): 
        running = 0 
        return 
     #link pygames clock to set framerate 
     clock.tick(framerate) 


     for cell in board: 
      #adds cells dimensions and co-ordinates to object rectangle 
      rectangle = (cell[0]*cellDims[0], cell[1]* cellDims[1], 
          cellDims[0], cellDims[1]) 
      #pygame draws the rectabgle on the background using the relevant 
      #colour and dimensions and co-ordinates outlined in 'rectangle' 
      square = pygame.draw.rect(background, colours[board[cell]], 
             rectangle) 
     #blits and displays object on the background 
     background.blit(square) 
     screen.blit(background, (0,0)) 
     pygame.display.flip() 


if __name__ == "__main__": 
    main() 

답변

1

은 "리턴 보드"문은 프로그램을 종료합니다.

또한 사각형을 화면 변수에 직접 블릿 (즉, 배경 변수가 필요 없음) 할 수 있습니다.