Adam's Lair Forum

game development and casual madness
It is currently 2019/09/17, 15:17

All times are UTC + 1 hour [ DST ]




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: 2016/12/11, 07:40 
Junior Member
Junior Member

Joined: 2016/05/15, 18:27
Posts: 45
Role: Hobbyist
Hey,

So I'm currently making a psychology program for a friend. The basic idea is that there's a screen with a color on it. It's a certain color, with a corresponding arrow key. If you push the correct arrow key, it makes a 'Ding' sound. If you push the incorrect key, it makes a buzzer sound. The program is working perfectly fine, other than the fact that the loops seem to fall through each other somehow? For example, a blue slide is generated. I push 'Left' and it plays both the 'Ding' and buzzer sounds. This indicates to me that it's also incorrectly keeping track of the score, by increasing wrongAnswers and correctAnswers at the same time.

Could it be Duality works so quickly that it still registers the keyhit, making the next slide generated take that as the answer as well? I highly doubt it, since I'm using KeyHit.

Could someone please review my code and help me?

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Duality;
using Duality.Input;
using Duality.Resources;

namespace Code
{
    //Used to generate slides in the program based on the current phase
    public enum LogicPattern
    {
        Initial, //Blue's correct response is left, while red's is right.
        Reversal, //Rules reverse. Blue is right, red is left.
        Noiseless, //Rules same as reversal, only there is no noise when the correct answer is entered.
        Alternating, //Left and then right must be continuously pressed. Color does not matter.
        Still //Screen will be generated. All input increases the wrong answer value. The player is to sit still for 60 seconds without touching anything.
    }

    //Used in GenerateSlide method in order to more easily change the slide color.
    public enum SlideStatus
    {
        Red,
        Blue,
        Random
    }

    public enum SlideColor
    {
        Red,
        Blue
    }

    //For the alternating phase of the program.
    public enum NextKeyToBePressed
    {
        Left,
        Right
    }

    /// <summary>
    /// Contains actual logic of program.
    /// </summary>
    public class GameLogic : Component, ICmpInitializable, ICmpUpdatable
    {
        private GameObject backgroundObject;
        private Background background;
        private BlackOverlay blackOverlay;
        private Random random = new Random();
        //Used to store the phase the program is in.
        private LogicPattern currentPhase;
        private NextKeyToBePressed nextKey;
        private SlideColor slideColor;
        private SlideStatus slideGenerationStatus;

        //Create PhaseData instances in order to store necessary data.
        private PhaseData initial = new PhaseData("Normal. Blue's correct response is left, while red's is right.");
        private PhaseData reversal = new PhaseData("Initial rules reverse. Blue's correct response is right, while red's is left.");
        private PhaseData noiseless = new PhaseData("Rules are the same as reversal, but no noise is played when the correct answer is entered.");
        private PhaseData alternating = new PhaseData("Left and then right must be pressed. This must be done repeatedly. Color is irrelevant.");
        private PhaseData still = new PhaseData("Slide will be generated. The player is not to enter any keys. Doing so adds to the wrong answer value.");

        public ContentRef<Sound> CorrectSound { get; set; }
        public ContentRef<Sound> IncorrectSound { get; set; }

        void ICmpInitializable.OnInit(InitContext context)
        {
            if (context == InitContext.Activate)
            {
                backgroundObject = GameObj.ParentScene.FindGameObject<Background>();
                background = backgroundObject.GetComponent<Background>();
                currentPhase = LogicPattern.Initial;
                nextKey = NextKeyToBePressed.Left;
                blackOverlay = GameObj.ParentScene.FindGameObject<BlackOverlay>().GetComponent<BlackOverlay>();
            }
        }

        void ICmpInitializable.OnShutdown(ShutdownContext context)
        {
        }

