Java Game
Posted: Thu Feb 19, 2004 7:37 pm
I don't know if java is client side.. but it is web based
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.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.
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++){
if (directionїtemp] > 2)
directionїtemp] = 2;}
x += directionї0] * speed;
y += directionї1] * speed;
}
}Code: Select all
import javax.swing.* ;
import java.awt.* ;
public class compplayer
{
protected int x, y, size, speed, power, point;
compplayer()
{
x = 100;
y = 50;
size = 20;
speed = 3;
power = 5;
point = 0;
}
public void choose(int ballx)
{
if (ballx - (size / 2) < x)
x -= speed;
if (ballx - (size / 2) > x)
x +=speed;
}
}Code: Select all
import javax.swing.* ;
import java.awt.* ;
public class player
{
protected int x, y, size, speed, power, point;
player()
{
x = 100;
y = 400;
size = 20;
speed = 5;
power = 5;
point = 0;
}
}