• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

key conflicts

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am designing a pong game but I am have trouble with key conflicts. can anyone get me any advice on keylistners or reccomend an online tutorial on the subject
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you explain in a bit more detail what problems you're having -- i.e., what you're doing, and what's not working as you'd expect? Let's continue this discussion over in the Swing/AWT/SWT/JFace forum -- I'm moving this thread there.
 
Nikos kat
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ernest, thanks again for your help.

If you remember I'm trying my hand at game programing and I'm trying my own version of Pong. My code doesn't let me control both paddles at once using the keyboard, im considering trying to use 1 mouse and 1 keyboard.




Let me show you the code

import java.awt.*;

import javax.swing.*;
import java.awt.event.*;


//a crude version of pong with two green paddles and a green ball that doesn't move yet
public class PongGame extends JFrame implements Runnable
{

public static final int GAME_WIDTH = 1000;
public static final int GAME_HEIGHT = 600;
public static final int LEFT_PADDLE_FROM_LEFT = 10;
public static final int RIGHT_PADDLE_FROM_RIGHT = GAME_WIDTH - 10 - Paddle.WIDTH;

public static final int MOVE_PADDLE_UP = 1;
public static final int MOVE_PADDLE_DOWN = 2;
public static final int NO_MOVE = 0;

private int leftPaddleMovementStatus;
private int rightPaddleMovementStatus;

private Thread thread;
private boolean running = false;

private Ball ball;
private Paddle leftPaddle;
private Paddle rightPaddle;

//used in double duffering
private Image dbImage;
private Graphics dbg;
//used in game update to move paddles





public PongGame()
{

setVisible(true);

//start the ball right in the center of the game
ball = new Ball(GAME_WIDTH/2 - Ball.DIAMETER/2, GAME_HEIGHT/2 - Ball.DIAMETER/2);

leftPaddle = new Paddle(LEFT_PADDLE_FROM_LEFT, GAME_HEIGHT/2 - Paddle.HEIGHT/2 );
rightPaddle = new Paddle(RIGHT_PADDLE_FROM_RIGHT, GAME_HEIGHT/2 - Paddle.HEIGHT/2);

setUpListeners();

thread = new Thread(this);
thread.start();
}


private void setUpListeners() {
Container clientArea = getContentPane();
clientArea.setFocusable(true);
addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_UP ){
rightPaddleMovementStatus = MOVE_PADDLE_UP;
}
if(e.getKeyCode() == KeyEvent.VK_DOWN ){
rightPaddleMovementStatus = MOVE_PADDLE_DOWN;
}
if(e.getKeyCode() == KeyEvent.VK_W ){
leftPaddleMovementStatus = MOVE_PADDLE_UP;
}
if(e.getKeyCode() == KeyEvent.VK_S ){
leftPaddleMovementStatus = MOVE_PADDLE_DOWN;
}
}});
}

public void run(){
running = true;
while(running){
gameUpdate();
render();
paintScreen();
try{
Thread.sleep(20);
}
catch(InterruptedException e){};
}
System.exit(0);
}

public void gameUpdate(){
//check that paddle does not run of screen, if safe move the paddle
if(leftPaddle.getPoint().y > 0 && leftPaddle.getPoint().y < GAME_HEIGHT - Paddle.HEIGHT
&& rightPaddle.getPoint().y > 0 && rightPaddle.getPoint().y < GAME_HEIGHT - Paddle.HEIGHT){

if(leftPaddleMovementStatus == MOVE_PADDLE_UP){
leftPaddle.getPoint().y -= 2;
}
if(leftPaddleMovementStatus == MOVE_PADDLE_DOWN){
leftPaddle.getPoint().y += 2;
}
if(rightPaddleMovementStatus == MOVE_PADDLE_UP){
rightPaddle.getPoint().y -= 2;
}
if(leftPaddleMovementStatus == MOVE_PADDLE_DOWN){
rightPaddle.getPoint().y += 2;
}
//reset status for next keylisten event
leftPaddleMovementStatus = NO_MOVE;
rightPaddleMovementStatus = NO_MOVE;
}
}

public void render(){


if(dbImage == null){
dbImage = createImage(GAME_WIDTH,GAME_HEIGHT);
if(dbImage == null){
System.out.println("error in making image");

}
else{
dbg = dbImage.getGraphics();
}
}

dbg.setColor(Color.black);
dbg.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
dbg.setColor(Color.green);

//draw paddles
int x = leftPaddle.getPoint().x;
int y = leftPaddle.getPoint().y;
dbg.fillRect(x, y, x + Paddle.WIDTH, y + Paddle.HEIGHT );
x = rightPaddle.getPoint().x;
y = rightPaddle.getPoint().y;
dbg.fillRect(x, y, x + Paddle.WIDTH, y + Paddle.HEIGHT );

//draw ball
dbg.fillOval(ball.getPoint().x, ball.getPoint().x,Ball.DIAMETER,Ball.DIAMETER);
}
/**
public void paint(Graphics gfx){
Container c = getContentPane();
Graphics g = c. getGraphics();

gfx.setColor(Color.black);
gfx.fillRect(0,0,22,22);
}
*/
private void paintScreen()
// actively render the buffer image to the screen
{
Container clientArea = getContentPane();
Dimension d = new Dimension(GAME_WIDTH, GAME_HEIGHT);
clientArea.setPreferredSize(d);

pack();
try {
Graphics g = clientArea.getGraphics(); // get the panel�s graphic context
if ((g != null) && (dbImage != null))
g.drawImage(dbImage, 0, 0, null);
g.dispose();
}
catch (Exception e)
{ System.out.println("Graphics not work");
}
}


public static void main(String[] args) {
new PongGame();
}

}


import java.awt.*;




public class Ball {

public static final int DIAMETER = 20;

private Point point;

public Ball(int xCoord, int yCoord){
point = new Point(xCoord,yCoord);
}

public Point getPoint(){
return point;
}
}

import java.awt.*;


public class Paddle {

public static final int WIDTH = 10;
public static final int HEIGHT = 40;

private Point point;

public Paddle(int xCoord, int yCoord){
point = new Point(xCoord,yCoord);
}

public Point getPoint(){
return point;
}
}
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
have a look at the code in this thread, it might give you some ideas

http://forum.java.sun.com/thread.jspa?forumID=256&threadID=488614
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic