⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 absolute.java

📁 一个射击类的小游戏
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// Absolute Space

import java.awt.*;
import java.net.*;
import java.applet.Applet;
import java.applet.AudioClip;

public class Absolute extends Applet implements Runnable
{
  Dimension	d;
  Font 		largefont = new Font("Helvetica", Font.PLAIN, 24);
  Font		smallfont = new Font("Helvetica", Font.PLAIN, 14);

  FontMetrics	fmsmall, fmlarge;
  Graphics	goff;
  Image		ii;
  Thread	thethread;

  boolean	ingame=false;

  int		x, y, mousex, mousey, oldx, oldy, dx=0, dy=0, count, shield=0;
  boolean	showtitle=true;
  Image		ship;
  Image[]	fire;
  int		firecnt=0;

  // Bullet variables
  Image		bullet;
  int[]		bx;
  int[]		by;
  final int	bmy=16, bul_xs=54, bul_ys=8;

  // Meteor variables
  Image		meteor;
  int		maxmet, metcount, mtotal, mrenew, metmy;
  int[]		metx;
  int[]		mety;
  int[]		metf;
  boolean[] 	metr;
  final int	sxmet=80, symet=84;

  // These are for the star field
  public int starsX[];
  public int starsY[];
  public Color starsC[];
  public int numStars = 30;
  public int speed = 6, xSize, ySize;

  // Variables for big boom
  Image[]	boom;
  int		rndbx, rndby, rndcnt=777;
  final int	sxbom=71, sybom=100, bframes=4;

  // Global Variables
  int		distance=0, maxdist=2000;
  int		slevel, blevel, difflev, bosslevel;
  int		smax, bmax;
  int		scur, bcur, renew, rcnt=0, sstretch, txtalign=100;
  long		score;

  // Sounds
  AudioClip	blast, crash, kill;

  // Bosses here
  // Sunbird
  boolean	sunbird, sbefore, safter;
  int		sbx, sby, sbmove, maxtribe, tribe;
  int[]		sbfx, sbfy;

  final int	maxshield=9;
  final int	backcol=0x102040;
  final int	fireframe=2;
  final int	borderwidth=0;
  final int	sxsize=90, sysize=39, sxfire=11, syfire=6;
  final int	movex=10, movey=5;
  final int	scoreheight=45;
  final int	screendelay=300;

  public String getAppletInfo()
  {
    return("Absolute Space - by Aleksey Udovydchenko");
  }

  public void init()
  {
    Graphics g;
    int n;
    d = size();
    setBackground(Color.black);
    g=getGraphics();
    g.setFont(smallfont);
    fmsmall = g.getFontMetrics();
    g.setFont(largefont);
    fmlarge = g.getFontMetrics();
    ship = getImage(getCodeBase(), "ship.gif");
    bullet = getImage(getCodeBase(), "bullet.gif");
    fire = new Image[fireframe];
    for (n=0; n<fireframe; n++) {
	fire[n] = getImage(getCodeBase(), "fire"+n+".gif");
    }
    boom = new Image[bframes+1];
    for (n=0; n<=bframes; n++) {
	boom[n] = getImage(getCodeBase(), "boom"+n+".gif");
    }

    xSize = d.width - borderwidth*2;
    ySize = d.height - borderwidth*2 - scoreheight;

    x = (xSize - sxsize) / 2;
    y = ySize - sysize - scoreheight - borderwidth;
    mousex = -1;

    // will be override by command line parameters
    blevel = 3;
    slevel = 3;

    bx = new int[blevel*10];
    by = new int[blevel*10];

    for (n=0; n<blevel*10; n++) {
	bx[n] = -1;
    }

    // Meteor initiliaze
    meteor = getImage(getCodeBase(), "meteor.gif");
    maxmet = d.height / symet + 1;
    maxmet = maxmet * 10;
    metx = new int[maxmet];
    mety = new int[maxmet];
    metf = new int[maxmet];
    metr = new boolean[maxmet];

    // Audio
    try {
      blast = getAudioClip(new URL(getDocumentBase(), "blast.au"));
      crash = getAudioClip(new URL(getDocumentBase(), "collisn.au"));
      kill = getAudioClip(new URL(getDocumentBase(), "mdestr.au"));
    }
    catch (MalformedURLException e) {}

    blast.play();  blast.stop();
    crash.play();  crash.stop();
    kill.play();   kill.stop();

    initStars();

    rndcnt = 777;

    // Bosses
    // Sunbird
    sbfx = new int[11];
    sbfy = new int[11];
    sbfx[0] = 10;
    sbfy[0] = 0;
    sbfx[1] = 15;
    sbfy[1] = 10;
    sbfx[2] = 0;
    sbfy[2] = 10;
    sbfx[3] = 3;
    sbfy[3] = 15;
    sbfx[4] = 17;
    sbfy[4] = 15;
    sbfx[5] = 20;
    sbfy[5] = 20;
    sbfx[6] = 23;
    sbfy[6] = 15;
    sbfx[7] = 37;
    sbfy[7] = 15;
    sbfx[8] = 40;
    sbfy[8] = 10;
    sbfx[9] = 25;
    sbfy[9] = 10;
    sbfx[10] = 30;
    sbfy[10] = 0;

  }

// This creates the starfield in the background
        public void initStars () {
         starsX = new int[numStars];
         starsY = new int[numStars];
	 starsC = new Color[numStars];
          for (int i = 0; i < numStars; i++) {
           starsX[i] = (int) ((Math.random() * xSize - 1) + 1);
           starsY[i] = (int) ((Math.random() * ySize - 1) + 1);
	   starsC[i] = NewColor();
          }
        }

