Initial version

This commit is contained in:
csd4ni3l
2025-06-10 11:05:55 +02:00
commit 333a4e307e
27 changed files with 1880 additions and 0 deletions

92
game/enemy.py Normal file
View File

@@ -0,0 +1,92 @@
from ursina import *
from utils.constants import min_enemy_speed, max_enemy_speed, enemy_health, min_enemy_movement, max_enemy_movement
from ursina.shaders import lit_with_shadows_shader
class Enemy(Entity):
def __init__(self, player, shootables_parent, x, y, z, texture):
super().__init__(parent=shootables_parent, model='cube', collider='box', texture=texture, x=x, y=y, z=z, shader=lit_with_shadows_shader)
self.health_bar = Entity(parent=self, y=1.2, model='cube', color=color.red, world_scale=(1.5,.1,.1))
self.max_hp = enemy_health
self.hp = self.max_hp
self.type = random.choice(["left", "right", "top", "bottom"])
self.movement_done = 0
self.speed = random.uniform(min_enemy_speed, max_enemy_speed)
self.movement_amount = random.uniform(min_enemy_movement, max_enemy_movement)
self.player = player
self.path_line = Entity(parent=self.parent, model='cube', color=color.red, position=(0, 0), rotation=(0, 0, 0), shader=lit_with_shadows_shader)
self.update_path_line()
def update_path_line(self):
remaining = self.movement_amount - self.movement_done
if remaining <= 0:
return
start = self.position
end = self.position
if self.type == "left":
end -= Vec3(max(0.05, remaining), 0, 0)
scale = Vec3(max(0.05, remaining), 0.05, 0.05)
elif self.type == "right":
end += Vec3(max(0.05, remaining), 0, 0)
scale = Vec3(max(0.05, remaining), 0.05, 0.05)
elif self.type == "top":
end += Vec3(0, max(0.05, remaining), 0)
scale = Vec3(0.05, max(0.05, remaining), 0.05)
elif self.type == "bottom":
end -= Vec3(0, max(0.05, remaining), 0)
scale = Vec3(0.05, max(0.05, remaining), 0.05)
mid = (start + end) / 2
self.path_line.position = mid
self.path_line.scale = scale
def update(self):
self.health_bar.alpha = max(0, self.health_bar.alpha - time.dt)
self.health_bar.look_at_2d(self.player, axis="x")
self.look_at_2d(self.player, axis="y")
if self.type == "left":
if self.movement_done < self.movement_amount:
self.x -= self.speed
self.movement_done += self.speed
else:
self.type = "right"
self.movement_done = 0
elif self.type == "right":
if self.movement_done < self.movement_amount:
self.x += self.speed
self.movement_done += self.speed
else:
self.type = "left"
self.movement_done = 0
elif self.type == "top":
if self.movement_done < self.movement_amount:
self.y += self.speed
self.movement_done += self.speed
else:
self.type = "bottom"
self.movement_done = 0
elif self.type == "bottom":
if self.movement_done < self.movement_amount:
self.y -= self.speed
self.movement_done += self.speed
else:
self.type = "top"
self.movement_done = 0
self.update_path_line()
@property
def hp(self):
return self._hp
@hp.setter
def hp(self, value):
self._hp = value
self.health_bar.world_scale_x = self.hp / self.max_hp * 1.5
self.health_bar.alpha = 1

72
game/game.py Normal file
View File

@@ -0,0 +1,72 @@
from game.inventory import Inventory
from game.player import Player
from game.enemy import Enemy
from utils.constants import weapons, min_enemy_y, max_enemy_y
from ursina import *
from ursina.shaders import lit_with_shadows_shader
import os
file_names = os.listdir("enemy_images")
class Game():
def __init__(self, pypresence_client) -> None:
self.pypresence_client = pypresence_client
pypresence_client.update(state='Training Aim', details=f'Hits: 0/0 Accuracy: 0%')
self.ground = Entity(model='plane', collider='box', scale=64, texture='grass', texture_scale=(4,4), shader=lit_with_shadows_shader)
self.info_label = Text("Score: 0 Hits: 0/0 Accuracy: 0%", parent=camera.ui, position=(-0.1, 0.475))
self.inventory = Inventory(slots=len(weapons))
for n, weapon in enumerate(weapons):
self.inventory.append(weapons[weapon]["image"], weapon, n)
self.player = Player(self.info_label, self.inventory, pypresence_client)
self.shootables_parent = Entity()
mouse.traverse_target = self.shootables_parent
self.enemies = []
for i in range(15):
self.summon_enemy()
self.player.summon_enemy = self.summon_enemy
self.sun = DirectionalLight()
self.sun.look_at(Vec3(1,-1,-1))
self.sky = Sky()
self.sky.update = self.update
def summon_enemy(self):
if not len(self.enemies) >= 50:
self.enemies.append(Enemy(self.player, self.shootables_parent, self.player.x + (random.randint(12, 24) * random.choice([1, -1])), random.randint(min_enemy_y, max_enemy_y), self.player.z + (random.randint(12, 24) * random.choice([1, -1])), "enemy_images/" + random.choice(file_names)))
def update(self):
Sky.update(self.sky)
if held_keys["escape"]:
self.back_to_main_menu()
elif held_keys["space"]:
self.summon_enemy()
def back_to_main_menu(self):
self.hide()
from menus.main import Main
Main(self.pypresence_client)
def hide(self):
destroy(self.ground)
destroy(self.sun)
destroy(self.sky)
Sky.instances.remove(self.sky)
destroy(self.info_label)
self.inventory.hide()
self.player.hide()
destroy(self.shootables_parent)
for enemy in self.enemies:
destroy(enemy)

71
game/inventory.py Normal file
View File

