1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| class MainView(View):
logo = None btn_start = None btn_exit = None context = None
def __init__(self, context): self.btn_start = Button('Start Game', self._on_start) self.btn_exit = Button('Exit Game', self._on_exit) self.context = context self.logo = pygame.image.load('img/logo.png') w, h = self.logo.get_size() self.context.screen = pygame.display.set_mode( (int(w * 1.5), int(h * 2)), 0, 32) pygame.display.set_caption('Python Snake Game v1.0') View.__init__(self, context.screen)
def on_draw(self): self._on_draw() self.btn_start.on_draw(self.context.screen) self.btn_exit.on_draw(self.context.screen)
def on_event(self, event): self.btn_start.on_event(event) self.btn_exit.on_event(event)
def _on_exit(self): print('exit game') self.context.running = False
def _on_start(self): print('start game') self.context.view = GameView(self.context)
def _on_draw(self): ''' 计算各个按钮的位置以及Logo的位置 ''' s_w, s_h = self.context.screen.get_size() l_w, l_h = self.logo.get_size() x, y, bs_w, bs_h = self.btn_start.get_box() x, y, be_w, be_h = self.btn_exit.get_box() y_start = l_h + 50 ''' 界面元素排版 ''' self.btn_start.pos = (s_w/2 - bs_w / 2, y_start) self.btn_exit.pos = (s_w/2 - be_w / 2, y_start + bs_h + 4) self.context.screen.blit(self.logo, (s_w/2-l_w/2, s_h/2-l_h/2))
|