“模块‘pygame’没有属性‘init’"运行一个简单的 pygame 脚本 [英] "module 'pygame' has no attribute 'init'" running a simple pygame script

查看:97
本文介绍了“模块‘pygame’没有属性‘init’"运行一个简单的 pygame 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行我的 pygame 代码时,出现以下错误:

<预><代码>>>>重启:C:/Users/lanra/Desktop/2018 backups/2018 python/pygame/pygame 2.py回溯(最近一次调用最后一次):文件C:/Users/lanra/Desktop/2018 backups/2018 python/pygame/pygame 2.py",第 1 行,导入pygame文件C:/Users/lanra/Desktop/2018 backups/2018 python/pygame\pygame.py",第 3 行, 中pygame.init()AttributeError: 模块pygame"没有属性init"

我的代码:

导入pygamepygame.init()赢 = pygame.display.set_mode((500,500))pygame.display.set_caption("第一场比赛")x = 50y = 50宽度 = 40高度 = 60vel = 5运行=真运行时:pygame.time.delay(100)对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:运行 = 错误pygame.draw.rect(获胜,(255,0,0))pygame.quit()

解决方案

看起来您的测试脚本本地有一个名为 pygame.py 的脚本.这是导入而不是库.

修复方法是重命名您的本地 pygame.py 脚本(这可能是您正在研究 Pygame 的另一个版本),以免发生冲突.通常,请避免将项目文件命名为与您使用的库相同的名称.

您的代码中还有其他错误(请阅读 Pygame 文档和示例),但这将是您需要应用的第一个修复程序,并且它并非特定于 Pygame.

这是您的代码的工作版本:

导入pygamepygame.init()赢 = pygame.display.set_mode((500,500))pygame.display.set_caption("第一场比赛")x = 50y = 50宽度 = 40高度 = 60vel = 5为真:pygame.time.delay(100)对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:pygame.quit()放弃()pygame.draw.rect(获胜,(255,0,0),win.get_rect())pygame.display.update()

注意pygame.draw.rect 的额外必需参数并调用pygame.display.update().还修改了 while 循环,因为一旦你调用了 pygame.quit() 你不想调用像 pygame.event.get() 这样的东西,否则你会得到关于无视频系统的错误消息.

When i run my pygame code, i get the following error:

>>> 
 RESTART: C:/Users/lanra/Desktop/2018 backups/2018 python/pygame/pygame 2.py 
Traceback (most recent call last):
  File "C:/Users/lanra/Desktop/2018 backups/2018 python/pygame/pygame 2.py", line 1, in <module>
    import pygame
  File "C:/Users/lanra/Desktop/2018 backups/2018 python/pygame\pygame.py", line 3, in <module>
    pygame.init()
AttributeError: module 'pygame' has no attribute 'init'

My code:

import pygame
pygame.init()

win = pygame.display.set_mode((500,500))

pygame.display.set_caption("first game")

x = 50
y = 50
width = 40
height = 60
vel = 5

run= True
while run:
    pygame.time.delay(100)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False


    pygame.draw.rect(win, (255,0,0))

    pygame.quit()

解决方案

Looks like you have a script called pygame.py locally to your test script. This is getting imported instead of the library.

The fix is to rename your local pygame.py script (which is presumably another version of this one as you are figuring out Pygame) so it does not conflict. In general, avoid naming your project files the same as the libraries that you are using.

You also have other errors in your code (please read the Pygame documentation and examples), but this will be the first fix you need to apply, and it is not specific to Pygame.

Here is a working version of your code:

import pygame
pygame.init()

win = pygame.display.set_mode((500,500))

pygame.display.set_caption("first game")

x = 50
y = 50
width = 40
height = 60
vel = 5

while True:
    pygame.time.delay(100)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

    pygame.draw.rect(win, (255,0,0), win.get_rect())

    pygame.display.update()

Note the extra required param to pygame.draw.rect and call to pygame.display.update(). Also modified the while loop, as once you have called pygame.quit() you don't want to call things like pygame.event.get() else you will get error messages about no video system.

这篇关于“模块‘pygame’没有属性‘init’"运行一个简单的 pygame 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