2017-08-05 15 views
0

私は、ゲームのビュー(スタータービュー)から画面を表示し続けるゲームビューに移行しようとすると、ゲームのpygameのMVC arquitectureを使用しています変更する代わりにメニュービュー。ここで[Pygame]:ゲームの表示は変更されません

は、ビュークラスです:

pygame.init() 

class MainDisplay: 
    # Handles current view and all it's events and acts as broker between views 
    def __init__(self, view): 
     self.current_view = view 

    def draw(self): 
     self.current_view.drawAll() 

    def functions(self): 
     self.current_view.functions() 

class MenuDisplay: 
    # Menu View 
    def __init__(self): 
     self.button = pygame.image.load("src/trans-button.png") 
     self.button = pygame.transform.scale(self.button, (display_width - 84, 70)) 
     self.start_button = self.button 
     self.tutorial_button = self.button 
     self.top10_button = self.button 
     self.salir_button = self.button 
     self.initial_pos = 220 
     self.event_i = self.event_g = self.event_t = self.event_e = False 
     self.text_color = (255,255,0) 

    def checkSelection(self, posX, startPosY, endPosY): 
     # We get the mouse coordinates 
     self.mouse_x, self.mouse_y = pygame.mouse.get_pos() 

     # Then proceed to evaluate it position in relation with the buttons 
     if(self.mouse_x >= posX and self.mouse_x <= self.button.get_width() + posX and self.mouse_y >= startPosY and self.mouse_y <= endPosY): 
      return True 

    def functions(self): 
     # We set the initial position to it's default 
     self.initial_pos = 220 

     # checkSelection(posX, startPosY, endPosY) 
     self.event_i = self.checkSelection(42, self.initial_pos, self.initial_pos + self.button.get_height()) 
     self.event_e = self.checkSelection(42, self.initial_pos + self.button.get_height()*3 + 75, self.initial_pos + self.button.get_height()*4 + 75) 

     if self.event_i: 
      print 'Begin' 
      current_display = GameDisplay() 
     if self.event_e: 
      pygame.quit() 
      quit() 
      sys.exit() 

     self.event_i = self.event_g = self.event_t = self.event_e = False 

    def drawButtons(self): 
     # Draws the button elements from the Menu view 
     self.initial_pos = 220 

     window.blit(self.start_button, (42,self.initial_pos)) 
     window.blit(self.tutorial_button, (42,self.initial_pos + self.button.get_height() + 25)) 
     window.blit(self.top10_button, (42,self.initial_pos + self.button.get_height()*2 + 50)) 
     window.blit(self.salir_button, (42,self.initial_pos + self.button.get_height()*3 + 75)) 

    def drawText(self): 
     # Draws the texts elements for the buttons 
     self.initial_pos = 220 + 25 

     # Setting Text 
     self.start_text = 'Inicar Partida' 
     self.tutorial_text = 'Como Jugar' 
     self.top10_text = 'Top 10' 
     self.salir_text = 'Salir del Juego' 

     # Setting Font 
     self.myfont = pygame.font.SysFont("Verdana", 19) 
     self.myfont.set_bold(True) 

     # Render Text 
     self.start = self.myfont.render(self.start_text, 1, self.text_color) 
     self.tutorial = self.myfont.render(self.tutorial_text, 1, self.text_color) 
     self.top10 = self.myfont.render(self.top10_text, 1, self.text_color) 
     self.salir = self.myfont.render(self.salir_text, 1, self.text_color) 

     # Ajustamos la posicion XY del texto para que este en el medio del boton 
     self.textPosX = (display_width/2) - (self.start.get_width()/2) - 21 
     self.textPosY = self.initial_pos - 5 

     window.blit(self.start, (self.textPosX,self.textPosY)) 

     self.textPosX = (display_width/2) - (self.tutorial.get_width()/2) - 21 

     window.blit(self.tutorial, (self.textPosX, self.initial_pos + self.button.get_height() + 25 -5)) 

     self.textPosX = (display_width/2) - (self.top10.get_width()/2) - 21 

     window.blit(self.top10, (self.textPosX, self.initial_pos + self.button.get_height()*2 + 50 -5)) 

     self.textPosX = (display_width/2) - (self.salir.get_width()/2) - 21 

     window.blit(self.salir, (self.textPosX, self.initial_pos + self.button.get_height()*3 + 75 -5)) 

    def drawAll(self): 
     # Executes all the draw functions 
     self.drawButtons() 
     self.drawText() 


