XNa practical keyboard-move an image based on the arrow key (3)

Source: Internet
Author: User

Next to xNa test article (II), we will change the program to the following (note "add 2" and "delete Add "):

 

Using system; using system. collections. generic; using Microsoft. xNa. framework; using Microsoft. xNa. framework. audio; using Microsoft. xNa. framework. content; using Microsoft. xNa. framework. gamerservices; using Microsoft. xNa. framework. graphics; using Microsoft. xNa. framework. input; using Microsoft. xNa. framework. media; using Microsoft. xNa. framework. net; using Microsoft. xNa. framework. storage; namespace myfirstgame {/ // <Summary> // This is the main type for your game // </Summary> public class game1: Microsoft. xNa. framework. game {graphicsdevicemanager graphics; spritebatch; // <summary> // defines a texture2d object (new) /// </Summary> texture2d texture; /// <summary> /// define the image coordinate (add 2) /// </Summary> vector2 picposition; /// <summary> // width of the game interface (add 2) /// </Summary> int clientw = 0; // <summary> // game Height (add 2) /// </Summary> int clienth = 0; // <summary> // store the buttons (add 2) /// </Summary> List <Keys> keyslist; Public game1 () {graphics = new graphicsdevicemanager (this); content. rootdirectory = "content"; keyslist = new list <Keys> (); // (add 2 )} /// <summary> // allows the game to perform any initialization it needs to before starting to run. /// this is where it can query for any required services Nd load any non-graphic // related content. calling base. initialize will enumerate through any components // and initialize them as well. /// </Summary> protected override void initialize () {// todo: add your initialization logic here base. initialize ();} // <summary> // loadcontent will be called once per game and is the place to load // all of your content. /// </Summary> protected overrid E void loadcontent () {// create a new spritebatch, which can be used to draw textures. spritebatch = new spritebatch (graphicsdevice); // Add // After the control Initialization is complete, graphics. graphicsdevic will not be null. // draw the image test.jpg in 2D format to the current graphics. graphicsdevice texture = texture2d. fromfile (graphics. graphicsdevice, "test.jpg"); int picw = texture. width; // get the width of the loaded image. Int pich = texture. height; // obtain the height of the loaded image. clientw = graphics. graphi Csdevice. viewport. width; // set the width of the game interface clienth = graphics. graphicsdevice. viewport. height; // set the height of the game interface int centerx = (clientw-picw)/2; // obtain the X coordinate center point of the game interface int centery = (clienth-Pich)/2; // obtain the Y coordinate center of the game interface, picposition = new vector2 (centerx, centery); // todo: use this. content to load your game content here} // <summary> // unloadcontent will be called once per game and is the place to unload // All Content. /// </Summary> protected override void unloadcontent () {// todo: unload any non contentmanager content here} // <summary> // allows the game to run logic such as updating the world, // checking for collisions, gathering input, and playing audio. /// </Summary> /// <Param name = "gametime"> provides a snapshot of timing values. </param> protected override void Update (gametime ){ // Allows the game to exit if (gamepad. getstate (playerindex. one ). buttons. back = buttonstate. pressed) This. exit (); // (add 2) Start keyboardstate = keyboard. getstate (); // obtain the key entered by the keyboard. The last key state is maintained: Keys [] keys = keyboardstate. getpressedkeys (); // foreach (Keys key in keys) {If (keyslist. count = 0) {// use the current keyboardhandle (key);} else {// use the stored button foreach (Keys K in keyslist) {If (Key = k) cont Inue; // repeat keyboardhandle (k) ;}} keyslist with the current button. clear (); foreach (Keys K in keys) keyslist. add (k); // (add 2) end // todo: add your update logic here base. update (gametime);} // <summary> // This is called when the game shocould draw itself. /// </Summary> /// <Param name = "gametime"> provides a snapshot of timing values. </param> protected override void draw (gametime) {graphicsdevice. clear (Color. cornflowerblue); // todo: add your drawing code here // Add // painting driver start spritebatch. begin (); // draws a texture-loaded image to its graphicsdevice, and defines the 2D coordinates of the image and the color of the image. // Spritebatch. draw (texture, new vector2 (0.0f, 0.0f), color. red); // (cancel adding) spritebatch. draw (texture, picposition, color. white); // (add 2) // end graphicsdevice painting spritebatch. end (); base. draw (gametime) ;}# region keyboard buttons (New 2) Private void keyboardhandle (Keys key) {Switch (key) {case keys. up: // moveup (); break; case keys. down: // movedown (); break; case keys. left: // moveleft (); break; case keys. right: // right-click moveright (); break ;}# endregion # region move the coordinates in the upper-left corner of the image (add 2) and increase the coordinates in Y to the right, add the following X coordinates: // <summary> // move the image up /// </Summary> private void moveup () {If (picposition. y> 0) // controls the mobile border picposition. y-= 5; // move up to 5 units} // <summary> // move down the image /// </Summary> private void movedown () {If (picposition. Y <clienth-texture. height) // controls the mobile border picposition. Y + = 5; // move 5 units down} // <summary> // move the image to the left /// </Summary> private void moveleft () {If (picposition. x> 0) // controls the mobile border picposition. x-= 5;} // <summary> // move the image to the right /// </Summary> private void moveright () {If (picposition. x <clientw-texture. width) // controls the mobile border picposition. X + = 5 ;}# endregion }}

 

Note the following important points:

Keyboardstate = keyboard. getstate (); // gets the key State entered by the keyboard.

Keys [] keys = keyboardstate. getpressedkeys (); // obtains the information about the current key. The last key is retained when no key is pressed on the keyboard.

 

 

Please refer to the notes. It is easier to use some functions. Haha

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.