1

我刚刚从它的网站( http://pygame.org/download.shtml )的下载页面下载了pygame(python模块),我选择了名为“pygame-1.9.1.win32-py2.7.msi”的包. 当我试图执行一个程序时,我得到了这个错误:

Traceback (most recent call last):
  File "C:\Users\kkkkllkk53\The Mish Mash Files\langtons ant.py", line 16, in <module>
    windowSurface = pygame.display.set_mode((window_length, window_length), 0, 32)
error: Couldn't create DIB section

我不知道这是什么意思。有人可以帮我吗?

我使用的是 64 位 Windows 7 hp 笔记本电脑。有问题的程序是试图可视化“兰顿的蚂蚁”,它是这样的:

import pygame, sys

black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)

colour_code = { True: red, False: white }

pygame.init()
mainClock = pygame.time.Clock()

cell_length = 10
num_cells_per_side = 10000

window_length = cell_length * num_cells_per_side
windowSurface = pygame.display.set_mode((window_length, window_length), 0, 32)
pygame.display.set_caption('Langtons ant')
windowSurface.fill(white)

def cell_2_rect(x, y):
    rect_x = ( 5000 + x ) * cell_length
    rect_y = ( 5000 + y ) * cell_length
    return pygame.Rect( rect_x, rect_y )

ant_x = 0
ant_y = 0
ant_angle = 1

angles = { 0: [1, 0], 1: [0, -1], 2: [-1, 0], 3: [0, 1] }

row = [ False ] * num_cells_per_side
matrix = [ row.copy() ] * num_cells_per_side

def turn_ant():
    turn = matrix[ant_y, ant_x]
    if turn:
        ant_angle += 1
    else:
        ant_angle -= 1
    ant_angle = ant_angle % 4

def move_ant():
    displacement = angles[ ant_angle ]
    delta_x = displacement[0]
    delta_y = displacement[1]
    ant_x += delta_x
    ant_y += delta_y

def update_square():
    cell = matrix[ant_x][ant_y]
    cell = not cell
    pygame.draw.rect( windowSurface, colour_code[cell], cell_2_rect(ant_x, ant_y) )

def show_ant():
    pygame.draw.rect( windowSurface, red, cell_2_rect(ant_x, ant_y) )

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    update_square()
    turn_ant()
    move_ant()
    show_ant()
    pygame.display.update()
    mainClock.tick(100)
4

1 回答 1

0

您正在尝试创建一个太大的显示:

cell_length = 10
num_cells_per_side = 10000

window_length = cell_length * num_cells_per_side
windowSurface = pygame.display.set_mode((window_length, window_length), 0, 32)

等于:(100000,100000)

尝试类似的东西(800x600)

如果你需要一个更大的世界,你可以尝试创建一个更大的表面(不是显示),然后只将你需要的内容传送到屏幕上。

于 2014-04-13T03:29:12.437 回答