For instance, consider a brick breaker game where once the ball falls below the paddle, the user would get a displayed message (using SpriteFont) asking whether he would like to play again. If that condition was met,and the sprite font stated 'Play again, enter 'y' for yes, 'n' for no' how would one proceed to take that input in XNA?
Well, first you're gonna want to have a boolean to see if the game has ended or not. If you didn't have the boolean then the user could press Y or N anytime and the game would restart.
It should be simple to setup one anyways, just have it detect when the ball falls below the paddle and set the bool to true;
Then, the rest should be quite simple to figure out.
if (gameRestart)
{
if (Keyboard.GetState().IsKeyDown(Keys.Y))
{
// code for restart goes here
}
else if (Keyboard.GetState().IsKeyDown(Keys.N))
{
// do nothing, I guess?
}
}
Here is a tutorial on detecting key presses in XNA: http://msdn.microsoft.com/en-us/library/bb203902.aspx
Related
I've been working on some game project for college work. My problem is that I have list of enemies that is moving left-right and when touching left or right edge goes down a bit, and what i want them to do is to shot bullets from random enemy while game lasts,but I have managed for bullet to show and shot from random enemy but that is it :'D, it just goes that one time and it needs to go while game lasts. So here is my method for shooting to me it looks all right and cant figure out why is just one shot.
I'm beginner so sorry if my code looks stupid.
private int MoveEnemyBullet()
{
int randomnum = r.Next(30);
shooter = enemies[randomnum];
bullet.Goto_Sprite(shooter);
bulletEnemy.Add(bullet);
bullet.SetVisible(true);
bullet.Active = true;
while (START)
{
if(bulletEnemy.Count!=0)
bullet.Y += bullet.speed;
Wait(0.01);
if (bullet.TouchingEdge())
{
bullet.SetVisible(false);
}
}
Game.StartScript(MoveEnemyBullet);
return 0;
}
Your code is hard to read since there is only a little of it. What is bullet, bulletEnemy or START? I would ask why you don't have a list of bullets also, but i think that you want to only have one bullet at a time on the screen.
There might be a problem with TouchingEdge() method. Something similar happened to me, the bullet was not destroyed when it whent out of the bounds of the screen, only when it hit its target, so the bullet was still "alive" but outside the screen, still moving. If this happened to you, the bullet was never set to invisible.
Also, it does not seem to me that you change the value of START, which would mean that your while loop never ends. If you change it to false in the SetVisible(false) method, then this is not the problem.
I believe Goto_Sprite(shooter) changes the position of the bullet to the new shooter, but you should check if that one is correct too.
I am attempting to make a simple third person camera rotate around my character. I want the X key to rotate right and the Z key to rotate left. I have this working. My issue is, when I have one of the keys pressed and held (say X), then press any other key, Input.GetKey(KeyCode.X) stops returning true. So for the following simple example, it would print the message until another key is pressed, then will not again until I re-press the key.
void LateUpdate()
{
if(Input.GetKey(KeyCode.X))
{
print("X is down");
}
}
This prevents me from being able to rotate the camera while moving my character, for as soon as I attempt to change directions via an arrow key, it renders my if statement false. Is there another method I should be using?
Edit - This is not an issue with Unity or my code. I was using Teamviewer, which must handle the inputs differently
This works for me. Could it be you are using else if while checking if the button is pressed?
if (Input.GetKey(KeyCode.X))
{
print("X is down");
}
if (Input.GetKey(KeyCode.Y))
{
print("Y is down");
}
It also works for me if i have only 1 if. Even if i press any other key "X is down" is printed.
This could be a hardware issue. Different quality levels of keyboards have different behavior. For example, I once had a keyboard where if I pressed more than three keys at the same time, it would not register when they were released, and my character would be stuck running or spinning or whatever until I pressed and released the key again. Expensive "gaming" keyboards should handle multiple simultaneous keypresses better than others. YMMV.
It ended up being due to remote access. I was using Teamviewer to remote into the laptop that was running Unity. Once I used the laptop directly it worked as expected.
Recently I am trying to make basic games in Console-Application, like to move the cursor with the arrows (maybe as a very basic start of snake and games that you move something all over the Console screen), and I wanted to ask if it's possible to make that the cursor won't stop to move, and just change the direction when I press on some arrow? Is it something with events?
Thanks.
I would suggest something like the following:
bool isRight, isUp; //Used to determine direction
while (gamePlaying)
{
if(Console.KeyAvailable)
{
Console.ReadKey(); //Do your thing in here
}
}
So you check if a key is being pressed, and if it is you read it.
Also, bear in mind if you're making a console game you'll want to double-buffer your screen and use the Win32 API to minimise write calls if possible or your screen with flicker horribly.
I do not believe there is anyway to control the cursor position of the console window. You can read or write text, but that's about it. So if you want to make a console game, it will need to be text-based. You may be able to make an game ASCII art based where new text is printed to the console fast enough that it appears to be animated, but I don't think the console can go fast enough to make this work well.
I am making a game using the Monogame engine and I just need help on one part of my code. Now, every time the character dies in the game, the code just ends. this is because of these lines of code.
else
{
System.Environment.Exit(0);
}
if(y < 0)
{
System.Environment.Exit(0);
}
Now I want the game to have a 'game over' screen every time a user collides with one of the obstacles and this screen will allow you to go to the menu and allow users to play again. the classes I have at the minute is the game1.cs class, the character class and the obstacle class. Thanks for all your help.
You're going to want to keep track of the game state. As an example -- you might only have two states "Playing" and "GameOver". Instead of exiting in your code snippets, set the game state from "Playing" to "GameOver".
Now in the Update call -- you'll want to check for user input during GameOver and allow them to exit the game completely or start a new game.
In the Draw call -- you'll want to check the game state to decide what to render to the screen.
I made a game where the player controls a square, whenever it hits a wall it is supposed to die. The Square is a Picture Box and the walls are picture boxes too. You can move using W,A,S and D. I was thinking about doing something similar to:
if(Square.Top == Square.Top + Square.Height)
and then restart the game. Is there any better way doing it? Istead of having alot of IFs? Whenever a control touches another to do something?
Thanks alot !
Yes, you get the control's containing rectangle by Control.Bounds, and then use IntersectsWith with another rectangle.
if(Square.Bounds.IntersectsWith(Wall.Bounds))
{
// ...
}
Keep in mind that it won't trigger when your square touches a wall, only when it starts going on top of it, but I assume that's what you want.