class GameDisplay(): 
    # Game view 
    def __init__(self): 
     print 'Hello World' 
     self.button = pygame.image.load("src/trans-button.png") 
     self.button = pygame.transform.scale(self.button, ((display_width/2) - 40, 65)) 
     self.return_button = pygame.transform.scale(self.button, (75, 20)) 
     self.event_r = False 

    def drawButtons(self): 

     self.initial_pos_y = 0 
     self.initial_pos_x = display_width - 75 

     window.blit(self.return_button, (initial_pos_y, initial_pos_x)) 

    def drawAll(self): 
     # Executes all the draw functions 
     self.drawButtons() 

    def checkSelection(self, posX, startPosY, endPosY): 
     # We get the mouse coordinates 
     self.mouse_x, self.mouse_y = pygame.mouse.get_pos() 

     # Then proceed to evaluate it position in relation with the buttons 
     if(self.mouse_x >= posX and self.mouse_x <= self.button.get_width() + posX and self.mouse_y >= startPosY and self.mouse_y <= endPosY): 
      return True 

    def functions(self): 
     # We set the initial position to it's default 
     self.initial_pos_y = 0 
     self.initial_pos_x = display_width - 75 

     # checkSelection(posX, startPosY, endPosY) 
     self.event_r = self.checkSelection(initial_pos_x, self.initial_pos_y, self.initial_pos_y - self.return_button.get_height()) 

私は将来的にはさらに追加する予定以来MainDisplayクラスは、各ビュー間の遷移を処理を担当します。アプリケーションが起動すると、パラメータとしてスタータビューを受け取るcurrent_displayという新しいMainDisplayを作成します。

# Setting up Display 
current_display = MainDisplay(MenuDisplay()) 

実行されるのはメインループです。

最初のビューは魅力のように機能します。

ユーザーが開始ボタンをクリックすると、次のビューに移動する必要があります。クリックイベントは、マウスの位置がボタンの位置と衝突したかどうかをチェックします。

def functions(self): 
    # We set the initial position to it's default 
    self.initial_pos = 220 

    # checkSelection(posX, startPosY, endPosY) 
    self.event_i = self.checkSelection(42, self.initial_pos, self.initial_pos + self.button.get_height()) 
    self.event_e = self.checkSelection(42, self.initial_pos + self.button.get_height()*3 + 75, self.initial_pos + self.button.get_height()*4 + 75) 

    if self.event_i: 
     print 'Begin' 
     current_display = GameDisplay() 
    if self.event_e: 
     pygame.quit() 
     quit() 
     sys.exit() 

    self.event_i = self.event_g = self.event_t = self.event_e = False 

def checkSelection(self, posX, startPosY, endPosY): 
     # We get the mouse coordinates 
     self.mouse_x, self.mouse_y = pygame.mouse.get_pos() 

     # Then proceed to evaluate it position in relation with the buttons 
     if(self.mouse_x >= posX and self.mouse_x <= self.button.get_width() + posX and self.mouse_y >= startPosY and self.mouse_y <= endPosY): 
      return True 

イベントが確実に発生するように、私はデバッグ用のプリントを追加しました。 GameDisplay __ init __は動作しますが、 'Hello World'メッセージは表示されますが、UIビューは変更されません。

はここで完全なコードです:

私は)

どうもありがとうマーティンが欠落していたため、エラーだった

import pygame 
import sys 
from random import randint 

pygame.init() 

class MainDisplay: 
    # Handles current view and all it's events and acts as broker between views 
    def __init__(self, view): 
     self.current_view = view 

    def draw(self): 
     self.current_view.drawAll() 

    def functions(self): 
     self.current_view.functions() 

