пигментная графическая сетка - PullRequest
3 голосов
/ 21 ноября 2011

Здравствуйте, я новичок в Python и графическом программировании в целом.В настоящее время я занимаюсь созданием сеток на практике, и у меня возникают проблемы с получением Pygame для размещения объектов поверх окна поверхности.

Ниже мой код с комментариями.Я считаю, что проблема может быть связана с функцией blit, но я не уверен.Любая помощь будет оценена.Кроме того, оболочка Python не выделяет никаких исключений или ошибок, но программа не запускается.

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 Ответ

1 голос
/ 21 ноября 2011

Оператор return board выходит из вашей программы до того, как будет выполнено любое перетаскивание.

Кроме того, вы можете перетащить квадраты непосредственно в экранную переменную, то есть: вам не нужна фоновая переменная.

...