Java Game

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
1veedo
Forum Commoner
Posts: 31
Joined: Thu Feb 19, 2004 3:59 pm
Location: With my computer wherever that may be.
Contact:

Java Game

Post by 1veedo »

I don't know if java is client side.. but it is web based :D This is my first atempt at a java game.

Basicaly, it wont run and I don't know why! It's not comented but it should be easy to understand. It's almost like it's structured - of cource it isn't but the programm flow is prety straight froward.

Code: Select all

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

public class Pong extends JApplet 
   implements KeyListener,MouseListener, ActionListener
{      
   private engine game;
      
   public void init() 
   { 
      game = new engine();
      
      Container c = getContentPane(); 
      c.setBackground(Color.black);
      c.addKeyListener(this); 
      c.addMouseListener(this);
      c.requestFocus();
      
      game.start();
   } 
      
   public void keyPressed(KeyEvent e) 
   { 
      int code = e.getKeyCode(); 
         
      if ( code == KeyEvent.VK_LEFT )
         game.plLeft();
      if ( code == KeyEvent.VK_RIGHT )    
         game.plRight(); 
   } 
 
   public void keyTyped (KeyEvent e) 
   { 
   
   } 
 
   public void keyReleased (KeyEvent e) 
   { 
   
   }      
     
   public void mousePressed(MouseEvent e)
   {
  	  Container c = getContentPane();
  	  c.requestFocus();
  	  
  	  repaint();
   }
   
   public void paint(Graphics g)
   {
   	  super.paint(g);
  	  game.draw(g);
   }
   
   public void mouseClicked(MouseEvent e)
   {
   }

   public void mouseReleased(MouseEvent e)
   {
   }
   public void mouseEntered(MouseEvent e)
   {
   }

   public void mouseExited(MouseEvent e)
   {
   }
   
   public void actionPerformed (ActionEvent e)
   {
   }
}

Code: Select all

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

public class engine
   implements ActionListener
{      
   private bal ball;
   private compplayer enemy;
   private player pl;
   private Timer t;
   
   engine()
   {
      ball = new bal();
      enemy = new compplayer();
      pl = new player();
   }
   
   public void start()
   {
      Timer t = new Timer (30, this);
      t.start();
   }
   
   public void actionPerformed (ActionEvent e)
   {
   	  enemy.choose(ball.x);
   	  ball.moove();
   }
   
   public void plLeft()
   {
   	  pl.x -= pl.speed;
   }
   
   public void plRight()
   {
   	  pl.x += pl.speed;
   }
   
   public void draw (Graphics g)
   { 
      g.fillRect(pl.x, pl.y, pl.x + pl.size, pl.y + 20);
      g.fillRect(enemy.x, enemy.y, enemy.x + enemy.size, enemy.y + 20);
      g.fillOval(ball.x, ball.y, ball.x + 10, ball.y + 10);
   }
}

Code: Select all

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

public class bal
{
   protected int x, y, speed, power = 0, directionї];
   
   bal()
   {
   	  direction= new intї2];
   	  x = 200;
      y = 200;
      speed = 3;
      directionї0] = 1; //x
      directionї1] = 1; //y
   }
   
   public void moove()
   {
   	  int temp;
   
      for (temp = 0; temp <2; temp++)&#123;
         if (direction&#1111;temp] > 2)
            direction&#1111;temp] = 2;&#125;
         
      x += direction&#1111;0] * speed;
      y += direction&#1111;1] * speed;
   &#125;
   
&#125;

Code: Select all

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

public class compplayer
&#123;
   protected int x, y, size, speed, power, point;
   
   compplayer()
   &#123;
   	  x = 100;
      y = 50;
      size = 20;
      speed = 3;
      power = 5;
      point = 0;
   &#125;
   
   public void choose(int ballx)
   &#123;
   	  if (ballx - (size / 2) < x)
         x -= speed;
   
      if (ballx - (size / 2) > x)
         x +=speed;
   &#125;

&#125;

Code: Select all

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

public class player
&#123;
   protected int x, y, size, speed, power, point;
   
   player()
   &#123;
   	  x = 100;
      y = 400;
      size = 20;
      speed = 5;
      power = 5;
      point = 0;
   &#125;
&#125;
It compiles but doesn't show a ball, padels or anything. Just a black background. The probablem is where Pong: paint(g) cals engine: draw(g), It's something simple but I'm jsut not experienced enough with this language to know what the eror is.
1veedo
Forum Commoner
Posts: 31
Joined: Thu Feb 19, 2004 3:59 pm
Location: With my computer wherever that may be.
Contact:

Post by 1veedo »

Ok, I see. I'll post the fixed code next week (I have to go camping). Everything is working, jsut need to port some more source from my C++ pong game.
Post Reply