        void ICmpUpdatable.OnUpdate()
        {
            //Refreshes slide color every frame
            if (slideColor == SlideColor.Red)
                background.ChangeColor(247, 117, 117);
            else if (slideColor == SlideColor.Blue)
                background.ChangeColor(118, 181, 245);

            //Have different logic for each stage of the program.
            switch (currentPhase)
            {
                case LogicPattern.Initial:
                    GenerateSlide();

                    if (slideColor == SlideColor.Blue)
                    {
                        if (DualityApp.Keyboard.KeyHit(Key.Left))
                        {
                            initial.amountCorrect++;
                            CorrectAnswer();
                        }
                        else if (DualityApp.Keyboard.KeyHit(Key.Right))
                        {
                            initial.amountWrong++;
                            IncorrectAnswer();
                        }
                    }

                    else if (slideColor == SlideColor.Red)
                    {
                        if (DualityApp.Keyboard.KeyHit(Key.Left))
                        {
                            initial.amountWrong++;
                            IncorrectAnswer();
                        }
                        else if (DualityApp.Keyboard.KeyHit(Key.Right))
                        {
                            initial.amountCorrect++;
                            CorrectAnswer();
                        }
                    }
                   
                    break;

                case LogicPattern.Reversal:
                    GenerateSlide();

                    if (slideColor == SlideColor.Blue)
                    {
                        if (DualityApp.Keyboard.KeyHit(Key.Left))
                        {
                            reversal.amountWrong++;
                            IncorrectAnswer();
                        }
                        else if (DualityApp.Keyboard.KeyHit(Key.Right))
                        {
                            reversal.amountCorrect++;
                            CorrectAnswer();
                        }
                    }

                    else if (slideColor == SlideColor.Red)
                    {
                       
                        if (DualityApp.Keyboard.KeyHit(Key.Left))
                        {
                            reversal.amountCorrect++;
                            CorrectAnswer();
                        }
                        else if (DualityApp.Keyboard.KeyHit(Key.Right))
                        {
                            reversal.amountWrong++;
                            IncorrectAnswer();
                        }
                    }

                    break;

                case LogicPattern.Noiseless:
                    GenerateSlide();

                    if (slideColor == SlideColor.Blue)
                    {
                        if (DualityApp.Keyboard.KeyHit(Key.Left))
                        {
                            noiseless.amountWrong++;
                            blackOverlay.Flash();
                            slideGenerationStatus = SlideStatus.Random;
                        }
                        else if (DualityApp.Keyboard.KeyHit(Key.Right))
                        {
                            noiseless.amountCorrect++;
                            blackOverlay.Flash();
                            slideGenerationStatus = SlideStatus.Random;
                        }
                    }

                    else if (slideColor == SlideColor.Red)
                    {

                        if (DualityApp.Keyboard.KeyHit(Key.Left))
                        {
                            noiseless.amountCorrect++;
                            blackOverlay.Flash();
                            slideGenerationStatus = SlideStatus.Random;
                        }
                        else if (DualityApp.Keyboard.KeyHit(Key.Right))
                        {
                            noiseless.amountWrong++;
                            blackOverlay.Flash();
                            slideGenerationStatus = SlideStatus.Random;
                        }
                    }
                    break;

                case LogicPattern.Alternating:
                    GenerateSlide();

                    if (nextKey == NextKeyToBePressed.Left)
                    {
                        if (DualityApp.Keyboard.KeyHit(Key.Left))
                        {
                            alternating.amountCorrect++;
                            nextKey = NextKeyToBePressed.Right;
                            CorrectAnswer();
                        }
                        else if (DualityApp.Keyboard.KeyHit(Key.Right))
                        {
                            alternating.amountWrong++;
                            nextKey = NextKeyToBePressed.Right;
                            IncorrectAnswer();
                        }
                    }
                    if (nextKey == NextKeyToBePressed.Right)
                    {
                        if (DualityApp.Keyboard.KeyHit(Key.Right))
                        {
                            alternating.amountCorrect++;
                            nextKey = NextKeyToBePressed.Left;
                            CorrectAnswer();
                        }
                        else if (DualityApp.Keyboard.KeyHit(Key.Left))
                        {
                            alternating.amountWrong++;
                            nextKey = NextKeyToBePressed.Left;
                            IncorrectAnswer();
                        }
                    }
                    break;

                case LogicPattern.Still:

                    break;
            }
        }

        /// <summary>
        /// Generates a new slide, then stops the generator from spamming new slides.
        /// </summary>
        private void GenerateSlide()
        {
            switch (slideGenerationStatus)
            {
                case SlideStatus.Red:
                    slideColor = SlideColor.Red;
                    slideGenerationStatus = SlideStatus.Red;
                    break;

                case SlideStatus.Blue:
                    slideColor = SlideColor.Blue;
                    slideGenerationStatus = SlideStatus.Blue;
                    break;

                case SlideStatus.Random:
                    int _randomValue = random.Next(0, 2); //Generate random value between 0 and 1

                    if (_randomValue == 0) //Make slide red
                    {
                        slideColor = SlideColor.Red;
                        slideGenerationStatus = SlideStatus.Red;
                    }
                       
                    else if (_randomValue == 1) //Make slide blue
                    {
                        slideColor = SlideColor.Blue;
                        slideGenerationStatus = SlideStatus.Blue;
                    }
                       
                    break;
            }
        }

        private void IncorrectAnswer()
        {
            DualityApp.Sound.PlaySound(IncorrectSound);
            blackOverlay.Flash();
            slideGenerationStatus = SlideStatus.Random;
        }

        private void CorrectAnswer()
        {
            DualityApp.Sound.PlaySound(CorrectSound);
            blackOverlay.Flash();
            slideGenerationStatus = SlideStatus.Random;
        }
    }
}


Top
 Profile  
 
PostPosted: 2016/12/11, 07:51 
Junior Member
Junior Member

Joined: 2016/05/15, 18:27
Posts: 45
Role: Hobbyist
ovicior wrote:
Hey,

So I'm currently making a psychology program for a friend. The basic idea is that there's a screen with a color on it. It's a certain color, with a corresponding arrow key. If you push the correct arrow key, it makes a 'Ding' sound. If you push the incorrect key, it makes a buzzer sound. The program is working perfectly fine, other than the fact that the loops seem to fall through each other somehow? For example, a blue slide is generated. I push 'Left' and it plays both the 'Ding' and buzzer sounds. This indicates to me that it's also incorrectly keeping track of the score, by increasing wrongAnswers and correctAnswers at the same time.

Could it be Duality works so quickly that it still registers the keyhit, making the next slide generated take that as the answer as well? I highly doubt it, since I'm using KeyHit.

Could someone please review my code and help me?

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Duality;
using Duality.Input;
using Duality.Resources;

namespace Code
{
    //Used to generate slides in the program based on the current phase
    public enum LogicPattern
    {
        Initial, //Blue's correct response is left, while red's is right.
        Reversal, //Rules reverse. Blue is right, red is left.
        Noiseless, //Rules same as reversal, only there is no noise when the correct answer is entered.
        Alternating, //Left and then right must be continuously pressed. Color does not matter.
        Still //Screen will be generated. All input increases the wrong answer value. The player is to sit still for 60 seconds without touching anything.
    }

    //Used in GenerateSlide method in order to more easily change the slide color.
    public enum SlideStatus
    {
        Red,
        Blue,
        Random
    }

    public enum SlideColor
    {
        Red,
        Blue
    }

    //For the alternating phase of the program.
    public enum NextKeyToBePressed
    {
        Left,
        Right
    }

    /// <summary>
    /// Contains actual logic of program.
    /// </summary>
    public class GameLogic : Component, ICmpInitializable, ICmpUpdatable
    {
        private GameObject backgroundObject;
        private Background background;
        private BlackOverlay blackOverlay;
        private Random random = new Random();
        //Used to store the phase the program is in.
        private LogicPattern currentPhase;
        private NextKeyToBePressed nextKey;
        private SlideColor slideColor;
        private SlideStatus slideGenerationStatus;

        //Create PhaseData instances in order to store necessary data.
        private PhaseData initial = new PhaseData("Normal. Blue's correct response is left, while red's is right.");
        private PhaseData reversal = new PhaseData("Initial rules reverse. Blue's correct response is right, while red's is left.");
        private PhaseData noiseless = new PhaseData("Rules are the same as reversal, but no noise is played when the correct answer is entered.");
        private PhaseData alternating = new PhaseData("Left and then right must be pressed. This must be done repeatedly. Color is irrelevant.");
        private PhaseData still = new PhaseData("Slide will be generated. The player is not to enter any keys. Doing so adds to the wrong answer value.");

        public ContentRef<Sound> CorrectSound { get; set; }
        public ContentRef<Sound> IncorrectSound { get; set; }

        void ICmpInitializable.OnInit(InitContext context)
        {
            if (context == InitContext.Activate)
            {
                backgroundObject = GameObj.ParentScene.FindGameObject<Background>();
                background = backgroundObject.GetComponent<Background>();
                currentPhase = LogicPattern.Initial;
                nextKey = NextKeyToBePressed.Left;
                blackOverlay = GameObj.ParentScene.FindGameObject<BlackOverlay>().GetComponent<BlackOverlay>();
            }
        }

        void ICmpInitializable.OnShutdown(ShutdownContext context)
        {
        }

        void ICmpUpdatable.OnUpdate()
        {
            //Refreshes slide color every frame
            if (slideColor == SlideColor.Red)
                background.ChangeColor(247, 117, 117);
            else if (slideColor == SlideColor.Blue)
                background.ChangeColor(118, 181, 245);

            //Have different logic for each stage of the program.
            switch (currentPhase)
            {
                case LogicPattern.Initial:
                    GenerateSlide();

                    if (slideColor == SlideColor.Blue)
                    {
                        if (DualityApp.Keyboard.KeyHit(Key.Left))
                        {
                            initial.amountCorrect++;
                            CorrectAnswer();
                        }
                        else if (DualityApp.Keyboard.KeyHit(Key.Right))
                        {
                            initial.amountWrong++;
                            IncorrectAnswer();
                        }
                    }

                    else if (slideColor == SlideColor.Red)
                    {
                        if (DualityApp.Keyboard.KeyHit(Key.Left))
                        {
                            initial.amountWrong++;
                            IncorrectAnswer();
                        }
                        else if (DualityApp.Keyboard.KeyHit(Key.Right))
                        {
                            initial.amountCorrect++;
                            CorrectAnswer();
                        }
                    }
                   
                    break;

                case LogicPattern.Reversal:
                    GenerateSlide();

                    if (slideColor == SlideColor.Blue)
                    {
                        if (DualityApp.Keyboard.KeyHit(Key.Left))
                        {
                            reversal.amountWrong++;
                            IncorrectAnswer();
                        }
                        else if (DualityApp.Keyboard.KeyHit(Key.Right))
                        {
                            reversal.amountCorrect++;
                            CorrectAnswer();
                        }
                    }

                    else if (slideColor == SlideColor.Red)
                    {
                       
                        if (DualityApp.Keyboard.KeyHit(Key.Left))
                        {
                            reversal.amountCorrect++;
                            CorrectAnswer();
                        }
                        else if (DualityApp.Keyboard.KeyHit(Key.Right))
                        {
                            reversal.amountWrong++;
                            IncorrectAnswer();
                        }
                    }

                    break;

                case LogicPattern.Noiseless:
                    GenerateSlide();

                    if (slideColor == SlideColor.Blue)
                    {
                        if (DualityApp.Keyboard.KeyHit(Key.Left))
                        {
                            noiseless.amountWrong++;
                            blackOverlay.Flash();
                            slideGenerationStatus = SlideStatus.Random;
                        }
                        else if (DualityApp.Keyboard.KeyHit(Key.Right))
                        {
                            noiseless.amountCorrect++;
                            blackOverlay.Flash();
                            slideGenerationStatus = SlideStatus.Random;
                        }
                    }

                    else if (slideColor == SlideColor.Red)
                    {

                        if (DualityApp.Keyboard.KeyHit(Key.Left))
                        {
                            noiseless.amountCorrect++;
                            blackOverlay.Flash();
                            slideGenerationStatus = SlideStatus.Random;
                        }
                        else if (DualityApp.Keyboard.KeyHit(Key.Right))
                        {
                            noiseless.amountWrong++;
                            blackOverlay.Flash();
                            slideGenerationStatus = SlideStatus.Random;
                        }
                    }
                    break;

                case LogicPattern.Alternating:
                    GenerateSlide();

                    if (nextKey == NextKeyToBePressed.Left)
                    {
                        if (DualityApp.Keyboard.KeyHit(Key.Left))
                        {
                            alternating.amountCorrect++;
                            nextKey = NextKeyToBePressed.Right;
                            CorrectAnswer();
                        }
                        else if (DualityApp.Keyboard.KeyHit(Key.Right))
                        {
                            alternating.amountWrong++;
                            nextKey = NextKeyToBePressed.Right;
                            IncorrectAnswer();
                        }
                    }
                    if (nextKey == NextKeyToBePressed.Right)
                    {
                        if (DualityApp.Keyboard.KeyHit(Key.Right))
                        {
                            alternating.amountCorrect++;
                            nextKey = NextKeyToBePressed.Left;
                            CorrectAnswer();
                        }
                        else if (DualityApp.Keyboard.KeyHit(Key.Left))
                        {
                            alternating.amountWrong++;
                            nextKey = NextKeyToBePressed.Left;
                            IncorrectAnswer();
                        }
                    }
                    break;

                case LogicPattern.Still:

                    break;
            }
        }

        /// <summary>
        /// Generates a new slide, then stops the generator from spamming new slides.
        /// </summary>
        private void GenerateSlide()
        {
            switch (slideGenerationStatus)
            {
                case SlideStatus.Red:
                    slideColor = SlideColor.Red;
                    slideGenerationStatus = SlideStatus.Red;
                    break;

                case SlideStatus.Blue:
                    slideColor = SlideColor.Blue;
                    slideGenerationStatus = SlideStatus.Blue;
                    break;

                case SlideStatus.Random:
                    int _randomValue = random.Next(0, 2); //Generate random value between 0 and 1

                    if (_randomValue == 0) //Make slide red
                    {
                        slideColor = SlideColor.Red;
                        slideGenerationStatus = SlideStatus.Red;
                    }
                       
                    else if (_randomValue == 1) //Make slide blue
                    {
                        slideColor = SlideColor.Blue;
                        slideGenerationStatus = SlideStatus.Blue;
                    }
                       
                    break;
            }
        }

        private void IncorrectAnswer()
        {
            DualityApp.Sound.PlaySound(IncorrectSound);
            blackOverlay.Flash();
            slideGenerationStatus = SlideStatus.Random;
        }

        private void CorrectAnswer()
        {
            DualityApp.Sound.PlaySound(CorrectSound);
            blackOverlay.Flash();
            slideGenerationStatus = SlideStatus.Random;
        }
    }
}

Nevermind, figured it out. Turns out I needed to switch the places of my loops, and test for input and then background color rather than background color and then the input.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group