Jump to content

  • Log In with Google      Sign In   
  • Create Account

Analizer

Member Since 09 Sep 2012
Offline Last Active Mar 27 2014 09:50 AM

Topics I've Started

animation and jump class XNA 4.0

10 December 2013 - 09:44 AM

Hello, I've been stuck on this problem for the past day, I can't seem to figure out how to combine two different classes together- animation and jump class. I'm getting two different sprites on the screen, one of the sprites has animation and is moving the other sprite has no animation but is jumping... and I don't know how to get past this stage to have the animation AND jump on one sprite. 

This is for my uni maths assignment and the game needs to have all 3 Newton Laws.

Here is the class code:

namespace GPT2PhysicsGame
{
    public class Player
    {
        Texture2D playerImage;
        Vector2 playerPosition, tempCurrentFrame;
        float moveSpeed = 220f;
        KeyboardState keyState; 
        Animation playerAnimation = new Animation();


        Texture2D texture;
        Vector2 position;
        Vector2 velocity;

        bool hasJumped;

        public void Character(Texture2D newTexture, Vector2 newPosition)
        {
            texture = newTexture;
            position = newPosition;
            hasJumped = true;
        }

        public void Initialize()
        {
            playerPosition = new Vector2(0, 500); // by setting the values the player starting position can be changed
            playerAnimation.Initialize(playerPosition, new Vector2(5, 4));
            tempCurrentFrame = Vector2.Zero;
        }

        public void LoadContent(ContentManager Content)
        {
            playerImage = Content.Load<Texture2D>("player");
            playerAnimation.AnimationImage = playerImage;
            texture = Content.Load<Texture2D>("jump");
        }

        public void Update(GameTime gameTime)
        {
            keyState = Keyboard.GetState();
            playerAnimation.Active = true;

            playerPosition = playerAnimation.Position;

            
            if (keyState.IsKeyDown(Keys.Right) || keyState.IsKeyDown(Keys.D))
            {
                playerPosition.X += moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
                tempCurrentFrame.Y = 1;
            }
            else if (keyState.IsKeyDown(Keys.Left) || keyState.IsKeyDown(Keys.A))
            {
                playerPosition.X -= moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
                tempCurrentFrame.Y = 0;
            }

            else
                playerAnimation.Active = false;

            int rightEdge = 725;
            if (playerPosition.X > rightEdge)
                playerPosition = new Vector2(rightEdge, playerPosition.Y);

            if (playerPosition.X < 0)
                playerPosition = new Vector2(0, playerPosition.Y);
            
            position += velocity;

            if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                playerPosition.X += moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
                tempCurrentFrame.Y = 1;
                velocity.X = 3f;

            }

            else if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                playerPosition.X -= moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
                tempCurrentFrame.Y = 0;
                velocity.X = -3f;
            }
            else velocity.X = 0f;

            if (Keyboard.GetState().IsKeyDown(Keys.Space) && hasJumped == false)
            {
                position.Y -= 10f;
                velocity.Y = -5f;
                hasJumped = true;
            }

            if (hasJumped == true)
            {
                float i = 1;
                velocity.Y += 0.15f * i;
            }

            if (position.Y + texture.Height >= 450)
                hasJumped = false;

            if (hasJumped == false)
                velocity.Y = 0f;

            tempCurrentFrame.X = playerAnimation.CurrentFrame.X;
            playerAnimation.Position = playerPosition;
            playerAnimation.CurrentFrame = tempCurrentFrame;
            playerAnimation.Update(gameTime);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            playerAnimation.Draw(spriteBatch);
            spriteBatch.Draw(texture, position, Color.White);
        }
    }
}

the code is there and I think its only matter of organising it.

 

Any help please?


making a bitmap follow a character

23 April 2013 - 10:39 AM

Hi.
 
I have created a function for an enemy and I'm trying to make the bitmap follow my character and when they interact the character dies and the game ends.
 
Can someone give me heads up?
 
Here is the class code:
 
#ifndef _ENEMY_H_
#define _ENEMY_H_

#include <allegro.h>

class Enemy
{
public:
int x;
int y;
BITMAP *enemy;
Enemy();
~Enemy();
void blitenemy(BITMAP*buffer);
};

#endif

 

 

 
and here is the cpp code:
 
#include <allegro.h>
#include "Enemy.h"

Enemy::Enemy()
{
srand(time(NULL));
x = rand() % 400 + 1;
y = rand() % 300 + 1;
enemy = load_bitmap("enemy.bmp",NULL); 
}

Enemy::~Enemy()
{
destroy_bitmap(enemy);
}

void Enemy::blitenemy(BITMAP*buffer)
{
masked_blit(enemy,buffer,0,0,x,y,800,600);
}

 

 


allegro 4 object interaction + object collision

16 April 2013 - 08:47 AM

Hello. 
 
