🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: aus.computers.java
Article #1050

CAN ANYONE HELP? - with java source code for assignment due tomorrow

#1050
From: Claudia
Date: Wed, 27 Oct 1999 00:00
222 lines
7052 bytes
Hi

Claudia here.  I need some desperate assistance with the following:-

Objective:

To make fish move in left and right directions and allow
them to be killed by bullets (I know this sounds bizarre but it's
source code that I've copied and tried to combine in the one coding but
there seems to be problems which I'm unable to detect.  I'm not very
good at this stuff and am very desperate to get some answers.

Sprite and SpriteVector codes are also used as part of this code.

Alternatively, if anyone has any good game ideas using animation,
double buffering, media tracker and sprites and is fairly simple I
would be more than willing to listen.



These are my errors when compiled:


Fish.java:21: '(' expected.
  Point         speed[] =       {new Point(-2,0),
       ^
Fish.java:101: Identifier expected.
    sv = new SpriteVector(new ImageBackground(this, back));
      ^
Fish.java:113: Type expected.
    while (Thread.currentThread() == animate) {
    ^
3 errors




This is the source code:

// Fish Class
// Fish.java

// Imports
import java.applet.Applet;
import java.awt.*;
import java.util.Random;
import java.awt.event.*;

public class Fish extends Applet implements Runnable {
  Image         offImage, back;
  Image[]       Fish = new Image[9];
  Image         bullet;
  Graphics      offGrfx;
  Thread        animate;
  MediaTracker  tracker;
  SpriteVector  sv;
  int           delay = 83; // 12 fps
  Random        rand = new

  Point 	speed[] = 	{new Point(-2,0),
  				new Point(4,1),
  				new Point(3,0),
  				new Point(4,0),
  				new Point(2,0),
  				new Point(1,-4),
  				new Point(-3,0),
  				new Point(-4,0),
  				new Point(-1,0)};

  public int			orgX =300, orgY = 400;	// origin of bullet
  public int			bulletVel = 15;		// speed of bullet

  public String getAppletInfo() {
    return (new String("JFish by Jasmin Marcolin derived from an applet
Atoms by Michael Morrison"));
  }

  public void init() {
    // Load and track the images
      tracker = new MediaTracker(this);
    back = getImage(getCodeBase(), "images/Fishback.gif");
    tracker.addImage(back, 0);
    for (int i=1; i==fish.length; i++) {
    	fish[i-1] = getImage(getCodeBase(), "images/Fish"+ i + ".gif");
    	tracker.addImage(fish[i-1], 0);
    bullet = getImage(getCodeBase(), "images/bullet.gif");
    tracker.addImage(bullet, 0);

    // Define, instantiate and register a MouseListener object.
    this.addMouseListener(new MouseAdapter() {
      public void mousePressed(MouseEvent e) {
      	int aimX, aimY, velX, velY;
      	double aim, rvelX, rvelY;
      	aimX = e.getX() - orgX;
      	aimY = e.getY() - orgY;
      	if (aimX == 0) {
      		rvelY = bulletVel;
      		velY = (int)bulletVel;
      		velX = 0;
      	} else {
      		aim = (double)aimY/(double)aimX;
      		rvelX = -bulletVel/(Math.sqrt(1 + aim * aim));
      		rvelY = aim * rvelX;
      		velY = (int)Math.round(rvelY);
      		velX = (int)Math.round(rvelX);
      	}
        addBullet(new Point(velX,velY),rvelY);
      }
    });

  }
}
  public void addBullet(Point vel, double rvelY) {
         sv.add(new Sprite(this, bullet, new Point(orgX,orgY),
        	vel, 0, Sprite.BA_DIE, true, rvelY));
  }

  public void start() {
    if (animate == null) {
      animate = new Thread(this);
      animate.start();
    }
  }

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

  public void run() {
    try {
      tracker.waitForID(0);
    }
    catch (InterruptedException e) {
      return;
    }
}
    // Create and add the sprites
    sv = new SpriteVector(new ImageBackground(this, back));


    for (int i = 0; i < fish.length; i++) {
      Point pos = sv.getEmptyPosition(new Dimension(
        fish[i].getWidth(this), fish[i].getHeight(this)));
      sv.add(createFish(pos, i));
    }

	
    // Update everything
    long t = System.currentTimeMillis();
    while (Thread.currentThread() == animate) {
      if (spriteCounter==0) {
      	sv.add(createFish(new Point(0,20)));
      	spriteCounter=50;
      } else spriteCounter--;
      sv.update();
      repaint();
      try {
        t += delay;
        Thread.sleep(Math.max(0, t - System.currentTimeMillis()));
      }
      catch (InterruptedException e) {
        break;
      }
    }
  }

  public void update(Graphics g) {
    // Create the offscreen graphics context
    if (offGrfx == null) {
      offImage = createImage(size().width, size().height);
      offGrfx = offImage.getGraphics();
    }

    // Draw the sprites
    sv.draw(offGrfx);

    // Draw the image onto the screen
    g.drawImage(offImage, 0, 0, null);
  }

  public void paint(Graphics g) {
    if ((tracker.statusID(0, true) & MediaTracker.ERRORED) != 0) {
      // Draw the error rectangle
      g.setColor(Color.red);
      g.fillRect(0, 0, size().width, size().height);
      return;
    }
    if ((tracker.statusID(0, true) & MediaTracker.COMPLETE) != 0) {
      // Draw the offscreen image
      g.drawImage(offImage, 0, 0, null);
    }
    else {
      // Draw the title message (while the images load)
      Font        f1 = new Font("TimesRoman", Font.BOLD, 28),
                  f2 = new Font("Helvetica", Font.PLAIN, 16);
      FontMetrics fm1 = g.getFontMetrics(f1),
                  fm2 = g.getFontMetrics(f2);
      String      s1 = new String("Machine Harpooned Fish"),
                  s2 = new String("Loading images...");
      g.setFont(f1);
      g.drawString(s1, (size().width - fm1.stringWidth(s1)) / 2,
        ((size().height - fm1.getHeight()) / 2) + fm1.getAscent());
      g.setFont(f2);
      g.drawString(s2, (size().width - fm2.stringWidth(s2)) / 2,
        size().height - fm2.getHeight() - fm2.getAscent());
    }
  }

  private Sprite createFish(Point pos, int i) {
    return new Sprite(this, fish[i], pos, speed[i], 0, Sprite.BA_WRAP);
  }
}


IF ANYONE COULD HELP I WOULD BE SOOOOO GRATEFUL!!!


* Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!
Technical Details
Message-ID: <11f733ec.33b42c39@usw-ex0101-001.remarq.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10d6db,2be9284067a5196,start
X-Google-Attributes: gid10d6db,public
From: Claudia <jmarcoliNOjmSPAM@ozemail.com.au.invalid>
Subject: CAN ANYONE HELP? - with java source code for assignment due tomorrow
Date: 1999/10/27
Message-ID: <11f733ec.33b42c39@usw-ex0101-001.remarq.com>
X-Deja-AN: 541116481
Bytes: 6289
X-Originating-Host: 203.108.0.57
X-Complaints-To: wrenabuse@remarq.com
X-Trace: WReNphoon3 941012414 10.0.2.3 (Wed, 27 Oct 1999 01:20:14 PDT)
Organization: http://www.remarq.com: The World's Usenet/Discussions Start Here
NNTP-Posting-Date: Wed, 27 Oct 1999 01:20:14 PDT
X-Wren-Trace: eJeympuCxY/E3Y6egNSZkoykiIiXntaIntWbm5PNj8LYw4fF14bSwdXG3cfO
Newsgroups: aus.computers.java