我在pygame中使用MVC arquitecture进行游戏,但是当我尝试从菜单视图(起始视图)转换到游戏视图时,屏幕将继续显示菜单视图而不是更改。 / p>
以下是观点类:
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())
执行的是主循环。
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()
第一个视图就像一个魅力。
当用户点击开始按钮时,它应该转到下一个视图,我处理它的点击事件,检查鼠标位置是否与按钮位置发生碰撞。
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 __确实有效,因为&#39; Hello World&#39;消息确实显示,但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()
答案 0 :(得分:0)
错误是因为我错过了a)
非常感谢马丁。