class MenuDisplay: 
    # Menu View 
    def __init__(self): 
     self.button = pygame.image.load("src/trans-button.png") 
     self.button = pygame.transform.scale(self.button, (display_width - 84, 70)) 
     self.start_button = self.button 
     self.tutorial_button = self.button 
     self.top10_button = self.button 
     self.salir_button = self.button 
     self.initial_pos = 220 
     self.event_i = self.event_g = self.event_t = self.event_e = False 
     self.text_color = (255,255,0) 

    def checkSelection(self, posX, startPosY, endPosY): 
     # We get the mouse coordinates 
     self.mouse_x, self.mouse_y = pygame.mouse.get_pos() 

     # Then proceed to evaluate it position in relation with the buttons 
     if(self.mouse_x >= posX and self.mouse_x <= self.button.get_width() + posX and self.mouse_y >= startPosY and self.mouse_y <= endPosY): 
      return True 

    def functions(self): 
     # We set the initial position to it's default 
     self.initial_pos = 220 

     # checkSelection(posX, startPosY, endPosY) 
     self.event_i = self.checkSelection(42, self.initial_pos, self.initial_pos + self.button.get_height()) 
     self.event_e = self.checkSelection(42, self.initial_pos + self.button.get_height()*3 + 75, self.initial_pos + self.button.get_height()*4 + 75) 

     if self.event_i: 
      print 'Begin' 
      current_display = GameDisplay() 
     if self.event_e: 
      pygame.quit() 
      quit() 
      sys.exit() 

     self.event_i = self.event_g = self.event_t = self.event_e = False 

    def drawButtons(self): 
     # Draws the button elements from the Menu view 
     self.initial_pos = 220 

     window.blit(self.start_button, (42,self.initial_pos)) 
     window.blit(self.tutorial_button, (42,self.initial_pos + self.button.get_height() + 25)) 
     window.blit(self.top10_button, (42,self.initial_pos + self.button.get_height()*2 + 50)) 
     window.blit(self.salir_button, (42,self.initial_pos + self.button.get_height()*3 + 75)) 

    def drawText(self): 
     # Draws the texts elements for the buttons 
     self.initial_pos = 220 + 25 

     # Setting Text 
     self.start_text = 'Inicar Partida' 
     self.tutorial_text = 'Como Jugar' 
     self.top10_text = 'Top 10' 
     self.salir_text = 'Salir del Juego' 

     # Setting Font 
     self.myfont = pygame.font.SysFont("Verdana", 19) 
     self.myfont.set_bold(True) 

     # Render Text 
     self.start = self.myfont.render(self.start_text, 1, self.text_color) 
     self.tutorial = self.myfont.render(self.tutorial_text, 1, self.text_color) 
     self.top10 = self.myfont.render(self.top10_text, 1, self.text_color) 
     self.salir = self.myfont.render(self.salir_text, 1, self.text_color) 

     # Ajustamos la posicion XY del texto para que este en el medio del boton 
     self.textPosX = (display_width/2) - (self.start.get_width()/2) - 21 
     self.textPosY = self.initial_pos - 5 

     window.blit(self.start, (self.textPosX,self.textPosY)) 

     self.textPosX = (display_width/2) - (self.tutorial.get_width()/2) - 21 

     window.blit(self.tutorial, (self.textPosX, self.initial_pos + self.button.get_height() + 25 -5)) 

     self.textPosX = (display_width/2) - (self.top10.get_width()/2) - 21 

     window.blit(self.top10, (self.textPosX, self.initial_pos + self.button.get_height()*2 + 50 -5)) 

     self.textPosX = (display_width/2) - (self.salir.get_width()/2) - 21 

     window.blit(self.salir, (self.textPosX, self.initial_pos + self.button.get_height()*3 + 75 -5)) 

    def drawAll(self): 
     # Executes all the draw functions 
     self.drawButtons() 
     self.drawText() 


class GameDisplay(): 
    # Game view 
    def __init__(self): 
     print 'Hello World' 
     self.button = pygame.image.load("src/trans-button.png") 
     self.button = pygame.transform.scale(self.button, ((display_width/2) - 40, 65)) 
     self.return_button = pygame.transform.scale(self.button, (75, 20)) 
     self.event_r = False 

    def drawButtons(self): 

     self.initial_pos_y = 0 
     self.initial_pos_x = display_width - 75 

     window.blit(self.return_button, (initial_pos_y, initial_pos_x)) 

    def drawAll(self): 
     # Executes all the draw functions 
     self.drawButtons() 

    def checkSelection(self, posX, startPosY, endPosY): 
     # We get the mouse coordinates 
     self.mouse_x, self.mouse_y = pygame.mouse.get_pos() 

     # Then proceed to evaluate it position in relation with the buttons 
     if(self.mouse_x >= posX and self.mouse_x <= self.button.get_width() + posX and self.mouse_y >= startPosY and self.mouse_y <= endPosY): 
      return True 

    def functions(self): 
     # We set the initial position to it's default 
     self.initial_pos_y = 0 
     self.initial_pos_x = display_width - 75 

     # checkSelection(posX, startPosY, endPosY) 
     self.event_r = self.checkSelection(initial_pos_x, self.initial_pos_y, self.initial_pos_y - self.return_button.get_height()) 

# Display Settings 
display_width = 830 
display_height = 625 

window = pygame.display.set_mode((display_width, display_height)) 

pygame.display.set_caption("This is my Game") 

clock = pygame.time.Clock() 

# Background Settings 
background = pygame.image.load("src/background.jpg") 
background = pygame.transform.scale(background, (display_width, display_height)) 

# Setting up Display 
current_display = MainDisplay(MenuDisplay()) 

crashed = False 

# Game Loop 
while not crashed: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      crashed = True 
     if event.type == pygame.MOUSEBUTTONDOWN: 
      current_display.functions() 

    window.blit(background, (0,0)) 
    current_display.draw() 
    pygame.display.update() 
    clock.tick(60) 

#End Game 
pygame.quit() 
quit() 
sys.exit() 
+1

MainDisplayクラスを再度初期化するのではなく、GameDisplayをパラメータとして渡すupdateViewsという関数を追加しました。 –

+2

あなたの質問は答えとしてではなく、コメントとして答えてください。 – ppperry

答えて

0

関連する問題