import bubble_web_module as bw import pygame # Pygame startup win_width = 1024 win_height = 768 pygame.display.init() pygame.font.init() screen = pygame.display.set_mode((win_width, win_height)) clock = pygame.time.Clock() font = pygame.font.SysFont("Courier New", 16) done = False paused = False bw.bubble_radius = 25 # Creating / updating a GLOBAL variable in the bw module. This # variable represents the radius of all bubbles bubble_area = [0, 0, win_width, win_height] bubbles = bw.create_bubbles(20, bubble_area) num_webs = 1 bubble_area_buffer = 0 selected_bubble = 0 while not done: # Update dt = clock.tick() / 1000.0 if paused: dt = 0.0 # ... adjust the bubble_area (in case bubble_area_buffer changed) bubble_area = [bubble_area_buffer // 2, bubble_area_buffer // 2, win_width - bubble_area_buffer, win_height - bubble_area_buffer] # ... update the bubbles bw.update_bubbles(bubbles, dt, bubble_area) # ... as a test of find_neighbors and distance total_distance = 0.0 # (remove the [] and the comment as you begin development) neighbors = bw.find_neighbors(bubbles, bubbles[selected_bubble], num_webs) for n in neighbors: total_distance += bw.distance(bubbles[selected_bubble], n) # Input evt = pygame.event.poll() if evt.type == pygame.QUIT: done = True elif evt.type == pygame.KEYDOWN: if evt.key == pygame.K_ESCAPE: done = True if evt.key == pygame.K_p: paused = not paused # ... adjust number of webs if evt.key == pygame.K_LEFT: num_webs = max(num_webs - 1, 1) elif evt.key == pygame.K_RIGHT: num_webs = min(num_webs + 1, len(bubbles) - 1) # ... adjust play-area if evt.key == pygame.K_UP: bubble_area_buffer = max(0, bubble_area_buffer - 5) elif evt.key == pygame.K_DOWN: bubble_area_buffer = min(bubble_area_buffer + 5, win_height - 20) # ... adjust selected bubble if evt.key == pygame.K_SPACE: selected_bubble = (selected_bubble + 1) % len(bubbles) # Drawing screen.fill((64, 64, 64)) pygame.draw.rect(screen, (0,0,0), bubble_area) bw.draw_bubbles(screen, bubbles, num_webs, selected_bubble) screen.blit(font.render("# Webs: " + str(num_webs), False, (255,255,255)), (0,0)) screen.blit(font.render("fps: " + str(int(clock.get_fps())), False, (255,255,255)), (0, 16)) if paused: temps = font.render("-paused-", False, (255,255,255)) screen.blit(temps, (win_width // 2 - temps.get_width() // 2, win_height // 2 - temps.get_height() // 2)) screen.blit(font.render("[left/right]: change # webs [up/down]:change area, [space]:cycle-selected [p]:pause", False, (255,255,255)), (0, win_height - 16)) screen.blit(font.render("total distance: " + str(int(total_distance)), False, (255,255,255)), (win_width // 2, 0)) pygame.display.flip() pygame.font.quit() pygame.display.quit()