I'm having a bug with my snake game - c#

I'm making a Snake game on C#, but I'm having a glitch with my keys.
I'm following the exact code found here. http://codesmesh.com/snake-game-in-c/
The error that I get is that... for example if I was going down, if I pressed right and up at the EXACT same time, where i basically press it together, the snake goes up and walks into its own body, killing itself.
This is my keydown section in my code.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
//Space is just to restart the form.
if (e.KeyData == Keys.Space)
{
timer1.Enabled = true;
codesmeshlabel.Text= "";
spaceBarLabel.Text = "";
down = false;
up = false;
left = false;
right = true;
}
if (e.KeyData == Keys.Down && up == false)
{
down = true;
up = false;
right = false;
left = false;
}
if (e.KeyData == Keys.Up && down == false)
{
down = false;
up = true;
right = false;
left = false;
}
if (e.KeyData == Keys.Left && right == false)
{
down = false;
up = false;
right = false;
left = true;
}
if (e.KeyData == Keys.Right && left == false)
{
down = false;
up = false;
right = true;
left = false;
}
}

This sounds like you are changing the direction your snake is traveling to "right", and then setting it to "up" before the snake moves again.
Somewhere in your code I assume you have a game loop that updates the screen every (however long). To prevent this from happening you would need to change your design so that after your game has accepted a direction change (key.left/right/up/down) it waits until AFTER the screen has been updated once to accept a second direction change. This should fix your bug :)
As m.t.bennett pointed out another option would be to check each keypress to see if it is the opposite direction of current travel, and if so ignore it. Could be more elegant.

Related

How to make my character play after clicking a button, then bots auto play?

I am creating this simple 3D game but I'm having an issue with player vs computer mode. I want to be able to play vs 1 to 3 bots and when it's my turn I want the game to wait for me to click, but when it's the bots turn I want to them to auto play. What really happens with my code is that everything goes to auto play. I know the issue is with Update function because Update is called once per frame but I don't know how to fix it. Can you help me? The idea is to use a tracker/counter like i = 0 first. When it's 0 it means my character will play so it should wait for my mouse to be pressed. When it's not 0 it will be bots turn. This is the section of the code. Function veprimetELojtareve is the same as the code inside if(!bota).
private int i = 0;
// Update is called once per frame
void Update()
{
if (!bota)
{
if (Input.GetMouseButtonDown(0)) // Left Click
{
if (hasLanded)
Reset();
RollDice();
}
if (rb.IsSleeping() && !hasLanded && thrown)
{
hasLanded = true;
rb.useGravity = false;
rb.isKinematic = true;
SideValueCheck();
}
else if (rb.IsSleeping() && hasLanded && diceValue == 0)
RollAgain();
}
else
{
if (i == 0)
veprimetELojtareve();
else
{
if (hasLanded)
Reset();
RollDice();
if (rb.IsSleeping() && !hasLanded && thrown)
{
hasLanded = true;
rb.useGravity = false;
rb.isKinematic = true;
SideValueCheck();
}
else if (rb.IsSleeping() && hasLanded && diceValue == 0)
RollAgain();
}
i = (i + 1) % numriLojtareve;
Debug.Log("Vlera e i eshte: " + i);
}
}

Why am i not able to move the picture box if i have a checkbox in my form

I've started creating a game and i ran into a problem:
I cant place check boxes because it somehow stops the "players" movement.
I'm moving my "player" using arrow keys.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Left)
{
left = true;
}
if (e.KeyCode == Keys.Right)
{
right = true;
}
if (e.KeyCode == Keys.Up)
{
up = true;
}
if (e.KeyCode == Keys.Down)
{
down = true;
}
}
And to reset the controls:
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
left = false;
}
if (e.KeyCode == Keys.Right)
{
right = false;
}
if (e.KeyCode == Keys.Up)
{
up = false;
}
if (e.KeyCode == Keys.Down)
{
down = false;
}
}
So how can i place a check box and still control my "player"?
The game working without the check box:
https://i.imgur.com/oJmn2uD.gifv
Then i put the check box and not being able to move the "player":
https://i.imgur.com/53WOGjW.gifv
What are other ways to add a checkbox sort of control that i could use for settings tab that would work?
The answer was that i cant use arrows keys because they act as input keys so i replaced them with WASD and it works like a charm!

How can I check if the level is loaded in Unity3D

I've started learning C# and Unity3D this year and I've come across a problem. I'm loading a new level and the player object gets initialized before the the level has finished loading.
I think I only need to check whether the level is loaded or not before initializing the object. I don't know if I could use LoadLevelAsync. I'm using Unity3D Personal Edition.
My current code:
MasterClient.cs
void Update(){
if(SceneUpdateRequired)
{
Application.LoadLevel(SceneUpdateID);
if (Application.isLoadingLevel == false)
{
SceneUpdateRequired = false;
SceneUpdateCompleted = true;
}
}
}
void CharacterLoginUpdate(){
if (SceneUpdateCompleted == true)
{
GameObject networkPlayerObj = (GameObject)Instantiate(NPlayerObj, PlayerLoginUpdatePosition, PlayerLoginUpdateRotation);
NPlayer networkPlayer = networkPlayerObj.GetComponent<NPlayer>();
networkPlayer.UpdatePlayerInstantiateCompleted(PlayerLoginUpdateNetworkID, PlayerLoginUpdateHP, PlayerLoginUpdateClass, PlayerLoginUpdateLevel, PlayerLoginUpdateName, PlayerLoginUpdatePosition);
PlayerLoginUpdateRequired = false;
}
}
The problem with your code is your are loading level in update where if you dont control the task with booleans you are going to end up with big problems.
your update code should be as follows
void Update(){
if(SceneUpdateRequired)
{
Application.LoadLevel(SceneUpdateID);
SceneUpdateRequired = false;
}
if (Application.isLoadingLevel == false)
{
SceneUpdateRequired = false;
SceneUpdateCompleted = true;
}
}
This way the scene code will try to load level only when you request it to and not every time in update loop as loading levels is heavy.
Another thing is you might encounter one more problem if you need to use
SceneUpdateRequired,SceneUpdateCompleted variables somewhere else the
if (Application.isLoadingLevel == false)
{
SceneUpdateRequired = false;
SceneUpdateCompleted = true;
}
the above part which is in update loop will reset the variables everytime whenever its not loading the level. To prevent this you will have to introduce another boolean flag to stop its update happening everytime. So your code will end up looking like this if you want to prevent everyframe update
void Update() {
if(SceneUpdateRequired){
Application.LoadLevel(SceneUpdateID);
SceneUpdateRequired = false;
}
if (Application.isLoadingLevel == false && StartCheckingLoadLevel){
SceneUpdateRequired = false;
SceneUpdateCompleted = true;
StartCheckingLoadLevel = false;
}
}
void StartLoad() {
SceneUpdateRequired = true;
SceneUpdateCompleted = false;
StartCheckingLoadLevel = true;
}
void CharacterLoginUpdate(){
if (SceneUpdateCompleted == true) {
Debug.Log("Do Something after load");
} else {
Debug.Log("Scene not yet loaded");
}
}
Hope this helps.

C# pressing key breaks movement of an object

I'm creating ping pong game. I managed to move both lines (picture box) at same time if keys for movement are pressed down. Problem is that if control for one player is pressed down and then other player JUST clicks (1 time) it breaks movement of other player,so that he needs to press key again. I tried to fix it with keypress and Keyboard.IsKeyDownbut no luck.
Here is my code:
public void Form1_KeyDown(object sender, KeyEventArgs e)
{
Keys up1 = (Keys)Enum.Parse(typeof(Keys), form1.p5_text_kontrole_gor1.Text , true);
Keys down1 = (Keys)Enum.Parse(typeof(Keys), form1.p5_text_kontrole_dol1.Text , true);
Keys up2 = (Keys)Enum.Parse(typeof(Keys), form1.p5_text_kontrole_gor2.Text, true);
Keys down2 = (Keys)Enum.Parse(typeof(Keys), form1.p5_text_kontrole_dol2.Text, true);
if (e.KeyCode == Keys.Escape)
Application.Exit();
if(e.KeyCode == up1)
{
goup1 = true;
}
if (e.KeyCode == down1)
{
godown1 = true;
}
if (e.KeyCode == up2)
{
goup2 = true;
}
if (e.KeyCode == down2)
{
godown2 = true;
}
igra1();
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
Keys up1 = (Keys)Enum.Parse(typeof(Keys), form1.p5_text_kontrole_gor1.Text, true);
Keys down1 = (Keys)Enum.Parse(typeof(Keys), form1.p5_text_kontrole_dol1.Text, true);
Keys up2 = (Keys)Enum.Parse(typeof(Keys), form1.p5_text_kontrole_gor2.Text, true);
Keys down2 = (Keys)Enum.Parse(typeof(Keys), form1.p5_text_kontrole_dol2.Text, true);
if (e.KeyCode == up1)
{
goup1 = false;
}
if (e.KeyCode == down1)
{
godown1 = false;
}
if (e.KeyCode == up2)
{
goup2 = false;
}
if (e.KeyCode == down2)
{
godown2 = false;
}
igra1();
}
public void igra1()
{
if (goup1)
{
if (form1.p6_ploscek1.Top > form1.panel6_pongIgra.Top)
form1.p6_ploscek1.Top -= 15;
}
if (goup2)
{
if (form1.p6_ploscek2.Top > form1.panel6_pongIgra.Top)
form1.p6_ploscek2.Top -= 15;
}
if (godown1)
{
if (form1.p6_ploscek1.Bottom < form1.panel6_pongIgra.Bottom)
form1.p6_ploscek1.Top += 15;
}
if (godown2)
{
if (form1.p6_ploscek2.Bottom < form1.panel6_pongIgra.Bottom)
form1.p6_ploscek2.Top += 15;
}
}
I believe you're relying on the fact that keys get repeated by Windows while you hold them down to make your game pieces move. The first key being held down will stop repeating because the new key being held down is repeating instead.
To fix this move the pieces in the Tick() event of a Timer control. In the KeyDown/KeyUp events simply change the state of a variable that represents whether the associated piece should be moving (and what direction it should go). The Timer code will look at the state variables and act accordingly...

When I disable MouseLook in Unity, I can still look up and down

I made a pop up menu and when it comes up, I don't want to be able to move or look around. The movement part is all good, but when I disable MouseLook, I can still look up and down. How can I fix this? Also, I don't want to freeze the game time because I might add multiplayer later and I don't want it to freeze the game for other players. Any help will be appreciated.
Here's my code:
if(canOpen == true && isOpen == false && Input.GetKeyDown(KeyCode.E)) {
isOpen = true;
canClose = true;
player.GetComponent<FPSInputController>().enabled = false;
player.GetComponent<CharacterMotor>().enabled = false;
player.GetComponent<MouseLook>().enabled = false;
}
else if(isOpen == true && Input.GetKeyDown(KeyCode.E) && canClose == true) {
isOpen = false;
canOpen = false;
player.GetComponent<FPSInputController>().enabled = true;
player.GetComponent<CharacterMotor>().enabled = true;
player.GetComponent<MouseLook>().enabled = true;
}
There are two MouseLook components on Unity's standard First Person Controller: one on the player root that handles rotation about the y-axis (MouseX), and another on the Camera for the x-axis (MouseY).
To enable/disable both, you could use:
foreach(var mouseLook in player.GetComponentsInChildren<MouseLook>())
mouseLook.enabled = false;

Categories

Resources