Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
asteroids-game
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
MORIO Elias
asteroids-game
Commits
9efec5ff
Unverified
Commit
9efec5ff
authored
1 month ago
by
Clément REPEL
Committed by
GitHub
1 month ago
Browse files
Options
Downloads
Plain Diff
Merge pull request #25 from eliasmorio/two-players
introduce second player
parents
90363aed
045a5c26
Branches
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
Applications/Asteroids/Game.cpp
+59
-24
59 additions, 24 deletions
Applications/Asteroids/Game.cpp
Applications/Asteroids/Game.h
+4
-0
4 additions, 0 deletions
Applications/Asteroids/Game.h
with
63 additions
and
24 deletions
Applications/Asteroids/Game.cpp
+
59
−
24
View file @
9efec5ff
...
...
@@ -25,11 +25,13 @@ Game::Game(Clavier* clavier) : entityCount(0), exitRequested(false),
playerInvincible
(
false
),
invincibilityTimer
(
0
),
blinkTimer
(
0
),
showPlayer
(
true
)
{
this
->
clavier
=
clavier
;
playerShip2
=
nullptr
;
}
Game
::~
Game
()
{
delete
playerShip
;
delete
playerShip2
;
for
(
int
i
=
0
;
i
<
asteroids
.
getSize
();
++
i
)
{
delete
asteroids
.
get
(
i
);
}
...
...
@@ -39,7 +41,8 @@ Game::~Game() {
}
void
Game
::
init
(
int
selectedShipType
)
{
playerShip
=
new
Ship
(
FIXED
(
160
,
1
),
FIXED
(
100
,
1
),
FIXED
(
0
,
1
),
FIXED
(
0
,
1
),
-
90
,
selectedShipType
);
playerShip
=
new
Ship
(
FIXED
(
80
,
1
),
FIXED
(
100
,
1
),
FIXED
(
0
,
1
),
FIXED
(
0
,
1
),
-
90
,
selectedShipType
);
playerShip2
=
new
Ship
(
FIXED
(
240
,
1
),
FIXED
(
100
,
1
),
FIXED
(
0
,
1
),
FIXED
(
0
,
1
),
-
90
,
selectedShipType
);
spawnAsteroids
(
5
);
...
...
@@ -91,6 +94,7 @@ void Game::handleInput() {
return
;
}
// Controls for playerShip
if
(
clavier
->
isKeyPressed
(
'a'
))
{
playerShip
->
rotate
(
5
);
}
...
...
@@ -100,10 +104,25 @@ void Game::handleInput() {
if
(
clavier
->
isKeyPressed
(
'w'
))
{
playerShip
->
increaseVelocity
();
}
if
(
clavier
->
isKeyPressed
(
'
'
))
{
if
(
clavier
->
isKeyPressed
(
'
c
'
))
{
Bullet
*
bullet
=
playerShip
->
shootBullet
();
bullets
.
add
(
bullet
);
}
// Controls for playerShip2
if
(
clavier
->
isKeyPressed
(
'j'
))
{
playerShip2
->
rotate
(
5
);
}
if
(
clavier
->
isKeyPressed
(
'l'
))
{
playerShip2
->
rotate
(
-
5
);
}
if
(
clavier
->
isKeyPressed
(
'i'
))
{
playerShip2
->
increaseVelocity
();
}
if
(
clavier
->
isKeyPressed
(
'n'
))
{
Bullet
*
bullet
=
playerShip2
->
shootBullet
();
bullets
.
add
(
bullet
);
}
}
void
Game
::
updateScoreboard
()
{
...
...
@@ -149,6 +168,7 @@ void Game::render() {
if
(
showPlayer
)
{
drawEntity
(
playerShip
);
drawEntity
(
playerShip2
);
}
for
(
int
i
=
0
;
i
<
asteroids
.
getSize
();
++
i
)
{
...
...
@@ -193,8 +213,9 @@ void Game::spawnAsteroids(int count) {
Vec2
position
(
FIXED
(
random
(
0
,
319
),
1
),
FIXED
(
random
(
0
,
179
),
1
));
Asteroid
*
asteroid
=
new
Asteroid
(
position
.
x
,
position
.
y
,
FIXED
(
random
(
-
1
,
1
),
1
),
FIXED
(
random
(
-
1
,
1
),
1
),
AsteroidSize
::
LARGE
);
// Ensure the asteroid does not spawn in the safe zone around the ship
while
(
Physics
::
checkCollision
(
playerShip
->
getPosition
(),
safeZoneRadius
,
asteroid
->
getPosition
(),
asteroid
->
getRadius
()))
{
// Ensure the asteroid does not spawn in the safe zone around the ships
while
(
Physics
::
checkCollision
(
playerShip
->
getPosition
(),
safeZoneRadius
,
asteroid
->
getPosition
(),
asteroid
->
getRadius
())
||
Physics
::
checkCollision
(
playerShip2
->
getPosition
(),
safeZoneRadius
,
asteroid
->
getPosition
(),
asteroid
->
getRadius
()))
{
position
=
Vec2
(
FIXED
(
random
(
0
,
319
),
1
),
FIXED
(
random
(
0
,
179
),
1
));
asteroid
->
setPosition
(
position
);
}
...
...
@@ -227,9 +248,10 @@ void Game::update() {
}
}
// Update ship
// Update ship
s
if
(
!
playerInvincible
)
{
playerShip
->
move
(
DELTA_TIME
);
playerShip2
->
move
(
DELTA_TIME
);
}
// Update bullets
...
...
@@ -282,30 +304,17 @@ void Game::update() {
}
}
// Check for collisions between the ship and asteroids
// Check for collisions between the ship
s
and asteroids
if
(
!
playerInvincible
)
{
for
(
int
i
=
0
;
i
<
asteroids
.
getSize
();
++
i
)
{
Entity
*
asteroidEntity
=
static_cast
<
Entity
*>
(
asteroids
.
get
(
i
));
if
(
Physics
::
checkCollision
(
playerShip
->
getPosition
(),
playerShip
->
getRadius
(),
asteroidEntity
->
getPosition
(),
asteroidEntity
->
getRadius
()))
{
Asteroid
*
asteroid
=
static_cast
<
Asteroid
*>
(
asteroidEntity
);
--
lives
;
playerShip
->
setPosition
(
GAME_CENTER
);
playerShip
->
setAngle
(
-
90
);
playerShip
->
setVelocity
(
Vec2
(
0
,
0
));
AsteroidSize
asteroidSize
=
asteroid
->
getSize
();
asteroid
->
handleCollisionWithShip
(
asteroids
);
playerInvincible
=
true
;
invincibilityTimer
=
3500
;
// 3,5s time for the asteroid to move from the spawn
blinkTimer
=
0
;
showPlayer
=
true
;
break
;
handleShipCollision
(
playerShip
,
asteroidEntity
);
}
if
(
Physics
::
checkCollision
(
playerShip2
->
getPosition
(),
playerShip2
->
getRadius
(),
asteroidEntity
->
getPosition
(),
asteroidEntity
->
getRadius
()))
{
handleShipCollision
(
playerShip2
,
asteroidEntity
);
}
}
}
...
...
@@ -318,4 +327,30 @@ void Game::update() {
if
(
lives
<=
0
)
{
exitRequested
=
true
;
}
}
// Helper method to handle ship collisions
void
Game
::
handleShipCollision
(
Ship
*
ship
,
Entity
*
asteroidEntity
)
{
Asteroid
*
asteroid
=
static_cast
<
Asteroid
*>
(
asteroidEntity
);
--
lives
;
// Respawn the ship at its initial position
if
(
ship
==
playerShip
)
{
ship
->
setPosition
(
Vec2
(
FIXED
(
80
,
1
),
FIXED
(
100
,
1
)));
// Left side for playerShip
}
else
if
(
ship
==
playerShip2
)
{
ship
->
setPosition
(
Vec2
(
FIXED
(
240
,
1
),
FIXED
(
100
,
1
)));
// Right side for playerShip2
}
ship
->
setAngle
(
-
90
);
ship
->
setVelocity
(
Vec2
(
0
,
0
));
AsteroidSize
asteroidSize
=
asteroid
->
getSize
();
asteroid
->
handleCollisionWithShip
(
asteroids
);
playerInvincible
=
true
;
invincibilityTimer
=
3500
;
// 3.5s time for the asteroid to move from the spawn
blinkTimer
=
0
;
showPlayer
=
true
;
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Applications/Asteroids/Game.h
+
4
−
0
View file @
9efec5ff
...
...
@@ -29,6 +29,7 @@ class RenderThread;
class
Game
{
private:
Ship
*
playerShip
;
Ship
*
playerShip2
;
// Second player ship
Set
ships
;
Set
bullets
;
Set
asteroids
;
...
...
@@ -47,6 +48,9 @@ private:
int
blinkTimer
;
bool
showPlayer
;
// Helper method for handling ship collisions
void
handleShipCollision
(
Ship
*
ship
,
Entity
*
asteroidEntity
);
public:
// Synchronization primitives
static
Semaphore
inputSem
;
...
...
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