I'm making a Java pong clone and I've been running into a few troubles.
EDIT: (Just realised that this is probably in the wrong section, sorry. New to java would probably be a better section.)
After looking all over the place and pulling together on everything I've found over about 8 months by myself without help, I feel that I'm ready to make a Java game. I figured I'd start off with something that isn't too complicated, and then move on to more difficult stuff later on. So I chose to make a clone of pong.
For the purpose of writing code, testing it and compiling it in eclipse, I haven't written a main method, but I will be sure to put one in.
main.java
Code:
package blank;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.*;
import java.applet.*;
public class main extends Applet implements KeyListener, Runnable{
ball ball;
paddles player;
paddles computer;
Thread t1;
private Image dbImage;
private Graphics dbg;
int ballSpeedx = 10;
int ballSpeedy = 1;
public void init() {
setSize(1000, 600);
addKeyListener(this);
player = new paddles();
computer = new paddles();
ball = new ball(30, 30, 485, 285, ballSpeedx, ballSpeedy, 10);
}
public void start() {
t1 = new Thread(this);
t1.start();
}
public void stop() {
t1.stop();
}
public void run() {
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while(true) {
ball.move(3, 3);
if(ball.getX() > 1000 - ball.getWidth()) {
player.score();
int bx = ball.getX();
int by = ball.getY();
ball.setLocation(bx, by);
} else if(ball.getX() < 0 + ball.getWidth()) {
computer.score();
}
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void paint(Graphics g) {
int pw = 30;
int ph = 120;
int xpos = 0;
int ypos = 300 - (ph/2);
ball.paint(g);
player.drawPaddle(g, xpos, ypos, pw, ph);
computer.drawPaddle(g, 970, ypos, pw, ph);
player.drawScores(g, player.getScore(), 100, 100);
computer.drawScores(g, computer.getScore(), 850, 100);
}
public void update(Graphics g) {
if(dbImage == null) {
dbImage = createImage ((int)this.getSize().width, (int)this.getSize().height);
dbg = dbImage.getGraphics ();
}
dbg.setColor(getBackground());
dbg.fillRect(0, 0, (int)this.getSize().width, (int)this.getSize().height);
dbg.setColor(getForeground());
paint(dbg);
g.drawImage(dbImage, 0, 0, this);
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_UP) {
int ypos = player.getY();
ypos += 10;
}
if(key == KeyEvent.VK_DOWN) {
int ypos = player.getY();
ypos += 10;
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}
ball.java
Code:
package blank;
import java.awt.Color;
import java.awt.Graphics;
public class ball{
private int pos_x;
private int pos_y;
private int first_x;
private int first_y;
private int width;
private int height;
int x_speed;
int y_speed;
int maxspeed;
public ball(int w, int h, int x, int y, int dx, int dy, int ms) {
pos_x = x;
pos_y = y;
first_x = x;
first_y = y;
x_speed = dx;
y_speed = dy;
width = w;
height = h;
maxspeed = ms;
}
public void move(int dy, int dx) {
first_x += dx;
first_y += dy;
}
public void paint(Graphics g) {
g.setColor(Color.red);
g.fillRect(first_x, first_y, width, height);
}
public int getX() {
return pos_x;
}
public int getY() {
return pos_y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public void setLocation(int bx, int by) {
bx = first_x;
by = first_y;
pos_x = bx;
pos_y = by;
}
}
paddles.java
Code:
package blank;
import java.applet.*;
import java.awt.*;
import java.util.*;
import java.net.*;
public class paddles{
private int score;
private int ypos = 240;
public paddles() {
score = 0;
}
public int getScore() {
return score;
}
public void score() {
score++;
}
public void drawPaddle(Graphics g, int xpos, int ypos, int pw, int ph) {
g.setColor(Color.red);
g.fillRect(xpos, ypos, pw, ph);
}
public void drawScores(Graphics g, int score, int locx, int locy) {
g.drawString("Score: " + score, locx, locy);
}
public int getY() {
return ypos;
}
}
The things I'm really struggling to do is to:
- get the ball to return to the middle of the screen once it leaves the screen
- get the player paddle to move
- get the scores to update
Does anybody have any suggestions as to how I could get these three things working?
(I'm kinda new to java outside of the ACM libraries.)
Re: I'm making a Java pong clone and I've been running into a few troubles.
Quote:
Originally Posted by
mcurry
EDIT: (Just realised that this is probably in the wrong section, sorry. New to java would probably be a better section.)
Moved from Java 2D
db