Add 100% accuracy test and add controller support for the game only(not ui)

This commit is contained in:
csd4ni3l
2025-06-25 15:43:58 +02:00
parent 33877059e0
commit a7f5b94ae8
6 changed files with 112 additions and 47 deletions

View File

@@ -44,12 +44,10 @@ class Game():
self.shootables_parent = Entity()
mouse.traverse_target = self.shootables_parent
if self.game_mode == "1 minute test":
enemy_num = 10
elif self.game_mode == "training":
enemy_num = 25
else:
if self.game_mode == "waves":
enemy_num = self.player.wave_enemies_left
else:
enemy_num = 15
for _ in range(enemy_num):
self.summon_enemy()
@@ -62,7 +60,7 @@ class Game():
self.sky.input = self.input
if self.game_mode == "training":
self.create_enemies_label = Text("Use n to create new targets.", parent=camera.ui, position=(-0.85, -0.4), scale=1.3)
self.create_enemies_label = Text("Use n or Controller A to create new targets.", parent=camera.ui, position=(-0.875, -0.4), scale=1)
def summon_enemy(self):
enemy_stats = random.choice(list(self.enemy_types.items()))[1]
@@ -89,10 +87,13 @@ class Game():
if self.game_mode == "1 minute test" and time.perf_counter() - self.player.test_start >= 1:
self.game_over()
if self.game_mode == r"100% accuracy test" and self.player.shots_fired - self.player.shots_hit >= 1:
self.game_over()
def input(self, key):
if key == "escape":
if key == "escape" or key == "gamepad start":
self.back_to_main_menu()
elif key == "n" and not self.game_mode == "1 minute test":
elif not self.game_mode == "1 minute test" and (key == "n" or key == "gamepad a"):
self.summon_enemy()
def game_over(self):
@@ -115,6 +116,12 @@ class Game():
Main(self.pypresence_client)
def hide(self):
if self.game_over_triggered:
destroy(self.main)
destroy(self.game_over_label)
destroy(self.exit_button)
return
destroy(self.ground)
destroy(self.sun)
destroy(self.sky)
@@ -127,11 +134,6 @@ class Game():
self.inventory.hide()
self.player.hide()
if self.game_over_triggered:
destroy(self.main)
destroy(self.game_over_label)
destroy(self.exit_button)
if self.game_mode == "training":
destroy(self.create_enemies_label)

View File

@@ -25,9 +25,9 @@ class Inventory():
if key.isnumeric() and int(key) <= self.slot_number:
self.switch_to(int(key) - 1)
if key == "scroll down":
if key == "scroll down" or key == "gamepad dpad right":
self.switch_to(min(self.slot_number - 1, self.current_slot + 1))
elif key == "scroll up":
elif key == "scroll up" or key == "gamepad dpad left":
self.switch_to(max(0, self.current_slot - 1))
def append(self, item, name, slot):

View File

