2d Player Movement by Jcamden17th Oct 2011 23:38
|
---|
Summary 2d Movement using keyboard input Description Hey all, This code will move your character around a 2d screen using the keyboard. There are 2 functions. MovePlayer(p) and MovePlayerWithAngle(p). Both functions do the same thing. The Angle one will target the mouse and rotate around it based off user input. There are loads of ways to do this but I want to make it easy to understand. Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com // Movement.h // // This header file is for basic 2d movement using a sprite // #define NONE (-1) #define PLAYER_ID (1) #define RIGHT_ANGLE (270) #define LEFT_ANGLE (90) #define RESET_ANGLE (0) #define PLAYER_SPEED (2) // number of pixels to move the player typedef struct Player_ { WORD pid; // Player id to move WORD x; // X coord WORD y; // y coord float angle; // Angle for mouse movement float vel; // Velocity to move player }Player; // Basic keyboard movement void InitializePlayer(Player *p, WORD id, WORD x, WORD y, float vel) // Initializes player { p->pid = id; p->x = x; p->y = y; p->vel = vel; dbSprite(p->pid,p->x,p->y,p->pid); dbOffsetSprite(p->pid, (dbSpriteWidth(p->pid) / 2), (dbSpriteHeight(p->pid) / 2)); } void FreePlayerMemory(Player *p) // deletes player sprites and allocated memory { free(p); dbDeleteSprite(p->pid); } void MovePlayer(Player *p) // Moves player using up,down,left, and right keys { if(dbUpKey() == 1) { p->y-=PLAYER_SPEED; dbSprite(p->pid, p->x, p->y, p->pid); } if(dbDownKey() == 1) { p->y+=PLAYER_SPEED; dbSprite(p->pid, p->x, p->y, p->pid); } if(dbLeftKey() == 1) { p->x-=PLAYER_SPEED; dbRotateSprite(p->pid, LEFT_ANGLE); dbSprite(p->pid, p->x, p->y, p->pid); dbRotateSprite(p->pid, RESET_ANGLE); } if(dbRightKey() == 1) { p->x+=PLAYER_SPEED; dbRotateSprite(p->pid, RIGHT_ANGLE); dbSprite(p->pid, p->x, p->y, p->pid); dbRotateSprite(p->pid, RESET_ANGLE); } } void MovePlayerWithAngle(Player *p) // Moves player using keys however aims at the mouse { int MouseX = dbMouseX(); int MouseY = dbMouseY(); float angle = dbAtanFull(MouseX - dbSpriteX(p->pid), dbSpriteY(p->pid) - MouseY); dbRotateSprite(p->pid, angle); if(dbUpKey() == 1) { p->y-=PLAYER_SPEED; dbSprite(p->pid, p->x, p->y, p->pid); } if(dbDownKey() == 1) { p->y+=PLAYER_SPEED; dbSprite(p->pid, p->x, p->y, p->pid); } if(dbLeftKey() == 1) { p->x-=PLAYER_SPEED; dbSprite(p->pid, p->x, p->y, p->pid); } if(dbRightKey() == 1) { p->x+=PLAYER_SPEED; dbSprite(p->pid, p->x, p->y, p->pid); } } // Main.cpp // #include "DarkGDK.h" #include "Movement.h" void DarkGDK ( void ) { dbSyncOn(); dbSyncRate(60); dbDisableEscapeKey(); dbSetImageColorKey( 255, 0, 255 ); // Allocate memory for our Player structure Player *p = (Player*) malloc(sizeof(Player)); // Load the image dbLoadImage("Player.bmp", PLAYER_ID); // Initialize the player on the screen InitializePlayer(p,PLAYER_ID,100,100,PLAYER_SPEED); while ( LoopGDK ( ) ) { // MovePlayer(p); // Moves player using keys MovePlayerWithAngle(p); // Moves player using keys, rotates player with the mouse if ( dbEscapeKey ( ) ) break; dbSync(); } // Make sure we delete all images and free memory here FreePlayerMemory(p); return; } |