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

📄 eventex2.java

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

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

public class EventEx2 extends GameApplet
{

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

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

  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
    spr = new Sprite(this, sprImg, 100, 100, 1, true);	//create sprite
    loadExplosion();	//this will load an array of images

    gfx = new Gfx(this, new Color(200, 200, 0));

    spr2.setAnimOnlyOnce(true);	//this will make the sprite invisible
				//after one animation.	
				//to restart an animation use: runAnim();
  }

  public void paint(Graphics g)
  {
	      gfx.cls();		//clear the screen
	      gfx.drawSprite(spr);	//draw the first Sprite
	      gfx.drawSprite(spr2);
	      gfx.drawString("LastKeyCode: " + lastkey,10,10);
 	      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
		{
			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.
		{
			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:
	spr2 = 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
   }
   public boolean mouseMove(Event e, int x, int y)
  {
	spr.moveTo(x-spr.getWidth()/2, y-spr.getHeight()/2);
	return true;
   }
  
  public boolean mouseDown(Event e, int x, int y)
  {
	spr2.moveTo(x-spr2.getWidth()/2, y-spr2.getHeight()/2);
	spr2.runAnim();
	return true;
   }
	
  public boolean keyDown(Event e, int key)
  {
	switch(key)
	{
		case Event.LEFT:
			spr.moveBy(-2,0);
			break;
		case Event.RIGHT:
			spr.moveBy(2,0);
			break;
		case Event.UP:
			spr.moveBy(0,-2);
			break;
		case Event.DOWN:
			spr.moveBy(0,2);
			break;
	}
	lastkey = key;
	return true;
  }
}

⌨️ 快捷键说明

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