class TitleScreen(Screen): WELCOME_SCREEN_BACKGROUND = "images/WelcomeScreen_color.jpg" TITLE_SCREEN_BUTTON_TEXT = "Start the Strategery!" def __init__(self): # create screen area self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), 0, 32) # Fill background background = pygame.Surface(self.screen.get_size()) background = background.convert() background.fill((0, 0, 0)) self.background_image = pygame.image.load( TitleScreen.WELCOME_SCREEN_BACKGROUND).convert_alpha() self.buttons = Btn_grp() self.buttons.new_btn(rect=(560, 480, 175, 20), caption=TitleScreen.TITLE_SCREEN_BUTTON_TEXT) def update_all(self): """ update & put on screen """ self.buttons.btn_grp_update(self.screen) #print("update done") #TEST: works def render(self): # Go ahead and update the screen with what we've set to be drawn self.screen.blit(self.background_image, [0, 0]) # Set the screen background pygame.display.flip() def handle_events(self): """ returns true if the program should continue execution returns false if the program should halt """ for event in pygame.event.get(): if event.type == pygame.QUIT: print("quitting...") pygame.quit() return False if event.type == pygame.KEYDOWN: if event.key == pygame.K_RETURN: global currentScreen currentScreen = OptionsScreen() return True return True def button_events(self, event): for button in self.buttons.btn_list: if "click" in button.handleEvent(event): if button.caption == TitleScreen.TITLE_SCREEN_BUTTON_TEXT: #do something print("I made it so")
class TitleScreen(Screen): WELCOME_SCREEN_BACKGROUND = "images/WelcomeScreen_color.jpg" TITLE_SCREEN_BUTTON_TEXT = "Start the Strategery!" def __init__(self): # create screen area self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), 0, 32) # Fill background background = pygame.Surface(self.screen.get_size()) background = background.convert() background.fill((0, 0, 0)) self.background_image = pygame.image.load(TitleScreen.WELCOME_SCREEN_BACKGROUND).convert_alpha() self.buttons = Btn_grp() self.buttons.new_btn(rect = (560,480,175,20), caption = TitleScreen.TITLE_SCREEN_BUTTON_TEXT) def update_all(self): """ update & put on screen """ self.buttons.btn_grp_update(self.screen) #print("update done") #TEST: works def render(self): # Go ahead and update the screen with what we've set to be drawn self.screen.blit(self.background_image, [0,0]) # Set the screen background pygame.display.flip() def handle_events(self): """ returns true if the program should continue execution returns false if the program should halt """ for event in pygame.event.get(): if event.type == pygame.QUIT: print("quitting...") pygame.quit() return False if event.type == pygame.KEYDOWN: if event.key == pygame.K_RETURN: global currentScreen currentScreen = OptionsScreen() return True return True def button_events(self, event): for button in self.buttons.btn_list: if "click" in button.handleEvent(event): if button.caption == TitleScreen.TITLE_SCREEN_BUTTON_TEXT: #do something print("I made it so")
class GameScreen(Screen): SMALL_BUTTON_GREEN = "images/btn_green.png" SMALL_BUTTON_GRAY = "images/btn_gray.png" SMALL_BUTTON_RED = "images/btn_red.png" TEST_IMAGE = "images/white_tank.png" def __init__(self): Screen.__init__(self) # Fill background background = pygame.Surface(self.screen.get_size()) background = background.convert() background.fill((0, 0, 0)) # Create a grid of tiles self.grid_map = TileGrid(self.screen) # create group of players, # each with one team of units placed on map self.players = PlayerUnitGroup(self.screen) self.melee_engine = MeleeEngine(self.grid_map, self.players, self.players.ttl_players) # create player/unit interface area self.player_command = PlayerCommand() self.buttons = Btn_grp() self.buttons.new_btn(rect=(750, 500, 18, 24), caption="hi", normal=GameScreen.TEST_IMAGE) self.buttons.new_btn(rect=(650, 500, 18, 24), normal=GameScreen.TEST_IMAGE) self.buttons.new_btn(rect=(690, 500, 10, 10), caption="B", normal=GameScreen.SMALL_BUTTON_GRAY, down=GameScreen.SMALL_BUTTON_GREEN, highlight=GameScreen.SMALL_BUTTON_RED) self.buttons.new_btn(rect=(625, 550, 150, 20), caption="MAKE IT SO!") def update_all(self): """ update everything & put on screen """ # Set the screen background self.screen.fill(BLACK) # Update the grid to screen self.grid_map.update_grid() # update player_command area self.player_command.draw_messageboard(self.screen) # Update player groups, units to screen, melee groups self.players.update_players(self.player_command, self.grid_map, self.melee_engine) self.buttons.btn_grp_update(self.screen) def usr_events(self, pos): """ determine action based upon mouse & keyboard input pos: mouse position coordinates """ # Check to see if click was in grid field for testing if self.grid_map.in_field(pos): #print("you're clicked on:", pos) # do whatever happens when something # gets clicked on in battle area self.grid_map.grid_clicked(pos) new_coord = self.grid_map.coord_to_grid(pos) self.players.assign_player_grp_targ_tile(new_coord) self.player_command.player_msg = "" def button_events(self, event): # check for clicks on a button # (not in mousebuttondown to allow for mouseover highlight) for button in self.buttons.btn_list: if "click" in button.handleEvent(event): if button.caption == "B": self.player_command.player_msg = \ "That was the round button" if button.caption == "hi": self.player_command.player_msg = \ "Nothing here but us buttons." if button.caption == "MAKE IT SO!": Unit.movement(self.grid_map, self.players) self.player_command.player_msg = "" """ check for unit-related button events """ for player in self.players.active_list: for group in player.units: for unit in group.group_list: for button in unit.unit_btns: if "click" in button.handleEvent(event): if button.caption == "B": unit.active = "True" unit.txt_status = "Targeting" self.player_command.player_msg = \ "Click on target tile." elif button.caption == "A": unit.txt_status = "A Button" else: unit.txt_status = "Unidentified Button" def handle_events(self): """ returns true if program should continue execution returns false if program should halt """ for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() return False # User clicks the mouse. Get the position elif event.type == pygame.MOUSEBUTTONDOWN: GameScreen.usr_events(self, pygame.mouse.get_pos()) """ check for user-related button events """ GameScreen.button_events(self, event) return True
class OptionsScreen(Screen): CREATE_SERVER_BUTTON_TEXT = "Create Server" JOIN_SERVER_BUTTON_TEXT = "Connect to Server" def __init__(self): # create screen area self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), 0, 32) # Fill background background = pygame.Surface(self.screen.get_size()) background = background.convert() background.fill((0, 0, 0)) self.buttons = Btn_grp() self.buttons.new_btn(rect=(250, 350, 120, 20), caption=OptionsScreen.CREATE_SERVER_BUTTON_TEXT) self.buttons.new_btn(rect=(400, 350, 160, 20), caption=OptionsScreen.JOIN_SERVER_BUTTON_TEXT) def update_all(self): """ update & put on screen """ self.screen.fill(BLACK) # Set the screen background self.buttons.btn_grp_update(self.screen) def render(self): message1_sf = DEFAULT_GAME_FONT.render("OPTIONS SCREEN", True, pygame.Color('white')) self.screen.blit(message1_sf, (300, 250, SCREEN_WIDTH, SCREEN_HEIGHT)) IPaddr = ask(self.screen, "IP Address") ################################################ Server Code#################################### serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serversocket.bind(('localhost', 8089)) serversocket.listen(5) # become a server socket, maximum 5 connections while True: connection, address = serversocket.accept() buf = connection.recv(64) if len(buf) > 0: print(buf) break ############################################################################################### # ################################### Client Code################################################# # clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # clientsocket.connect((IPaddr, 8089)) # clientsocket.send('If this is printed, the socket works!') ################################################################################################ # Go ahead and update the screen with what we've set to be drawn pygame.display.flip() def handle_events(self): """ returns true if the program should continue execution returns false if the program should halt """ for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() return False if event.type == pygame.KEYDOWN: if event.key == pygame.K_RETURN: global currentScreen currentScreen = GameScreen() return True return True def button_events(self, event): # check for clicks on a button # (not in mousebuttondown to allow for mouseover highlight) for button in self.buttons.btn_list: if "click" in button.handleEvent(event): if button.caption == OptionsScreen.CREATE_SERVER_BUTTON_TEXT: print("create server placeholder") if button.caption == OptionsScreen.JOIN_SERVER_BUTTON_TEXT: print("join server placeholder")
class GameScreen(Screen): SMALL_BUTTON_GREEN = "images/btn_green.png" SMALL_BUTTON_GRAY = "images/btn_gray.png" SMALL_BUTTON_RED = "images/btn_red.png" TEST_IMAGE = "images/white_tank.png" def __init__(self): Screen.__init__(self) # Fill background background = pygame.Surface(self.screen.get_size()) background = background.convert() background.fill((0, 0, 0)) # Create a grid of tiles self.grid_map = TileGrid(self.screen) # create group of players, # each with one team of units placed on map self.players = PlayerUnitGroup(self.screen) self.melee_engine = MeleeEngine(self.grid_map, self.players, self.players.ttl_players) # create player/unit interface area self.player_command = PlayerCommand() self.buttons = Btn_grp() self.buttons.new_btn(rect = (750,500,18,24), caption = "hi", normal = GameScreen.TEST_IMAGE) self.buttons.new_btn(rect =(650,500,18,24), normal = GameScreen.TEST_IMAGE) self.buttons.new_btn( rect = (690,500,10,10), caption = "B", normal = GameScreen.SMALL_BUTTON_GRAY, down = GameScreen.SMALL_BUTTON_GREEN, highlight = GameScreen.SMALL_BUTTON_RED) self.buttons.new_btn(rect = (625,550,150,20), caption = "MAKE IT SO!") def update_all(self): """ update everything & put on screen """ # Set the screen background self.screen.fill(BLACK) # Update the grid to screen self.grid_map.update_grid() # update player_command area self.player_command.draw_messageboard(self.screen) # Update player groups, units to screen, melee groups self.players.update_players(self.player_command, self.grid_map, self.melee_engine) self.buttons.btn_grp_update(self.screen) def usr_events(self, pos): """ determine action based upon mouse & keyboard input pos: mouse position coordinates """ # Check to see if click was in grid field for testing if self.grid_map.in_field(pos): #print("you're clicked on:", pos) # do whatever happens when something # gets clicked on in battle area self.grid_map.grid_clicked(pos) new_coord = self.grid_map.coord_to_grid(pos) self.players.assign_player_grp_targ_tile(new_coord) self.player_command.player_msg = "" def button_events(self, event): # check for clicks on a button # (not in mousebuttondown to allow for mouseover highlight) for button in self.buttons.btn_list: if "click" in button.handleEvent(event): if button.caption == "B": self.player_command.player_msg = \ "That was the round button" if button.caption == "hi": self.player_command.player_msg = \ "Nothing here but us buttons." if button.caption == "MAKE IT SO!": Unit.movement(self.grid_map, self.players) self.player_command.player_msg = "" """ check for unit-related button events """ for player in self.players.active_list: for group in player.units: for unit in group.group_list: for button in unit.unit_btns: if "click" in button.handleEvent(event): if button.caption == "B": unit.active = "True" unit.txt_status = "Targeting" self.player_command.player_msg = \ "Click on target tile." elif button.caption == "A": unit.txt_status = "A Button" else: unit.txt_status = "Unidentified Button" def handle_events(self): """ returns true if program should continue execution returns false if program should halt """ for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() return False # User clicks the mouse. Get the position elif event.type == pygame.MOUSEBUTTONDOWN: GameScreen.usr_events(self,pygame.mouse.get_pos()) """ check for user-related button events """ GameScreen.button_events(self,event) return True
class OptionsScreen(Screen): CREATE_SERVER_BUTTON_TEXT = "Create Server" JOIN_SERVER_BUTTON_TEXT = "Connect to Server" def __init__(self): # create screen area self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), 0, 32) # Fill background background = pygame.Surface(self.screen.get_size()) background = background.convert() background.fill((0, 0, 0)) self.buttons = Btn_grp() self.buttons.new_btn( rect = (250,350,120,20), caption = OptionsScreen.CREATE_SERVER_BUTTON_TEXT) self.buttons.new_btn( rect = (400,350,160,20), caption = OptionsScreen.JOIN_SERVER_BUTTON_TEXT) def update_all(self): """ update & put on screen """ self.screen.fill(BLACK) # Set the screen background self.buttons.btn_grp_update(self.screen) def render(self): message1_sf = DEFAULT_GAME_FONT.render("OPTIONS SCREEN", True, pygame.Color('white')) self.screen.blit(message1_sf, (300, 250, SCREEN_WIDTH, SCREEN_HEIGHT)) IPaddr = ask(self.screen, "IP Address") ################################################ Server Code#################################### serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serversocket.bind(('localhost', 8089)) serversocket.listen(5) # become a server socket, maximum 5 connections while True: connection, address = serversocket.accept() buf = connection.recv(64) if len(buf) > 0: print(buf) break ############################################################################################### # ################################### Client Code################################################# # clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # clientsocket.connect((IPaddr, 8089)) # clientsocket.send('If this is printed, the socket works!') ################################################################################################ # Go ahead and update the screen with what we've set to be drawn pygame.display.flip() def handle_events(self): """ returns true if the program should continue execution returns false if the program should halt """ for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() return False if event.type == pygame.KEYDOWN: if event.key == pygame.K_RETURN: global currentScreen currentScreen = GameScreen() return True return True def button_events(self, event): # check for clicks on a button # (not in mousebuttondown to allow for mouseover highlight) for button in self.buttons.btn_list: if "click" in button.handleEvent(event): if button.caption == OptionsScreen.CREATE_SERVER_BUTTON_TEXT: print("create server placeholder") if button.caption == OptionsScreen.JOIN_SERVER_BUTTON_TEXT: print("join server placeholder")