def update(self, events, mouse_coords): if self.first: self.first = False if self.enable_tutorial: SND.music_tutorial() else: SND.music_game() if self.enable_tutorial: self.do_tutorial_update() if self.model.budget < 0: self.next = YouLose(self) events = self.filter_hover_ui_events(events, mouse_coords) self.board.update(events) if self.model.session.is_done(): reward = Reward(self.model, self.model.session) self.model.budget += reward.calculate_next_budget() self.model.reward = reward self.next = WrapperMenu(self, self.model) self.mouse_xy = mouse_coords
def update(self, events, mouse_pos): self.cursor = mouse_pos if self.first: self.first = False SND.music_title() for event in events: if event.mousedown: if self.currently_over != None: self.do_command(self.currently_over) elif event.keydown: if event.key == 'enter': self.do_command('new_game') elif event.key == 't': self.do_command('tutorial') elif event.key == 'o': self.do_command('options')
def update(self, events, mouse_xy): if self.first: self.first = False SND.music_menu() self.mouse_xy = mouse_xy for event in events: if event.mousedown: x = event.x - MENU_LEFT y = event.y - MENU_TOP for element in self.get_ui_elements(): if element.type == 'BUTTON' and element.is_enabled(): if x >= element.left and x < element.right and y >= element.top and y < element.bottom: element.action() break mx, my = self.mouse_xy for i in range(len(self.tab_regions)): region = self.tab_regions[i] if region != None: if mx >= region[0] and mx < region[2] and my >= region[1] and my < region[3]: self.active_menu = self.ordered[i] self.elements = None break
def main(): pygame.init() is_fullscreen = False resizeable = pygame.RESIZABLE real_screen = pygame.display.set_mode(SCREEN_SIZE, resizeable) pygame.display.set_caption("Swamped Sysadmins") virtual_screen = pygame.Surface((GAME_WIDTH, GAME_HEIGHT)).convert_alpha() active_scene = TitleScene() mouse_pos = (0, 0) counter = 0 while True: begin = time.time() if MAGIC_POTATO.is_full_screen() != is_fullscreen: is_fullscreen = MAGIC_POTATO.is_full_screen() if is_fullscreen: real_screen = pygame.display.set_mode((800, 600), pygame.FULLSCREEN) else: real_screen = pygame.display.set_mode(SCREEN_SIZE, resizeable) SND.ensure_music_volume() events = [] last_mouse_event = None for e in pygame.event.get(): if e.type == pygame.MOUSEBUTTONDOWN: mouse_pos = e.pos x, y = mouse_pos last_mouse_event = Event('mousedown', x, y, e.button == 1) events.append(last_mouse_event) elif e.type == pygame.MOUSEBUTTONUP: mouse_pos = e.pos x, y = mouse_pos last_mouse_event = Event('mouseup', x, y, e.button == 1) events.append(last_mouse_event) elif e.type == pygame.MOUSEMOTION: mouse_pos = e.pos x, y = mouse_pos last_mouse_event = Event('mousemove', x, y, False) events.append(last_mouse_event) elif e.type == pygame.QUIT: return elif e.type == pygame.KEYDOWN: pressed_keys = pygame.key.get_pressed() if e.key == pygame.K_F4 and (pressed_keys[pygame.K_LALT] or pressed_keys[pygame.K_RALT]): return elif e.key == pygame.K_w and (pressed_keys[pygame.K_LCTRL] or pressed_keys[pygame.K_RCTRL]): return elif e.key == pygame.K_ESCAPE: return else: events.append(Event('keydown', e.key, 0, False)) elif e.type == pygame.VIDEORESIZE: w, h = e.size SCREEN_SIZE[0] = w SCREEN_SIZE[1] = h real_screen = pygame.display.set_mode(SCREEN_SIZE, resizeable) if last_mouse_event != None: mouse_pos = (last_mouse_event.x, last_mouse_event.y) active_scene.update(events, mouse_pos) active_scene.render(virtual_screen, counter) pygame.transform.scale(virtual_screen, real_screen.get_size(), real_screen) pygame.display.flip() next_scene = active_scene.next if next_scene != None: active_scene.next = None active_scene = next_scene if active_scene == 'exit': return counter += 1 end = time.time() diff = end - begin delay = 1.0 / FPS wait = delay - diff if wait > 0: time.sleep(wait)
def update(self): self.state_counter += 1 self.ttl -= 1 if self.state == 'flying': self.ttl = STARTING_TTL # still immune to the aging process while flying through the air progress = 1.0 * self.state_counter / FLYING_FRAMES antiprogress = 1 - progress self.x = int(START_X * antiprogress + self.landing_x * progress) self.y = int(START_Y * antiprogress + self.landing_y * progress) if self.state_counter == FLYING_FRAMES: self.x = self.landing_x self.y = self.landing_y self.state = 'ailed' self.state_counter = 0 if self.ailment == 'dead': self.state = 'dead' elif self.state == 'dead': if not self.replaced: self.replaced = True t = self.device_type self.resolution = 'ordered' if t == 'phone': if self.model.inventory_phones > 0: self.resolution = 'replaced' self.model.inventory_phones -= 1 else: self.model.special_order_phone() elif t == 'tablet': if self.model.inventory_tablets > 0: self.resolution = 'replaced' self.model.inventory_tablets -= 1 else: self.model.special_order_tablet() elif t == 'laptop': if self.model.inventory_laptops > 0: self.resolution = 'replaced' self.model.inventory_laptops -= 1 else: self.model.special_order_laptop() elif self.state == 'ailed': self.response_time += 1 if self.ailment == 'unknown': for staff in self.playboard.model.staff: dx = staff.x - self.x dy = staff.y - self.y if dx ** 2 + dy ** 2 < 32 ** 2: self.unknown_discovery_counter += 1 if self.unknown_discovery_counter >= UNKNOWN_TREAT_TIME: self.ailment = self.actual_ailment elif self.state == 'treated': self.response_time += 1 if self.ailment == 'sick': treat_time = SICK_TREAT_TIME elif self.ailment == 'sad': treat_time = SAD_TREAT_TIME elif self.ailment == 'angry': treat_time = ANGRY_TREAT_TIME elif self.ailment == 'crazy': treat_time = CRAZY_TREAT_TIME elif self.ailment == 'dead': treat_time = 9999999 # won't happen elif self.ailment == 'unknown': treat_time = UNKNOWN_TREAT_TIME if self.state_counter * self.treatment_ratio >= treat_time: self.state = 'new' self.resolution = 'treated' self.state_counter = 0 SND.play_device_fix() if self.ttl < 0 and self.state != 'dead': self.state = 'dead' self.state_counter = 0