初学python,运行pygame示例出错

cejay 2010-05-10 10:11:50
Python安装的是Python 3.1,pygame安装的是pygame-1.9.1.win32-py3.1.msi。不用pygame直接写Python脚本正常,但是pygame的示例就是不行,提示:
Traceback (most recent call last):
File "G:\python\pygame.py", line 1, in <module>
import sys,pygame,time
File "G:\python\pygame.py", line 2, in <module>
pygame.init()
AttributeError: 'module' object has no attribute 'init'



代码:

import sys,pygame,time
pygame.init()

size = width, height = 320, 240
speed = [2, 2]
black = 0, 0, 0
screen = pygame.display.set_mode(size)
ball = pygame.image.load("ball.gif")
ballrect = ball.get_rect()
running = 1
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = 0
ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]
screen.fill(black)
screen.blit(ball, ballrect)
pygame.display.flip()
time.sleep(0.03)

pygame.quit()




是不是pygame-1.9.1中没有init()了,去搜索引擎也没找到答案,求解
...全文
722 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
amu9900 2010-05-19
  • 打赏
  • 举报
回复
我这是没问题的,你按照这方法去查一下:

找到 PYTHON-SITEROOT\Lib\site-packages\pygame\ 这个目录(PYTHON-SITEROOT是python的安装目录)看下面有没有除__init__.py之外的文件,然后寻找一个叫base.pyd的文件,因为,你在做了import pygame的时候其实是导入的这个文件,你调用pygame.init()也是调用的pygame.base.init(),如果这个文件不存在,那肯定会
AttributeError: 'module' object has no attribute 'init'的

如果没问题,去查看下tests\base_test.py 有没有这方法:
def __PYGAMEinit__(): #called automatically by pygame.init()

如果没有,证明你的pygame装错了~看安装文档oncemore~
龙根 2010-05-13
  • 打赏
  • 举报
回复
3x和2x是不同系列的版本,差别很大的,现在大多是用2.x的,你换版本吧
cejay 2010-05-11
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 winbsd 的回复:]

版本的问题吧,试下低版本
[/Quote]
应该是,就是不知道现在版本的方法是怎样的了,我刚开始学Python,就想从Python3开始学吧,毕竟学了Python2以后可能Python3的东西会变多还得再学。
cejay 2010-05-11
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 thy38 的回复:]

第二行加上Python code
from pygame.locals import *
[/Quote]
加上后还是出错:
Traceback (most recent call last):
File "G:\python\pygame.py", line 1, in <module>
import sys,pygame,time
File "G:\python\pygame.py", line 2, in <module>
from pygame.locals import *
ImportError: No module named locals
dongnanyanhai 2010-05-11
  • 打赏
  • 举报
回复
帮顶,不知原因!
thy38 2010-05-11
  • 打赏
  • 举报
回复
第二行加上
from pygame.locals import *
CDSoftwareWj 2010-05-11
  • 打赏
  • 举报
回复
Py2.x的稳定,出来的时间长,支持的第三方库好用
winbsd 2010-05-11
  • 打赏
  • 举报
回复
版本的问题吧,试下低版本
cejay 2010-05-11
  • 打赏
  • 举报
回复
自己向上顶顶,期盼懂得的人看见帖子
cejay 2010-05-10
  • 打赏
  • 举报
回复
求助

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