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
Thank you, Customer Name
You'll receive a confirmation email soon.
Order number: 10000
Product Name
$0.00
- Product option 1
- Product option 2
- Product option 3
Note
Your customer's note will show here.
Subtotal | $0.00 |
Delivery | Free |
Sales Tax | $10.00 |
Delivery address
Customer Name
, Street, City, State
Zip Code, Country
City, NY Zip, Country
Phone number
Billing address
Customer Name
, Street, City, State
Zip Code, Country
, ,
Phone number
bottom of page