• 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
ChristianFrantz

creating a bullet

5 posts in this topic

i have a method that holds a constructor for a bullet

[source lang="java"] class Bullet
{
int x;
int y;
int width;
int height;
int bxspeed;

public Bullet(int x, int y, int width, int height, int bxspeed)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}//end ball

public void paintBullet(Graphics g)
{
g.setColor(Color.blue);
g.fillRect(spaceShip.xpos, spaceShip.ypos, 20, 10);
}
} [/source]

and when the enter key is pressed, i want a bullet to be created at the middle of the spaceShip, but whenever i create one it starts at the top left corner of the spaceship
0

Share this post


Link to post
Share on other sites
I'd guess Line 20:
g.fillRect(spaceShip.xpos+[b]xOffset[/b], spaceShip.ypos+[b]yOffset[/b], 20, 10);

where the offsets are the x and y distances from the top left corner of the spaceship to the point where you actually want it to spawn (middle of spaceShip)
0

Share this post


Link to post
Share on other sites
I am a complete novice at game dev. My experience is all mainly ASP.NET in C# and some Java desktop app development. I have only ever made one game, it was recently and using Java. It is a space invader / Galaga analog. My approach was different than yours as a whole, but I thought this may help you, because I had the same issue. Here is my bullet class:
[source lang="java"]package game;

import java.awt.*;

import javax.swing.ImageIcon;

//class that creates and manages a bullet instance
public class Bullet
{
//instance variables
//x and y coordinates of a bullet instance
private int x, y;

//image that holds the image of the bullet
private Image img;

//variable that holds the visibility of a bullet
private boolean visible;

//constructor
public Bullet(int xS, int yS)
{
x = xS;
y = yS;

ImageIcon newBullet = new ImageIcon("src/images/bullet.png");
img = newBullet.getImage();
visible = true;

}//end of constructor

//method that moves a bullet
public void move()
{
y += -2;
if(y < 5)
{
visible = false;
}

}//end of move

//method that returns bounds of bullet instance(used for collision detection
public Rectangle getBounds()
{
return new Rectangle(getX(), getY(), 10, 30);

}//end of getBounds method

//method to return x position of a bullet
public int getX()
{
return x;
}//end of getX

//method to return y position of a bullet
public int getY()
{
return y;
}//end of getY

//returns the Image of a Bullet
public Image getImage()
{
return img;

}//end of getImage

//method to set visibility of a bullet
public void setVisible(boolean con)
{
visible = con;

}//end of setVisible

//method to return visibility of a bullet
public boolean getVisible()
{
return visible;

}//end of getVisible

}//end of class Bullet[/source]


Here is my fire method in my player class:
[source lang="java"]public void fire()
{
Bullet z = new Bullet(x + 72, y - 20);
bullets.add(z);

}//end of fire()
[/source]

then I drew the bullets in the actionPerformed handler:
[source lang="java"] //event that fires every time the swing timer ends(currently set to 5 millis)
public void actionPerformed(ActionEvent arg0)
{

if(!playerDead)
{
ArrayList bullets = p.getBullets();

for(int i = 0; i < bullets.size(); i++)
{
Bullet m = (Bullet) bullets.get(i);
graphics.drawImage(m.getImage(), m.getX(), m.getY(), this);
}
}

}[/source]

here is my player constructor:
[source lang="java"]public Player()
{
//sets visibility to true
visible = true;

//get the path for spaceship image
ImageIcon ic = new ImageIcon("src/images/spaceCraft.png");

//loads the image file into the image object
img = ic.getImage();

//starting position
x = 500;
y = 800;

//instantiate bullet ArrayList
bullets = new ArrayList();

}//end of constructor[/source]

When I call the fire method the 72 and (-20) are the offset so the bullet looks like it is coming out of the nose of the fighter image that is the player instance. I am not sure of your game type but hopefully this helps!
1

Share this post


Link to post
Share on other sites
Yeah, the [i]location[/i] of any image (or component body) on the screen is always(?) at its top left corner, not its center. Edited by DrMadolite
0

Share this post


Link to post
Share on other sites
Let me suggest something a little different than what webdevap... has.

You should not have the bullet list in the Player class. It should be a global list (or held in your Game class if you have one). That bullet list should be able to contain bullets fired by the player or by enemies.

Also, you might want to inherit from a Super class for any object that has physical properties (X, Y, Height, Width, Rotation, Image, and X and Y Velocities).

Something like this (in pseudo-code, only listing the members and some functions)
[code]
class PhysicalObject
{
float xPos;
float yPos;
float xVelocity;
float yVelocity;
float Angle;
int Height;
int Width;
Image ObjectImage; // or maybe you have an animation class, if it animates

void Update()
{
xPos += xVelocity;
yPos += yVelocity;
}

void Draw()
{
ScreenDraw(ObjectImage, xPos, yPos);
}
};

class Bullet : public PhysicalObject
{
int Damage; // how hard it hits
bool IsPlayerOwner; // determines if it's a player bullet or enemy
int TimeToLive; // how long the bullets stays alive
};
[/code]

Then, when ever anything fires a bullet, you create Bullet, add it to a list. In your Game Update, you loop through the BulletList, move the bullets, and check for collisions. If it collides, you apply the damage and remove it from the list.

Good Luck! Edited by BeerNutts
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