【问题标题】:Problems running pygame script运行 pygame 脚本的问题
【发布时间】:2018-05-10 19:24:40
【问题描述】:

目前正在尝试运行 pygame 脚本。脚本本身很好,但我认为安装给我带来了一些问题。我正在使用 mac,我的脚本位于我的桌面上。如果我想将图像加载到脚本中,我必须将其放在“/users/himansu”中。此外,我在运行它时使用的是 spyder (anaconda)。我已经尝试过终端不断出现错误:

AttributeError: 模块 'pygame' 没有属性 'init'

当我在 spyder 中点击运行时,它会加载 pygame 脚本,但是我的键没有在屏幕上移动对象,而是光标保留在 spyder 应用程序中。我知道这与发布的代码没有任何关系下面:

import pygame
import os

pygame.init()

display_width = 800
display_height = 600

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


gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('A bit Racey')
clock = pygame.time.Clock()

carImg = pygame.image.load('racecar.png')


def car(x,y):
    gameDisplay.blit(carImg,(x,y))


def game_loop():


    x = (display_width * 0.45)
    y = (display_height * 0.8)

    x_change = 0
    print(x_change)

    gameExit = False

    while not gameExit:

        #event handling loop
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                #one way to exit the loop
                gameExit = True

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5
                elif event.key == pygame.K_RIGHT:
                    x_change = 5

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0

        x += x_change
        gameDisplay.fill(white)    
        car(x,y)
        pygame.display.update()
        clock.tick(60)
game_loop()
pygame.quit() 
quit()

当我移动一些文件时,代码有效(键移动了对象),但现在它恢复正常了。我在 spyder 中的工作目录是“/users/himansu”。我觉得如果我能让这个脚本在终端中运行,它可能会起作用。谢谢你。

【问题讨论】:

  • 有时 spyder/anaconda 需要依赖安装在 pip 中的库。你有没有试过conda install pygame 任何机会?
  • @Pygasm 谢谢你的回复。可悲的是仍然面临同样的问题
  • 呃哦,听起来肯定是一些内部的东西,你可能需要做一些虚拟环境调整。
  • 我也在终端中尝试过这个命令:conda install -c cogsci pygame。我现在收到此错误:UnsatisfiableError:发现以下规范存在冲突:- pygame - xlwt 使用“conda info ”查看每个包的依赖关系。有任何想法吗?谢谢
  • 尝试删除 pip 和 conda 中的 pygame,然后仅使用 conda 重新安装。也许它对同一个地方的冲突包感到困惑。

标签: python directory pygame anaconda spyder


【解决方案1】:

代码很好,我运行它并且它工作。尝试重新安装 pygame 或将 sys 添加到导入中

import pygame, sys, os

直接下载链接---->http://pygame.org/ftp/pygame-1.9.2a0.win32-py3.2.msi

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-26
    • 2013-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多