Created
May 8, 2017 23:27
-
-
Save diegofelipem/5fecda3d81d26a4c9db5af0eff9168d9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.game; | |
import java.awt.Dimension; | |
import java.awt.FlowLayout; | |
import java.awt.Graphics; | |
import java.awt.Graphics2D; | |
import java.awt.Rectangle; | |
import java.awt.RenderingHints; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.awt.event.KeyAdapter; | |
import java.awt.event.KeyEvent; | |
import javax.swing.JFrame; | |
import javax.swing.JLabel; | |
import javax.swing.JMenu; | |
import javax.swing.JMenuBar; | |
import javax.swing.JMenuItem; | |
import javax.swing.JOptionPane; | |
import javax.swing.JPanel; | |
import javax.swing.JSlider; | |
import javax.swing.KeyStroke; | |
import javax.swing.SwingUtilities; | |
import javax.swing.Timer; | |
public class GameMain { | |
Board boardGame; | |
JFrame frame; | |
public void initGui() { | |
frame = new JFrame("Tennis Game"); | |
frame.setJMenuBar(getMenu()); | |
this.boardGame = new Board(); | |
frame.getContentPane().add(boardGame); | |
frame.pack(); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.setLocationRelativeTo(null); | |
frame.setResizable(false); | |
frame.setVisible(true); | |
} | |
public JMenuBar getMenu() { | |
JMenuBar bar = new JMenuBar(); | |
JMenu menu = new JMenu("Game"); | |
JMenuItem newGame = new JMenuItem("New..."); | |
newGame.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.ALT_MASK)); | |
newGame.addActionListener(e -> { | |
int op = JOptionPane.showConfirmDialog(null, "Start a new Game?", "New Game", JOptionPane.YES_NO_OPTION, | |
JOptionPane.QUESTION_MESSAGE); | |
if (op == JOptionPane.OK_OPTION) { | |
boardGame.newGame(); | |
} | |
}); | |
JMenuItem startGame = new JMenuItem("Start..."); | |
startGame.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.ALT_MASK)); | |
startGame.addActionListener(e -> { | |
boardGame.starGame(); | |
}); | |
menu.add(newGame); | |
menu.add(startGame); | |
JMenu menu2 = new JMenu("Options"); | |
JMenuItem speedBall = new JMenuItem("Set Speed..."); | |
speedBall.addActionListener(e -> { | |
JSlider sl = getSpeedSlider(); | |
JOptionPane.showConfirmDialog(null, sl, "Select a speed", JOptionPane.DEFAULT_OPTION, | |
JOptionPane.PLAIN_MESSAGE); | |
boardGame.setSpeed(sl.getValue()); | |
}); | |
menu2.add(speedBall); | |
bar.add(menu); | |
bar.add(menu2); | |
return bar; | |
} | |
public JSlider getSpeedSlider() { | |
final int FPS_MIN = 1; | |
final int FPS_MAX = 4; | |
final int FPS_INIT = 1; | |
JSlider speedSlider = new JSlider(JSlider.HORIZONTAL, FPS_MIN, FPS_MAX, FPS_INIT); | |
speedSlider.setMajorTickSpacing(3); | |
speedSlider.setMinorTickSpacing(1); | |
speedSlider.setPaintTicks(true); | |
speedSlider.setPaintLabels(true); | |
return speedSlider; | |
} | |
public static void main(String[] args) { | |
SwingUtilities.invokeLater(() -> { | |
new GameMain().initGui(); | |
}); | |
} | |
} | |
//------------------------------ CLASSE BOARD ------------------------------// | |
class Board extends JPanel { | |
private static final long serialVersionUID = 1L; | |
private final int CANVAS_WIDTH = 400; | |
private final int CANVAS_HEIGHT = 300; | |
private final int UPDATE_INTERVAL = 10; | |
private Timer timer; | |
public Ball ball; | |
public Paddle paddle; | |
private JLabel scoreLabel, score; | |
public Board() { | |
paddle = new Paddle(this); | |
ball = new Ball(this); | |
setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0)); | |
scoreLabel = new JLabel("Score: "); | |
scoreLabel.setFont(getFont().deriveFont(12f)); | |
score = new JLabel(); | |
score.setFont(getFont().deriveFont(12f)); | |
this.add(scoreLabel); | |
this.add(score); | |
this.addKeyListener(new KeyAdapter() { | |
@Override | |
public void keyPressed(KeyEvent e) { | |
paddle.keyPressed(e); | |
} | |
@Override | |
public void keyReleased(KeyEvent e) { | |
paddle.keyReleased(e); | |
} | |
}); | |
ActionListener action = new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent arg0) { | |
updateBoard(); | |
repaint(); | |
} | |
}; | |
timer = new Timer(UPDATE_INTERVAL, action); | |
setFocusable(true); | |
} | |
@Override | |
public Dimension getPreferredSize() { | |
return new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT); | |
} | |
private void updateBoard() { | |
ball.move(); | |
paddle.move(); | |
repaint(); | |
} | |
public void gameOver() { | |
JOptionPane.showMessageDialog(this, "Game Over"); | |
newGame(); | |
} | |
public void starGame() { | |
timer.start(); | |
} | |
public void stop() { | |
timer.stop(); | |
} | |
public void newGame() { | |
stop(); | |
paddle = new Paddle(this); | |
ball = new Ball(this); | |
repaint(); | |
} | |
public void setSpeed(int speed) { | |
ball.setSpeed(speed); | |
} | |
@Override | |
protected void paintComponent(Graphics g) { | |
super.paintComponent(g); | |
Graphics2D g2 = (Graphics2D) g; | |
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); | |
ball.paint(g2); | |
paddle.paint(g2); | |
} | |
} | |
//------------------------------ CLASSE PADDLE ------------------------------// | |
class Paddle { | |
private int x = 0; | |
private final int topY; | |
public final int WIDTH = 100; | |
public final int HEIGHT = 10; | |
private int direction = 0; | |
private Board board; | |
public Paddle(Board board) { | |
this.board = board; | |
topY = board.getPreferredSize().height - 50; | |
x = board.getPreferredSize().width / 2 - WIDTH / 2; | |
} | |
public void move() { | |
if (x + direction > 0 && x + direction < board.getWidth() - WIDTH) { | |
x = x + direction; | |
} | |
} | |
public void paint(Graphics2D g2) { | |
g2.fillRect(x, topY, WIDTH, HEIGHT); | |
} | |
public void keyPressed(KeyEvent e) { | |
if (e.getKeyCode() == KeyEvent.VK_LEFT) { | |
direction = -5; | |
} | |
if (e.getKeyCode() == KeyEvent.VK_RIGHT) { | |
direction = 5; | |
} | |
} | |
public void keyReleased(KeyEvent e) { | |
direction = 0; | |
} | |
public Rectangle getBounds() { | |
return new Rectangle(x, topY, WIDTH, HEIGHT); | |
} | |
public int getTopY() { | |
return topY; | |
} | |
public int getX() { | |
return x; | |
} | |
} | |
//------------------------------ CLASSE BALL ------------------------------// | |
class Ball { | |
private int x = 0; | |
private int y = 15; | |
private final int DIAMETER = 30; | |
private int xSpeed = 1; | |
private int ySpeed = 1; | |
private Board board; | |
public Ball(Board board) { | |
this.board = board; | |
y = board.paddle.getTopY() - DIAMETER; | |
x = board.getPreferredSize().width / 2 - DIAMETER / 2; | |
} | |
public void move() { | |
if (x > board.getWidth() - DIAMETER || x < 0) { | |
xSpeed = -xSpeed; | |
} | |
if (y < 15) { | |
ySpeed = -ySpeed; | |
} | |
if (y > board.getHeight() - DIAMETER) { | |
board.gameOver(); | |
} | |
if (collision()) { | |
ySpeed = -ySpeed; | |
y = board.paddle.getTopY() - DIAMETER; | |
} | |
x += xSpeed; | |
y += ySpeed; | |
} | |
public void setSpeed(int speed) { | |
this.xSpeed = speed; | |
this.ySpeed = speed; | |
} | |
public void paint(Graphics2D g2) { | |
g2.fillOval(x, y, DIAMETER, DIAMETER); | |
} | |
public boolean collision() { | |
return board.paddle.getBounds().intersects(this.getBounds()); | |
} | |
public Rectangle getBounds() { | |
return new Rectangle(x, y, DIAMETER, DIAMETER); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment