Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Pong extends JFrame {
- JPanel Buttons= new JPanel();
- JPanel Test = new JPanel();
- GameThread gamethread;
- JPanel GamePanel = new JPanel();
- int x = 30; // Anfangskoordinaten Schläger 1
- int y = 260;
- int Px = 100, Py = 100; // Anfangskoordinaten Ball
- int x2 = 570, y2 = 260; // Anfangskoordinaten Schläger 2
- int dPx = 10, dPy = 10; // Geschwindigkeit Ball
- boolean Spieler1o = false; // Pfeiltaste oben am Anfang Taste nicht gedrückt
- boolean Spieler1u = false; // Pfeiltast unten
- boolean Spieler2o = false;
- boolean Spieler2u = false;
- static boolean start=false;
- public Pong() {
- JFrame frame = new JFrame();
- frame.setSize(800, 800);
- frame.setFocusable(true);
- frame.setLayout(new FlowLayout());
- frame.add(GamePanel,new GamePanel());
- frame.add( Buttons,new Buttons());
- frame.setVisible(true);
- }
- class Buttons extends JPanel implements ActionListener {
- JButton button1 = new JButton("Pause");
- JButton button2 = new JButton("Start Computer");
- JButton button3 = new JButton("Start Multiplayer");
- JButton button4 = new JButton("Beenden");
- Buttons() {
- setPreferredSize(new Dimension(100, 100));
- Buttons.setBackground(Color.YELLOW);
- Buttons.setLayout(new FlowLayout());
- button1.addActionListener(this);
- button2.addActionListener(this);
- button3.addActionListener(this);
- button4.addActionListener(this);
- Buttons.add(button1);
- Buttons.add(button2);
- Buttons.add(button3);
- Buttons.add(button4);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- }
- }
- class GamePanel extends JPanel { // draw ball and rackets
- GamePanel(){
- GamePanel.setPreferredSize(new Dimension(600,600));
- GamePanel.setBackground(Color.YELLOW);
- gamethread = new GameThread();
- gamethread.start();
- }
- public void paint(Graphics gr) { // beiden Schläger und Ball
- super.paint(gr);
- Graphics2D g = (Graphics2D) gr; // werden gezeichnet
- g.setColor(Color.YELLOW);
- g.fill(g.getClipBounds());
- g.setColor(Color.BLACK);
- g.fillRect(x, y, 10, 80); // Ball und Schläger werden gezeichnet
- g.fillOval(Px, Py, 30, 30);
- g.fillRect(x2, y2, 10, 80);
- }
- }
- class GameThread extends Thread implements KeyListener {
- GameThread() {
- addKeyListener(this);
- }
- public void run() {
- while(true) {
- TastaturEingabe();
- bewegeBall();
- repaint();
- try {
- Thread.sleep(50);
- System.out.println("Test");
- } catch (InterruptedException e) {
- return;
- }
- }
- }
- public void bewegeBall() {
- Px = Px + dPx; // die Bewegung
- Py = Py + dPy;
- if (Px < 0) { // Ball prallt von der Wand ab
- Px = 0;
- dPx = -dPx;
- }
- if (Py < 0) { // Ball prallt von der Wand ab
- Py = 0;
- dPy = -dPy;
- }
- if (Py > 570) { // Ball prallt von der Wand ab
- Py = 570;
- dPy = -dPy;
- }
- if (Px > 570) { // Ball prallt von der Wand ab
- Px = 570;
- dPx = -dPx;
- }
- if (Px <= 40 && Py >= y && Py <= y + 80) { // Hier soll er vom Schläger1 abprallen
- Px = 40;
- dPx = -dPx;
- }
- if (Px >= 540 && Py >= y2 && Py <= y2 + 80) {
- Px = 540;
- dPx = -dPx;
- dPy = -dPy;
- }
- }
- public void TastaturEingabe() {
- if (Spieler1o == true) { // Bewegung Schläger1
- y -= 12;
- }
- if (Spieler1u == true) {
- y += 12;
- }
- if (Spieler2o == true) { // Bewegung Schläger2
- y2 -= 12;
- }
- if (Spieler2u == true) {
- y2 += 12;
- }
- if (y2 > 520) { // 600-80 wegen Schlägerlänge
- y2 = 520; // Damit die Schläger nicht aus dem Bild verschwinden
- }
- if (y2 < 0) {
- y2 = 0;
- }
- if (y < 0) {
- y = 0;
- }
- if (y > 520) {
- y = 520;
- }
- }
- @Override
- public void keyPressed(KeyEvent e) {
- if (e.getKeyCode() == KeyEvent.VK_W) {
- Spieler1o = true;
- }
- if (e.getKeyCode() == KeyEvent.VK_S) {
- Spieler1u = true;
- }
- if (e.getKeyCode() == KeyEvent.VK_UP) { // Spieler2 hier wird geguckt ob Taste gedrückt
- Spieler2o = true;
- }
- if (e.getKeyCode() == KeyEvent.VK_DOWN) {
- Spieler2u = true;
- }
- }
- @Override
- public void keyReleased(KeyEvent e) {
- if (e.getKeyCode() == KeyEvent.VK_W) {
- Spieler1o = false;
- }
- if (e.getKeyCode() == KeyEvent.VK_S) {
- Spieler1u = false;
- }
- if (e.getKeyCode() == KeyEvent.VK_UP) {
- Spieler2o = false;
- }
- if (e.getKeyCode() == KeyEvent.VK_DOWN) {
- Spieler2u = false;
- }
- }
- @Override
- public void keyTyped(KeyEvent e) {
- // TODO Auto-generated method stub
- }
- }
- public static void main(String[] args) {
- new Pong();
- }
- }
Add Comment
Please, Sign In to add comment