I have been doing allegro programming since January this year and I'm trying to set-up an object interaction but I'm not sure where to start.
 
I'm doing a small adventure game and I want a character to pick up stuff from the ground for the game to end. I was looking for solution online but can't find anything that would help me out. 
 
I've got everything done (animation, background etc) apart from the objects and collision which I need help with.
 
How can I set-up object collision? What I mean is when my character will walk into a wall in the middle of the screen it would stop him and if there is a doorway he would go through.
 
Also when the character walks into an object for example a gold coin or a stick how can I make it disappear from the screen when he interacts with it?
 
Thanks, Adrian.
 
EDIT: here is my code; (I also have a camera class header file but it will not be needed) - void moveCharacter(void) is where the collision should happen I guess.
 
#include <allegro.h> //allegro library
#include "camera.h" //including camera class

#define BACK 1
#define FRONT 2
#define LEFT 3
#define RIGHT 4
/* defining character movements which will be used for
animation in void characterAnimation later on*/

volatile long speed_counter = 0; // A long integer which will store the value of the timer

void increment_speed_counter();

void moveCharacter(void); //character movement
void characterAnimation(void);
void collisionSide(void); //colision up-down
void collisionUp(void); //collision left-right
void change_frame(void);

int character_y; //character movement left-right integer
int character_x;//character movement up-down integer
int direction;
int resolution; 
int frame;
int movement;
int character_offset;
int character_width;

camera GameCamera; //declaring the camera class as a global veriable

BITMAP *character_right;
BITMAP *character_left;
BITMAP *character_front;
BITMAP *character_back;
BITMAP *tree;
BITMAP *background; //declaring background bitmap
BITMAP *buffer; //pointer to the buffer

int main(void) // main program starts
{
allegro_init(); //initialising allegro
    install_keyboard(); //installing keyboard
install_timer(); //timer so the game will run at the same speed on all PCs
LOCK_VARIABLE(speed_counter); // Used to set the timer which regulates the game speed
LOCK_FUNCTION(increment_speed_counter); //speed counter
set_color_depth(32); // colour depth

    resolution=set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0); //setting game window resolution
if(0 != resolution)
{
allegro_message("Graphics error. Check dekstop resolution and try again.");
return 0;
}

GameCamera.Init(); // initializing the camera for screen changing

tree = load_bitmap("tree.bmp",NULL);
if(!tree)
{
allegro_message("Error! No tree.bmp in this folder..."); //if no character.bmp in game folder showing error message
return 0;
}

character_right = load_bitmap("character_right.bmp",NULL);// ********************************************************
if(!character_right)
{
allegro_message("Error! No character_right.bmp in this folder..."); //if no character.bmp in game folder showing error message
return 0;
}

character_left = load_bitmap("character_left.bmp",NULL);// ********************************************************
if(!character_left)
{
allegro_message("Error! No character_left.bmp in this folder..."); //if no character.bmp in game folder showing error message
return 0;
}

character_back = load_bitmap("character_back.bmp",NULL);// ********************************************************
if(!character_back)
{
allegro_message("Error! No character_up.bmp in this folder..."); //if no character.bmp in game folder showing error message
return 0;
}

character_front = load_bitmap("character_front.bmp",NULL);// ********************************************************
if(!character_front)
{
allegro_message("Error! No character_front.bmp in this folder..."); //if no character.bmp in game folder showing error message
return 0;
}

background = load_bitmap("background.bmp",NULL);// ********************************************************
if(!background)
{
allegro_message("Error! No background.bmp in this folder..."); //if no background.bmp in game folder showing error message
return 0;
}

character_x = 93;
character_y = SCREEN_H /2;
buffer = create_bitmap(SCREEN_W, SCREEN_H);
install_int(moveCharacter,6);
install_int(change_frame,100);

