Results 1 to 3 of 3
- 06-08-2009, 10:46 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 17
- Rep Power
- 0
need help coding my game (inkball)
im trying to code a game for my class, but i need two lines ( or more probably) that will allow the ball to be redrawn white so it doesnt mess up the drawn lines, and for the ball to bounce off of the lines that ive drawn.
its a java applet
here is the code
i sort of have the ball being redrawn as white, but its not timed correctly with the delay.Java Code:// The "Pong" class. import java.applet.*; import java.awt.*; public class finalproject extends Applet implements Runnable { private int lastx, lasty; int x = 20, y = 20; int r = 20; int dx = 1, dy = -2; int a = 100, b = 100; int l = 10, w = 50; int da = -2, db = 2; Image buffer; Graphics g; Graphics bufferG; int delay = 4; Thread running; public void init () { buffer = this.createImage (bounds ().width, bounds ().height); bufferG = buffer.getGraphics (); g = this.getGraphics (); } public boolean mouseDown (Event e, int mouseX, int mouseY) { lastx = mouseX; lasty = mouseY; return true; } public boolean mouseDrag (Event e, int mouseX, int mouseY) { g.setColor (Color.black); g.drawLine (lastx, lasty, mouseX, mouseY); lastx = mouseX; lasty = mouseY; return true; } public void update (Graphics g) { paint (g); } public void start () { if (running == null) { running = new Thread (this, "running"); running.start (); } } public void stop () { if (running != null && running.isAlive ()) { running.stop (); } running = null; } public void run () { for (;;) { x = x + dx; y = y + dy; a = da; b = db; if (x > bounds ().width - 10) dx = -dx; if (x < 10) { dx = -dx; x = 15; } if (y > bounds ().height - 10) dy = -dy; if (y < 0) dy = -dy; if (a > bounds ().width - 10) da = -da; if (a < 0) da = -da; if (b > bounds ().height - 50) db = -db; if (b < 0) db = -db; if ((x < a + 10) && (x + 10 > a) && (y + 10 > b) && (y < b + 50) && (dx > da)) { dx = -dx; da = -da; } if ((x < a + 10) && (x + 10 > a) && (y + 10 > b) && (y < b + 50) && (dx < da)) { dx = -dx; da = -da; } if ((y + 50 > b) && (y < b + 50) && (x + 10 > a) && (x < a + 10) && (dy < db)) { dy = -dy; db = -db; } repaint (); try { running.sleep (delay); } catch (InterruptedException e) { ; } } } public void paint (Graphics g) { g.setColor (Color.yellow); g.fillOval (x, y, r, r); for (int wastetime = 1 ; wastetime <= 199999 ; wastetime++) g.setColor (Color.white); g.fillOval (x, y, r, r); } // Place the body of the drawing method here } // paint method
if you run it, youll see what im trying to do. its sort of like an inkball game, where you draw the line with the mouse, then the ball should bounce off of it.
thanks for any help, im new at thisLast edited by tornbacchus; 06-08-2009 at 10:48 PM.
- 06-09-2009, 02:37 AM #2
Member
- Join Date
- Apr 2009
- Posts
- 17
- Rep Power
- 0
ne1? .
- 06-09-2009, 04:38 AM #3
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
You are using deprecated methods that are very rarely used now. Using them just adds problems to your code and makes it harder for anyone to understand. Also, the graphics do not work well in the first place... you may want to rethink this, as it appears to have many faults.
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
Similar Threads
-
Implementing "Game Over" in Minesweeper game based on Gridworld framework.
By JFlash in forum New To JavaReplies: 2Last Post: 08-05-2010, 04:49 AM -
Better coding
By tomiu in forum New To JavaReplies: 1Last Post: 04-09-2009, 07:19 PM -
Help with really simple coding
By tigertomas in forum New To JavaReplies: 10Last Post: 01-24-2009, 04:47 AM -
coding help
By accies76 in forum New To JavaReplies: 5Last Post: 11-12-2008, 08:15 PM -
Help On Coding problem
By mandrake446 in forum New To JavaReplies: 3Last Post: 12-08-2007, 07:01 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks