Jump to content




Recent Status Updates

  • Photo
      18 Aug
    KodeKool

    When faced with a wall of errors and no hope to fix them, remember the following "Programs always do what you tell them to, and seldom what you want them to, but eventually you'll run out of things that can go wrong and it'll just work. and that's the secret to good programming."

    Show comments (2)
View All Updates

Developed by Kemal Taskin
Photo
- - - - -

Simple game in Java


  • Please log in to reply
2 replies to this topic

#1 sufler

sufler

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 04 December 2008 - 07:28 AM

Hello!
I barely learn Threads and animation in Java. I was trying to make a simple game. This is the code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class Pong extends JFrame implements Runnable {
int pilkax, pilkay, palx, paly;
Thread runner, paletka;
  Image pilkaImg;
  Graphics pilkaG;
  int czyMinus, koniecpal;
  long losowa;
  boolean czyPilka, czyPaletka;


public Pong(){
czyMinus = 0;
setSize(340, 260);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
    pilkaImg = createImage(this.size().width, this.size().height);
    pilkaG = pilkaImg.getGraphics();

addMouseListener(new MouseListener(){

public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
start();
}

}
);

addKeyListener(new KeyListener(){
public void keyPressed(KeyEvent e){
if(e.getKeyChar()=='a'){
startpal();
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
});

poczatek();

}

public void startpal(){
czyPaletka = true;
    if (paletka == null) {
      paletka = new Thread(this);
      paletka.start();
    }
}

    public void stoppal() {
    if (paletka != null) {
      paletka.stop();
      paletka = null;
    }
  }

  public void start() {
  czyPilka = true;
    if (runner == null) {
      runner = new Thread(this);
      runner.start();
    }
  }
  
    public void stop() {
    if (runner != null) {
      runner.stop();
      runner = null;
    }
  }
          
public void poczatek(){
palx=170;
paly=230;
pilkax=180;
pilkay=paly-20;
czyMinus=3; 
czyPilka = false;
czyPaletka = false;         
}
          

public void run() {

if(czyPaletka){
czyPaletka = false;
for(int i = 0; i < 4; i++){
palx--;
try { Thread.sleep( 10  );}
       catch (InterruptedException ex) {}
           repaint();
	   
}

/*while(true){
for(; palx >= 20; palx--) {
try { Thread.sleep( 10  );}
       catch (InterruptedException ex) {}
           repaint();
}
for(; palx <= 170; palx++) {
try { Thread.sleep( 10  );}
       catch (InterruptedException ex) {}
           repaint();
}
}*/
}
if(czyPilka){
czyPilka = false;
 while(true){
      for(; pilkay >= 40; pilkay--) {
	  
	   
	  
	 switch(czyMinus){
	  case 0 :
	  pilkax++;
	  break;
	  case 1 :
	  pilkax--;
	  break;
	  } 
	  
	  if(pilkay==40&&pilkax>=20&&pilkax<=320){
	  losowa = Math.round(1*Math.random());
	  czyMinus = (int) losowa;
	  }
	  if(pilkax==300&&pilkay>=40&&pilkay<=240){
	  czyMinus = 1;
	  }
	  if(pilkay==240&&pilkax>=20&&pilkax<=320){
	  czyMinus = 1;
	  }
	  koniecpal = palx + 60;
	  if(pilkay>=paly&&pilkax>=palx&&pilkax<=koniecpal){
	  losowa = Math.round(1*Math.random());
	  czyMinus = (int) losowa;
	  break;
	  }	  
	  if(pilkax==20&&pilkay>=20&&pilkay<=240){
	  czyMinus = 0;
	  }		  
          
       try { Thread.sleep( 10 /*10*/ );}
       catch (InterruptedException ex) {}
           repaint();
      }
     // powrot pilki

      for(; pilkay <= 220; pilkay++) {
	  
	   
	  
	  switch(czyMinus){
	  case 0 :
	  pilkax++;
	  break;
	  case 1 :
	  pilkax--;
	  break;
	  }	
	  
	  if(pilkay==40&&pilkax>=20&&pilkax<=320){
	  losowa = Math.round(1*Math.random());
	  czyMinus = (int) losowa;
	  }
	  if(pilkax==300&&pilkay>=40&&pilkay<=240){
	  czyMinus = 1;
	  }
	  if(pilkay==240&&pilkax>=20&&pilkax<=320){
	  czyMinus = 1;
	  }
	  koniecpal = palx + 60;
	  if(pilkay>=paly&&pilkax>=palx&&pilkax<=koniecpal){
	  losowa = Math.round(1*Math.random());
	  czyMinus = (int) losowa;
	  break;
	  }	  
	  if(pilkax==20&&pilkay>=20&&pilkay<=240){
	  czyMinus = 0;
	  }	  
	  
       try { Thread.sleep( 10 );}
       catch (InterruptedException ex) {}
           repaint();
       }
 }
}
}

public void SprawdzKrawedzie(){

 switch(czyMinus){
	  case 0 :
	  pilkax++;
	  break;
	  case 1 :
	  pilkax--;
	  break;
	  }	  
	  
	  if(pilkay==40&&pilkax>=20&&pilkax<=320){
	  losowa = Math.round(1*Math.random());
	  czyMinus = (int) losowa;
	  }
	  if(pilkax==320&&pilkay>=40&&pilkay<=240){
	  losowa = Math.round(1*Math.random());
	  czyMinus = (int) losowa;
	  }
	  if(pilkay==240&&pilkax>=20&&pilkax<=320){
	  losowa = Math.round(1*Math.random());
	  czyMinus = (int) losowa;
	  }
	  if(pilkax==20&&pilkay>=20&&pilkay<=240){
	  losowa = Math.round(1*Math.random());
	  czyMinus = (int) losowa;
	  }	 

}

  public void update(Graphics gDC) {
     paint(gDC);
  }

 
public void paint(Graphics gDC){


pilkaG.clearRect(0, 0, getSize().width, getSize().height);
pilkaG.setColor(Color.RED);
koniecpal = palx + 40;

pilkaG.setColor(Color.BLACK);
pilkaG.drawRect(20, 40, 300, 200);
pilkaG.fillRect(palx, paly, 40 /*40*/, 10);
pilkaG.setColor(Color.BLUE);
pilkaG.fillOval(pilkax, pilkay, 20, 20);

gDC.drawImage(pilkaImg, 0, 0, this);
}

  public void destroy() {
    pilkaG.dispose();
  }

public static void main(String[] args){
new Pong();
}
}

Variables' names are in Polish. pilka (actually piłka) - ball, pal (actually paletka) - palette.
I don't know:
-Why the ball doesn't bounce from the palette.
-How to move the palette after press eg. 'a'
-Why sometimes the application shows Null Pointer Exception.

Please, explain me it.
  • 0

#2 Chinmoy

Chinmoy

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 365 posts

Posted 04 December 2008 - 09:23 PM

let me tell you one thing first. Swing is not thread safe. And lemme try this code and il try to tell you...
  • 0

God is real... unless declared an integer

my blog :: http://techarraz.com/


#3 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 05 December 2008 - 01:38 PM

Hello!
I barely learn Threads and animation in Java. I was trying to make a simple game. This is the code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class Pong extends JFrame implements Runnable {
int pilkax, pilkay, palx, paly;
Thread runner, paletka;
  Image pilkaImg;
  Graphics pilkaG;
  int czyMinus, koniecpal;
  long losowa;
  boolean czyPilka, czyPaletka;


public Pong(){
czyMinus = 0;
setSize(340, 260);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
    pilkaImg = createImage(this.size().width, this.size().height);
    pilkaG = pilkaImg.getGraphics();

addMouseListener(new MouseListener(){

public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
start();
}

}
);

addKeyListener(new KeyListener(){
public void keyPressed(KeyEvent e){
if(e.getKeyChar()=='a'){
startpal();
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
});

poczatek();

}

public void startpal(){
czyPaletka = true;
    if (paletka == null) {
      paletka = new Thread(this);
      paletka.start();
    }
}

    public void stoppal() {
    if (paletka != null) {
      paletka.stop();
      paletka = null;
    }
  }

  public void start() {
  czyPilka = true;
    if (runner == null) {
      runner = new Thread(this);
      runner.start();
    }
  }
  
    public void stop() {
    if (runner != null) {
      runner.stop();
      runner = null;
    }
  }
          
public void poczatek(){
palx=170;
paly=230;
pilkax=180;
pilkay=paly-20;
czyMinus=3; 
czyPilka = false;
czyPaletka = false;         
}
          

public void run() {

if(czyPaletka){
czyPaletka = false;
for(int i = 0; i < 4; i++){
palx--;
try { Thread.sleep( 10  );}
       catch (InterruptedException ex) {}
           repaint();
	   
}

/*while(true){
for(; palx >= 20; palx--) {
try { Thread.sleep( 10  );}
       catch (InterruptedException ex) {}
           repaint();
}
for(; palx <= 170; palx++) {
try { Thread.sleep( 10  );}
       catch (InterruptedException ex) {}
           repaint();
}
}*/
}
if(czyPilka){
czyPilka = false;
 while(true){
      for(; pilkay >= 40; pilkay--) {
	  
	   
	  
	 switch(czyMinus){
	  case 0 :
	  pilkax++;
	  break;
	  case 1 :
	  pilkax--;
	  break;
	  } 
	  
	  if(pilkay==40&&pilkax>=20&&pilkax<=320){
	  losowa = Math.round(1*Math.random());
	  czyMinus = (int) losowa;
	  }
	  if(pilkax==300&&pilkay>=40&&pilkay<=240){
	  czyMinus = 1;
	  }
	  if(pilkay==240&&pilkax>=20&&pilkax<=320){
	  czyMinus = 1;
	  }
	  koniecpal = palx + 60;
	  if(pilkay>=paly&&pilkax>=palx&&pilkax<=koniecpal){
	  losowa = Math.round(1*Math.random());
	  czyMinus = (int) losowa;
	  break;
	  }	  
	  if(pilkax==20&&pilkay>=20&&pilkay<=240){
	  czyMinus = 0;
	  }		  
          
       try { Thread.sleep( 10 /*10*/ );}
       catch (InterruptedException ex) {}
           repaint();
      }
     // powrot pilki

      for(; pilkay <= 220; pilkay++) {
	  
	   
	  
	  switch(czyMinus){
	  case 0 :
	  pilkax++;
	  break;
	  case 1 :
	  pilkax--;
	  break;
	  }	
	  
	  if(pilkay==40&&pilkax>=20&&pilkax<=320){
	  losowa = Math.round(1*Math.random());
	  czyMinus = (int) losowa;
	  }
	  if(pilkax==300&&pilkay>=40&&pilkay<=240){
	  czyMinus = 1;
	  }
	  if(pilkay==240&&pilkax>=20&&pilkax<=320){
	  czyMinus = 1;
	  }
	  koniecpal = palx + 60;
	  if(pilkay>=paly&&pilkax>=palx&&pilkax<=koniecpal){
	  losowa = Math.round(1*Math.random());
	  czyMinus = (int) losowa;
	  break;
	  }	  
	  if(pilkax==20&&pilkay>=20&&pilkay<=240){
	  czyMinus = 0;
	  }	  
	  
       try { Thread.sleep( 10 );}
       catch (InterruptedException ex) {}
           repaint();
       }
 }
}
}

public void SprawdzKrawedzie(){

 switch(czyMinus){
	  case 0 :
	  pilkax++;
	  break;
	  case 1 :
	  pilkax--;
	  break;
	  }	  
	  
	  if(pilkay==40&&pilkax>=20&&pilkax<=320){
	  losowa = Math.round(1*Math.random());
	  czyMinus = (int) losowa;
	  }
	  if(pilkax==320&&pilkay>=40&&pilkay<=240){
	  losowa = Math.round(1*Math.random());
	  czyMinus = (int) losowa;
	  }
	  if(pilkay==240&&pilkax>=20&&pilkax<=320){
	  losowa = Math.round(1*Math.random());
	  czyMinus = (int) losowa;
	  }
	  if(pilkax==20&&pilkay>=20&&pilkay<=240){
	  losowa = Math.round(1*Math.random());
	  czyMinus = (int) losowa;
	  }	 

}

  public void update(Graphics gDC) {
     paint(gDC);
  }

 
public void paint(Graphics gDC){


pilkaG.clearRect(0, 0, getSize().width, getSize().height);
pilkaG.setColor(Color.RED);
koniecpal = palx + 40;

pilkaG.setColor(Color.BLACK);
pilkaG.drawRect(20, 40, 300, 200);
pilkaG.fillRect(palx, paly, 40 /*40*/, 10);
pilkaG.setColor(Color.BLUE);
pilkaG.fillOval(pilkax, pilkay, 20, 20);

gDC.drawImage(pilkaImg, 0, 0, this);
}

  public void destroy() {
    pilkaG.dispose();
  }

public static void main(String[] args){
new Pong();
}
}

Variables' names are in Polish. pilka (actually piłka) - ball, pal (actually paletka) - palette.
I don't know:
-Why the ball doesn't bounce from the palette.
-How to move the palette after press eg. 'a'
-Why sometimes the application shows Null Pointer Exception.

Please, explain me it.


Feels kinda bloated. I will help you when I am done with my work, feels like this could be something fun for both you and me.
  • 0