[JAVA] My program just freezes like 2 seconds in. No warnings, no errors, no messages, nothing!

Started by
1 comment, last by ChristofferKrook 11 years, 6 months ago
Hey there! I just finished up a little pong clone to show a friend interested in beginning programming, but now when i thought i was "done", i realise that the program just freezes.. nothing is alive. I know this due to System.out.println(); not working when it is frozen.

Could someone here please help me? Thanks in advance!
I know i am asking alot, but really, thanks


import java.awt.*;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.swing.JFrame;
public class Main extends Canvas implements Runnable{
//
//variables
//
//
private keyListener input = new keyListener(this);

private static final long serialVersionUID = 1L;
public static final int HEIGHT = 200 * 5;
public static final int WIDTH = 320 * 5;

private boolean running = false;

public Rectangle bbox1, bbox2, bbox3;

private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);

public int enemySpeed = 1;
public int ballSpeedX = 4;
public int ballSpeedY = 4;

public int posX, posY, width, height, enemyX, enemyY, score, enemyScore;
public int ballPosX, ballPosY;

boolean someoneScored = false;

int newDir;
//
//
//main
//
//
public Main(){
posX = 80;
posY = HEIGHT/2 - 80;
width = 40;
height = 160;
enemyX = WIDTH - 80 - width;
enemyY = posY;
ballPosX = WIDTH / 2 - 20;
ballPosY = HEIGHT / 2 - 20;
score = 0;
enemyScore = 0;
}

public void start(){
running = true;
new Thread(this).start();
}

public void stop(){
running = false;
}

public void run() {
while(running){
loop();
}
}

public void loop(){

tick();
render();
}
//
//
//gameloop
//
//
public void tick(){

if(input.up){
System.out.println("I am still alive");
posY -= 1;
}
if(input.down){
System.out.println("I am still alive");
posY += 1;;
}
if(input.enter && someoneScored == true) relaunchBall();



bbox1 = new Rectangle(enemyX, enemyY, width, height);
bbox2 = new Rectangle(ballPosX, ballPosY, 40, 40);
bbox3 = new Rectangle(posX, posY, width, height);

if(bbox1.intersects(bbox2)){

}

ballPosX += ballSpeedX / 2;
ballPosY += ballSpeedY / 2;

if(ballPosY >= HEIGHT - 40){
ballSpeedY *= -1;
}
if(ballPosX >= WIDTH - 40){
ballPosX = WIDTH / 2 - 40;
}
if(ballPosY < 0){
ballSpeedY *= -1;
}

if(ballPosY <= enemyY + 80){
enemyY -= 1;
}
if(ballPosY >= enemyY + 80){
enemyY += 1;
}


if(ballSpeedX <= 0){
moveTowardsCenter();
}



if(ballPosX >= enemyX){
someoneScored = true;
score += 1;
ballPosX = WIDTH / 2 - 20;
ballPosY = HEIGHT / 2 - 20;
ballSpeedX = 0;
ballSpeedY = 0;
}

if(ballPosX <= 0){
enemyScore += 1;
ballPosX = WIDTH / 2 - 20;
ballPosY = HEIGHT / 2 - 20;
ballSpeedX = 0;
ballSpeedY = 0;
someoneScored = true;
}

if(bbox1.intersects(bbox2)){
ballPosX -= 4;
ballSpeedX *= -1;
}

if(bbox3.intersects(bbox2)){
ballPosX += 4;
ballSpeedX *= -1;
}
//System.out.println(posY);



}
//
//
//drawing
//
//
public void render(){
BufferStrategy bs = getBufferStrategy();
if(bs == null){
createBufferStrategy(3);
return;
}

Graphics g = image.getGraphics();
g.setColor(Color.BLACK);
g.fillRect(0, 0, WIDTH, HEIGHT);
//g.dispose();


g = bs.getDrawGraphics();
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);

//Draw here. g before
g.setColor(Color.RED);
g.fillRect(posX, posY, width, height);
g.fillRect(enemyX, enemyY, width, height);
g.fillOval(ballPosX, ballPosY, 40, 40);
g.drawString("Score: " + score, 50, 60);
g.drawString("EnemyScore: " + enemyScore, WIDTH - 150, 60);
if(someoneScored){
g.drawString("Press enter to relaunch the ball", WIDTH/2, 80);
}


g.dispose();
bs.show();
}
//
//
//for "dead" ball
//
//
void relaunchBall(){
Random r = new Random();

newDir = r.nextInt(4);
someoneScored = false;
System.out.println(newDir);

switch(newDir){
case 0:
ballSpeedX = 4;
ballSpeedY = 4;
break;
case 1:
ballSpeedX = 4;
ballSpeedY = -4;
break;
case 2:
ballSpeedX = -4;
ballSpeedY = -4;
break;
case 3:
ballSpeedX = -4;
ballSpeedY = 4;
break;
}
}
//
//
//For the ball to move towards the center when the ball is going towards the player
//
//
void moveTowardsCenter(){
int desiredPos = HEIGHT / 2;
int realYValue = enemyY + 80;

if(realYValue <= desiredPos){
enemyY += 1;
}
if(realYValue >= desiredPos){
enemyY -= 1;
}
while(realYValue == desiredPos){
enemyY = HEIGHT / 2 - 80;
}
}

public static void main(String[] args){
Main game = new Main();
game.setMinimumSize(new Dimension(WIDTH, HEIGHT));
game.setMaximumSize(new Dimension(WIDTH, HEIGHT));
game.setPreferredSize(new Dimension(WIDTH, HEIGHT));

JFrame frame = new JFrame("Pong");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(game);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);

game.start();
}


}
Advertisement
Your problem lies in the moveTowardsCenter() function. In there you have this code:
[source lang="java"] while(realYValue == desiredPos){
enemyY = HEIGHT / 2 - 80;
}[/source]
This creates an infinite loop. I would expect you wanted this:
[source lang="java"] if(realYValue == desiredPos){
enemyY = HEIGHT / 2 - 80;
}[/source]
Thanks! That solved it! Thank you for putting your time into this! ^^

Now i just need to improve this retarded/unbeatable AI...

This topic is closed to new replies.

Advertisement