def paint(self, x, y, width, height): screen = pygame.display.get_surface() grass = singleton_image_cache.get("grass.png") screen.blit(grass, (x, y)) if self.get_img_name() is not None: image = singleton_image_cache.get(self.get_img_name()) leftPadding = (width - image.get_width()) / 2 topPadding = (height - image.get_height()) / 2 screen.blit(image, (x + leftPadding, y + topPadding)) # if the square is workable, display the number of workers on it. if self.workable() and self.num_workers > 0: worker_text = self.font.render(str(self.num_workers), True, (0, 0, 0)) screen.blit(worker_text, (x, y))
def show_main_menu(paused = False): '''shows main menue. Allows options to be actuated''' screen = pygame.display.get_surface() splashImage = singleton_image_cache.get("splash.png") screen.blit(splashImage, (0,0)) # Update the display pygame.display.flip() # Freeze until a key is pressed proceed = False while not proceed: # If the user hit one of the inputs, proceed for event in pygame.event.get(): if event.type == KEYDOWN: key = event.unicode if key == 'i': draw_instructions(paused) elif key == 's': from level import goto_level goto_level(0) proceed = True elif key == 'q': pygame.quit() sys.exit() time.sleep(0.02) # avoid hogging the CPU
def draw_instructions(paused = False): '''draws a visual guide to the game''' width_msg_offset = width_msg * 3 / 4 PAD = 30 screen = pygame.display.get_surface() image_farm = singleton_image_cache.get("farm.png") image_house = singleton_image_cache.get("house.png") house_msg = "This is a house. You need houses for workers" house_msg += " to live in or else no one will come to" house_msg += " your city. 10 workers can live in each house." farm_msg = "This is a farm. Farms grow food for your workers" farm_msg += " to eat. Assign workers here to gather food." lines = word_wrap(farm_msg, width_msg_offset, font_msg) farm_height = font_msg.get_height() * len(lines) lines.append("") lines.extend(word_wrap(house_msg, width_msg_offset, font_msg)) house_height = font_msg.get_height() * len(lines) lines.append("") lines.extend(word_wrap("Press any key to continue", width_msg_offset, font_msg)) # Render each line with a dark gray color lines = [font_msg.render(l, True, (50, 50, 50)) for l in lines] # The height of the message is the sum of the heights of each line tot_height = sum([l.get_height() for l in lines]) x1 = screen.get_width()/2 - width_msg/2 # left edge of the box y1 = screen.get_height()/2 - tot_height/2 # starting y coord x2 = screen.get_width()/2 - width_msg_offset/2 + PAD PAD = 30 # Draw background box pygame.draw.rect(screen, (240, 240, 240), (x1 - PAD, y1 - PAD, width_msg + 2*PAD, tot_height + 2*PAD)) pygame.draw.rect(screen, (50, 50, 50), (x1 - PAD, y1 - PAD, width_msg + 2*PAD, tot_height + 2*PAD), 4) y = y1 for line in lines: screen.blit(line, (x2, y)) y += line.get_height() screen.blit(image_farm, (x1, y1 + (farm_height - image_farm.get_height())/2)) height_diff = house_height - farm_height screen.blit(image_house, (x1, y1 + farm_height + (height_diff - image_house.get_height())/2)) # Update the displayi pygame.display.flip() # Freeze until a key is pressed proceed = False while not proceed: # If the user hit any key, proceed for event in pygame.event.get(): if event.type == KEYDOWN: proceed = True time.sleep(0.01) # avoid hogging the CPU if not paused: splashImage = singleton_image_cache.get("splash.png") screen.blit(splashImage, (0,0)) else: new_render() proceed = False ## The second screen starts here image_mine = singleton_image_cache.get("mine.png") image_stream = singleton_image_cache.get("stream.png") image_tree = singleton_image_cache.get("tree.png") mine_msg = "This is a mine. Assign workers here to gather gold." stream_msg = "This is a stream. Workers can gather gold from here too, but not quite as quickly." tree_msg = "This is a forest. Workers can gather wood here." lines = word_wrap(mine_msg, width_msg_offset, font_msg) mine_height = font_msg.get_height() * len(lines) lines.append("") lines.extend(word_wrap(stream_msg, width_msg_offset, font_msg)) stream_height = font_msg.get_height() * len(lines) lines.append("") lines.extend(word_wrap(tree_msg, width_msg_offset, font_msg)) tree_height = font_msg.get_height() * len(lines) lines.append("") lines.extend(word_wrap("Press any key to continue", width_msg_offset, font_msg)) lines = [font_msg.render(l, True, (50, 50, 50)) for l in lines] # The height of the message is the sum of the heights of each line tot_height = sum([l.get_height() for l in lines]) y1 = screen.get_height()/2 - tot_height/2 # starting y coord # Draw background box pygame.draw.rect(screen, (240, 240, 240), (x1 - PAD, y1 - PAD, width_msg + 2*PAD, tot_height + 2*PAD)) pygame.draw.rect(screen, (50, 50, 50), (x1 - PAD, y1 - PAD, width_msg + 2*PAD, tot_height + 2*PAD), 4) y = y1 for line in lines: screen.blit(line, (x2, y)) y += line.get_height() screen.blit(image_mine, (x1, y1 + (mine_height - image_mine.get_height())/2) ) height_diff = stream_height - mine_height screen.blit(image_stream, (x1, y1 + mine_height + (height_diff - image_stream.get_height())/2)) height_diff = tree_height - stream_height screen.blit(image_tree, (x1, y1 + stream_height + (height_diff - image_tree.get_height())/2)) pygame.display.flip() while not proceed: # If the user hit any key, proceed for event in pygame.event.get(): if event.type == KEYDOWN: proceed = True time.sleep(0.01) # avoid hogging the CPU if not paused: screen.blit(splashImage, (0,0)) else: new_render() # The third Screen starts here proceed = False image_grave = singleton_image_cache.get("grave.png") grave_msg = "This is a cemetery. If your workers die then these will start popping up. They can't" grave_msg += " be destroyed and take up valuable real estate, so be careful!" lines = word_wrap(grave_msg, width_msg_offset, font_msg) lines.append("") lines.extend(word_wrap("Press any key to continue", width_msg_offset, font_msg)) lines = [font_msg.render(l, True, (50, 50, 50)) for l in lines] tot_height = sum([l.get_height() for l in lines]) y1 = screen.get_height()/2 - tot_height/2 # starting y coord pygame.draw.rect(screen, (240, 240, 240), (x1 - PAD, y1 - PAD, width_msg + 2*PAD, tot_height + 2*PAD)) pygame.draw.rect(screen, (50, 50, 50), (x1 - PAD, y1 - PAD, width_msg + 2*PAD, tot_height + 2*PAD), 4) y = y1 for line in lines: screen.blit(line, (x2, y)) y += line.get_height() screen.blit(image_grave, (x1, y1 + tot_height/2 - (image_mine.get_height())/2)) pygame.display.flip() while not proceed: # If the user hit any key, proceed for event in pygame.event.get(): if event.type == KEYDOWN: proceed = True time.sleep(0.01) # avoid hogging the CPU if not paused: show_main_menu() else: new_render() show_pause_menu()