English 中文(简体)
乳制品制图网
原标题:pygame graphics grid

Hello,我是新鲜的,可以概括地介绍节目编排。 目前,我正在以实际方式建立电网为主,在把物体放在地窗上方面,我遇到了问题。

Below is my code with comments. I reckon that the problem may be to do with the blit function but I am unsure. Any help would be appreciated. Also, the Python shell does not highlight any exceptions or errors, but the program does not run.

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()
问题回答

The "return board" statement is exiting your program before any blitting is done.

而且,你可以直接在屏幕变量上打碎广场:你不需要背景变量。





相关问题
OpenGL 3D Selection

I am trying to create a 3D robot that should perform certain actions when certain body parts are clicked. I have successfully (sort of) implemented picking in that if you click on any x-plane part, it ...

Why and when should you use hoops 3d graphics? [closed]

My company needs 3D visualization for our commercial applications (CAD, mesh manipulation, computational geometry). We are tired of true vision 3D (tv3d), which we ve been using for years (poor ...

Prerequisite for learning directx

I am from .net C# background and I want to learn DirectX. I have knowledge of C++ but I am fairly new to graphic world. I am little confused about how to start learning directx, should I start ...

Radial plotting algorithm

I have to write an algorithm in AS3.0 that plots the location of points radially. I d like to input a radius and an angle at which the point should be placed. Obviously I remember from geometry ...

Determine VRAM size on windows

I need to determine roughly how much VRAM a system s graphics card has. I know all the reasons why I shouldn t but I do. It doesn t need to be perfect (some cards lie etc.) but I need a ballpark. On ...

Mask white color out of PNG image on iPhone

I know that there are already 2 relevant posts on that, but it s not clear... So the case is this...: I have a UIImage which consists of a png file requested from a url on the net. Is it possible to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

热门标签