frame =0;
character_offset =0;
character_width =60; //width of the character bitmap. No images exceed 60 pixels
direction = FRONT;
movement = 0;

    while(!key[KEY_ESC])
    {
blit(background,buffer,GameCamera.getcameraX(),GameCamera.getcameraY(),0,0,SCREEN_W,SCREEN_H); //drawing the background to the screen

switch(direction) //animation function for character movement
{
case RIGHT: 
switch(frame)
{
case 0:
character_offset= 0;
character_width= 50;
break;

case 1:
character_offset= 51;
character_width= 74;
break;

case 2:
character_offset= 125;
character_width= 65;
break;

case 3:
character_offset= 190;
character_width= 58;
break;
}

if(movement==0)
{
character_offset= 0;
character_width= 50;
}

masked_blit(character_right,buffer,character_offset,0,character_x,character_y,character_width,104);
break;

case LEFT: 
switch(frame)
{
case 0:
character_offset= 0;
character_width= 61;
break;

case 1:
character_offset= 62;
character_width= 72;
break;

case 2:
character_offset= 134;
character_width= 58;
break;

case 3:
character_offset= 192;
character_width= 63;
break;
}

if(movement==0)
{
character_offset= 0;
character_width= 61;
}
masked_blit(character_left,buffer,character_offset,0,character_x,character_y,character_width,104);
break;

case FRONT: 
switch(frame)
{
case 0:
character_offset= 0;
character_width= 59;
break;

case 1:
character_offset= 60;
character_width= 66;
break;

case 2:
character_offset= 126;
character_width= 67;
break;

case 3:
character_offset= 193;
character_width= 64;
break;
}

if(movement==0)
{
character_offset= 0;
character_width= 59;
}
masked_blit(character_front,buffer,character_offset,0,character_x,character_y,character_width,104);
break;

case BACK: 
switch(frame)
{
case 0:
character_offset= 0;
character_width= 59;
break;

case 1:
character_offset= 60;
character_width= 62;
break;

case 2:
character_offset= 122;
character_width= 66;
break;

case 3:
character_offset= 188;
character_width= 57;
break;
}

if(movement==0)
{
character_offset= 0;
character_width= 59;
}
masked_blit(character_back,buffer,character_offset,0,character_x,character_y,character_width,104);
break;
}

blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
clear_bitmap(buffer); //clearing the bitmap
    }
    return 0;

destroy_bitmap(character_right);
destroy_bitmap(character_left);
destroy_bitmap(character_back);
destroy_bitmap(character_front);
destroy_bitmap(buffer);
destroy_bitmap(screen); 
// destroying all bitmaps allows the computer to clear it from the system memory
}
END_OF_MAIN()

void moveCharacter(void) //character movement function
{
int tempx;
int tempy;
     movement=0;

     if(key[KEY_LEFT])
     {
         movement = 1;
         direction = LEFT;
         character_x-=1;
         tempx=GameCamera.getcameraX();

         if((character_x < 0) && (tempx == 0 ))
         {
             character_x = 0;
             GameCamera.setcameraX(0);
         }

         if((character_x < 0) && (tempx == 800 ))
         {
             character_x = 800;
             GameCamera.setcameraX(0);
         }
         if((character_x < 0) && (tempx == 1600 ))
         {
             character_x = 800;
             GameCamera.setcameraX(800);
         }
         if((character_x < 0) && (tempx == 2400 ))
         {
             character_x = 800;
             GameCamera.setcameraX(1600);
         }
     }

     if(key[KEY_RIGHT])
     {
         movement =1;
         direction = RIGHT;
         character_x+=1;
         tempx=GameCamera.getcameraX();

if (character_x > 0 && (character_x == 600)); 

         if((character_x > 800) && (tempx == 0 ))
         {
             character_x = 0;
             GameCamera.setcameraX(800);
         }
         if((character_x > 800) && (tempx == 800))
         {
             character_x = 0;
             GameCamera.setcameraX(1600);
         }
         if((character_x > 800) && (tempx == 1600 ))
         {
             character_x = 0;
             GameCamera.setcameraX(2400);
         }
         if((character_x > 800 ) && (tempx == 2400) )
         {
             character_x = 800;
             GameCamera.setcameraX(2400);
         }
     }

     if(key[KEY_UP])
     {
         movement = 1;
         direction = BACK;
         character_y-=1;
         tempy=GameCamera.getcameraY();

         if((character_y < 0) && (tempy == 0 ))
         {
             character_y = 0;
             GameCamera.setcameraY(0);
         }
         if((character_y < 0) && (tempy == 600 ))
         {
             character_y = 600;
             GameCamera.setcameraY(0);
         }
         if((character_y < 0) && (tempy == 1200 ))
         {
             character_y = 600;
             GameCamera.setcameraY(600);
         }
         if((character_y < 0) && (tempy == 1800 ))
         {
             character_y = 600;
             GameCamera.setcameraY(1200);
         }
     }

     if(key[KEY_DOWN])
     {
         movement =1;
         direction = FRONT;
         character_y +=1;
         tempy=GameCamera.getcameraY();

         if((character_y > 600) && (tempy == 0 ))
         {
             character_y = 0;
             GameCamera.setcameraY(600);
         }

         if((character_y > 600) && (tempy == 600 ))
         {
             character_y = 0;
             GameCamera.setcameraY(1200);
         }
         if((character_y > 600) && (tempy == 1200 ))
         {
             character_y = 0;
             GameCamera.setcameraY(1800);
         }
         if((character_y > 600 ) && (tempy == 1800 ))
         {
             character_y = 600;
             GameCamera.setcameraY(1800);
         }
}
}
void change_frame() // A function to increment the speed counter
{
  frame++; // increment the speed counter by one.
  if(frame > 3)
 frame=0;
}
END_OF_FUNCTION(change_frame); </code>

 

 


PARTNERS