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

📄 spriteshow.java

📁 how to make a game using java.
💻 JAVA
字号:
// ThreadLines.java
// [Imperial Snowman Soft]

import java.awt.*;
import java.applet.*;
import GameLib.*;

public class SpriteShow extends GameApplet
{

  int x1 = 0;
  int speed = 0;
  Image sprImg, bg, expl[];

  Gfx gfx;	 //The GAMELIB - Graphix class (double-buffering)
  Sprite spr, spr2, spr3;

  public void init()
  {
    speed = Integer.parseInt(getParameter("speed"));	//get value of the parameter speed
    if(speed > 99) speed = 99;
    setSleepTime(100-speed); // a new method (GameApplet), set the sleep time (millisec)

    sprImg = getImage(getCodeBase(), "picture.gif"); 	//the sprite image
    bg = getImage(getCodeBase(), "bg.gif"); 	//the background image

    spr = new Sprite(this, sprImg, 100, 100, 0.6, true);	//create sprite
    spr2 = new Sprite(this, sprImg, 100, 100, 1, true);	//create sprite

    loadExplosion();	//this will load an array of images

    spr.setDirection(Sprite.DIR_EAST); //set direction to east
    spr.setCollisionRect(new Rectangle(11, 17, 57, 54)); //define a collision rectangle
	//if you don't do it the collision rect will be the whole image.
    spr2.setDirection(Sprite.DIR_EAST);
    spr2.setCollisionRect(new Rectangle(11, 17, 57, 54));

    spr3.setDirection(Sprite.DIR_EAST);	//set the direction to east = Point(2,0)
    spr3.setAnimOnlyOnce(true);	//this will make the sprite invisible
				//after one animation.	
				//to restart an animation use: runAnim();

    spr.setAutoMoving(true);	//if this value is set to true, your sprite
				//will move automatically with the defined velocity
				//and the specified direction
    spr2.setAutoMoving(true);

    gfx = new Gfx(this, bg);	// Create a new Gfx(for double-buffering) with a background image.

  }

  public void paint(Graphics g)
  {
	      gfx.cls();		//clear the screen
	      gfx.drawSprite(spr);	//draw the first Sprite
	      gfx.setColor(90,90,190);	//new method: setColor(long r, long g, long b) - define the draw color
	      gfx.getOffGraphics().drawRect(spr.getCollisionRect().x, spr.getCollisionRect().y, spr.getCollisionRect().width, spr.getCollisionRect().height);
	      gfx.setColor(90,190,90);	
	      gfx.getOffGraphics().drawRect(spr.getPosition().x, spr.getPosition().y, spr.getWidth(), spr.getHeight());

	      gfx.drawSprite(spr2);
	      gfx.setColor(90,90,190);
	      gfx.getOffGraphics().drawRect(spr2.getCollisionRect().x, spr2.getCollisionRect().y, spr2.getCollisionRect().width, spr2.getCollisionRect().height);
	      gfx.setColor(90,190,90);
	      gfx.getOffGraphics().drawRect(spr2.getPosition().x, spr2.getPosition().y, spr2.getWidth(), spr2.getHeight());

	      gfx.drawSprite(spr3);
	      gfx.drawString("The green rectangles are the sizes of the images.", 10, 10);
	      gfx.drawString("The blue rectangles represent the self defined.", 10, 25);
	      gfx.drawString("collision rectangles.", 10, 35);
	      gfx.setColor(190,190,90);
	      gfx.drawString("Collision: " + spr.rectCollision(spr2), 10, 50);
 	      gfx.refresh();		//refresh the screen. "flip" the backbuffer
  }

  public void game(int id)	//this method is the most important of the GameApplet
			//the run() method of the GameApplet calls it two times each run
  {
	switch(id)	
	{
		case 1:	//game(1) is called before the Applet sleeps
		{
			if (spr.getPosition().x > 220)
				spr.setDirection(Sprite.DIR_WEST); //change direction
			if (spr.getPosition().x < -40)
				spr.setDirection(Sprite.DIR_EAST);
			if (spr2.getPosition().x > 220)
				spr2.setDirection(Sprite.DIR_WEST);
			if (spr2.getPosition().x < -40)
				spr2.setDirection(Sprite.DIR_EAST);
			if (spr.rectCollision(spr2))	//if spr collides with spr2
				spr3.runAnim();		//show the explosion
			spr3.moveTo(spr2.getPosition());	//the explosion is alway on the same position
							//like the second sprite.
			repaint(); // It's necessary to add repaint(); somewhere or else you won't see anything.
		                break;
		}
		
		case 2:	//game(2) is called after the thread had slept.
		{
			//we won't do anything here
			break;
		}
	}
  }

  public void loadExplosion()
  {
	expl = new Image[12]; //create an array of 12 images
	//load all images:
	expl[0] = getImage(getCodeBase(), "ex1.gif");
	expl[1] = getImage(getCodeBase(), "ex2.gif");
	expl[2] = getImage(getCodeBase(), "ex3.gif");
	expl[3] = getImage(getCodeBase(), "ex4.gif");
	expl[4] = getImage(getCodeBase(), "ex5.gif");
	expl[5] = getImage(getCodeBase(), "ex6.gif");
	expl[6] = getImage(getCodeBase(), "ex7.gif");
	expl[7] = getImage(getCodeBase(), "ex8.gif");
	expl[8] = getImage(getCodeBase(), "ex9.gif");
	expl[9] = getImage(getCodeBase(), "ex10.gif");
	expl[10] = getImage(getCodeBase(), "ex11.gif");
	expl[11] = getImage(getCodeBase(), "ex12.gif");
	//create the sprite:
	spr3 = new Sprite(this, expl, 100, 100, 0, 0, 1, true, false);
		//the constructor for an array of images, explaind in the
		//tutorial and the Sprite.java file
   }
}

⌨️ 快捷键说明

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