diff --git a/Game_V2.py b/Game_V2.py index 7635862..3b4e7ca 100644 --- a/Game_V2.py +++ b/Game_V2.py @@ -1,320 +1,320 @@ -import pygame -from random import randint -import math - -SCREEN_X = 1920 -SCREEN_Y = 1015 - -# pygame setup -pygame.init() -pygame.display.set_caption("Vampire survivors") -screen = pygame.display.set_mode((SCREEN_X, SCREEN_Y)) -clock = pygame.time.Clock() -running = True - -class Player(pygame.sprite.Sprite): - def __init__(self, pos, group): - super().__init__(group) - self.idle_image = self.image = pygame.image.load("Sprites/Idle/Player_right.png").convert_alpha() - self.rect = self.image.get_rect(center = pos) - self.direction = pygame.math.Vector2() - self.speed = 2.25 - - #health - self.current_health = 1000 #health - self.maximum_health = 1000 - self.health_bar_lenght = 500 - self.health_ratio = self.maximum_health / self.health_bar_lenght - self.target_health = 1000 - self.health_change_speed = 5 - - # Shooting - self.can_shoot = True - self.shoot_cooldown_time = 100 # Milliseconds - self.shoot_cooldown = 0 - - - - # animations - self.image_direction = 1 - self.animation_timer = 0 - self.current_sprite = 0 - self.running_sprites = [] - for i in range(6): - self.running_sprites.append(pygame.image.load("Sprites/Running/Player_run_right_"+str(i+1)+".png").convert_alpha()) - - - def get_damage(self, amount): - if self.target_health > 0: - self.target_health -= amount - if self.target_health <= 0: - self.target_health = 0 - - - def get_health(self, amnount): - if self.target_health < self.maximum_health: - self.target_health += amnount - if self.target_health >= self.maximum_health: - self.target_health = self.maximum_health - - - - def ahealth_bar(self): - - self.current_health += (self.target_health-self.current_health)/10 - - # If self.current_health < self.target_health - transition_colour = (0, 255, 0) - health_bar_width = self.current_health/self.health_ratio - transition_bar_width = self.target_health/self.health_ratio - - if self.current_health > self.target_health: - health_bar_width = self.target_health/self.health_ratio - transition_bar_width = self.current_health/self.health_ratio - transition_colour = (255, 255, 0) - - health_bar_rect = pygame.Rect(10,45,health_bar_width,25) - transition_bar_rect = pygame.Rect(10,45,transition_bar_width,25) - - pygame.draw.rect(screen, transition_colour, transition_bar_rect) - pygame.draw.rect(screen,(255,0,0),health_bar_rect) - pygame.draw.rect(screen,(255,255,255), (10,45,self.health_bar_lenght,25),4) - - def input(self): - keys = pygame.key.get_pressed() - speed_multiplier_x = 0 - speed_multiplier_y = 0 - - if self.direction.x > 0: - self.image_direction = 1 - elif self.direction.x < 0: - self.image_direction = -1 - - if keys[pygame.K_w]: - speed_multiplier_y = -1 - elif keys[pygame.K_s]: - speed_multiplier_y = 1 - - # Check if A and D keys are pressed - if keys[pygame.K_a]: - speed_multiplier_x = -1 - elif keys[pygame.K_d]: - speed_multiplier_x = 1 - - # Calculate movement direction based on keys pressed - self.direction.x = speed_multiplier_x - self.direction.y = speed_multiplier_y - - - self.direction.y = (keys[pygame.K_s]) - (keys[pygame.K_w]) - self.direction.x = (keys[pygame.K_d]) - (keys[pygame.K_a]) - - if self.direction.x != 0 and self.direction.y != 0: - self.direction.normalize_ip() - - self.direction.x *= self.speed - self.direction.y *= self.speed - - - if pygame.mouse.get_pressed()[0] and self.can_shoot: - direction = -(pygame.math.Vector2(self.rect.topleft) - pygame.mouse.get_pos() - camera_group.offset) - direction = pygame.math.Vector2.normalize(direction) - new_projectile = Projectile(self.rect.center, direction, camera_group) - self.can_shoot = False - self.shoot_cooldown = pygame.time.get_ticks() - - def update(self): - self.input() - self.ahealth_bar() - self.rect.center += self.direction * self.speed - - now = pygame.time.get_ticks() - if now - self.shoot_cooldown > self.shoot_cooldown_time: - self.can_shoot = True - - if now - self.animation_timer > 50: - if pygame.Vector2.length(self.direction) == 0: - self.image = self.idle_image - self.current_sprite = 0 - else: - self.animation_timer = pygame.time.get_ticks() - self.current_sprite += 1 - if self.current_sprite > len(self.running_sprites)-1: - self.current_sprite = 0 - self.image = self.running_sprites[self.current_sprite] - -class Arm(pygame.sprite.Sprite): - def __init__(self, pos, group): - super().__init__(group) - self.base_image = pygame.image.load("Sprites/minigun.png").convert_alpha() - self.base_image = pygame.transform.scale(self.base_image, (40, 15)) - - self.image_direction = 1 - - self.direction = pygame.Vector2(0, 0) - self.speed = 10 - angle = math.degrees(math.atan2(-self.direction.y, self.direction.x)) - self.image = pygame.transform.rotate(self.base_image, angle) - self.rect = self.image.get_rect(center = pos) - - def update(self): - self.rect.center = player.rect.center - self.direction = -(pygame.math.Vector2(self.rect.topleft) - pygame.mouse.get_pos() - camera_group.offset) - self.direction = pygame.math.Vector2.normalize(self.direction) - angle = math.degrees(math.atan2(-self.direction.y, self.direction.x)) - if(abs(angle)<90): - self.image_direction = 1 - else: - self.image_direction = -1 - self.rect.center -= pygame.Vector2(math.sin(math.radians(angle-90))*20, math.cos(math.radians(angle-90))*20) - if(abs(angle)>90): - angle = math.degrees(math.atan2(self.direction.y, self.direction.x))+180 - self.image = pygame.transform.rotate(self.base_image, angle) - - -class Projectile(pygame.sprite.Sprite): - def __init__(self, pos, direction, group): - super().__init__(group) - self.image = pygame.image.load("Sprites/bullet.png").convert_alpha() - self.image_direction = 1 - - self.direction = direction - self.speed = 15 - angle = math.degrees(math.atan2(-self.direction.y, self.direction.x)) - - self.image = pygame.transform.rotate(self.image, angle) - self.rect = self.image.get_rect(center = pos) - self.rect.center += self.direction * 50 - - self.life_timer = 10000 # Milliseconds - self.spawned_time = pygame.time.get_ticks() - - def collision(self): - for enemy in enemies: - distance = pygame.math.Vector2.length(pygame.math.Vector2(enemy.rect.center) - self.rect.center) - if distance < 20: - enemy.damage(1) - self.kill() - break - - def update(self): - self.rect.center += self.direction * self.speed - self.collision() - - now = pygame.time.get_ticks() - if now - self.spawned_time > self.life_timer: - self.kill() - -class Enemy(pygame.sprite.Sprite): - def __init__(self, pos, group): - super().__init__(group) - self.image = pygame.surface.Surface((20, 20)) - self.image.fill("red") - self.rect = self.image.get_rect(center = pos) - self.pos = self.rect.center - self.direction = pygame.math.Vector2() - self.speed = 2 - self.health = 2 - - self.image_direction = 1 - - - - def damage(self, damage): - self.health -= 1 - if self.health <= 0: - enemies.remove(self) - self.kill() - - - def update(self): - self.direction = pygame.math.Vector2(player.rect.center) - self.rect.center - if(pygame.math.Vector2.length(self.direction)<20): - player.get_damage(200) - self.kill() - enemies.remove(self) - self.pos += self.direction.normalize() * self.speed - self.rect.center = (round(self.pos.x), round(self.pos.y)) - -class CameraGroup(pygame.sprite.Group): - def __init__(self): - super().__init__() - self.display_surface = pygame.display.get_surface() - - self.offset = pygame.math.Vector2(300, 100) - self.half_w = self.display_surface.get_size()[0] // 2 - self.half_h = self.display_surface.get_size()[1] // 2 - - def center_target_camera(self, target): - self.offset.x = target.rect.centerx - self.half_w - self.offset.y = target.rect.centery - self.half_h - - def custom_draw(self, player): - self.center_target_camera(player) - - for sprite in sorted(self.sprites(), key=lambda sprite: sprite.rect.centery): - offset_pos = sprite.rect.topleft - self.offset - if sprite.image_direction == -1: - self.display_surface.blit(pygame.transform.flip(sprite.image, True, False), offset_pos) - else: - self.display_surface.blit(sprite.image, offset_pos) - -def spawn_enemy(): - if len(enemies) < 20: - random_x = randint(-1000, 1000) - random_y = randint(-1000, 1000) - enemies.append(Enemy((player.rect.centerx + random_x, player.rect.centery + random_y), camera_group)) - - -spawn_enemy_event = pygame.USEREVENT + 1 -#Setup -camera_group = CameraGroup() -enemies = [] - - - - -player = Player((500, 500), camera_group) -Arm((0, 0), camera_group) -pygame.time.set_timer(spawn_enemy_event, 100) - -crosshair_img = pygame.transform.scale(pygame.image.load('Sprites/crosshair.png'), (30, 30)) -cursor = pygame.cursors.Cursor((0, 0), crosshair_img) -pygame.mouse.set_cursor(cursor) -pygame.mouse.set_visible(True | False) - - - -while running: - for e in pygame.event.get(): - if e.type == pygame.QUIT: - running = False - pygame.quit() - - if e.type == spawn_enemy_event: - spawn_enemy() - - if e.type == pygame.KEYDOWN: - if e.key == pygame.K_UP: - player.get_health(200) - - if e.type == pygame.KEYDOWN: - if e.key == pygame.K_DOWN: - player.get_damage(200) - - - - - - screen.fill("cyan") - - camera_group.update() - camera_group.custom_draw(player) - - - pygame.display.flip() - - clock.tick(60) - - - +import pygame +from random import randint +import math + +SCREEN_X = 1280 +SCREEN_Y = 720 + +# pygame setup +pygame.init() +pygame.display.set_caption("Vampire survivors") +screen = pygame.display.set_mode((SCREEN_X, SCREEN_Y)) +clock = pygame.time.Clock() +running = True + +class Player(pygame.sprite.Sprite): + def __init__(self, pos, group): + super().__init__(group) + self.idle_image = self.image = pygame.image.load("Sprites/Idle/Player_right.png").convert_alpha() + self.rect = self.image.get_rect(center = pos) + self.direction = pygame.math.Vector2() + self.speed = 2.25 + + #health + self.current_health = 1000 #health + self.maximum_health = 1000 + self.health_bar_lenght = 500 + self.health_ratio = self.maximum_health / self.health_bar_lenght + self.target_health = 1000 + self.health_change_speed = 5 + + # Shooting + self.can_shoot = True + self.shoot_cooldown_time = 100 # Milliseconds + self.shoot_cooldown = 0 + + + + # animations + self.image_direction = 1 + self.animation_timer = 0 + self.current_sprite = 0 + self.running_sprites = [] + for i in range(6): + self.running_sprites.append(pygame.image.load("Sprites/Running/Player_run_right_"+str(i+1)+".png").convert_alpha()) + + + def get_damage(self, amount): + if self.target_health > 0: + self.target_health -= amount + if self.target_health <= 0: + self.target_health = 0 + + + def get_health(self, amnount): + if self.target_health < self.maximum_health: + self.target_health += amnount + if self.target_health >= self.maximum_health: + self.target_health = self.maximum_health + + + + def ahealth_bar(self): + + self.current_health += (self.target_health-self.current_health)/10 + + # If self.current_health < self.target_health + transition_colour = (0, 255, 0) + health_bar_width = self.current_health/self.health_ratio + transition_bar_width = self.target_health/self.health_ratio + + if self.current_health > self.target_health: + health_bar_width = self.target_health/self.health_ratio + transition_bar_width = self.current_health/self.health_ratio + transition_colour = (255, 255, 0) + + health_bar_rect = pygame.Rect(10,45,health_bar_width,25) + transition_bar_rect = pygame.Rect(10,45,transition_bar_width,25) + + pygame.draw.rect(screen, transition_colour, transition_bar_rect) + pygame.draw.rect(screen,(255,0,0),health_bar_rect) + pygame.draw.rect(screen,(255,255,255), (10,45,self.health_bar_lenght,25),4) + + def input(self): + keys = pygame.key.get_pressed() + speed_multiplier_x = 0 + speed_multiplier_y = 0 + + if self.direction.x > 0: + self.image_direction = 1 + elif self.direction.x < 0: + self.image_direction = -1 + + if keys[pygame.K_w]: + speed_multiplier_y = -1 + elif keys[pygame.K_s]: + speed_multiplier_y = 1 + + # Check if A and D keys are pressed + if keys[pygame.K_a]: + speed_multiplier_x = -1 + elif keys[pygame.K_d]: + speed_multiplier_x = 1 + + # Calculate movement direction based on keys pressed + self.direction.x = speed_multiplier_x + self.direction.y = speed_multiplier_y + + + self.direction.y = (keys[pygame.K_s]) - (keys[pygame.K_w]) + self.direction.x = (keys[pygame.K_d]) - (keys[pygame.K_a]) + + if self.direction.x != 0 and self.direction.y != 0: + self.direction.normalize_ip() + + self.direction.x *= self.speed + self.direction.y *= self.speed + + + if pygame.mouse.get_pressed()[0] and self.can_shoot: + direction = -(pygame.math.Vector2(self.rect.topleft) - pygame.mouse.get_pos() - camera_group.offset) + direction = pygame.math.Vector2.normalize(direction) + new_projectile = Projectile(self.rect.center, direction, camera_group) + self.can_shoot = False + self.shoot_cooldown = pygame.time.get_ticks() + + def update(self): + self.input() + self.ahealth_bar() + self.rect.center += self.direction * self.speed + + now = pygame.time.get_ticks() + if now - self.shoot_cooldown > self.shoot_cooldown_time: + self.can_shoot = True + + if now - self.animation_timer > 50: + if pygame.Vector2.length(self.direction) == 0: + self.image = self.idle_image + self.current_sprite = 0 + else: + self.animation_timer = pygame.time.get_ticks() + self.current_sprite += 1 + if self.current_sprite > len(self.running_sprites)-1: + self.current_sprite = 0 + self.image = self.running_sprites[self.current_sprite] + +class Arm(pygame.sprite.Sprite): + def __init__(self, pos, group): + super().__init__(group) + self.base_image = pygame.image.load("Sprites/minigun.png").convert_alpha() + self.base_image = pygame.transform.scale(self.base_image, (40, 15)) + + self.image_direction = 1 + + self.direction = pygame.Vector2(0, 0) + self.speed = 10 + angle = math.degrees(math.atan2(-self.direction.y, self.direction.x)) + self.image = pygame.transform.rotate(self.base_image, angle) + self.rect = self.image.get_rect(center = pos) + + def update(self): + self.rect.center = player.rect.center + self.direction = -(pygame.math.Vector2(self.rect.topleft) - pygame.mouse.get_pos() - camera_group.offset) + self.direction = pygame.math.Vector2.normalize(self.direction) + angle = math.degrees(math.atan2(-self.direction.y, self.direction.x)) + if(abs(angle)<90): + self.image_direction = 1 + else: + self.image_direction = -1 + self.rect.center -= pygame.Vector2(math.sin(math.radians(angle-90))*20, math.cos(math.radians(angle-90))*20) + if(abs(angle)>90): + angle = math.degrees(math.atan2(self.direction.y, self.direction.x))+180 + self.image = pygame.transform.rotate(self.base_image, angle) + + +class Projectile(pygame.sprite.Sprite): + def __init__(self, pos, direction, group): + super().__init__(group) + self.image = pygame.image.load("Sprites/bullet.png").convert_alpha() + self.image_direction = 1 + + self.direction = direction + self.speed = 15 + angle = math.degrees(math.atan2(-self.direction.y, self.direction.x)) + + self.image = pygame.transform.rotate(self.image, angle) + self.rect = self.image.get_rect(center = pos) + self.rect.center += self.direction * 50 + + self.life_timer = 10000 # Milliseconds + self.spawned_time = pygame.time.get_ticks() + + def collision(self): + for enemy in enemies: + distance = pygame.math.Vector2.length(pygame.math.Vector2(enemy.rect.center) - self.rect.center) + if distance < 20: + enemy.damage(1) + self.kill() + break + + def update(self): + self.rect.center += self.direction * self.speed + self.collision() + + now = pygame.time.get_ticks() + if now - self.spawned_time > self.life_timer: + self.kill() + +class Enemy(pygame.sprite.Sprite): + def __init__(self, pos, group): + super().__init__(group) + self.image = pygame.surface.Surface((20, 20)) + self.image.fill("red") + self.rect = self.image.get_rect(center = pos) + self.pos = self.rect.center + self.direction = pygame.math.Vector2() + self.speed = 2 + self.health = 2 + + self.image_direction = 1 + + + + def damage(self, damage): + self.health -= 1 + if self.health <= 0: + enemies.remove(self) + self.kill() + + + def update(self): + self.direction = pygame.math.Vector2(player.rect.center) - self.rect.center + if(pygame.math.Vector2.length(self.direction)<20): + player.get_damage(200) + self.kill() + enemies.remove(self) + self.pos += self.direction.normalize() * self.speed + self.rect.center = (round(self.pos.x), round(self.pos.y)) + +class CameraGroup(pygame.sprite.Group): + def __init__(self): + super().__init__() + self.display_surface = pygame.display.get_surface() + + self.offset = pygame.math.Vector2(300, 100) + self.half_w = self.display_surface.get_size()[0] // 2 + self.half_h = self.display_surface.get_size()[1] // 2 + + def center_target_camera(self, target): + self.offset.x = target.rect.centerx - self.half_w + self.offset.y = target.rect.centery - self.half_h + + def custom_draw(self, player): + self.center_target_camera(player) + + for sprite in sorted(self.sprites(), key=lambda sprite: sprite.rect.centery): + offset_pos = sprite.rect.topleft - self.offset + if sprite.image_direction == -1: + self.display_surface.blit(pygame.transform.flip(sprite.image, True, False), offset_pos) + else: + self.display_surface.blit(sprite.image, offset_pos) + +def spawn_enemy(): + if len(enemies) < 20: + random_x = randint(-1000, 1000) + random_y = randint(-1000, 1000) + enemies.append(Enemy((player.rect.centerx + random_x, player.rect.centery + random_y), camera_group)) + + +spawn_enemy_event = pygame.USEREVENT + 1 +#Setup +camera_group = CameraGroup() +enemies = [] + + + + +player = Player((500, 500), camera_group) +Arm((0, 0), camera_group) +pygame.time.set_timer(spawn_enemy_event, 100) + +crosshair_img = pygame.transform.scale(pygame.image.load('Sprites/crosshair.png'), (30, 30)) +cursor = pygame.cursors.Cursor((0, 0), crosshair_img) +pygame.mouse.set_cursor(cursor) +pygame.mouse.set_visible(True | False) + + + +while running: + for e in pygame.event.get(): + if e.type == pygame.QUIT: + running = False + pygame.quit() + + if e.type == spawn_enemy_event: + spawn_enemy() + + if e.type == pygame.KEYDOWN: + if e.key == pygame.K_UP: + player.get_health(200) + + if e.type == pygame.KEYDOWN: + if e.key == pygame.K_DOWN: + player.get_damage(200) + + + + + + screen.fill("cyan") + + camera_group.update() + camera_group.custom_draw(player) + + + pygame.display.flip() + + clock.tick(60) + + +