Skip to content
Snippets Groups Projects
Commit 85de824b authored by Jie Luo's avatar Jie Luo
Browse files

Redevelopment: View and Controller

parent cebbcf6b
No related branches found
No related tags found
No related merge requests found
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.HashMap;
import java.util.HashSet;
import javax.swing.*;
public class Game extends JPanel implements KeyListener, ActionListener {
private int height, width;
private Timer t = new Timer(5, this);
private boolean first;
private HashSet<String> keys = new HashSet<String>();
// pad
private final int SPEED = 1;
private int padH = 10, padW = 40;
private int bottomPadX, topPadX;
private int inset = 10;
// ball
private double ballX, ballY, velX = 1, velY = 1, ballSize = 20;
// score
private int scoreTop, scoreBottom;
public Game() {
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
first = true;
t.setInitialDelay(100);
t.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
height = getHeight();
width = getWidth();
// initial positioning
if (first) {
bottomPadX = width / 2 - padW / 2;
topPadX = bottomPadX;
ballX = width / 2 - ballSize / 2;
ballY = height / 2 - ballSize / 2;
first = false;
}
// bottom pad
Rectangle2D bottomPad = new Rectangle(bottomPadX, height - padH - inset, padW, padH);
g2d.fill(bottomPad);
// top pad
Rectangle2D topPad = new Rectangle(topPadX, inset, padW, padH);
g2d.fill(topPad);
// ball
Ellipse2D ball = new Ellipse2D.Double(ballX, ballY, ballSize, ballSize);
g2d.fill(ball);
// scores
String scoreB = "Bottom: " + new Integer(scoreBottom).toString();
String scoreT = "Top: " + new Integer(scoreTop).toString();
g2d.drawString(scoreB, 10, height / 2);
g2d.drawString(scoreT, width - 50, height / 2);
}
@Override
public void actionPerformed(ActionEvent e) {
// side walls
if (ballX < 0 || ballX > width - ballSize) {
velX = -velX;
}
// top / down walls
if (ballY < 0) {
velY = -velY;
++ scoreBottom;
}
if (ballY + ballSize > height) {
velY = -velY;
++ scoreTop;
}
// bottom pad
if (ballY + ballSize >= height - padH - inset && velY > 0)
if (ballX + ballSize >= bottomPadX && ballX <= bottomPadX + padW)
velY = -velY;
// top pad
if (ballY <= padH + inset && velY < 0)
if (ballX + ballSize >= topPadX && ballX <= topPadX + padW)
velY = -velY;
ballX += velX;
ballY += velY;
// pressed keys
if (keys.size() == 1) {
if (keys.contains("LEFT")) {
bottomPadX -= (bottomPadX > 0) ? SPEED : 0;
}
else if (keys.contains("RIGHT")) {
bottomPadX += (bottomPadX < width - padW) ? SPEED : 0;
}
}
// AI
double delta = ballX - topPadX;
if (delta > 0) {
topPadX += (topPadX < width - padW) ? SPEED : 0;
}
else if (delta < 0) {
topPadX -= (topPadX > 0) ? SPEED : 0;
}
repaint();
}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
switch (code) {
case KeyEvent.VK_LEFT:
keys.add("LEFT");
break;
case KeyEvent.VK_RIGHT:
keys.add("RIGHT");
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
int code = e.getKeyCode();
switch (code) {
case KeyEvent.VK_LEFT:
keys.remove("LEFT");
break;
case KeyEvent.VK_RIGHT:
keys.remove("RIGHT");
break;
}
}
}
import java.awt.*;
import javax.swing.*;
public class Pong_viewAndController extends JFrame{
private JButton start = new JButton("Start");
private JButton suspend = new JButton("Suspend");
private JButton tutorial = new JButton("Tutorial");
private JButton save = new JButton("Save");
private JButton exit = new JButton("Exit");
public Pong_viewAndController(){
super("FaultInOurPong");
this.setSize(700,500);
this.setResizable(false);
JPanel game_interface = new JPanel();
game_interface.setLayout(new BoxLayout(game_interface, BoxLayout.X_AXIS));
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel,BoxLayout.Y_AXIS));
start.setMaximumSize(suspend.getPreferredSize());
start.setAlignmentX(Component.CENTER_ALIGNMENT);
buttonPanel.add(start);
buttonPanel.add(Box.createVerticalStrut(10));
suspend.setAlignmentX(Component.CENTER_ALIGNMENT);
buttonPanel.add(suspend);
buttonPanel.add(Box.createVerticalStrut(10));
tutorial.setMaximumSize(suspend.getPreferredSize());
tutorial.setAlignmentX(Component.CENTER_ALIGNMENT);
buttonPanel.add(tutorial);
buttonPanel.add(Box.createVerticalStrut(10));
save.setMaximumSize(suspend.getPreferredSize());
save.setAlignmentX(Component.CENTER_ALIGNMENT);
buttonPanel.add(save);
buttonPanel.add(Box.createVerticalStrut(10));
exit.setMaximumSize(suspend.getPreferredSize());
exit.setAlignmentX(Component.CENTER_ALIGNMENT);
buttonPanel.add(exit);
JPanel west = new JPanel(new GridBagLayout());
west.add(buttonPanel);
Game game = new Game();
//game_interface.add(buttonPanel);
game_interface.add(game);
add(west,BorderLayout.WEST);
add(game_interface);
setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args){
Pong_viewAndController view_controller = new Pong_viewAndController();
}
}
public class Temp {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("hello world");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment