import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SoccerGame extends JFrame { private static final int WIDTH = 800; private static final int HEIGHT = 600; private static final int PLAYER_SIZE = 50; private static final int PLAYER_SPEED = 5; private GamePanel gamePanel; private Point player1Position; private Point player2Position; public SoccerGame() { super("Soccer Game"); setSize(WIDTH, HEIGHT); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false); gamePanel = new GamePanel(); gamePanel.setBackground(Color.GREEN); add(gamePanel); player1Position = new Point(WIDTH / 4, HEIGHT / 2); player2Position = new Point(WIDTH * 3 / 4, HEIGHT / 2); addKeyListener(new KeyboardListener()); setFocusable(true); } private class GamePanel extends JPanel { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.WHITE); g.fillOval(player1Position.x, player1Position.y, PLAYER_SIZE, PLAYER_SIZE); g.fillOval(player2Position.x, player2Position.y, PLAYER_SIZE, PLAYER_SIZE); } } private class KeyboardListener extends KeyAdapter { @Override public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); switch (keyCode) { case KeyEvent.VK_W: player1Position.y = Math.max(player1Position.y - PLAYER_SPEED, 0); break; case KeyEvent.VK_S: player1Position.y = Math.min(player1Position.y + PLAYER_SPEED, HEIGHT - PLAYER_SIZE); break; case KeyEvent.VK_A: player1Position.x = Math.max(player1Position.x - PLAYER_SPEED, 0); break; case KeyEvent.VK_D: player1Position.x = Math.min(player1Position.x + PLAYER_SPEED, WIDTH - PLAYER_SIZE); break; case KeyEvent.VK_I: player2Position.y = Math.max(player2Position.y - PLAYER_SPEED, 0); break; case KeyEvent.VK_K: player2Position.y = Math.min(player2Position.y + PLAYER_SPEED, HEIGHT - PLAYER_SIZE); break; case KeyEvent.VK_J: player2Position.x = Math.max(player2Position.x - PLAYER_SPEED, WIDTH / 2); break; case KeyEvent.VK_L: player2Position.x = Math.min(player2Position.x + PLAYER_SPEED, WIDTH - PLAYER_SIZE); break; } gamePanel.repaint(); } } public static void main(String[] args) { SoccerGame game = new SoccerGame(); game.setVisible(true); } } import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SoccerGame extends JFrame { private static final int WIDTH = 800; private static final int HEIGHT = 600; private static final int PLAYER_SIZE = 50; private static final int PLAYER_SPEED = 5; private GamePanel gamePanel; private Point player1Position; private Point player2Position; public SoccerGame() { super("Soccer Game"); setSize(WIDTH, HEIGHT); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false); gamePanel = new GamePanel(); gamePanel.setBackground(Color.GREEN); add(gamePanel); player1Position = new Point(WIDTH / 4, HEIGHT / 2); player2Position = new Point(WIDTH * 3 / 4, HEIGHT / 2); addKeyListener(new KeyboardListener()); setFocusable(true); } private class GamePanel extends JPanel { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.WHITE); g.fillOval(player1Position.x, player1Position.y, PLAYER_SIZE, PLAYER_SIZE); g.fillOval(player2Position.x, player2Position.y, PLAYER_SIZE, PLAYER_SIZE); } } private class KeyboardListener extends KeyAdapter { @Override public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); switch (keyCode) { case KeyEvent.VK_W: player1Position.y = Math.max(player1Position.y - PLAYER_SPEED, 0); break; case KeyEvent.VK_S: player1Position.y = Math.min(player1Position.y + PLAYER_SPEED, HEIGHT - PLAYER_SIZE); break; case KeyEvent.VK_A: player1Position.x = Math.max(player1Position.x - PLAYER_SPEED, 0); break; case KeyEvent.VK_D: player1Position.x = Math.min(player1Position.x + PLAYER_SPEED, WIDTH - PLAYER_SIZE); break; case KeyEvent.VK_I: player2Position.y = Math.max(player2Position.y - PLAYER_SPEED, 0); break; case KeyEvent.VK_K: player2Position.y = Math.min(player2Position.y + PLAYER_SPEED, HEIGHT - PLAYER_SIZE); break; case KeyEvent.VK_J: player2Position.x = Math.max(player2Position.x - PLAYER_SPEED, WIDTH / 2); break; case KeyEvent.VK_L: player2Position.x = Math.min(player2Position.x + PLAYER_SPEED, WIDTH - PLAYER_SIZE); break; } gamePanel.repaint(); } } public static void main(String[] args) { SoccerGame game = new SoccerGame(); game.setVisible(true); } }
top of page
bottom of page