Update Game_V2.py

master
Gerhards Kristians Tillers 2024-03-03 10:07:51 +00:00
parent 0b11590e52
commit 7b0dae6f96
1 changed files with 320 additions and 320 deletions

View File

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