gpt4 book ai didi

python - Pygame 和类

转载 作者:行者123 更新时间:2023-12-02 05:00:44 24 4
gpt4 key购买 nike

所以我正在类中制作 tkinter/pygame 游戏,而 python 在类中时无法识别 pygame。(不用担心我将如何单独启动窗口,一旦窗口工作就会完成

from tkinter import *
from pygame import *
class firstscreen:
def __init__(self):
self.window = Tk()
self.window.minsize(300, 250)
self.window.resizable(width=False, height=False)
self.window.title("Login")
self.username = ""
self.password = ""
self.userlabel = Label(self.window, text="Username")
self.userlabel.place(x=50, y=50)
self.passlabel = Label(self.window, text="Password")
self.passlabel.place(x=50, y=100)
self.userentry = Entry(self.window, textvariable=self.username)
self.userentry.place(x=110, y=50)
self.passentry = Entry(self.window, textvariable=self.password, show="*")
self.passentry.place(x=110, y=100)
self.loginbutton = Button(self.window, text="Login", command = self.login())
self.loginbutton.place(x=80, y=140)
self.registerbutton = Button(self.window, text="Register",command=self.login())
self.registerbutton.place(x=140, y=140)
self.window.mainloop()
def login(self):
print("Hello")

class gamewindow():
def __init__(self):
pygame.init()
game = pygame.display.set_mode((800, 300))
runlogin = firstscreen()
rungame = gamewindow()
NameError:name 'pygame' is not defined

(第 29 行,pygame.init() )

最佳答案

必须是其中之一

from pygame import *

# [...]

class gamewindow():
def __init__(self):
init()
game = display.set_mode((800, 300))

import pygame

# [...]

class gamewindow():
def __init__(self):
pygame.init()
game = pygame.display.set_mode((800, 300))

参见The import system - Submodules

关于python - Pygame 和类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60002464/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com