在python和pygam中使用类

2024-06-16 14:26:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个脚本的游戏,我刚开始,一个是主游戏,另一个是要显示3个图像时,第一个脚本运行,所以当我运行第一个脚本(主游戏.py)它应该使用第二个脚本的类和函数运行(启动.py)然后启动程序会显示一个带有游戏标题的图像和一个由屏幕创建的图像,然后我会把它放在游戏菜单上。但它当然不起作用,这里是我的主要游戏代码:

import pygame, random, math, sys, os, time
import startUp
from pygame.locals import *
pygame.init()

while True:

    #quit button 
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    startUp.Begin.Run()

第二个脚本的代码(启动.py)公司名称:

^{pr2}$

如果出现以下错误消息,则会有所帮助:

第一个脚本:

Traceback (most recent call last):
  File "C:\Users\claude\Desktop\game\dnagame.py", line 2, in <module>
    import startUp
  File "C:\Users\claude\Desktop\game\startUp.py", line 5, in <module>
    class Begin(self):
NameError: name 'self' is not defined

第二个:

Traceback (most recent call last):
  File "C:/Users/claude/Desktop/game/startUp.py", line 5, in <module>
    class Begin(self):
NameError: name 'self' is not defined

感谢您的时间和帮助!在


Tags: inpy图像importself脚本event游戏
2条回答
class Begin(self):

您应该从object继承

^{pr2}$

您还需要创建一个实例

begin = startUp.Begin()
begin.Run()

类必须从object而不是self继承:

class Begin(object):

self是实例方法的第一个参数使用的约定名称,它是对实例本身的引用。在

相关问题 更多 >