class Interface(object): def __init__(self, world): self.world = world self.load_image = self.world.load_image self.display = world.display self.menu = MainMenu(self) self.dialog = Dialog(self, self.display) self.state = "" def show_dialog(self, owner, player, handler): self.dialog.init_dialog(owner, player, handler) self.state = "dialog" self.world.state = "itf" def show_menu(self, state="game"): self.menu.set_state(state) self.state = "menu" self.world.state = "itf" def return_event(self): for event in pygame.event.get(): if event.type == KEYDOWN: return event else: return EmptyEvent() def draw(self): if self.state == "menu": self.menu.key_loop(self.return_event()) self.menu.draw(self.display) elif self.state == "dialog": self.dialog.key_loop(self.return_event()) self.dialog.draw() else: self.world.state = "game"
class Game(): def __init__(self): self.scroll = 0 self.player = Player(WIN_WIDTH / 2, 360) self.items = [Chest(WIN_WIDTH / 2 + 540, 310, 'images/case.png', "Сейф 1", 320 // 4, 240 // 3), Picture(WIN_WIDTH / 2 - 150, 240, 'images/picture.png', "Картина", 320 // 4, 240 // 3), Books(WIN_WIDTH / 2 + 200, 300, 'images/books.png', "Книги",320 // 2, 240 // 2), Jail(WIN_WIDTH / 2 + 750, 269, 'images/jail.png', "Заключенный", 320, 240), Table(WIN_WIDTH / 2 - 300, 300, 'images/table.png', "Стол", 320 // 2, 240 // 2)] self.dialog = Dialog("- Добро пожаловать в мир твоих самых страшных кошмаров, жалкий офисный червяк.") self.left = self.right = self.up = self.down = False def loop(self, screen): clock = pygame.time.Clock() communicate = Communicate(["- Что, где я ?", "- Добро пожаловать в мир твоих самых страшных кошмаров, жалкий офисный червяк", "- Я ничего не понимаю, что происходит, я просто пошел за кофе, а потом… Ничего не помню", "- Вся твоя жизнь - самый скучный симулятор, но сейчас у тебя появился шанс хотя бы умереть интересно ", "- Что, нет, выпустите меня", "- Единственный, кто может выпустить тебя - ты сам, ты всю жизнь думал за других, решал за других , пришло время отвечать за себя", "- Но кто это, почему, за что?", "- Ты знаешь…", "- *Пришло время выбираться, кроме себя, тебе не на кого надеяться, поэтому сделай что-нибудь, чтобы выжить*"]) while True: bg.blit(images, (0 + self.scroll, 0)) for i in self.items: i.update() i.draw(bg, self.scroll) screen.blit(bg, (0, 0)) if (self.player.rect.x < 0 and self.scroll < 0): self.scroll += 2 if (self.player.rect.x > 651 and self.scroll > -480): self.scroll -= 2 for e in pygame.event.get(): if e.type == pygame.QUIT: return if e.type == KEYDOWN and e.key == K_LEFT: self.left = True if e.type == KEYDOWN and e.key == K_RIGHT: self.right = True # if e.type == KEYDOWN and e.key == K_UP: # down = True # if e.type == KEYDOWN and e.key == K_DOWN: # up = True if e.type == KEYUP and e.key == K_LEFT: self.left = False if e.type == KEYUP and e.key == K_RIGHT: self.right = False if e.type == KEYDOWN and e.key == K_SPACE: self.down = True if e.type == KEYUP and e.key == K_SPACE: self.down = False # if e.type == KEYUP and e.key == K_DOWN: # up = False self.player.update(self.left, self.right, self.down, self.items, self.scroll, self.dialog) if (self.down == True): communicate.currentText = communicate.next() if (len(communicate.data) > 0): self.dialog.setText(communicate.data[0]) self.player.draw(screen) self.dialog.draw(screen) self.down = False pygame.display.update() def quit(self): pass
def draw(self, renderer): #print "drawing!!!" return Dialog.draw(self, renderer)
def main(): pygame.init() SCREEN_WIDTH = 900 SCREEN_HEIGHT = 800 board = Board() is_piece_draging = False focused_piece = None is_check = False is_promoted = False mouse_offset_x = 0 mouse_offset_y = 0 current_player_color = WHITE next_player_color = BLACK screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) clock = pygame.time.Clock() # set center of window board_offset_x = (screen.get_width() - board.get_width()) // 2 board_offset_y = (screen.get_height() - board.get_height()) // 2 running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if is_promoted: dialog.handle_events(event) else: if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: # Left Mouse Button x = event.pos[0] - board_offset_x y = event.pos[1] - board_offset_y focused_piece = board.get_collided_piece( (x, y), current_player_color) if focused_piece is not None: is_piece_draging = True board.move_up(focused_piece) mouse_offset_x = focused_piece.rect.x - x mouse_offset_y = focused_piece.rect.y - y elif event.type == pygame.MOUSEMOTION: if is_piece_draging: mouse_x, mouse_y = event.pos # move a piece (in window) focused_piece.rect.x = mouse_x - board_offset_x + mouse_offset_x focused_piece.rect.y = mouse_y - board_offset_y + mouse_offset_y elif event.type == pygame.MOUSEBUTTONUP: if event.button == 1: is_piece_draging = False # when piece is moved if focused_piece is not None and board.set_piece_position( focused_piece): if isinstance( focused_piece, Pawn) and focused_piece.is_promotion(): is_promoted = True dialog = Dialog(focused_piece.color) else: # maybe there is check is_check = board.is_check( current_player_color, next_player_color) # generate valid moves for next player # but when opponent has no move # it's game over if board.generate_valid_moves_for_player_pieces( next_player_color, current_player_color): # but there can be a draw if board.is_stalemate( current_player_color, next_player_color): print("Remis") # otherwise one player wins else: if current_player_color == WHITE: print( "Wygrał gracz z kolorem biały") else: print( "Wygrał gracz z kolorem czarnym" ) running = False # change player if current_player_color == WHITE: current_player_color = BLACK next_player_color = WHITE else: current_player_color = WHITE next_player_color = BLACK board.blit_self() if is_promoted and dialog.piece_choose is not None: board.pawn_promotion(focused_piece, dialog.piece_choose) is_check = board.is_check(current_player_color, next_player_color) if board.generate_valid_moves_for_player_pieces( next_player_color, current_player_color): # but there can be a draw if board.is_stalemate(current_player_color, next_player_color): print("Remis") # otherwise one player wins else: if current_player_color == WHITE: print("Wygrał gracz z kolorem biały") else: print("Wygrał gracz z kolorem czarnym") running = False # change player if current_player_color == WHITE: current_player_color = BLACK next_player_color = WHITE else: current_player_color = WHITE next_player_color = BLACK is_promoted = False screen.fill((0, 0, 0)) if is_check: board.draw_check_warning(current_player_color) if is_piece_draging: board.draw_valid_moves(focused_piece) board.update() screen.blit(board, (board_offset_x, board_offset_y)) if is_promoted: dialog.draw(screen) pygame.display.flip() clock.tick(60)