def game(players): input_box = InputBox(440, 340, 480, 50) quit_button = Button(690, 520, 80, 50, 'QUIT', 'quit') start_button = Button(680, 440, 100, 50, 'START', 'start') player_text = Player(140, 160, 130, 50) play = GuessWord() snowman = SnowMan() rand_word = play.random_word() print_word = [False for i in range(len(rand_word))] in_letter = [] out_letter = [] done = False start_game = False while not done: for event in pg.event.get(): if event.type == pg.QUIT: done = True pg.quit() quit() input_box.handle_event(event) quit_button.handle_event(event) start_game = start_button.handle_event(event) SCREEN.fill(PURPLE) quit_button.draw(SCREEN) if len(out_letter) == 14: start_button.draw(SCREEN) else: if in_letter == list(rand_word): if players: won = (len(in_letter) + len(out_letter)) % 2 if won: player_text.text = 'Player 2!' else: player_text.text = 'Player 1!' player_text.draw(SCREEN) start_button.draw(SCREEN) else: input_box.draw(SCREEN) if input_box.text in rand_word: if input_box.text not in in_letter: in_letter = play.check_in(input_box.text, print_word) else: if input_box.text not in out_letter: if len(out_letter) <= 14: out_letter.append(input_box.text) play.draw_lines(SCREEN, print_word) if start_game: done = True snowman.wrong_letter = out_letter snowman.draw_snowman(SCREEN) pg.display.flip()
def main(): clock = pg.time.Clock() input_box1 = InputBox(100, 100, 140, 32) done = False while not done: for event in pg.event.get(): if event.type == pg.QUIT: done = True input_box1.handle_event(event) screen.fill((30, 30, 30)) input_box1.draw(screen) input_box1.update() pg.display.flip() clock.tick(30)
def main(): pygame.init() screen = pygame.display.set_mode((600, 450)) screen.fill(pygame.Color('white')) mp = MapParams() input_box = InputBox(50, 10, 140, 32) im = Image.open('data/search_icon.png') im = im.resize((30, 30)) im.save('data/search_icon.png') button = pygame.image.load('data/search_icon.png').convert_alpha() b_rect = pygame.Rect(10, 10, 50, 50) reset = Button('Сброс поискового результата', 350, 10, 24) while True: event = pygame.event.wait() if event.type == pygame.QUIT: # Выход из программы break elif event.type == pygame.KEYUP: # Обрабатываем различные нажатые клавиши. mp.update(event) elif event.type == pygame.MOUSEBUTTONUP: # Выполняем поиск по клику мышки. if event.button == 1: # LEFT_MOUSE_BUTTON mp.add_reverse_toponym_search(event.pos) if b_rect.collidepoint(event.pos): text = input_box.text toponym = reverse_geocode(text) if toponym: point = toponym['Point']['pos'].split() mp.lon = float(point[0]) mp.lat = float(point[1]) mp.search_result = SearchResult( point, toponym["metaDataProperty"] ["GeocoderMetaData"]["text"] if toponym else None, toponym["metaDataProperty"]["GeocoderMetaData"] ["Address"].get("postal_code") if toponym else None) else: input_box.text = 'Ничего не найдено' if reset.rect.collidepoint(event.pos): mp.search_result = None elif event.button == 3: # RIGHT_MOUSE_BUTTON mp.add_reverse_org_search(event.pos) elif event.type == pygame.KEYDOWN: if event.key == pygame.K_RETURN and input_box.active: text = input_box.text toponym = reverse_geocode(text) if toponym: point = toponym['Point']['pos'].split() mp.lon = float(point[0]) mp.lat = float(point[1]) mp.search_result = SearchResult( point, toponym["metaDataProperty"]["GeocoderMetaData"]["text"] if toponym else None, toponym["metaDataProperty"]["GeocoderMetaData"] ["Address"].get("postal_code") if toponym else None) else: input_box.text = 'Ничего не найдено' map_file = load_map(mp) screen.blit(pygame.image.load(map_file), (0, 0)) if mp.search_result: if mp.use_postal_code and mp.search_result.postal_code: text = render_text(mp.search_result.postal_code + ", " + mp.search_result.address) else: text = render_text(mp.search_result.address) screen.blit(text, (20, 400)) input_box.handle_event(event) input_box.update() input_box.draw(screen) screen.blit(button, b_rect) reset.draw(screen) screen.blit(screen, (0, 0)) pygame.display.flip() pygame.quit() os.remove(map_file)
def main(): pygame.init() window = Window((1000, 640)) bola = Bola((400, 450), 0.5, COLORS.deepskyblue) # Font FontText.title = os.path.join(os.getcwd(), "data", "font", "Kenney Blocks.ttf") FontText.normal = os.path.join(os.getcwd(), "data", "font", "Kenney Mini Square.ttf") FontText.update() si_teks = FontText.font_semi_normal.render(f"Show info >", False, COLORS.black) si_rect = si_teks.get_rect(x=20, y=20) show_info = False stat_teks = FontText.font_semi_normal.render(f"< Bola stats :", False, COLORS.black) stat_rect = stat_teks.get_rect(x=20, y=20) custom_vel = InputBox((0, 0), (100, 30), 0, text_color=COLORS.black) custom_vel.font = FontText.font_small btn_right = Button((0,0), (50,50), COLORS.black) btn_left = Button((0,0), (50,50), COLORS.black, left=True) while window.run: events = pygame.event.get() dt = window.clock.tick(120) * 0.001 * window.fps / 1.5 window.update() for event in events: if event.type == QUIT: pygame.quit() sys.exit() if event.type == KEYDOWN: if event.key == K_ESCAPE: pygame.quit() sys.exit() if event.type == MOUSEBUTTONDOWN: if not show_info: if event.button == 1 and collision_test(si_rect.x, si_rect.y, si_rect.w, si_rect.h, event.pos): show_info = True else: if event.button == 1 and collision_test(stat_rect.x, stat_rect.y, stat_rect.w, stat_rect.h, event.pos): show_info = False # Input handle custom_vel.pos.x = window.size[0] - 120 custom_vel.pos.y = (window.size[1]/2) - 100 btn_right.pos.x = window.size[0] - 60 btn_right.pos.y = (window.size[1]/2) - 60 btn_left.pos.x = window.size[0] - 120 btn_left.pos.y = (window.size[1]/2) - 60 InputBox.handle_event(events) Button.handle_event(events) if btn_right.get_click(): bola.velocity.x = custom_vel.value elif btn_left.get_click(): bola.velocity.x = -custom_vel.value # Bola handle bola.get_input(events) bola.grab() bola.update(dt, window) # Bola bola.render(window.surface) if bola.selected: bola.render_string(window.surface) # Custom Velocity custom_vel.render(window.surface) btn_right.render(window.surface) btn_left.render(window.surface) # # Tanah # pygame.draw.line(window.surface, COLORS.black, (0, 600), (window.size[0], 600)) teks = FontText.font_small.render(f"fps = {int(window.clock.get_fps())}", False, COLORS.black) window.surface.blit(teks, (window.size[0] - 100, 20)) teks = FontText.font_small.render(f"Elastis = {bola.can_bounce}", False, COLORS.black) window.surface.blit(teks, (20, 610)) teks = FontText.font_small.render(f"Konstan = {bola.constant}", False, COLORS.black) window.surface.blit(teks, (220, 610)) teks = FontText.font_small.render(f"Dalam box = {bola.in_box}", False, COLORS.black) window.surface.blit(teks, (420, 610)) teks = FontText.font_small.render("Custom", False, COLORS.black) window.surface.blit(teks, (custom_vel.pos.x, custom_vel.pos.y - 50)) teks = FontText.font_small.render("Velocity X", False, COLORS.black) window.surface.blit(teks, (custom_vel.pos.x, custom_vel.pos.y - 30)) # info if not show_info: window.surface.blit(si_teks, si_rect) else: window.surface.blit(stat_teks, stat_rect) teks = [ f"radius (r)", f"angle degrees", f"keliling", f"massa", f"friction", f"vel x", f"vel y", f"koefisien", ] value = [ f"= {bola.size/2}", f"= {bola.angle_degrees:.2f}", f"= {int(bola.keliling)}", f"= {int(bola.massa)}", f"= {bola.friction:.3f}", f"= {bola.velocity.x:.3f}", f"= {bola.velocity.y:.3f}", f"= {bola.koef:.2f}", ] # Teks y = 50 x = render_teks(window.surface, teks, 20, y, get_x=True) x += 40 # Value y = render_teks(window.surface, value, x, y, get_y=True) # Keys y += 20 teks = FontText.font_small.render("- Keys :", False, COLORS.black) window.surface.blit(teks, (20, y)) y += 20 teks = [ "A/D", "W", "E/Q", "S/C", "2/1", "F/G", "B", ] value = [ "= bergerak kiri/kanan", "= lompat", "= Besar/Kecilin bola", "= Elastis/Konstan", "= naik/turunin friction", "= pasang/hapus titik", "= dalam/luar box", ] # Teks x = render_teks(window.surface, teks, 20, y, True) x += 40 # Value render_teks(window.surface, value, x, y) pygame.display.flip()
#============================================================ frame_num = 0 frame_num_countor = 0 day_num = 0 Mycountor = count_total_SIR() Run_flag = False data2 = [0, 0, 0, 0] init_num = 0 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() for box in para_1_boxes_l: box.handle_event(event) for box in para_2_boxes_l: box.handle_event(event) for box in para_3_boxes_l: box.handle_event(event) button_box.handle_event(event) reset_button_box.handle_event(event) Run_flag = button_box.get_flag() Reset_flag = reset_button_box.get_flag() screen_main.fill(WHITE) #Draw buildings Dining_Hall.disp() drawName(Dining_Hall, screen_main)
class NameScene(SceneBase): def __init__(self): SceneBase.__init__(self) self.FONT = pygame.font.SysFont(FONT_FAMILY, FONT_SIZE) self.play_button = pygame.Rect(WIN_WIDTH * 5.5 / 10, WIN_HEIGHT * 3 / 5, WIN_WIDTH / 5, WIN_HEIGHT / 10) self.back_button = pygame.Rect(WIN_WIDTH * 2.5 / 10, WIN_HEIGHT * 3 / 5, WIN_WIDTH / 5, WIN_HEIGHT / 10) self.play_text = self.FONT.render('PLAY', False, pygame.Color(WHITE)) self.back_text = self.FONT.render('back', False, pygame.Color(BLUE)) self.enter_name_text = self.FONT.render('Enter your name', False, pygame.Color(BLUE)) self.name_input_form = InputBox(WIN_WIDTH * 2.5 / 10, WIN_HEIGHT * 2 / 5, WIN_WIDTH / 2, WIN_HEIGHT / 10) self.hint_text = self.FONT.render('', False, pygame.Color(RED)) self.blank_from = True def process_input(self, events, pressed_keys): for event in events: if event.type == pygame.MOUSEBUTTONDOWN: mouse_pos = event.pos if self.play_button.collidepoint(mouse_pos): if self.name_input_form.get_text() != '': self.save() self.switch_to_scene(GameScene()) self.blank_from = False else: self.hint_text = self.FONT.render( 'Enter name!', False, pygame.Color(RED)) if self.back_button.collidepoint(mouse_pos): from menu_scene import MenuScene self.switch_to_scene(MenuScene()) self.name_input_form.handle_event(event) def update(self): pygame.display.update() def render(self, screen): screen.fill(pygame.Color(BACKGROUND_COLOR)) # Play button position screen.blit(self.play_text, (WIN_WIDTH * 5.5 / 10, WIN_HEIGHT * 3 / 5)) # Back score button screen.blit(self.back_text, (WIN_WIDTH * 2.5 / 10, WIN_HEIGHT * 3 / 5)) # Enter name text screen.blit(self.enter_name_text, (WIN_WIDTH * 2.5 / 10, WIN_HEIGHT * 1 / 5)) # Hint text screen.blit(self.hint_text, (WIN_WIDTH * 2.5 / 10, WIN_HEIGHT * 4 / 5)) # Draw input form with text self.name_input_form.draw(screen) def save(self): f = open(SCORE_FILE, 'a') f.write(self.name_input_form.text + ':') f.close()