2014-02-25 22 views
0

我最近開始用python和pygame做一個遊戲,我有一個主腳本,其中所有其他腳本導入,然後它運行它們,但我有兩個主要問題。首先是當我在主腳本中運行第一個腳本時,它似乎在該類之後停止,並且不繼續其他腳本。使用類在不同的腳本與python和pygame

我想要的遊戲做的:

展示你的照片作爲片頭。 (工作)

然後一旦學分完成啓動一個菜單,此刻這只是一個播放按鈕和一個Rect,它會有一個碰撞響應來檢測玩家是否點擊了它。 (不工作)

這裏是我的腳本:

RUN.py(一起運行一切遊戲主):

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

#classes setup 
Begin = startUp.Begin() 
Menu = Menu.HUD() 
while True: 

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



    #show 3 pics for the opening credits 
    Begin.Run() 

    #bring up the menu once the opening credits are done 
    Menu.Run() 

然後Menu.py(該遊戲的菜單)

import pygame, time, random 
from pygame.locals import * 
pygame.init() 

class HUD(object): 

    def __init__(object): 
     object.playButtonUp = "playButtonUp.png" 
     object.playButtonUpHover = "playButtonUpHover.png" 
     object.playButtonDown = "playButtonDown.png" 

     object.Black = (0, 0, 0) 
     object.Red = (0, 255, 0) 
     object.screen = pygame.display.set_mode((1440, 720), 0, 32) 

     object.playButtonUp = pygame.image.load(object.playButtonUp).convert_alpha() 
     object.playButtonUpHover = pygame.image.load(object.playButtonUpHover).convert_alpha() 
     object.playButtonDown = pygame.image.load(object.playButtonDown).convert_alpha() 

    def Run(object): 
     #object.screen.fill(object.Black) 

     #creat rects 
     object.playButtonRect = Rect(464, 232, 256, 128) 

     #blit imagers 
     object.screen.blit(object.playButtonUp, (464, 232)) 

有沒有錯誤信息的,它似乎工作,但圖像不顯示在屏幕上。

感謝您的時間和幫助!

+0

是否'Begin.Run()'結束?順便說一句,[標準的Python命名約定](http://legacy.python.org/dev/peps/pep-0008/#method-names-and-instance-variables)是爲函數和方法名使用小寫,和CapWords的類名稱。 – LeartS

+0

我不熟悉'pygame',但可以兩次調用'pygame.init()'嗎?你可以在'Menu.py'中重複這個。 – Alfe

+0

那麼這就是我需要做的,開始。運行()結束,但如何? – user3342999

回答

1

您的結構不正確。你已經合併了兩種不同的方法。

你有一個運行腳本,應該先顯示3張圖片,然後顯示菜單。

現在,您有一個while循環,調用Begin和Menu的運行功能,直到關閉遊戲。

不將文件應該是這樣的:

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

#classes setup 
Begin = startUp.Begin() 
Begin.run() 

Menu = Menu.HUD() 
Menu.run() 

Menu.py

class HUD(object): 
    def run(object): 
     while True: 
      object.playButtonRect = Rect(464, 232, 256, 128) 
      object.screen.blit(object.playButtonUp, (464, 232)) 
      for event in pygame.event.get(): 
       if event.type == QUIT: 
        pygame.quit() 
        sys.exit() 
相關問題