def set_pos(self): """ Choose a spawn point for the zombie. """ pos = rect.Pos(random.random(), random.choice([-self.radius, 1 + self.radius])) # pos = rect.Pos(0.5, 0.5) if random.choice([True, False]): self.pos = rect.Pos(pos.y, pos.x) else: self.pos = pos
def __init__(self): Character.__init__(self) self.pos = rect.Pos(0.5, 0.5) # Multiplied by screen width. self.radius = 0.02 # Multiplied by screen width. self.velocity = 0.15 # Screen widths per second. self.running_time = None self.hand_offset = rect.Pos(0.6, 0.41) # Multiplied by the radius. self.gun = pistol.Pistol() self.max_health = 100 self.health = self.max_health
def set_point2(self, main): """ Set self.point2 - just another point on the path the bullet would travel. """ dx = math.sin(main.game.player.rotation) dy = math.cos(main.game.player.rotation) multiplier = 1.4 self.point2 = rect.Pos(self.point1.x + dy * multiplier, self.point1.y + dx * multiplier)
def reset_point2(self, main): """ If a zombie is hit, make the pistol shot end at that zombie. """ dx = math.sin(main.game.player.rotation) dy = math.cos(main.game.player.rotation) distance = math.sqrt((self.point1.x - self.zombie.pos.x)**2 + (self.point1.y - self.zombie.pos.y)**2) multiplier = distance / (dx**2 + dy**2) self.point2 = rect.Pos(self.point1.x + dy * multiplier, self.point1.y + dx * multiplier)
def mouse_pos(self, main, scale_down=False): """ Get the mouse position on the game window. """ mouse_pos_tup = pygame.mouse.get_pos() mouse_pos = rect.Pos(mouse_pos_tup[0], mouse_pos_tup[1]) mouse_pos.x -= main.game_window_offset.x mouse_pos.y -= main.game_window_offset.y if scale_down: mouse_pos.x /= main.game_window_width mouse_pos.y /= main.game_window_width return mouse_pos
def set_window(self, size=rect.Size(800, 600)): """ Set self.window to a new window of dimentions size (and self.full_window_rect) Set self.game_window to a square surface to be centred on the main window. """ self.full_window = pygame.display.set_mode(size.get_values(), pygame.RESIZABLE) self.full_window_size = size self.game_window_width = min([size.w, size.h]) self.game_window = pygame.Surface((self.game_window_width, ) * 2) self.game_window_offset = rect.Pos( x=(self.full_window_size.w - self.game_window_width) / 2, y=(self.full_window_size.h - self.game_window_width) / 2)
def show_health_bar(self, main): """ Show a little health bar above the characters head. Gets values from self.max_health and self.health. """ bar_width = self.radius * 2 bar_height = bar_width * 0.2 health_per = max(self.health, 0) / self.max_health bar_pos = rect.Pos(self.pos.x - bar_width / 2, self.pos.y - self.radius - bar_height * 2) bar_pos = bar_pos.scale_for_window(main) bar_width *= main.game_window_width bar_height *= main.game_window_width pygame.draw.rect(main.game_window, (200, 0, 0), (bar_pos[0], bar_pos[1], bar_width, bar_height)) pygame.draw.rect( main.game_window, (0, 200, 0), (bar_pos[0], bar_pos[1], bar_width * health_per, bar_height))
def move(self, main): """ Move the character based on WASD input. """ keys = pygame.key.get_pressed() movement = rect.Pos(0, 0) if keys[pygame.K_w]: movement.y -= 1 if keys[pygame.K_s]: movement.y += 1 if keys[pygame.K_a]: movement.x -= 1 if keys[pygame.K_d]: movement.x += 1 self.last_movement = movement multiplier = 1 - abs(0.29289 * int(movement.x and movement.y)) self.pos.x += movement.x * main.dtime * self.velocity * multiplier self.pos.y += movement.y * main.dtime * self.velocity * multiplier
def show_clip(self, main): """ Show a bar in the top right of the screen, representing bullets in the gun. """ bar_height = main.game_window_width // 20 bar_width = bar_height * 5 clip_per = max(self.player.gun.clip, 0) / self.player.gun.clip_size bar_pos = rect.Pos( main.full_window_size.w - bar_width - bar_height * (2 / 5), bar_height * (2 / 5)) if self.player.gun.reloading: bar_fill = bar_width - bar_width * (self.player.gun.reloading / self.player.gun.reload_time) else: bar_fill = bar_width * clip_per pygame.draw.rect(main.full_window, (0, 0, 0), (bar_pos.x, bar_pos.y, bar_width, bar_height)) pygame.draw.rect(main.full_window, (150, 150, 150), (bar_pos.x, bar_pos.y, round(bar_fill), bar_height))
def show(self, main): """ Show the gun. """ image = main.images["guns"]["pistol"]["pistol"] image_size = rect.Size(pygame_rect=image.get_rect()) scale = (2 * self.width * main.game_window_width * main.game.player.radius) / image_size.h image_size.w *= scale image_size.h *= scale scaled_image = pygame.transform.scale(image, image_size.get_rounded_values()) rotated_image = pygame.transform.rotate( scaled_image, -math.degrees(main.game.player.rotation)) hand_pos = main.game.player.hand_pos() hand_pos.scale_up(main.game_window_width, main.game_window_width) rotated_size = rect.Size(pygame_rect=rotated_image.get_rect()) image_pos = rect.Pos(hand_pos.x - rotated_size.w / 2, hand_pos.y - rotated_size.h / 2) main.game_window.blit(rotated_image, image_pos.get_rounded_values())
def __init__(self): self.radius = 0.05 self.pos = rect.Pos(0.5, 0.5) self.rotation = 0 self.max_health = 10 self.health = self.max_health