好吧,所以我開始玩pygame。但我有一個問題。我試圖以某種方式增強我的代碼,使其有組織,所以我決定在這裏使用類。它看起來像這樣:在Pygame中使用類
import pygame
from pygame.locals import *
import sys
pygame.init()
class MainWindow:
def __init__(self, width, height):
self.width=width
self.height=height
self.display=pygame.display.set_mode((self.width,self.height))
pygame.display.set_caption("Caption")
def background(self)
img = pygame.image.load("image.png")
self.display.blit(img, (0,0))
mainWindow = MainWindow(800,600)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.exit()
sys.exit()
mainWindow.background()
pygame.display.update()
好吧,作品。但是,如果我想要,例如用白色填充窗戶?然後,我必須定義一個方法fill()
,這將只是self.display.fill()
,對不對?有沒有辦法,通常處理它,沒有在我的班級中定義數百個pygame已經存在的方法?
還有一件事。如果我用我的類的東西,而我搞砸了,我總是得到這個消息:
File "C:/Python35/game.py", line 23, in <module>
pygame.display.update()
pygame.error
實際上我不知道到底是什麼錯誤。如果我通常這樣做,沒有班級,那麼我得到像pygame object blabla has no method blablabla
或類似的東西,我只知道發生了什麼。有沒有辦法解決這個問題,並找出發生了什麼?
在此先感謝您的幫助!
你可以設置一個定義顏色的父類,然後你的MainWindow可以從它繼承 –
是的,我知道,但我想要一個類似於你的情況,但是當MainWindow繼承像一些pygame類時,所以方法被繼承,不需要定義它們XD我猜這是不可能的:( – Frynio
爲什麼不只是將我建議的類設置爲混合? –