I am making a game for two players, where there are two rows of objects scrolling along the screen. When any of the player see two similar objects they press the enter key.
So the player to press the key first and has the right response increments a score and the game starts again.
I have two problems with this game:
1) How can I detect if the screen has similar objects because there is no collision they are just passing by.
2) How to make the images appear randomly each time.
The answer to this really depends on what you're doing. To a large degree, you'll need to just kind of figure out how you want to do it, and make it happen.
Having said that, here's a few thoughts that come to my mind. I'm assuming that you have different objects that are represented on the screen with images (Texture2Ds). I'd probably create a class for the objects, and if there's no difference between them, other than the image that you draw them with, that class might look something like this:
public class GameObject { public Vector2 Position { get; set; } public ObjectType Type { get; set; } // Maybe something else about how the object is moving? Velocity or something? }
I'd then create an enumeration of the available types. Something like this:
public enum ObjectType { Hamburger, Bucket, Torpedo, Jello } // These are the different kinds of objects that are being displayed. The class above is using this enumeration.
You'd also need to write code that will find the right Texture2D for a specific ObjectType. But having the game object store just an identifier for what it is using, rather than the actual texture to draw keeps good separation between the backend updating engine and the frontend drawing code, which is important and useful.
I'd then create a List<GameObject> to store all of the objects that you're keeping track of at any given point in time. As you generate objects, they'll get added to this list, and as objects leave the screen and they're removed.
So you asked the question about how to randomly generate these game objects. Well, you'll probably rely heavily on [[[random-number-generation | the Random class]] to pick random numbers for you. You'd use it to pick a type (random.Next(4) if you have four choices, etc.) and probably a random position. Just have it pick values for the x and y coordinate of the position that are a little bit off the screen. (I'm assuming you don't want objects to pop up in the middle of the screen, but drift onto the screen.) You may also need to randomly populate other properties of the object, like velocity or whatever else you want it to keep track of.
Your other question was about "collision detection". I'll admit, in this case, it is a bit of a misnomer. What people call collision detection should really just be called "intersection detection" instead. In the normal case, we'd be trying to detect whether two objects are intersecting with each other, which would denote a collision. In this case, all we really want is to know if the object we're drawing and the view area intersect or not. That's how you'd tell if the object is visible. You'll still be able to use collision detection code to do this, even though it isn't technically a collision. If you're simply using Texture2Ds, you can do basic rectangle collision detection with each object and a rectangle that represents the full screen.
Rectangle screenRectangle = new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight); foreach (GameObject gameObject in gameObjects) { Texture2D texture = LookupTexture(gameObject.Type); Rectangle objectRectangle = new Rectangle( gameObject.Position.X - texture.Width / 2, // x-coordinate, adjusted so the object is centered around the position gameObject.Position.Y - texture.Height /2, // y-coordinate, adjusted so the object is centered around the position texture.Width, texture.Height); if (screenRectangle.Intersects(objectRectangle)) { // If we get in here, we know the object is on the screen. } }
Like I said, you're the one making the game, and to some degree, you're going to need to figure out what, exactly, you want. But this should give you some ideas about where to go.
Why don't You just keep track of what objects You have on screen? When create new object, add it to list, when object leaves screen (You will need to know its coordinates for drawing anyway), remove it from list. If any of players push the button, You run a check if there are similar objects in list.
class Object
{
int X;
int Y;
byte type; // lets say 10 different objects
// other stuff
}
Object[] object= new Object[20] //20= max number of objects in game at a time
byte[] list= new byte[10] // for each type- one cell
if (playerPushButton)
{
Array.Clear(list, 0, 10); // this is function to empty array {array name, start index, length to clear}
for(int x = 0; x < 20; x++) // for all objects check which type are they
{
if (object[x].type<>0) //if object has a type or You can use another variable in class ".exist"
{list[x]++} //Increase number of objects of that type
}
foreach (byte objectsNum in list) //for every type of objects
{
if (objectsNum>= 2) //if there are 2 or more objects of this type
{SomeoneWin} //Now You just find out which of players was pushing the button
}
}
void Update(void)
{
move all objects
check if their x/y is not bigger than screen width/height
if so, remove from list
google out some random number generator
if (random=true)
{make new object of random type}
}
