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
e1b8c4dc
Commit
e1b8c4dc
authored
4 months ago
by
Jantz
Browse files
Options
Downloads
Patches
Plain Diff
Enemy now faces the direction of the movement.
parent
24c39c42
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
enemy.py
+35
-9
35 additions, 9 deletions
enemy.py
world.py
+1
-1
1 addition, 1 deletion
world.py
with
36 additions
and
10 deletions
enemy.py
+
35
−
9
View file @
e1b8c4dc
"""
This module contains the Enemy class.
"""
import
pygame
as
pg
import
math
class
Enemy
:
"""
Class for the enemy object.
"""
def
__init__
(
self
,
hp
:
float
,
speed
:
float
,
damage
:
float
,
texture_path
:
str
):
def
__init__
(
self
,
hp
:
float
,
speed
:
float
,
damage
:
float
,
full_path_time
:
float
,
texture_path
:
str
):
"""
Initializes the enemy object.
"""
self
.
hp
=
hp
self
.
speed
=
speed
...
...
@@ -13,9 +14,10 @@ class Enemy:
self
.
texture
=
pg
.
image
.
load
(
self
.
texture_path
).
convert_alpha
()
self
.
path_position
=
0
self
.
screen_position
=
(
0
,
0
)
self
.
full_path_time
=
20
self
.
full_path_time
=
full_path_time
self
.
render_size
=
64
def
calculate_current_path_segment
(
self
,
path
:
tuple
[
tuple
[
int
,
int
]])
->
int
:
def
calculate_current_path_segment
(
self
,
path
:
tuple
[
tuple
[
int
,
int
]])
->
tuple
[
int
,
float
]
:
"""
Returns the index of current path segment and the distance along the path segment.
...
...
@@ -39,6 +41,7 @@ class Enemy:
if
current_path_length
>=
full_path_length
:
# Return the end of the last path segment if the path_position more than 1
return
len
(
path
)
-
2
,
1
def
move_along_path
(
self
,
dt
:
float
,
path
:
tuple
[
tuple
[
int
,
int
]]):
"""
Moves the enemy along the path.
...
...
@@ -56,22 +59,45 @@ class Enemy:
path
[
path_segment
][
1
]
+
(
path
[
path_segment
+
1
][
1
]
-
path
[
path_segment
][
1
])
*
segment_position
)
def
draw
(
self
,
surface
):
def
face_movement_direction
(
self
,
path
):
"""
Draws the enemy to the passed surface.
Rotates the enemy
'
s texture to face the direction of movement.
Parameters:
path:
The waypoints of the path.
"""
# Get current path segment
path_segment
,
not_used
=
self
.
calculate_current_path_segment
(
path
)
# Get the start and end positions of the current segment
current_segment_start
=
path
[
path_segment
]
current_segment_end
=
path
[
path_segment
+
1
]
# Calculate the direction vector
dx
=
current_segment_end
[
0
]
-
current_segment_start
[
0
]
dy
=
current_segment_end
[
1
]
-
current_segment_start
[
1
]
# Calculate the angle in degrees
angle
=
-
math
.
degrees
(
math
.
atan2
(
dy
,
dx
))
# Rotate the texture
self
.
rotated_texture
=
pg
.
transform
.
rotate
(
self
.
texture
,
angle
)
def
draw
(
self
,
surface
,
path
:
tuple
[
tuple
[
int
,
int
]]):
"""
Draws the enemy to the passed surface using the rotated texture.
Parameters:
surface:
The surface to draw the enemy to.
path:
The waypoints of the path.
"""
self
.
texture_rect
=
self
.
texture
.
get_rect
()
self
.
face_movement_direction
(
path
)
self
.
texture_rect
=
self
.
rotated_texture
.
get_rect
()
self
.
texture_rect
.
center
=
self
.
screen_position
surface
.
blit
(
self
.
texture
,
self
.
texture_rect
)
# TODO: Rotate the sprite to face the direction of movement.
surface
.
blit
(
self
.
rotated_texture
,
self
.
texture_rect
)
class
Zombie
(
Enemy
):
"""
Class for the zombie object.
"""
def
__init__
(
self
):
"""
Initializes the zombie object.
"""
super
().
__init__
(
100
,
1
,
10
,
'
assets/images/zombie/
skeleton-idle_0
.png
'
)
super
().
__init__
(
100
,
1
,
10
,
30
,
'
assets/images/zombie/
zombie_128x128
.png
'
)
This diff is collapsed.
Click to expand it.
world.py
+
1
−
1
View file @
e1b8c4dc
...
...
@@ -82,7 +82,7 @@ class World:
"""
Draws the world elements to the passed surface.
"""
self
.
window
.
blit
(
self
.
texture
,
(
0
,
0
))
for
enemy
in
self
.
enemies
:
enemy
.
draw
(
self
.
window
)
enemy
.
draw
(
self
.
window
,
self
.
waypoints
)
self
.
turret_group
.
draw
(
self
.
texture
)
...
...
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