From e2bd00963a77ba6e4d2fe2c7c365b88eaca75883 Mon Sep 17 00:00:00 2001 From: Amir Daouadi Date: Wed, 12 Apr 2023 00:04:10 +0200 Subject: [PATCH] ajout d'un controlleur beta --- Controller/GameController.java | 44 ++++++++++++++++++++++++++++++++++ HGameController.java | 33 +++++++++++++++++++++++++ Player.java | 23 ++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 Controller/GameController.java create mode 100644 HGameController.java create mode 100644 Player.java diff --git a/Controller/GameController.java b/Controller/GameController.java new file mode 100644 index 0000000..8804fab --- /dev/null +++ b/Controller/GameController.java @@ -0,0 +1,44 @@ +package Controller; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; + +public class GameController implements KeyListener { + private int x; + private int y; + + public GameController(int x, int y) { + this.x = x; + this.y = y; + } + + @Override + public void keyTyped(KeyEvent e) { + } + + @Override + public void keyPressed(KeyEvent e) { + int keyCode = e.getKeyCode(); + + if (keyCode == KeyEvent.VK_UP) { + y--; + } else if (keyCode == KeyEvent.VK_DOWN) { + y++; + } else if (keyCode == KeyEvent.VK_LEFT) { + x--; + } else if (keyCode == KeyEvent.VK_RIGHT) { + x++; + } + } + + @Override + public void keyReleased(KeyEvent e) { + } + + public int getX() { + return x; + } + + public int getY() { + return y; + } +} diff --git a/HGameController.java b/HGameController.java new file mode 100644 index 0000000..dd2733f --- /dev/null +++ b/HGameController.java @@ -0,0 +1,33 @@ +package Controller; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; + +public class HGameController implements KeyListener { + private Player player; + + public HGameController(Player player) { + this.player = player; + } + + @Override + public void keyPressed(KeyEvent e) { + int keyCode = e.getKeyCode(); + if (keyCode == KeyEvent.VK_UP) { + player.move(-1, 0); + } else if (keyCode == KeyEvent.VK_DOWN) { + player.move(1, 0); + } else if (keyCode == KeyEvent.VK_LEFT) { + player.move(0, -1); + } else if (keyCode == KeyEvent.VK_RIGHT) { + player.move(0, 1); + } + } + + @Override + public void keyReleased(KeyEvent e) { + } + + @Override + public void keyTyped(KeyEvent e) { + } +} diff --git a/Player.java b/Player.java new file mode 100644 index 0000000..3fc280c --- /dev/null +++ b/Player.java @@ -0,0 +1,23 @@ +package Controller; +public class Player { + private int x; + private int y; + + public Player(int x, int y) { + this.x = x; + this.y = y; + } + + public void move(int dx, int dy) { + x += dx; + y += dy; + } + + public int getX() { + return x; + } + + public int getY() { + return y; + } +}