Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
Game engine template
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Equipe-14
Game engine template
Commits
7d3533fc
Commit
7d3533fc
authored
1 year ago
by
AUGIER Yoann
Browse files
Options
Downloads
Patches
Plain Diff
Correction fonctionnement des keylistenner pour MacOS, Linux et Windows
parent
c052ac84
No related branches found
No related tags found
No related merge requests found
Pipeline
#23886
failed
1 year ago
Stage: build
Stage: test
Changes
3
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/main/java/engine/Kernel.java
+86
-27
86 additions, 27 deletions
src/main/java/engine/Kernel.java
src/main/java/pong/PongApp.java
+4
-4
4 additions, 4 deletions
src/main/java/pong/PongApp.java
src/main/java/sprint2_demo/Grid.java
+1
-1
1 addition, 1 deletion
src/main/java/sprint2_demo/Grid.java
with
91 additions
and
32 deletions
src/main/java/engine/Kernel.java
+
86
−
27
View file @
7d3533fc
...
@@ -10,6 +10,7 @@ import pong.PongObject;
...
@@ -10,6 +10,7 @@ import pong.PongObject;
import
pong.PongRacket
;
import
pong.PongRacket
;
import
javax.swing.*
;
import
javax.swing.*
;
import
java.awt.event.ActionEvent
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.HashMap
;
...
@@ -42,7 +43,7 @@ public class Kernel {
...
@@ -42,7 +43,7 @@ public class Kernel {
pongObjectEntityHashMap
=
physicEngine
.
pongObjectEntityHashMap
;
pongObjectEntityHashMap
=
physicEngine
.
pongObjectEntityHashMap
;
this
.
update
();
this
.
update
();
this
.
setKeysListeners
((
JPanel
)
this
.
graphicEngine
.
getContentPane
());
this
.
setKeys
AndKeyReleased
Listeners
((
JPanel
)
this
.
graphicEngine
.
getContentPane
());
}
}
/**
/**
...
@@ -78,34 +79,92 @@ public class Kernel {
...
@@ -78,34 +79,92 @@ public class Kernel {
/**
/**
* Listen to keyboard events.
* Listen to keyboard events.
*/
*/
public
void
setKeysListeners
(
JPanel
mainPanel
)
{
public
void
setKeysAndKeyReleasedListeners
(
JPanel
mainPanel
)
{
mainPanel
.
addKeyListener
(
new
java
.
awt
.
event
.
KeyAdapter
()
{
InputMap
inputMap
=
mainPanel
.
getInputMap
(
JComponent
.
WHEN_IN_FOCUSED_WINDOW
);
public
void
keyPressed
(
java
.
awt
.
event
.
KeyEvent
evt
)
{
ActionMap
actionMap
=
mainPanel
.
getActionMap
();
switch
(
evt
.
getKeyCode
())
{
case
java
.
awt
.
event
.
KeyEvent
.
VK_UP
->
{
// Définir les actions pour les touches de pression
physicEngine
.
pongObjectEntityHashMap
.
get
(
gameObjects
.
get
(
0
)).
setSpeed
(
new
Coordinates2D
(
0
,
-
3
));
}
Action
upAction
=
new
AbstractAction
()
{
case
java
.
awt
.
event
.
KeyEvent
.
VK_DOWN
->
{
@Override
pongObjectEntityHashMap
.
get
(
gameObjects
.
get
(
0
)).
setSpeed
(
new
Coordinates2D
(
0
,
3
));
public
void
actionPerformed
(
ActionEvent
e
)
{
}
physicEngine
.
pongObjectEntityHashMap
.
get
(
gameObjects
.
get
(
0
)).
setSpeed
(
new
Coordinates2D
(
0
,
-
3
));
case
java
.
awt
.
event
.
KeyEvent
.
VK_Z
->
{
pongObjectEntityHashMap
.
get
(
gameObjects
.
get
(
1
)).
setSpeed
(
new
Coordinates2D
(
0
,
-
3
));
}
case
java
.
awt
.
event
.
KeyEvent
.
VK_S
->
{
pongObjectEntityHashMap
.
get
(
gameObjects
.
get
(
1
)).
setSpeed
(
new
Coordinates2D
(
0
,
3
));
}
}
};
Action
downAction
=
new
AbstractAction
()
{
@Override
public
void
actionPerformed
(
ActionEvent
e
)
{
physicEngine
.
pongObjectEntityHashMap
.
get
(
gameObjects
.
get
(
0
)).
setSpeed
(
new
Coordinates2D
(
0
,
3
));
}
}
};
Action
zAction
=
new
AbstractAction
()
{
@Override
public
void
actionPerformed
(
ActionEvent
e
)
{
physicEngine
.
pongObjectEntityHashMap
.
get
(
gameObjects
.
get
(
1
)).
setSpeed
(
new
Coordinates2D
(
0
,
-
3
));
}
}
};
public
void
keyReleased
(
java
.
awt
.
event
.
KeyEvent
evt
)
{
Action
sAction
=
new
AbstractAction
(
)
{
switch
(
evt
.
getKeyCode
())
{
@Override
case
java
.
awt
.
event
.
KeyEvent
.
VK_UP
,
java
.
awt
.
event
.
KeyEvent
.
VK_DOWN
->
{
public
void
actionPerformed
(
ActionEvent
e
)
{
pongObjectEntityHashMap
.
get
(
gameObjects
.
get
(
0
)).
setSpeed
(
new
Coordinates2D
(
0
,
0
));
physicEngine
.
pongObjectEntityHashMap
.
get
(
gameObjects
.
get
(
1
)).
setSpeed
(
new
Coordinates2D
(
0
,
3
));
}
}
case
java
.
awt
.
event
.
KeyEvent
.
VK_Z
,
java
.
awt
.
event
.
KeyEvent
.
VK_S
->
{
};
pongObjectEntityHashMap
.
get
(
gameObjects
.
get
(
1
)).
setSpeed
(
new
Coordinates2D
(
0
,
0
));
// Mapper les touches de pression aux actions
inputMap
.
put
(
KeyStroke
.
getKeyStroke
(
"UP"
),
"upAction"
);
inputMap
.
put
(
KeyStroke
.
getKeyStroke
(
"DOWN"
),
"downAction"
);
inputMap
.
put
(
KeyStroke
.
getKeyStroke
(
"pressed Z"
),
"zAction"
);
inputMap
.
put
(
KeyStroke
.
getKeyStroke
(
"pressed S"
),
"sAction"
);
// Mapper les actions aux méthodes correspondantes
actionMap
.
put
(
"upAction"
,
upAction
);
actionMap
.
put
(
"downAction"
,
downAction
);
actionMap
.
put
(
"zAction"
,
zAction
);
actionMap
.
put
(
"sAction"
,
sAction
);
// Définir les actions pour les touches de relâchement
Action
upReleaseAction
=
new
AbstractAction
()
{
@Override
public
void
actionPerformed
(
ActionEvent
e
)
{
physicEngine
.
pongObjectEntityHashMap
.
get
(
gameObjects
.
get
(
0
)).
setSpeed
(
new
Coordinates2D
(
0
,
0
));
}
};
Action
downReleaseAction
=
new
AbstractAction
()
{
@Override
public
void
actionPerformed
(
ActionEvent
e
)
{
physicEngine
.
pongObjectEntityHashMap
.
get
(
gameObjects
.
get
(
0
)).
setSpeed
(
new
Coordinates2D
(
0
,
0
));
}
}
};
Action
zReleaseAction
=
new
AbstractAction
()
{
@Override
public
void
actionPerformed
(
ActionEvent
e
)
{
physicEngine
.
pongObjectEntityHashMap
.
get
(
gameObjects
.
get
(
1
)).
setSpeed
(
new
Coordinates2D
(
0
,
0
));
}
}
};
Action
sReleaseAction
=
new
AbstractAction
()
{
@Override
public
void
actionPerformed
(
ActionEvent
e
)
{
physicEngine
.
pongObjectEntityHashMap
.
get
(
gameObjects
.
get
(
1
)).
setSpeed
(
new
Coordinates2D
(
0
,
0
));
}
}
});
};
// Mapper les touches de relâchement aux actions
inputMap
.
put
(
KeyStroke
.
getKeyStroke
(
"released UP"
),
"upReleaseAction"
);
inputMap
.
put
(
KeyStroke
.
getKeyStroke
(
"released DOWN"
),
"downReleaseAction"
);
inputMap
.
put
(
KeyStroke
.
getKeyStroke
(
"released Z"
),
"zReleaseAction"
);
inputMap
.
put
(
KeyStroke
.
getKeyStroke
(
"released S"
),
"sReleaseAction"
);
// Mapper les actions aux méthodes correspondantes pour le relâchement
actionMap
.
put
(
"upReleaseAction"
,
upReleaseAction
);
actionMap
.
put
(
"downReleaseAction"
,
downReleaseAction
);
actionMap
.
put
(
"zReleaseAction"
,
zReleaseAction
);
actionMap
.
put
(
"sReleaseAction"
,
sReleaseAction
);
mainPanel
.
setFocusable
(
true
);
mainPanel
.
setFocusable
(
true
);
}
}
...
...
This diff is collapsed.
Click to expand it.
src/main/java/pong/PongApp.java
+
4
−
4
View file @
7d3533fc
...
@@ -32,15 +32,15 @@ public class PongApp {
...
@@ -32,15 +32,15 @@ public class PongApp {
String
son
=
"src/main/resources/Sound/son.wav"
;
String
son
=
"src/main/resources/Sound/son.wav"
;
Sound
.
playMusic
(
son
);
Sound
.
playMusic
(
son
);
PongRacket
leftRacket
=
new
PongRacket
(
"Left racket"
,
"src/main/resources/pong/raquette.png"
,
new
Coordinates2D
(
widthDiff
+
5
,
100
),
racketWidth
,
racketHeight
);
PongRacket
leftRacket
=
new
PongRacket
(
"Left racket"
,
"src/main/resources/pong/raquette.png"
,
new
Coordinates2D
(
widthDiff
+
5
,
height
/
2
-
racketHeight
/
2
),
racketWidth
,
racketHeight
);
components
.
add
(
leftRacket
);
components
.
add
(
leftRacket
);
PongRacket
rightRacket
=
new
PongRacket
(
"Right racket"
,
"src/main/resources/pong/raquette.png"
,
new
Coordinates2D
(
rectWidth
+
widthDiff
-
5
-
racketWidth
,
100
),
racketWidth
,
racketHeight
);
PongRacket
rightRacket
=
new
PongRacket
(
"Right racket"
,
"src/main/resources/pong/raquette.png"
,
new
Coordinates2D
(
rectWidth
+
widthDiff
-
5
-
racketWidth
,
height
/
2
-
racketHeight
/
2
),
racketWidth
,
racketHeight
);
components
.
add
(
rightRacket
);
components
.
add
(
rightRacket
);
// TODO: Add rackets
// TODO: Add rackets
//
PongBall pongBall = new PongBall("Ball", "src/main/resources/pong/ball.png", new Coordinates2D(width/2, height/2), ballWidth, ballWidth);
PongBall
pongBall
=
new
PongBall
(
"Ball"
,
"src/main/resources/pong/ball.png"
,
new
Coordinates2D
(
width
/
2
,
height
/
2
),
ballWidth
,
ballWidth
);
PongBall
pongBall
=
new
PongBall
(
"Ball"
,
"src/main/resources/sprint2_demo/asteroid.png"
,
new
Coordinates2D
(
width
/
2
,
height
/
2
),
ballWidth
,
ballWidth
);
//
PongBall pongBall = new PongBall("Ball", "src/main/resources/sprint2_demo/asteroid.png", new Coordinates2D(width/2, height/2), ballWidth, ballWidth);
components
.
add
(
pongBall
);
components
.
add
(
pongBall
);
Kernel
kernel
=
new
Kernel
(
"Pong"
,
width
,
height
,
components
);
Kernel
kernel
=
new
Kernel
(
"Pong"
,
width
,
height
,
components
);
...
...
This diff is collapsed.
Click to expand it.
src/main/java/sprint2_demo/Grid.java
+
1
−
1
View file @
7d3533fc
...
@@ -45,7 +45,7 @@ public class Grid extends JFrame {
...
@@ -45,7 +45,7 @@ public class Grid extends JFrame {
try
{
try
{
kernel
.
start
();
// Start the kernel
kernel
.
start
();
// Start the kernel
kernel
.
setKeysListeners
(
playerPanel
);
// Set keys listeners
kernel
.
setKeys
AndKeyReleased
Listeners
(
playerPanel
);
// Set keys listeners
revalidate
();
revalidate
();
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
throw
new
RuntimeException
(
e
);
...
...
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