Skip to content

Commit a7f5b94

Browse files
committed
Add 100% accuracy test and add controller support for the game only(not ui)
1 parent 3387705 commit a7f5b94

File tree

6 files changed

+112
-47
lines changed

6 files changed

+112
-47
lines changed

game/game.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,10 @@ def __init__(self, pypresence_client, game_mode) -> None:
4444
self.shootables_parent = Entity()
4545
mouse.traverse_target = self.shootables_parent
4646

47-
if self.game_mode == "1 minute test":
48-
enemy_num = 10
49-
elif self.game_mode == "training":
50-
enemy_num = 25
51-
else:
47+
if self.game_mode == "waves":
5248
enemy_num = self.player.wave_enemies_left
49+
else:
50+
enemy_num = 15
5351

5452
for _ in range(enemy_num):
5553
self.summon_enemy()
@@ -62,7 +60,7 @@ def __init__(self, pypresence_client, game_mode) -> None:
6260
self.sky.input = self.input
6361

6462
if self.game_mode == "training":
65-
self.create_enemies_label = Text("Use n to create new targets.", parent=camera.ui, position=(-0.85, -0.4), scale=1.3)
63+
self.create_enemies_label = Text("Use n or Controller A to create new targets.", parent=camera.ui, position=(-0.875, -0.4), scale=1)
6664

6765
def summon_enemy(self):
6866
enemy_stats = random.choice(list(self.enemy_types.items()))[1]
@@ -89,10 +87,13 @@ def update(self):
8987
if self.game_mode == "1 minute test" and time.perf_counter() - self.player.test_start >= 1:
9088
self.game_over()
9189

90+
if self.game_mode == r"100% accuracy test" and self.player.shots_fired - self.player.shots_hit >= 1:
91+
self.game_over()
92+
9293
def input(self, key):
93-
if key == "escape":
94+
if key == "escape" or key == "gamepad start":
9495
self.back_to_main_menu()
95-
elif key == "n" and not self.game_mode == "1 minute test":
96+
elif not self.game_mode == "1 minute test" and (key == "n" or key == "gamepad a"):
9697
self.summon_enemy()
9798

9899
def game_over(self):
@@ -115,6 +116,12 @@ def back_to_main_menu(self):
115116
Main(self.pypresence_client)
116117

117118
def hide(self):
119+
if self.game_over_triggered:
120+
destroy(self.main)
121+
destroy(self.game_over_label)
122+
destroy(self.exit_button)
123+
return
124+
118125
destroy(self.ground)
119126
destroy(self.sun)
120127
destroy(self.sky)
@@ -126,11 +133,6 @@ def hide(self):
126133

127134
self.inventory.hide()
128135
self.player.hide()
129-
130-
if self.game_over_triggered:
131-
destroy(self.main)
132-
destroy(self.game_over_label)
133-
destroy(self.exit_button)
134136

135137
if self.game_mode == "training":
136138
destroy(self.create_enemies_label)

game/inventory.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ def input(self, key):
2525
if key.isnumeric() and int(key) <= self.slot_number:
2626
self.switch_to(int(key) - 1)
2727

28-
if key == "scroll down":
28+
if key == "scroll down" or key == "gamepad dpad right":
2929
self.switch_to(min(self.slot_number - 1, self.current_slot + 1))
30-
elif key == "scroll up":
30+
elif key == "scroll up" or key == "gamepad dpad left":
3131
self.switch_to(max(0, self.current_slot - 1))
3232

3333
def append(self, item, name, slot):

game/player.py

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
from ursina.shaders import lit_with_shadows_shader
44
from ursina.prefabs.ursfx import ursfx
5-
from ursina.prefabs.first_person_controller import FirstPersonController
65

76
from utils.preload import death_sound
87
from utils.constants import weapons
8+
from utils.utils import FixedFirstPersonController
99

1010
import json
1111
from pathlib import Path
1212

13-
class Player(FirstPersonController):
13+
class Player(FixedFirstPersonController):
1414
def __init__(self, game_mode, settings_dict, high_score, info_label, inventory, pypresence_client) -> None:
1515
super().__init__(model='cube', z=16, color=color.orange, origin_y=-.5, speed=8, collider='box', gravity=True, shader=lit_with_shadows_shader)
1616

@@ -47,48 +47,56 @@ def __init__(self, game_mode, settings_dict, high_score, info_label, inventory,
4747
self.settings_dict = json.load(file)
4848

4949
def update(self):
50-
super().update()
50+
if self.enabled:
51+
super().update()
5152

52-
if held_keys['left mouse']:
53-
self.shoot()
53+
if held_keys['left mouse'] or held_keys["gamepad right trigger"]:
54+
self.shoot()
5455

55-
self.x = max(-16, min(self.x, 16))
56-
self.z = max(-16, min(self.z, 16))
56+
self.x = max(-16, min(self.x, 16))
57+
self.z = max(-16, min(self.z, 16))
5758

58-
if self.game_mode == "waves":
59-
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 "
60-
elif self.game_mode == "1 minute test":
61-
info_text = f"Time Left: {round(60 - (time.perf_counter() - self.test_start))} "
62-
else:
63-
info_text = ""
59+
if self.game_mode == "waves":
60+
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 "
61+
elif self.game_mode == "1 minute test":
62+
info_text = f"Time Left: {round(60 - (time.perf_counter() - self.test_start))} "
63+
else:
64+
info_text = ""
6465

65-
info_text += f"Score: {self.score} High Score: {self.high_score} Hits: {self.shots_fired}/{self.shots_hit} Accuracy: {round(self.accuracy, 2)}%"
66-
self.info_label.text = info_text
66+
info_text += f"Score: {self.score} High Score: {self.high_score} Hits: {self.shots_fired}/{self.shots_hit} Accuracy: {round(self.accuracy, 2)}%"
67+
self.info_label.text = info_text
6768

68-
weapon_name = self.inventory.slot_names[self.inventory.current_slot]
69-
self.gun.texture = Texture(Path(self.settings_dict.get("weapons", weapons)[weapon_name]["image"]))
70-
self.weapon_attack_speed = self.settings_dict.get("weapons", weapons)[weapon_name]["atk_speed"]
71-
self.weapon_dmg = self.settings_dict.get("weapons", weapons)[weapon_name]["dmg"]
69+
weapon_name = self.inventory.slot_names[self.inventory.current_slot]
70+
self.gun.texture = Texture(Path(self.settings_dict.get("weapons", weapons)[weapon_name]["image"]))
71+
self.weapon_attack_speed = self.settings_dict.get("weapons", weapons)[weapon_name]["atk_speed"]
72+
self.weapon_dmg = self.settings_dict.get("weapons", weapons)[weapon_name]["dmg"]
7273

73-
if self.score > self.high_score:
74-
self.high_score = self.score
74+
if self.score > self.high_score:
75+
self.high_score = self.score
7576

76-
if time.perf_counter() - self.last_presence_update >= 3:
77-
self.last_presence_update = time.perf_counter()
78-
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)}%")
79-
77+
if time.perf_counter() - self.last_presence_update >= 3:
78+
self.last_presence_update = time.perf_counter()
79+
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)}%")
80+
8081
def summon_enemy(self):
8182
pass
8283

84+
def try_to_disable_muzzle_flash(self): # without this method, 100% accuracy test crashes, because using .disable on a destroyed entity is not allowed
85+
try:
86+
self.gun.muzzle_flash.disable()
87+
except:
88+
pass
89+
8390
def shoot(self):
8491
if not self.gun.on_cooldown:
8592
self.gun.on_cooldown = True
93+
8694
self.gun.muzzle_flash.enabled = True
8795

8896
if self.settings_dict.get("sfx", True):
8997
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)
9098

91-
invoke(self.gun.muzzle_flash.disable, delay=.05)
99+
invoke(self.try_to_disable_muzzle_flash, delay=.05)
92100
invoke(setattr, self.gun, 'on_cooldown', False, delay=self.weapon_attack_speed)
93101
self.shots_fired += 1
94102

menus/game_modes.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ def __init__(self, rpc):
1111

1212
self.main = Entity(parent=camera.ui, model='cube', color=color.dark_gray, scale=(1.8, 1.2), z=1)
1313
self.back_button = Button('Back', parent=camera.ui, color=color.gray, scale=(.1, .05), position=(-.8, .45), on_click=self.exit)
14-
self.title_label = Text(text="Select a mode to play.", position=(-0.4, 0.35), scale=3)
14+
self.title_label = Text(text="Select a mode to play.", position=(-0.4, 0.425), scale=3)
1515

1616
self.ui = [self.main, self.back_button, self.title_label]
1717

