Results 1 to 6 of 6
- 11-16-2011, 08:27 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 28
- Rep Power
- 0
Need help with pong semester project
Hey all so I'm making a pong game for my semester project. Currently, I'm just trying to get the basic engine up and running for it. I have no experience with graphics or windows programming before this so its all news to me. Well, I was going along, made my ball class and created an object and drew it on the screen. It displayed (even though not in the spot it should have), and then I went along adding some collision detection and ball movement, and now my ball disappeared! I tried commenting out the new code to no avail, not sure what happened but I will post my code and you can look at it!
Java Code:package Pong; import java.awt.*; import java.awt.image.BufferStrategy; import javax.swing.JFrame; public class Game extends JFrame { /** * 1 player Pong-type game programmed by * Jared Ready and Jake Saile. */ //Variable and object declaration //windowWidth and windowHeight are used for the game window resolution private int windowWidth = 800; private int windowHeight = 600; private Ball ball; public static void main(String[] args) { new Game(); } //Constructor for class Game. Sets up attributes for the game window. public Game() { this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(windowWidth, windowHeight); this.setResizable(false); this.setLocation(100, 100); this.setVisible(true); //Creates a buffer strategy with 2 buffers. Gets rid of screen flickering that would //occur without this.createBufferStrategy(2); initGame(); while(true) { long start = System.currentTimeMillis(); gameLoop(); while(System.currentTimeMillis()-start < 5) { //do nothing } } } //This initializes all variables and objects needed for Game class private void initGame() { ball = new Ball(windowWidth/2, windowHeight/2, 1,-1, 40); } //This is the method that handles the logic of the game private void gameLoop() { //We need to start with moving the ball to its new position ball.posX = ball.posX + ball.speedX; ball.posY = ball.posY + ball.speedY; //Checks for wall collision, and changes movement of ball if need be if(ball.posX <= 0 || ball.posX >= windowWidth - ball.ballSize) ball.speedX = -ball.speedX; if(ball.posY <= 0) ball.speedY = -ball.speedY; drawFrame(); } private void drawFrame() { //draws the updated frame BufferStrategy buffer = this.getBufferStrategy(); Graphics graphics = null; try { graphics = buffer.getDrawGraphics(); //Clears the back buffer by just making it black graphics.setColor(Color.BLACK); graphics.fillRect(0, 0, windowWidth, windowHeight); drawBall(graphics); } finally { //Destroys graphics object not being used now graphics.dispose(); } //Switches to display the back buffer buffer.show(); //This tells the system to synchronize the state of the graphics so that the //display is up to date Toolkit.getDefaultToolkit().sync(); } //Draws ball on the screen at the middle public void drawBall(Graphics graphics) { graphics.setColor(Color.RED); graphics.fillOval(ball.posX, ball.posY, ball.ballSize, ball.ballSize); } }Java Code:package Pong; public class Ball { /** * Class for holding our Ball object. * Just holds the dimensions and speed of the ball */ //Using public variables currently for ease of use //when drawing the ball //Speed public int speedX; public int speedY; //Start position public int posX; public int posY; //Size public int ballSize; //Contructor for the Ball class. //Parameters (position x, position y, speed on x axis, speed on y axis, size of ball) public Ball(int posX, int posY, int speedX, int speedY, int ballSize) { posX = this.posX; posY = this.posY; speedX = this.speedX; speedY = this.speedY; ballSize = this.ballSize; }
- 11-16-2011, 08:29 PM #2
Re: Need help with pong semester project
Try printing out the ball's position each frame to see where it is?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 11-17-2011, 08:30 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 28
- Rep Power
- 0
Re: Need help with pong semester project
I did try that, the coordinates always remain at (0,0) and the I made a print for the variables, also at (0,0)
- 11-17-2011, 02:39 PM #4
Re: Need help with pong semester project
Ah, I see. Look in Ball's constructor. What do you think those statements are doing? Check your assumptions.
Also, to do painting, you might want to follow this tutorial: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 11-17-2011, 09:43 PM #5
Member
- Join Date
- Nov 2011
- Posts
- 28
- Rep Power
- 0
Re: Need help with pong semester project
In my constructor? I think it's setting the variables for the object to whatever was passed into the constructor? Am I wrong?
- 11-17-2011, 09:46 PM #6
Member
- Join Date
- Nov 2011
- Posts
- 28
- Rep Power
- 0
Re: Need help with pong semester project
Oh wow! Thank you so much! I guess I misunderstood the this keyword. I THOUGHT it referred to the closest item with that name, ie. the most "local" one to it. So I thought it would be referring to the variable created in the constructor. Once again, thank you so much! I've been stuck for a couple days just because of that problem. Now I have a bouncing ball program :)
Similar Threads
-
Help - end of semester prioject. Linking multiple addresses to a client with 2 lists.
By jonytek in forum New To JavaReplies: 3Last Post: 06-07-2011, 05:40 AM -
Last Java assignment for the semester.
By Z-slasher in forum New To JavaReplies: 4Last Post: 04-28-2011, 01:30 AM -
Pong game
By hannes in forum Threads and SynchronizationReplies: 4Last Post: 02-03-2011, 10:52 AM -
My semester proj.
By ramsrocker in forum New To JavaReplies: 4Last Post: 03-08-2009, 05:25 AM -
Half a semester into java
By apfroggy0408 in forum New To JavaReplies: 3Last Post: 12-17-2007, 10:05 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks