|
| 1 | +import math |
| 2 | +import random |
| 3 | +import pygame |
| 4 | + |
| 5 | +from core import update |
| 6 | + |
| 7 | +BACKGROUND=(40, 110, 150) |
| 8 | +VICTORY_BG=(0xff, 0xd3, 0x00) |
| 9 | +DEFEAT_BG=(106, 40, 126) |
| 10 | + |
| 11 | +START_ALPHAS=7 |
| 12 | +GOAL_RADIUS=50 |
| 13 | +ALPHA_RADIUS=update.ALPHA_RADIUS # TODO refactor |
| 14 | +MAX_SPEED=10 |
| 15 | + |
| 16 | + |
| 17 | +def direction(key, speed=MAX_SPEED): |
| 18 | + if key == pygame.K_DOWN: |
| 19 | + return (0,speed) |
| 20 | + if key == pygame.K_UP: |
| 21 | + return (0,0-speed) |
| 22 | + if key == pygame.K_RIGHT: |
| 23 | + return (speed,0) |
| 24 | + if key == pygame.K_LEFT: |
| 25 | + return (0-speed,0) |
| 26 | + return (0,0) |
| 27 | + |
| 28 | + |
| 29 | +def reset_velocity(velocity, key): |
| 30 | + vx, vy = velocity |
| 31 | + if key in [pygame.K_DOWN, pygame.K_UP]: |
| 32 | + vy = 0 |
| 33 | + if key in [pygame.K_LEFT, pygame.K_RIGHT]: |
| 34 | + vx = 0 |
| 35 | + return (vx, vy) |
| 36 | + |
| 37 | + |
| 38 | +def get_background(num): |
| 39 | + min = [30, 100, 160] |
| 40 | + max = [85, 240, 255] |
| 41 | + def update_color(i, c): |
| 42 | + return random.randrange(min[i], max[i]) |
| 43 | + return [update_color(i,c) for (i,c) in enumerate(BACKGROUND)] |
| 44 | + |
| 45 | + |
| 46 | +def generate_position(screen_size): |
| 47 | + max_x, max_y = screen_size |
| 48 | + x = random.uniform(0, max_x) |
| 49 | + y = random.uniform(0, max_y) |
| 50 | + return (x,y) |
| 51 | + |
| 52 | + |
| 53 | +def generate_position_with_distance(player, image_size, distance, screen_size): |
| 54 | + pos = generate_position(screen_size) |
| 55 | + while update.touches_circle(player, image_size, pos, distance): |
| 56 | + pos = generate_position(screen_size) |
| 57 | + return pos |
| 58 | + |
| 59 | + |
| 60 | +def generate_alphas(player, number, image_size, screen_size): |
| 61 | + alphas = [] |
| 62 | + alpha_velocities = [] |
| 63 | + for i in range(number): |
| 64 | + alphas.append(generate_position_with_distance(player, image_size, 200, screen_size)) |
| 65 | + alpha_velocities.append((0,0)) |
| 66 | + return alphas, alpha_velocities |
| 67 | + |
| 68 | + |
| 69 | +class Level: |
| 70 | + |
| 71 | + def __init__(self, num_level, screen, resources): |
| 72 | + self.num_level = num_level |
| 73 | + self.screen = screen |
| 74 | + self.resources = resources |
| 75 | + self.survived_iterations = 0 |
| 76 | + |
| 77 | + self.screen_size = self.screen.get_size() |
| 78 | + self.image_size = self.resources.player_img.get_rect().size |
| 79 | + |
| 80 | + self.won = False |
| 81 | + self.lost = False |
| 82 | + |
| 83 | + self.player = (0,0) |
| 84 | + self.velocity = (0,0) |
| 85 | + |
| 86 | + self.goal = generate_position_with_distance(self.player, self.image_size, 350, self.screen_size) |
| 87 | + num_deltas = 0 |
| 88 | + if num_level >= 3: |
| 89 | + num_deltas = num_level - 2 |
| 90 | + num_alphas = START_ALPHAS + 3 * (self.num_level - 1) - 2*num_deltas |
| 91 | + self.alphas, self.alpha_velocities = generate_alphas(self.player, num_alphas, self.image_size, self.screen_size) |
| 92 | + self.background = get_background(self.num_level) |
| 93 | + |
| 94 | + self.delta_alphas, self.delta_velocities = generate_alphas(self.player, num_deltas, self.image_size, self.screen_size) |
| 95 | + |
| 96 | + |
| 97 | + def draw_img(self, img, center_pos): |
| 98 | + w = img.get_width() |
| 99 | + h = img.get_height() |
| 100 | + (x,y) = center_pos |
| 101 | + self.screen.blit(img, (x - w/2, y - h/2)) |
| 102 | + |
| 103 | + |
| 104 | + def draw(self): |
| 105 | + '''Draws the current state.''' |
| 106 | + if self.won: |
| 107 | + background = VICTORY_BG |
| 108 | + elif self.lost: |
| 109 | + background = DEFEAT_BG |
| 110 | + else: |
| 111 | + background = self.background |
| 112 | + |
| 113 | + self.screen.fill(background) |
| 114 | + self.draw_img(self.resources.goal_img, self.goal) |
| 115 | + |
| 116 | + self.draw_img(self.resources.player_img, self.player) |
| 117 | + for alpha in self.alphas: |
| 118 | + self.draw_img(self.resources.alpha_img, alpha) |
| 119 | + |
| 120 | + for delta in self.delta_alphas: |
| 121 | + self.draw_img(self.resources.delta_img, delta) |
| 122 | + |
| 123 | + pygame.display.flip() |
| 124 | + |
| 125 | + |
| 126 | + def tick(self): |
| 127 | + '''Updates the model one iteration. |
| 128 | +
|
| 129 | + If already won or lost, changes nothing. |
| 130 | + ''' |
| 131 | + if self.won or self.lost: |
| 132 | + return |
| 133 | + |
| 134 | + self.survived_iterations += 1 |
| 135 | + self.player = update.add(self.player, self.velocity) |
| 136 | + self.player = update.bounds(self.player, self.image_size, self.screen_size) |
| 137 | + self.alphas, self.alpha_velocities = update.move_alphas(self.alphas, self.alpha_velocities, self.screen_size) |
| 138 | + |
| 139 | + self.delta_alphas, self.delta_velocities = update.move_deltas(self.delta_alphas, self.delta_velocities, self.screen_size, self.player) |
| 140 | + |
| 141 | + if update.touches_circle(self.player, self.image_size, self.goal, GOAL_RADIUS): |
| 142 | + pygame.mixer.Sound.play(self.resources.door_sound) |
| 143 | + self.won = True |
| 144 | + |
| 145 | + for alpha in (self.alphas + self.delta_alphas): |
| 146 | + if not self.won and update.touches_circle(self.player, self.image_size, alpha, ALPHA_RADIUS): |
| 147 | + pygame.mixer.music.stop() |
| 148 | + pygame.mixer.Sound.play(self.resources.explosion_sound) |
| 149 | + self.lost = True |
| 150 | + break |
| 151 | + |
| 152 | + |
| 153 | + def compute_score(self): |
| 154 | + '''If won, returns max score. |
| 155 | +
|
| 156 | + Otherwise return asymptotically approaching max score based on survived_iterations. |
| 157 | + ''' |
| 158 | + max_score = self.num_level * 1000 |
| 159 | + if self.won: return max_score |
| 160 | + |
| 161 | + k = 1e-3 |
| 162 | + progression = 1 - math.exp(-k*self.survived_iterations) |
| 163 | + return math.floor(max_score * progression) |
| 164 | + |
| 165 | + |
| 166 | + def reset_direction(self, key): |
| 167 | + self.velocity = reset_velocity(self.velocity, key) |
| 168 | + |
| 169 | + |
| 170 | + def add_direction(self, key): |
| 171 | + d = direction(key) |
| 172 | + self.velocity = update.add(self.velocity, d) |
0 commit comments