18-
y = 0.1
18+
y = 0.2
1919

2020
for game_mode in game_modes:
21-
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))
21+
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))
2222
self.ui.append(button)
23-
y -= 0.21
23+
y -= 0.16
2424

2525
def play(self, game_mode):
2626
self.hide()

utils/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
max_enemy_movement = 10
55
enemy_health = 100
66

7-
game_modes = ["Training", "Waves", "1 Minute Test"]
7+
game_modes = ["Training", "Waves", "1 Minute Test", "100% Accuracy Test"]
88

99
weapons = {
1010
"assault_rifle": {"dmg": 20, "atk_speed": 0.2, "image": "assets/graphics/assaultrifle.png"},

utils/utils.py

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from panda3d.core import GraphicsPipeSelection
22
from ursina.prefabs.dropdown_menu import DropdownMenu
33
from ursina.prefabs.file_browser import FileBrowser
4+
from ursina.prefabs.first_person_controller import FirstPersonController
5+
from ursina import *
46

57
def get_closest_resolution():
68
allowed_resolutions = [(1366, 768), (1440, 900), (1600,900), (1920,1080), (2560,1440), (3840,2160)]
@@ -76,4 +78,57 @@ def is_float(string):
7678
float(string)
7779
return True
7880
except ValueError:
79-
return False
81+
return False
82+
83+
class FixedFirstPersonController(FirstPersonController):
84+
def update(self):
85+
self.rotation_y += mouse.velocity[0] * self.mouse_sensitivity[1]
86+
87+
look_x = mouse.velocity[0] + held_keys.get('gamepad right stick x', 0) * 0.01
88+
look_y = mouse.velocity[1] + held_keys.get('gamepad right stick y', 0) * 0.01
89+
90+
self.rotation_y += look_x * self.mouse_sensitivity[1]
91+
self.camera_pivot.rotation_x -= look_y * self.mouse_sensitivity[0]
92+
self.camera_pivot.rotation_x = clamp(self.camera_pivot.rotation_x, -90, 90)
93+
94+
self.direction = Vec3(
95+
self.forward * ((held_keys['w'] - held_keys['s']) + held_keys["gamepad left stick y"])
96+
+ self.right * ((held_keys['d'] - held_keys['a']) + held_keys["gamepad left stick x"])
97+
).normalized()
98+
99+
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)
100+
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)
101+
if not feet_ray.hit and not head_ray.hit:
102+
move_amount = self.direction * time.dt * self.speed
103+
104+
if raycast(self.position+Vec3(-.0,1,0), Vec3(1,0,0), distance=.5, traverse_target=self.traverse_target, ignore=self.ignore_list).hit:
105+
move_amount[0] = min(move_amount[0], 0)
106+
if raycast(self.position+Vec3(-.0,1,0), Vec3(-1,0,0), distance=.5, traverse_target=self.traverse_target, ignore=self.ignore_list).hit:
107+
move_amount[0] = max(move_amount[0], 0)
108+
if raycast(self.position+Vec3(-.0,1,0), Vec3(0,0,1), distance=.5, traverse_target=self.traverse_target, ignore=self.ignore_list).hit:
109+
move_amount[2] = min(move_amount[2], 0)
110+
if raycast(self.position+Vec3(-.0,1,0), Vec3(0,0,-1), distance=.5, traverse_target=self.traverse_target, ignore=self.ignore_list).hit:
111+
move_amount[2] = max(move_amount[2], 0)
112+
self.position += move_amount
113+
114+
# self.position += self.direction * self.speed * time.dt
115+
116+
117+
if self.gravity:
118+
# gravity
119+
ray = raycast(self.world_position+(0,self.height,0), self.down, traverse_target=self.traverse_target, ignore=self.ignore_list)
120+
121+
if ray.distance <= self.height+.1:
122+
if not self.grounded:
123+
self.land()
124+
self.grounded = True
125+
# make sure it's not a wall and that the point is not too far up
126+
if ray.world_normal.y > .7 and ray.world_point.y - self.world_y < .5: # walk up slope
127+
self.y = ray.world_point[1]
128+
return
129+
else:
130+
self.grounded = False
131+
132+
# if not on ground and not on way up in jump, fall
133+
self.y -= min(self.air_time, ray.distance-.05) * time.dt * 100
134+
self.air_time += time.dt * .25 * self.gravity

0 commit comments

Comments
 (0)