  public boolean keyDown(Event e, int key)
  {
    if (ingame)
    {
      mousex = -1;
      if (key == Event.LEFT)
          dx=-1;
      if (key == Event.RIGHT)
        dx=1;
      if (key == Event.UP)
          dy=-1;
      if (key == Event.DOWN)
        dy=1;
      if (key == ' ')
	if (bcur>0) FireGun();
      if (key == Event.ESCAPE)
        ingame=false;
    }
    else
    {
      if (key == 's' || key == 'S')
      {
        ingame=true;
	GameStart();
      }
    }
    return true;
  }

  public boolean keyUp(Event e, int key)
  {
    System.out.println("Key: "+key);
    if (key == Event.LEFT || key == Event.RIGHT)
       dx=0;
    if (key == Event.UP || key == Event.DOWN)
       dy=0;
    return true;
  }

  public void paint(Graphics g)
  {
    String s;
    Graphics gg;

    if (goff==null && d.width>0 && d.height>0)
    {
      ii = createImage(d.width, d.height);
      goff = ii.getGraphics();
    }
    if (goff==null || ii==null)
      return;

    goff.setColor(Color.black);
    goff.fillRect(0, 0, d.width, d.height);

    if (ingame)
      PlayGame();
    else
      ShowIntroScreen();
    g.drawImage(ii, 0, 0, this);
  }


  public void PlayGame()
  {
    NewMeteor();
    MoveShip();
    DrawPlayField();

    // Big bosses here
    if (sunbird) SunBird();

    ShowScore();
    distance++;
    score+=100;
    if (distance % maxdist == 0) {
	difflev++;
	if (difflev>2 & difflev<10) {
	  renew-=20;
	  bmax+=1;
	  smax+=1;
	  metmy++;
	  mrenew--;
	}
	if (difflev>3 & difflev<11) {
	  maxtribe++;
	  sbmove++;
	}
	if (difflev>3) {
	  sunbird = true;
	  tribe = maxtribe;
	}
    }

    // Renew Ship Energy
    rcnt++;
    if (rcnt % (renew / blevel) == 0) {
	bcur++;
	if (bcur>bmax) bcur=bmax;
    }
    if (distance % 500 == 0) {
	scur++;
	if (scur>smax) scur=smax;
    }
    if (rcnt>renew) rcnt=0;
  }

  public void ShowIntroScreen()
  {
    String s;

    DrawPlayField();
    goff.setFont(largefont);

    if (rndcnt > bframes) {
	rndbx = (int) (Math.random() * (xSize - sxbom) + 1);
	rndby = (int) (Math.random() * (ySize - sybom) + 1);
	rndcnt = 0;
    }

    goff.drawImage(boom[rndcnt], rndbx, rndby, this);
    rndcnt++;
    for (int i=0; i<xSize/bul_xs; i++) {
	goff.drawImage(bullet, i*bul_xs, 0, this);
	goff.drawImage(bullet, i*bul_xs, ySize-bul_ys, this);
    }

    if (showtitle)
    {
      goff.setColor(new Color(0xff0000));
      s="Absolute Space";
      goff.drawString(s,(d.width-fmlarge.stringWidth(s)) / 2, (d.height-scoreheight-borderwidth)/2 - 20);
      goff.setColor(new Color(0xff00ff));
      s="(c)2000 by Aleksey Udovydchenko";
      goff.setFont(smallfont);
      goff.drawString(s,(d.width-fmsmall.stringWidth(s))/2,(d.height-scoreheight-borderwidth)/2 + 10);
      s="freewebdesign@crosswinds.net";
      goff.drawString(s,(d.width-fmsmall.stringWidth(s))/2,(d.height-scoreheight-borderwidth)/2 + 30);
    }
    else
    {
      goff.setFont(smallfont);
      goff.setColor(new Color(0xffff00));
      s="Leftclick to start game";
      goff.drawString(s,(d.width-fmsmall.stringWidth(s))/2,(d.height-scoreheight-borderwidth)/2 - 10);
      goff.setColor(new Color(0x00ff00));
      s="Use cursor keys move, click or press SPACE to fire";
      goff.drawString(s,(d.width-fmsmall.stringWidth(s))/2,(d.height-scoreheight-borderwidth)/2 + 20);
      goff.setFont(largefont);
      goff.setColor(new Color(0xff00ff));
      s="LAST SCORE: "+score;
      goff.drawString(s,(d.width-fmlarge.stringWidth(s))/2,(d.height-scoreheight-borderwidth)/2 + 120);
    }
    count--;
    if (count<=0)
    { count=screendelay; showtitle=!showtitle; }
  }

  public void DrawPlayField()
  {

    // Show stars
    moveStars();
    for (int a = 0; a < numStars; a++) {
      goff.setColor(starsC[a]);
      goff.drawRect(starsX[a], starsY[a], 1, 1);
    }

    ShowMeteors();
    KillEmAll();
    goff.drawImage(ship, x, y, this); // paint ship

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -