Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Pong Paddle Movement Problem
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
LoganJAVA




PostPosted: Sat Jan 17, 2009 10:59 pm   Post subject: Pong Paddle Movement Problem

Hello,

I am making pong, and I know that the logic is missing, but could anyone edit my code so that the paddle moves up and down.

Java:
import java.awt.*;
import java.awt.event.*;
import java.awt.event.KeyEvent.*;
import javax.swing.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import java.util.*;
import java.io.*;
import java.awt.event.KeyListener.*;

public class Bouncer extends JFrame implements ActionListener
 {

    private JButton button;
    private JPanel panel;
   
    addKeyListener(new KeyAdapter()     {
            public void keyPressed(KeyEvent e)
            {
                aKeyWasPressed(e);
            }
        });
       
        Bouncer frame = new Bouncer();
        frame.setSize(500,500);
        frame.createGUI();
        frame.setVisible(true);
    }

    private void createGUI() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Container window = getContentPane();
        window.setLayout(new FlowLayout() );

        panel = new JPanel();
        panel.setPreferredSize(new Dimension(400, 400));
        panel.setBackground(Color.white);
        window.add(panel);

        button = new JButton("");
        panel.add(button);
        button.addActionListener(this);
    }

    public void actionPerformed(ActionEvent event) {
        Graphics paper = panel.getGraphics();
        Ball ball = new Ball(paper);
        ball.animate();     
    }
}

class Ball {

    private Graphics paper;
    private int xball = 30, xChange = 6;
    private int yball = 30, yChange = 2;
    private int diameter = 10;
    private int width = 380, height = 380;
    private int ypaddletop = 180;
        private int ypaddlebottom = 220;

    public Ball(Graphics graphics) {
        paper = graphics;
    }

    public void animate() {
        for (int n = 1; n < 1000; n=n+3) {
            move();
            test();
            draw();
            paddle();
            delay();
            delete();

        } 
    }
   

    private void move() {
        xball = xball + xChange;
        yball = yball + yChange;
    }
   
    private void paddle() {
        paper.setColor(Color.blue);
        paper.fillRect(20, ypaddletop, 10, 40);
    }

    private void test() {
        if (xball <= 0 || xball  >= width)
            xChange = -xChange;
       
        if (yball <= 0 || yball >= height)
            yChange = -yChange;
       }

    private void delay() {
        try {
            Thread.sleep(50);
        }
        catch (InterruptedException e) {
            return;
        }
    }

    private void draw() {
        paper.setColor(Color.red);
        paper.fillOval(xball, yball, diameter, diameter);
    }

    private void delete() {
        paper.setColor(Color.white);
        paper.fillOval (xball, yball, diameter, diameter);
    }
    /*
    public void keyPressed(KeyEvent ke)
  {
    System.out.println("key PRESSED");
    int dx = 10;
 
    switch (ke.getKeyCode())
    {
      case KeyEvent.VK_UP:
        ypaddletop = ypaddletop - 20;
        break;
      case KeyEvent.VK_DOWN:
        ypaddletop = ypaddletop + 20;
    }
  }
     // Handle the key typed event from the text field.
    public void keyTyped(KeyEvent e) {
        displayInfo(e, "KEY TYPED: ");
    }

    // Handle the key-released event from the text field.
    public void keyReleased(KeyEvent e) {
        displayInfo(e, "KEY RELEASED: ");
    }
*/


    private void aKeyWasPressed(KeyEvent e)
    {
        String keyName = KeyEvent.getKeyText(e.getKeyCode());
       
        int keycode = e.getKeyCode();
       
        String direction = "holding position.";
        if (keycode == KeyEvent.VK_UP)
            direction = "going up!";
        else if (keycode == KeyEvent.VK_DOWN)
            direction = "falling down!";
       
        label.setText("Key pressed: " + keyName + "     "
            + "Status: " + direction);

}

}





Mod Edit: Remember to use syntax tags! Thanks Smile
code:
[syntax="java"]Code Here[/syntax]
Sponsor
Sponsor
Sponsor
sponsor
LoganJAVA




PostPosted: Sat Jan 17, 2009 11:01 pm   Post subject: Re: Pong Paddle Movement Problem

I am trying to make Pong, and if you could help, that would be great. I am trying to use arrow key inputs to make the paddle move, and it is only one player.

Please Help. I am from the USA.. but, Canada
DemonWasp




PostPosted: Mon Jan 19, 2009 10:34 am   Post subject: RE:Pong Paddle Movement Problem

A few things first:
1. Use [ syntax = "java" ] and [ / syntax ], without the spaces, to format your code properly. This makes it easier for us to read, and more likely that we'll help. I see the <code> attempt, but it's BBCode, not HTML, so you want [ code ]; even that isn't as good as [ syntax ].
2. We aren't going to modify your code for you. We'll point you in the right direction, but we won't write your code for you.
3. There is an edit button, please use that instead of double-posting, particularly only 2 minutes after your original post.

It looks like all you'll need to do (assuming your code is set up properly, which I can't really tell without proper formatting) is make sure you redraw the paddle after changing it. You seem to be changing the value of ypaddletop corresponding to your keystrokes, but I can't see where you're drawing the paddle in its new location.
S_Grimm




PostPosted: Mon Jan 19, 2009 3:48 pm   Post subject: RE:Pong Paddle Movement Problem

1) Learn to buffer your program.
2)put the draw commands inside a method.


Java:

public void updateWorld()
{
Graphics2D g = (Graphics2D) strategy.getBufferStrategy();

movePlayer();//where you would check to see if the player has moved
moveBall(); //where your code for moving the ball is

drawPaddle (g);
drawBall(g);
drawScore(g);
//above are all separate methods to do a draw command.
}
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: