Results 1 to 2 of 2
- 02-13-2012, 09:38 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 2
- Rep Power
- 0
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
ball.javaJava 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) { } }
paddles.javaJava 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; } }
The things I'm really struggling to do is to: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; } }
- 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.)Last edited by mcurry; 02-13-2012 at 09:41 PM.
- 02-13-2012, 10:08 PM #2
Similar Threads
-
i'm new to java and having troubles making an ascii animation
By n_cruz21 in forum New To JavaReplies: 6Last Post: 02-13-2012, 09:57 PM -
Problem running Applet in IE but works when running just Java in Netbeans?
By rodneyc8063 in forum Java AppletsReplies: 7Last Post: 12-18-2011, 04:13 AM -
Copy or Clone java Linked Lists
By koko99 in forum AWT / SwingReplies: 1Last Post: 04-28-2011, 03:07 AM -
Java Game Expert Wanted to clone Chess.com
By TOPSECRET in forum Jobs OfferedReplies: 1Last Post: 04-28-2010, 03:20 AM -
An nslookup clone in Java
By Java Tip in forum java.netReplies: 0Last Post: 04-07-2008, 08:12 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks