我为我的个人 Python 项目写了一个简单的(我认为是这样的)星空动画。就在游戏进入“可玩”阶段的时候,我决定进行另一个项目;使用 Windows/Ubuntu 双引导进行实验。虽然 Windows/Ubuntu 实验的细节是一个真正的翻页,但相关问题更多的是“为什么这段代码在 Windows XP 上运行良好,但在 Ubuntu 12.04 上导致各种崩溃”?
此代码是使用 Pygame 1.9 为 Python 2.7 编写的。
import pygame, random, sys
from pygame import *
pygame.init()
width = 400
height = 400
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
DISPLAYSURF = pygame.display.set_mode((width, height))
stars = 50
starfield = []
for i in range(stars):
x = random.randrange(0, width)
y = random.randrange(0, height)
starfield.append([x, y])
clock = pygame.time.Clock()
FPS = 60
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
DISPLAYSURF.fill(BLACK)
starCounter = 0
for star in starfield:
starCounter += 1
x, y = star
starDraw = pygame.PixelArray(DISPLAYSURF)
starDraw[x][y] = WHITE
del starDraw
#fake parallax scrolling effect
star[1] += 1
if starCounter % 3 == 1:
star[1] += 1
if starCounter % 5 == 1:
star[1] += 1
if star[1] > height:
star[0] = random.randrange(0, width)
star[1] = 0
最终我通过用 surface.set_at() 替换 PixelArray 废话解决了这个问题,并删除了所有与实例化 PixelArray 对象相关的代码。但这不仅仅是(Pygame 降落伞品种的)段错误,我会收到各种奇怪的错误消息;关于 gcclibrary、关于 malloc() 等的一些东西。请记住,我之前没有使用任何 IDLE 或类似的东西;我总是从命令行和/或终端运行一切。
我以前遇到的那种错误不一定与我有关;事实上,Windows 只会让他们沉默,而 Ubuntu 则非常乐意指出并嘲笑他们。显然,这一定是 PEBKAC 的结果,但是 Windows 到底为什么要对我隐瞒这些信息呢?在这个论点的另一面;在 Ubuntu 下编码会原谅另一种坏习惯吗?如果 Windows 使某些类型的段错误保持沉默,那么 Ubuntu 是否会捕获一组不同的错误,我是否真的需要注意每个主要操作系统可能决定捕获或不捕获的所有不同的愚蠢错误?我只在这方面工作了大约一年,所以有很多信息我只是曝光不足并且不知道。
我的意思是,我想如果你想告诉我旧代码的错误,以防我觉得创建 PixelArray 对象是我将来需要做的事情,我也不介意。:)