Game development input control class test code

zhaozj2021-02-16  182

/ *

The INPUTMANAGER class test program, its function is to move the image, beat

* /

Import java.awt. *;

Import java.awt.event.keyevent;

Import java.swing.Imageicon;

Import com.graphics. *;

Import com.Input. *;

IMPORT COM.TEST. *;

Public Class InputManagerTest Extends Gamecore_ {

Public static void main (string args []) {

New INPUTMANAGERTEST (). Run ():

}

Protected GameAction JUMP;

Protected GameAction exit;

Protected GameAction MoveLeft;

Protected GameAction Moveright;

Protected GameAction Pause;

Protected InputManager InputManager;

Private Player Player;

PRIVATE BGIMAGE;

Private boolean pause;

Public void run () {

Super.init ();

Window window = screen.getfullscreenwindow ();

InputManager = New InputManager (Window);

CreateGameAction ();

CreateSprite ();

Paused = false;

}

Public void creategameAction () {

Jump = new gameAction ("jump", gameAction.dtect_initial_press_only);

EXIT = New gameAction ("exit", gameAction.dtect_inital_press_only);

MoveLeft = New GameAction ("Moveleft");

Moveright = New GameAction ("Moveright");

Pause = new gameAction ("pause", gameAction.Detect_inital_press_only);

INPUTMANAGER.MAPTOKEY (EXIT, Keyevent.vk_escape); // Mapping Exit Program

INPUTMANAGER.MAPTOKEY (PAUSE, Keyevent.vk_p); // Mapping Suspend program

// Use the space bar to make animation beat

INPUTMANAGER.MAPTOKEY (Jump, VK_SPACE);

// moving animation with arrows

InputManager.maptoKey (Moveleft, Keyevent.vk_left);

InputManager.maptoKey (Moveright, Keyevent.vk_right);

}

/ *

Load image, generate an animation Player ghost

* /

Public void createSprite () {

BGIMAGE = loading ("../ images / background.jpg");

Image Player1 = loading ("../ iamges / player1.png");

Image Player2 = loading ("../ iamges / player2.png");

// Generate an animation

Aniamtion Anim = New Animaiton ();

Anim.Addframe (Player1,200); Anim.AddFrame (Player2,300);

Player = New Player (Anim);

Player.SetFloor (Screen.getHEight () - Player.getHeight ());

}

/ **

No matter whether the game is temporarily done, check the input of the GameAction that may be pressed.

* /

Public void checksysteminput () {

IF (pause.ispressed ()) {

Pause.setpaused (! ispause ());

}

IF (exit.ispressed ()) {

STOP ();

}

}

/ *

The game is not paused, check the input that may be pressed

* /

Public void checkgameInput () {

Float velocityx = 0;

IF (moveleft.ispressed ()) {

VelocityX- = Player.Speed;

}

IF (Moveright.Ispressed ()) {

VelocityX = Player.Speed;

}

Player.SetVelocityX (VelocityX);

IF (jump.ispress () && player.getState ()! = Player.State_jumping)

{

Player.jump ();

}

}

/ *

Check if the game is suspended

* /

Public boolean ispaused () {

Return paused;

}

/ *

Set the game as paused state

* /

Public void setpaused (boolean p) {

IF (! paused == p) {

Paused = P;

INPUTMANAGER.RESETALLGAMEACTIONS ();

}

// Update Animation

Public void update (long elapsedtime) [

/ / Regardless of whether the game is suspended to check the input possible input

ChecksystemInput ();

IF (! ispaused ()) {

CheckgameInpu ();

Player.Update (ELAPSEDTIME);

}

}

/ *

Draw screen

* /

Public Void Draw (Graphics2D G) [

// painted background

GDRAWIMAGE (BGImage, 0, 0, NULL);

// painting a ghost

g.drawimage (Player.getiMage (), Math.Round (Player.Getx ()), Math.Round (Player.Gety ()), NULL);

}

}

转载请注明原文地址:https://www.9cbs.com/read-8847.html

New Post(0)