@@ -0,0 +1,71 @@
from ursina import Entity, Quad, color, camera, held_keys, destroy
class Inventory():
def __init__(self, x=0, y=4.5, width=12, height=1, slots=5):
self.width = width
self.height = height
self.x = x
self.y = y
self.slot_number = slots
self.slot_width = width / self.slot_number
self.slot_grid = {}
self.item_grid = {}
self.slot_names = {}
self.current_slot = 0
self.create_grid()
self.slot_grid[0].input = self.input
def switch_to(self, slot):
self.item_grid[self.current_slot].color = color.gray
self.slot_grid[self.current_slot].color = color.gray
self.current_slot = slot
self.item_grid[slot].color = color.white
self.slot_grid[slot].color = color.white
def input(self, key):
if key.isnumeric() and int(key) <= self.slot_number:
self.switch_to(int(key) - 1)
if key == "scroll up":
self.switch_to(min(self.slot_number - 1, self.current_slot + 1))
elif key == "scroll down":
self.switch_to(max(0, self.current_slot - 1))
def create_grid(self):
for slot in range(self.slot_number):
self.slot_grid[slot] = Entity(
parent = camera.ui,
model = Quad(radius=.015),
texture = 'white_cube',
scale = (self.slot_width * 0.1, self.height * 0.1),
origin = (-slot * (self.slot_width / 2), 0),
position = (-.55, -.4),
color = color.gray if slot != self.current_slot else color.white
)
def append(self, item, name, slot):
self.slot_names[slot] = name
self.item_grid[slot] = Entity(
parent = camera.ui,
model = Quad(radius=.015),
texture = item,
scale = (self.slot_width * 0.1, self.height * 0.1),
origin = (-slot * (self.slot_width / 2), 0),
position = (-.55, -.4),
z=-1,
color = color.gray if slot != self.current_slot else color.white
)
def hide(self):
for item_entity in self.item_grid.values():
destroy(item_entity)
for slot_entity in self.slot_grid.values():
destroy(slot_entity)

87
game/player.py Normal file
View File

@@ -0,0 +1,87 @@
from ursina.prefabs.ursfx import ursfx
from ursina.prefabs.first_person_controller import FirstPersonController
from ursina import *
from utils.constants import weapons, max_enemy_speed
from ursina.shaders import lit_with_shadows_shader
import json
class Player(FirstPersonController):
def __init__(self, info_label, inventory, pypresence_client) -> None:
super().__init__(model='cube', z=16, color=color.orange, origin_y=-.5, speed=8, collider='box', gravity=False, shader=lit_with_shadows_shader)
self.collider = BoxCollider(self, Vec3(0,1,0), Vec3(1,2,1))
self.gun = Entity(model='cube', parent=camera, position=(.5,-.25,.25), scale=(.3,.2,1), origin_z=-.5, color=color.red, on_cooldown=True, shader=lit_with_shadows_shader)
invoke(setattr, self.gun, 'on_cooldown', False, delay=1)
self.gun.muzzle_flash = Entity(parent=self.gun, z=1, world_scale=.5, model='quad', color=color.yellow, enabled=False, shader=lit_with_shadows_shader)
self.info_label = info_label
self.inventory = inventory
self.pypresence_client = pypresence_client
self.last_presence_update = time.perf_counter()
self.shots_fired = 0
self.shots_hit = 0
self.accuracy = 0
self.score = 0
self.weapon_attack_speed = 0
self.weapon_dmg = 0
with open("settings.json", "r") as file:
self.settings_dict = json.load(file)
def update(self):
super().update()
if held_keys['left mouse']:
self.shoot()
self.x = max(-16, min(self.x, 16))
self.z = max(-16, min(self.z, 16))
self.info_label.text = f"Score: {self.score} Hits: {self.shots_fired}/{self.shots_hit} Accuracy: {round(self.accuracy, 2)}%"
weapon_name = self.inventory.slot_names[self.inventory.current_slot]
self.weapon_attack_speed = weapons[weapon_name]["atk_speed"]
self.weapon_dmg = weapons[weapon_name]["dmg"]
if time.perf_counter() - self.last_presence_update >= 3:
self.last_presence_update = time.perf_counter()
self.pypresence_client.update(state='Training Aim', details=f"Score: {self.score} Hits: {self.shots_fired}/{self.shots_hit} Accuracy: {round(self.accuracy, 2)}%")
def summon_enemy(self):
pass
def shoot(self):
if not self.gun.on_cooldown:
self.gun.on_cooldown = True
self.gun.muzzle_flash.enabled = True
if self.settings_dict.get("sfx", True):
ursfx([(0.0, 0.0), (0.1, 0.9), (0.15, 0.75), (0.3, 0.14), (0.6, 0.0)], volume=0.5, wave='noise', pitch=random.uniform(-13,-12), pitch_change=-12, speed=3.0)
invoke(self.gun.muzzle_flash.disable, delay=.05)
invoke(setattr, self.gun, 'on_cooldown', False, delay=self.weapon_attack_speed)
self.shots_fired += 1
if mouse.hovered_entity and hasattr(mouse.hovered_entity, 'hp'):
mouse.hovered_entity.hp -= self.weapon_dmg
mouse.hovered_entity.blink(color.red)
self.score += int(distance(mouse.hovered_entity, self) * (mouse.hovered_entity.speed / max_enemy_speed))
self.shots_hit += 1
if mouse.hovered_entity.hp <= 0:
destroy(mouse.hovered_entity.health_bar)
destroy(mouse.hovered_entity.path_line)
destroy(mouse.hovered_entity)
self.summon_enemy()
self.accuracy = (self.shots_hit / self.shots_fired) * 100
def hide(self):
destroy(self.gun)
destroy(self.gun.muzzle_flash)
destroy(self)