Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Newbie help

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey, I've recently begun delving into Java (I was taking a half year course in my high school but we didnt get very far). But anyways, I made an applet that is "supposed" to be pong. It is you versus a computer that always hits the ball (I was going to make it 2 player once my key events worked). Anyways... you click start/begin and a ball goes back and forth between you and the computer. The problem I have is that before you click start you can move your paddle with the keys. However, after start is clicked, you can no longer use the keys.
I suspected the problem was coming from the timer I use so I moved the timer starter into my init() function. However, after this was done, the keys continued to work until start was clicked. The problem still persisted. Can you guys take a look at my code and see where my problem is? I apologize if readability is poor and/or the code is poor but well, I'm still really new to java . O, and I would also like to point out that after you click start, it NEVER goes into keyPressed again (I made a pop up in there to go up when it was accessed but it never appeared after start was clicked).
All help is welcomed .
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class background extends JApplet
implements ActionListener, KeyListener
{

Timer t;
final int ballstartx = 200, ballstarty = 200;
int ballx = 200, bally = 200, ballrad= 5, movement = 5, directionx = -1, directiony = -1, directy = -1;
JButton beginning;
int youx = 475, youy = 200, youmovement = 10;
int opponentx = 0, opponenty = 150, opponentmovement = 10;
boolean gameover = false, beginningyet = false;
Container c;

public void init()
{
beginning = new JButton("beginning");
beginning.addActionListener(this);
c = getContentPane();
c.setLayout(new FlowLayout());
c.add(beginning);
addKeyListener(this);
c.setBackground(Color.white);
t = new Timer(500, this);
c.setVisible(true);
}

public void actionPerformed(ActionEvent e)
{
Object o = e.getSource();
if(o == t){
movedown();
}
else if(o == beginning){
gameover = false;
t.start();
}
repaint();
}

public void keyPressed(KeyEvent e){
switch(e.getKeyCode()){
case KeyEvent.VK_DOWN:
youdown();
break;
case KeyEvent.VK_UP:
youup();
break;
default:
youup();
break;
}
repaint();
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}

public void youup()
{
youy -= youmovement;
}

public void youdown()
{
youy += youmovement;
}

public void movedown()
{
if(ballx >= youx && (bally < youy || bally > youy + 100))
{
t.stop();
gameover = true;
}
if(ballx < 10 || ballx > 475)
directionx = -directionx;
if(bally < 10 || bally > 450)
directiony = -directiony;
if(opponenty < -40 || opponenty > 400)
directy = -directy;

ballx += 2 * movement * directionx;
bally += movement * directiony;
opponenty += movement * directy;
}

public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.blue);
g.fillOval(ballx, bally, 2*ballrad,2*ballrad);
g.setColor(Color.red);
g.fillRect(youx, youy, 25, 100);
g.setColor(Color.red);
g.fillRect(opponentx, opponenty, 25, 100);


if(gameover){
g.setFont(new Font "Helvetica", Font.BOLD,72));
g.drawString("FAILURE!!", 25, 200);
ballx = ballstartx;
bally = ballstarty;
opponenty = ballstarty;
}
}
}
[ June 19, 2003: Message edited by: Matt Fyffe ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello:
I loaded up your code and your pong game worked fine for me. I actually had to correct a missing parenthesis for compilation--found at:
g.setFont(new Font "Helvetica", Font.BOLD,72));
There should be a parenthesis in front of "Helvetica". Not a huge problem, but anyway, it is true that you can move the paddle before "beginning" is initiated. This isn't necessarily a bad thing as the ball doesn't begin movement until the button is pressed. So the player can move the paddle all he/she wants--can't play till the button is pressed. A simple way to fix this though, is to actually make use of your boolean variable you declared, but did not use in your game for some reason: the variable "beginningyet". Perhaps utilizing this variable in keyPressed() would be a good idea.
As for the other issue, you said, "However, after start is clicked, you can no longer use the keys." I did not have this problem when running your game. Even after hitting the beginning button, I was able to continue to move the paddle and play the game (I had to increase the ball speed...speed of 5 is dreadfully slow to play ).
Hope my ramble helped. Good luck on your coding endeavors.
[ June 29, 2003: Message edited by: B. Zechmann ]
 
    Bookmark Topic Watch Topic
  • New Topic