【问题标题】:How to use pygame module?如何使用 pygame 模块?
【发布时间】:2019-11-02 15:23:36
【问题描述】:
import pygame
import time
import datetime
cup=int(input("please enter how many cups you want to drink in a day: "))
interval=int(input("please enter after how many minutes we should remind you: "))
waterat=time.time() #gives some random time which will help we know the duration
intervalsec=interval*60
if (cup>0):
    if (time.time()-waterat)>intervalsec:
        print("DRINK WATER!!!!!!")
        while (true):
            pygame.mixer.music.load('water.mp3')
            pygame.mixer.music.play(-1)
            #plays infinite time
            word=input("enter done if you done drinking water: ")
            if (word.lower=="done"):
                cup=cup-1
                waterat=time.time()
                f=open("drinktime.txt","a")
                f.write(datetime.datetime.now())
                #datetime function is inside datetime module
                break
time.sleep(60)
#program sleep for 60 sec

我得到的错误是-

Traceback (most recent call last):
  File "C:/Users/Nishant/Desktop/practice/water reminder/drink.py", line 1, in <module>
    import pygame
  File "C:\Users\Nishant\AppData\Roaming\Python\Python37\site-packages\pygame\__init__.py", line 120, in <module>
    from pygame.base import *
ModuleNotFoundError: No module named 'pygame.base'

【问题讨论】:

    标签: python pip pygame installation


    【解决方案1】:

    你的问题做得不好。您应该向我们提供更多详细信息,以便我们更好地回答您。您是否正确安装了pygame?你有什么版本的 Python?

    我复制了你的代码,它被解释得很好。这意味着您根本没有正确导入 pygame。

    我想你会在这个帖子中找到答案:

    Pygame "No module called pygame.base"

    【讨论】:

    • 我有 python 3.7.3,当我使用 python IDLE 运行它时,它需要输入但不播放音乐。当我使用 pycharm 运行它时,它会给出该错误。我已经安装了 pygame 1.9.6。我一次又一次尝试重新安装pygame,但没有任何效果/
    • 好的,我已经配置了python IDLE,所以我会坚持使用这个。我测试了你的代码,但它似乎在“if ((time.time()-waterat)>intervalsec):”之后不起作用。它会跳过第二个 if 中的所有代码。这可能是与您的条件语句而不是 pygame 相关的问题。
    • 我用确切的语句制作了另一个版本,但我删除了声音播放部分(因为 pygame 错误)。它工作正常。我不知道如何正确使用pygame,但是即使我的声音播放部分的语法错误,它也会打印出喝水。
    • 我建议您遵循 pygame 的教程。只需在 Google 上搜索并享受创建游戏的乐趣!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-20
    • 1970-01-01
    • 1970-01-01
    • 2018-08-05
    • 2020-11-27
    • 2020-05-13
    相关资源
    最近更新 更多