2016-02-26 58 views
0

好吧,所以我開始玩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或類似的東西,我只知道發生了什麼。有沒有辦法解決這個問題,並找出發生了什麼?

在此先感謝您的幫助!

+0

你可以設置一個定義顏色的父類,然後你的MainWindow可以從它繼承 –

+0

是的,我知道,但我想要一個類似於你的情況,但是當MainWindow繼承像一些pygame類時,所以方法被繼承,不需要定義它們XD我猜這是不可能的:( – Frynio

+0

爲什麼不只是將我建議的類設置爲混合? –

回答

4

你在這裏做什麼是正確的軌道,但它是做錯了方式。你的主要「遊戲循環」應該作爲一種方法在類本身內部,而不是在實際循環中從課堂外調用東西。這是你應該做的一個基本例子。

#Load and initialize Modules here 
import pygame 
pygame.init() 

#Window Information 
displayw = 800 
displayh = 600 
window = pygame.display.set_mode((displayw,displayh)) 

#Clock 
windowclock = pygame.time.Clock() 

#Load other things such as images and sound files here 
image = pygame.image.load("foo.png").convert #Use conver_alpha() for images with transparency 

#Main Class 
class MainRun(object): 
    def __init__(self,displayw,displayh): 
     self.dw = displayw 
     self.dh = displayh 
     self.Main() 

    def Main(self): 
     #Put all variables up here 
     stopped = False 

     while stopped == False: 
      window.fill((255,255,255)) #Tuple for filling display... Current is white 

      #Event Tasking 
      #Add all your event tasking things here 
      for event in pygame.event.get(): 
       if event.type == pygame.QUIT: 
        pygame.quit() 
        quit() 
       elif event.type == pygame.KEYDOWN: 
        stopped = True 

      #Add things like player updates here 
      #Also things like score updates or drawing additional items 
      #Remember things on top get done first so they will update in the order yours is set at 

      #Remember to update your clock and display at the end 
      pygame.display.update() 
      windowclock.tick(60) 

     #If you need to reset variables here 
     #This includes things like score resets 

    #After your main loop throw in extra things such as a main menu or a pause menu 
    #Make sure you throw them in your main loop somewhere where they can be activated by the user 

#All player classes and object classes should be made outside of the main class and called inside the class 
#The end of your code should look something like this 
if __name__ == __main__: 
    MainRun() 

主循環將調用本身創建對象MainRun()時。

如果您需要更多關於特定事物的例子,例如對象處理,請告訴我,我會看看我是否可以爲您提供更多信息。

我希望這可以幫助您完成您的編程,並祝您好運。編輯==================== ============

在這種情況下,這些特殊操作使它們成爲特定對象。而不是使用一種通用的方法blit你的對象,使每個對象都有自己的功能。這樣做是爲了讓您製作的每個對象具有更多的選項。代碼的一般想法如下...我在這裏創建了一個簡單的播放器對象。

#Simple player object 
class Player(object): 
    def __init__(self,x,y,image): 
     self.x = x 
     self.y = y 
     self.image = image 

    #Method to draw object 
    def draw(self): 
     window.blit(self.image,(self.x,self.y)) 

    #Method to move object (special input of speedx and speedy) 
    def move(self,speedx,speedy): 
     self.x += speedx 
     self.y += speedy 

現在這裏是你如何使用對象的方法...我已經包括一個事件循環,以幫助顯示如何使用移動功能。只要將此代碼添加到主循環中,無論它在哪裏,都將被設置。

#Creating a player object 
player = Player(0,0,playerimage) 

#When you want to draw the player object use its draw() method 
player.draw() 

#Same for moving the player object 
#I have included an event loop to show an example 
#I used the arrow keys in this case 
speedx = 0 
speedy = 0 

for event in pygame.event.get(): 
    if event.type == pygame.KEYDOWN: 
     if event.key == pygame.K_UP: 
      speedy = -5 
      speedx = 0 
     elif event.key == pygame.K_DOWN: 
      speedy = 5 
      speedx = 0 
     elif event.key == pygame.K_RIGHT: 
      speedy = 0 
      speedx = 5 
     elif event.key == pygame.K_LEFT: 
      speedy = 0 
      speedx = -5 
    elif event.type == pygame.KEYUP: 
     speedx = 0 
     speedy = 0 

#Now after you event loop in your object updates section do... 
player.move(speedx,speedy) 
#And be sure to redraw your player 
player.draw() 

#The same idea goes for other objects such as obstacles or even scrolling backgrounds 

確保在繪圖函數中使用相同的顯示名稱。

+0

好的,謝謝。但我想要這樣的東西:所以我有我的課,但你已經創建了一個MainRun()類。我希望它像MainWindow(),因爲它會像:awkay,'mainWindow = MainWindow(800,600)',得到我的窗口對象,現在可以爲它設置一個標題'mainWindow.caption(「我的窗口」)' ,好吧,現在讓我們把一個背景圖片放到它的'mainWindow.background(「image.png」)'。而'background()'方法仍然會調用pygame類的方法 - load,然後調用blit()。即使這些方法存在於pygame中,我仍然需要在我的類中定義它們,並且我不想這樣做 – Frynio

+0

好的,我已經更新了=== EDIT ===部分下的答案,以顯示如何使用其他對象在你的遊戲裏面。 – TacoTree11