@@ -2,15 +2,15 @@ from ursina import *
from ursina.shaders import lit_with_shadows_shader
from ursina.prefabs.ursfx import ursfx
from ursina.prefabs.first_person_controller import FirstPersonController
from utils.preload import death_sound
from utils.constants import weapons
from utils.utils import FixedFirstPersonController
import json
from pathlib import Path
class Player(FirstPersonController):
class Player(FixedFirstPersonController):
def __init__(self, game_mode, settings_dict, high_score, info_label, inventory, pypresence_client) -> None:
super().__init__(model='cube', z=16, color=color.orange, origin_y=-.5, speed=8, collider='box', gravity=True, shader=lit_with_shadows_shader)
@@ -47,48 +47,56 @@ class Player(FirstPersonController):
self.settings_dict = json.load(file)
def update(self):
super().update()
if self.enabled:
super().update()
if held_keys['left mouse']:
self.shoot()
if held_keys['left mouse'] or held_keys["gamepad right trigger"]:
self.shoot()
self.x = max(-16, min(self.x, 16))
self.z = max(-16, min(self.z, 16))
self.x = max(-16, min(self.x, 16))
self.z = max(-16, min(self.z, 16))
if self.game_mode == "waves":
info_text = f"Wave: {self.wave_number} Enemies Left: {self.wave_enemies_left} Time Left: {round(self.wave_time - (time.perf_counter() - self.last_wave_time), 2)}s "
elif self.game_mode == "1 minute test":
info_text = f"Time Left: {round(60 - (time.perf_counter() - self.test_start))} "
else:
info_text = ""
if self.game_mode == "waves":
info_text = f"Wave: {self.wave_number} Enemies Left: {self.wave_enemies_left} Time Left: {round(self.wave_time - (time.perf_counter() - self.last_wave_time), 2)}s "
elif self.game_mode == "1 minute test":
info_text = f"Time Left: {round(60 - (time.perf_counter() - self.test_start))} "
else:
info_text = ""
info_text += f"Score: {self.score} High Score: {self.high_score} Hits: {self.shots_fired}/{self.shots_hit} Accuracy: {round(self.accuracy, 2)}%"
self.info_label.text = info_text
info_text += f"Score: {self.score} High Score: {self.high_score} Hits: {self.shots_fired}/{self.shots_hit} Accuracy: {round(self.accuracy, 2)}%"
self.info_label.text = info_text
weapon_name = self.inventory.slot_names[self.inventory.current_slot]
self.gun.texture = Texture(Path(self.settings_dict.get("weapons", weapons)[weapon_name]["image"]))
self.weapon_attack_speed = self.settings_dict.get("weapons", weapons)[weapon_name]["atk_speed"]
self.weapon_dmg = self.settings_dict.get("weapons", weapons)[weapon_name]["dmg"]
weapon_name = self.inventory.slot_names[self.inventory.current_slot]
self.gun.texture = Texture(Path(self.settings_dict.get("weapons", weapons)[weapon_name]["image"]))
self.weapon_attack_speed = self.settings_dict.get("weapons", weapons)[weapon_name]["atk_speed"]
self.weapon_dmg = self.settings_dict.get("weapons", weapons)[weapon_name]["dmg"]
if self.score > self.high_score:
self.high_score = self.score
if self.score > self.high_score:
self.high_score = self.score
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} High Score: {self.high_score} Hits: {self.shots_fired}/{self.shots_hit} Accuracy: {round(self.accuracy, 2)}%")
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} High Score: {self.high_score} Hits: {self.shots_fired}/{self.shots_hit} Accuracy: {round(self.accuracy, 2)}%")
def summon_enemy(self):
pass
def try_to_disable_muzzle_flash(self): # without this method, 100% accuracy test crashes, because using .disable on a destroyed entity is not allowed
try:
self.gun.muzzle_flash.disable()
except:
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(self.try_to_disable_muzzle_flash, delay=.05)
invoke(setattr, self.gun, 'on_cooldown', False, delay=self.weapon_attack_speed)
self.shots_fired += 1

View File

@@ -11,16 +11,16 @@ class GameModeSelector:
self.main = Entity(parent=camera.ui, model='cube', color=color.dark_gray, scale=(1.8, 1.2), z=1)
self.back_button = Button('Back', parent=camera.ui, color=color.gray, scale=(.1, .05), position=(-.8, .45), on_click=self.exit)
self.title_label = Text(text="Select a mode to play.", position=(-0.4, 0.35), scale=3)
self.title_label = Text(text="Select a mode to play.", position=(-0.4, 0.425), scale=3)
self.ui = [self.main, self.back_button, self.title_label]
y = 0.1
y = 0.2
for game_mode in game_modes:
button = Button(text=game_mode, scale_x=1, scale_y=0.2, text_size=2, position=(0, y), on_click=lambda game_mode=game_mode: self.play(game_mode))
button = Button(text=game_mode, scale_x=1, scale_y=0.15, text_size=2, position=(0, y), on_click=lambda game_mode=game_mode: self.play(game_mode))
self.ui.append(button)
y -= 0.21
y -= 0.16
def play(self, game_mode):
self.hide()

