Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Project Zavi
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mihkelhain
Project Zavi
Commits
a445c926
Commit
a445c926
authored
4 months ago
by
Jantz
Browse files
Options
Downloads
Patches
Plain Diff
Added turret enemy targeting and shooting
parent
e1b8c4dc
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
turret.py
+23
-1
23 additions, 1 deletion
turret.py
world.py
+10
-0
10 additions, 0 deletions
world.py
with
33 additions
and
1 deletion
turret.py
+
23
−
1
View file @
a445c926
import
pygame
as
pg
from
enemy
import
Enemy
class
Turret
(
pg
.
sprite
.
Sprite
):
def
__init__
(
self
,
image
,
pos
):
def
__init__
(
self
,
image
,
pos
):
pg
.
sprite
.
Sprite
.
__init__
(
self
)
self
.
image
=
image
self
.
rect
=
self
.
image
.
get_rect
()
self
.
rect
.
center
=
pos
self
.
attack_damage
=
10
self
.
attack_cooldown
=
0.25
self
.
time_since_last_attack
=
self
.
attack_cooldown
def
find_nearest_enemy
(
self
,
enemies
:
list
[
Enemy
])
->
Enemy
:
"""
Returns the nearest enemy object.
Parameters:
enemies:
The list of enemies.
"""
nearest_enemy
=
None
nearest_distance
=
-
1
for
enemy
in
enemies
:
distance
=
((
self
.
rect
.
center
[
0
]
-
enemy
.
screen_position
[
0
])
**
2
+
(
self
.
rect
.
center
[
1
]
-
enemy
.
screen_position
[
1
])
**
2
)
**
0.5
if
distance
<
nearest_distance
or
nearest_distance
<
0
:
nearest_enemy
=
enemy
nearest_distance
=
distance
return
nearest_enemy
\ No newline at end of file
This diff is collapsed.
Click to expand it.
world.py
+
10
−
0
View file @
a445c926
...
...
@@ -77,6 +77,16 @@ class World:
if
not
collision
:
self
.
turret_group
.
add
(
temp_turret
)
print
(
self
.
cursor_turret
.
get_size
())
def
turret_attacks
(
self
,
dt
:
float
):
for
turret
in
self
.
turret_group
:
target
=
turret
.
find_nearest_enemy
(
self
.
enemies
)
if
target
is
not
None
:
turret
.
time_since_last_attack
+=
dt
if
turret
.
time_since_last_attack
>=
turret
.
attack_cooldown
:
target
.
hp
-=
turret
.
attack_damage
print
(
f
"
Enemy hit! HP:
{
target
.
hp
}
"
)
turret
.
time_since_last_attack
=
0
def
draw
(
self
):
"""
Draws the world elements to the passed surface.
"""
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment