• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
JohnnyBoy53

How to code bullets in side scroller

4 posts in this topic

So i have an R-type like game where the player controls the ship with WASD and shoots with spacebar. I am kind of stuck on how to implement the bullets though. I want the bullet to have the starting point of the current position of the player spaceship. How would the bullet objects work? If spacebar is pressed, would it create a bullet object and be destroyed if it came in contact with an alien ship or is off the screen? How would each bullet object differ from the previous bullets?
[source lang="cpp"]struct pShip
{
float posX;
float posY;

pShip(float x=0,float y=400)
{
posX = x;
posY = y;
}

float playerMovementForward(void)
{
posX += .3;
return posX;
};

float playerMovementBack(void)
{
posX -=.3;
return posX;
};

float playerMovementUp(void)
{
posY -=.3;
return posY;
};

float playerMovementDown(void)
{
posY +=.3;
return posY;
};

};
//Create PlayerShip object
pShip player;

float bulletX = player.posX;
float bulletY = player.posY;
//Create bullet logic
struct Bullets
{
float posX;
float posY;

Bullets(float x = bulletX, float y = bulletY)
{
posX = x;
posY = y;
};
//Fire, return x position of bullet

};


//////////////////////////////////////////////////////////////////////////////////////////////////
// GAME LOGIC //


//Player Controls
if (Key_Down(DIK_D) && player.posX < SCREENW - 100) {
Sprite_Draw_Frame(playerShip,player.playerMovementForward(),player.posY,1,100,100,2);
}

if (Key_Down(DIK_A) && player.posX > 5) {
Sprite_Draw_Frame(playerShip,player.playerMovementBack(),player.posY,frame,100,100,2);
}

if (Key_Down(DIK_W) && player.posY > 10 ){
Sprite_Draw_Frame(playerShip,player.posX,player.playerMovementUp(),frame,100,100,2);
}

if (Key_Down(DIK_S) && player.posY < SCREENH - 50){
Sprite_Draw_Frame(playerShip,player.posX,player.playerMovementDown(),frame,100,100,2);
}
//Firing bullets
Bullets bullet1;

if (Key_Down(DIK_SPACE)){
Sprite_Draw_Frame(bullet,bullet1.posX,bullet1.posY,frame,100,100,2);
}
//////////////////////////////////////////////////////////////////////////////////////////////////

[/source]
I only posted the relevant structures and functions, but when i press space bar, the bullet just appears where the ORIGINAL starting of the spaceship was (before moving the player ship), and it does not move. I've been messing around with it all day but can not find the solution. Thanks in advance.
0

Share this post


Link to post
Share on other sites
Try implementing a bullet movement function and call it per-frame after user input and before collision detection such as:

[source lang="cpp"]float bulletMovementFoward(void)
{
posX += .5; //Something faster than ship movement
return posX;
}
[/source]

However I suggest looking into frame independent movement to give object a smoother feel to it.
1

Share this post


Link to post
Share on other sites
Yes I have already tried that but ended up removing it as it did not work. I am now able to have the bullet sprite start where the ship is. The only problem is that the bullet sprite does not accelerate forward (most likely logic errors with bullet position being initialized to player ship position). It just appears in front of the ship. I have updated it to this.
[source lang="cpp"]if (Key_Down(DIK_SPACE)){
bullet1.posX= player.posX;
bullet1.posY= player.posY; //initialize bullet position to the current position of the ship

Sprite_Draw_Frame(playerShip,bullet1.bulletMovementFoward(),bullet1.posY,frame,100,100,2);
}[/source]

Thank you for your input Neo, only problem i have now is making the bullet sprite accelerate forward. Edited by JohnnyBoy53
0

Share this post


Link to post
Share on other sites
It's because bullet1 is declared locally, resulting in it being in the same spot every time. (Well, that's what it looks like anyway)

I would create a list of bullets and add them as they're fired, update them every frame, and destroy them when no longer needed(ie collision)
1

Share this post


Link to post
Share on other sites
[font="Times New Roman"][size="3"][color="#000000"]I gave a short answer when you were looking for an explanation.

How do bullet objects work? Bullet, player, and enemy objects should all have the same framework of functionality. Have a current state(s), process input/AI based on state and create new states, move based on state, check collisions and change state if collided, render, and repeat.[/color][/size][/font]
[font="Times New Roman"][size="3"][color="#000000"]Some example of states an object should have is wait, move, shoot, hit, and explode. For instance, ‘D’ input from player creates a move_forward state. A bullet should have a reoccurring state of move_forward. An enemy gets its state from some kind of AI.

Then after all the initial states for all objects have been created, then move objects based on the state they have.[/color][/size][/font]
[font="Times New Roman"][size="3"][color="#000000"]Cycle through all the objects and check if they collided with each other or collided with the out of bounds for the screen. Objects could have bounding box or circle. The screen should have a bounding box larger than the screen itself. The type of collision should have a specific state for the object.[/color][/size][/font]
[font="Times New Roman"][size="3"][color="#000000"]Render all your objects.[/color][/size][/font]
[font="Times New Roman"][size="3"][color="#000000"]Repeat cycle.

“If spacebar is pressed, would it create a bullet object and be destroyed if it came in contact with an alien ship or is off the screen?” Pressing the spacebar should create a shoot state for the player. During the move phase is where I would suggest creating a new object for bullets.

How would each bullet object differ from the previous bullets? Nickem explained it pretty well however I would add that you should give sometime between each fire state say half second or several frames of animation. [/color][/size][/font]
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0