View File

@@ -4,7 +4,7 @@ min_enemy_movement = 7
max_enemy_movement = 10
enemy_health = 100
game_modes = ["Training", "Waves", "1 Minute Test"]
game_modes = ["Training", "Waves", "1 Minute Test", "100% Accuracy Test"]
weapons = {
"assault_rifle": {"dmg": 20, "atk_speed": 0.2, "image": "assets/graphics/assaultrifle.png"},

View File

@@ -1,6 +1,8 @@
from panda3d.core import GraphicsPipeSelection
from ursina.prefabs.dropdown_menu import DropdownMenu
from ursina.prefabs.file_browser import FileBrowser
from ursina.prefabs.first_person_controller import FirstPersonController
from ursina import *
def get_closest_resolution():
allowed_resolutions = [(1366, 768), (1440, 900), (1600,900), (1920,1080), (2560,1440), (3840,2160)]
@@ -77,3 +79,56 @@ def is_float(string):
return True
except ValueError:
return False
class FixedFirstPersonController(FirstPersonController):
def update(self):
self.rotation_y += mouse.velocity[0] * self.mouse_sensitivity[1]
look_x = mouse.velocity[0] + held_keys.get('gamepad right stick x', 0) * 0.01
look_y = mouse.velocity[1] + held_keys.get('gamepad right stick y', 0) * 0.01
self.rotation_y += look_x * self.mouse_sensitivity[1]
self.camera_pivot.rotation_x -= look_y * self.mouse_sensitivity[0]
self.camera_pivot.rotation_x = clamp(self.camera_pivot.rotation_x, -90, 90)
self.direction = Vec3(
self.forward * ((held_keys['w'] - held_keys['s']) + held_keys["gamepad left stick y"])
+ self.right * ((held_keys['d'] - held_keys['a']) + held_keys["gamepad left stick x"])
).normalized()
feet_ray = raycast(self.position+Vec3(0,0.5,0), self.direction, traverse_target=self.traverse_target, ignore=self.ignore_list, distance=.5, debug=False)
head_ray = raycast(self.position+Vec3(0,self.height-.1,0), self.direction, traverse_target=self.traverse_target, ignore=self.ignore_list, distance=.5, debug=False)
if not feet_ray.hit and not head_ray.hit:
move_amount = self.direction * time.dt * self.speed
if raycast(self.position+Vec3(-.0,1,0), Vec3(1,0,0), distance=.5, traverse_target=self.traverse_target, ignore=self.ignore_list).hit:
move_amount[0] = min(move_amount[0], 0)
if raycast(self.position+Vec3(-.0,1,0), Vec3(-1,0,0), distance=.5, traverse_target=self.traverse_target, ignore=self.ignore_list).hit:
move_amount[0] = max(move_amount[0], 0)
if raycast(self.position+Vec3(-.0,1,0), Vec3(0,0,1), distance=.5, traverse_target=self.traverse_target, ignore=self.ignore_list).hit:
move_amount[2] = min(move_amount[2], 0)
if raycast(self.position+Vec3(-.0,1,0), Vec3(0,0,-1), distance=.5, traverse_target=self.traverse_target, ignore=self.ignore_list).hit:
move_amount[2] = max(move_amount[2], 0)
self.position += move_amount
# self.position += self.direction * self.speed * time.dt
if self.gravity:
# gravity
ray = raycast(self.world_position+(0,self.height,0), self.down, traverse_target=self.traverse_target, ignore=self.ignore_list)
if ray.distance <= self.height+.1:
if not self.grounded:
self.land()
self.grounded = True
# make sure it's not a wall and that the point is not too far up
if ray.world_normal.y > .7 and ray.world_point.y - self.world_y < .5: # walk up slope
self.y = ray.world_point[1]
return
else:
self.grounded = False
# if not on ground and not on way up in jump, fall
self.y -= min(self.air_time, ray.distance-.05) * time.dt * 100
self.air_time += time.dt * .25 * self.gravity