Login [Register]
Don't have an account? Register now to chat, post, use our tools, and much more.
Hey all! I've been working on a little project over the past few days to make a game similar to the Simon Classic game where a sequence of lights flash and then you have to re-create the sequence. Anyway, I was working on the player input function but I have never worked with countdown timers. My plan was to give the player a certain amount of time to respond before losing. Currently, I am using the timer set up code from the second counter example in CEdev (Not sure if this is the most efficient way to implement it it was just what I thought of in the given time). Any help is definitely welcome! Thank you in advance.

Code:
void runGame(){
    bool playing = true;
    bool key, prevKey;
    generateMoves();
    while(playing){
        int i;
        showComputerLights(curMove);
        for(i = 0; i < curMove; i++){
           
            timer_Control = TIMER1_DISABLE;
            timer_1_ReloadValue = timer_1_Counter = 32768;
            timer_Control = TIMER1_ENABLE | TIMER1_32K | TIMER1_0INT | TIMER1_DOWN;
           
            /*
            ADD CODE HERE
            */
        }
        playing = false;
    }
}
I suggest basing your code off of second_counter_2 instead of second_counter. It aligns a bit better with what you're trying to do. Set the desired time limit in timer_1_MatchValue_1, start/reset the timer with reset_counter(), continuously check if you've reached the time limit with if (timer_IntStatus & TIMER1_MATCH1), and if you reach the time limit, make sure to acknowledge it with timer_IntStatus = TIMER1_MATCH1.
I spent some time figuring it out and I decided to go with second_counter (1) because I just like the idea of having it count down for some reason (don't judge). Interesting problem I ran into is that the timer only works when the RAM is reset on the calculator (in CEmu at least, haven't tested it on a physical calculator yet). Does anyone know how to fix this problem? The reset_Timer() function is the same as second_counter_2 except for the fact that I set it to start at one second and count down.
Code:
/*
 *--------------------------------------
 * Program Name:SIMONCE
 * Author:matkeller19
 *--------------------------------------
*/

/* Keep these headers */
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <tice.h>

/* Standard headers (recommended) */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <graphx.h>
#include <keypadc.h>

#include "gfx/SimonCircleC.h"

#define ONE_SECOND      32768/1

/* Put your function prototypes here */
void unlitGame();
void litGame(int num);
void generateMoves();
void showComputerLights(int curMove);
void runGame();
void reset_timer();

int sequences = 0;
int moves[31];

void main(void) {

    srand(rtc_Time());
   
    timer_1_MatchValue_1 = ONE_SECOND;

    gfx_Begin();
    gfx_SetPalette(SimonCircleC_pal, sizeof_SimonCircleC_pal, 0);
    gfx_SetTransparentColor(1);
    gfx_ZeroScreen();

    gfx_SetDrawBuffer();
    gfx_BlitScreen();
   
    runGame();
   
    gfx_End();
}

void unlitGame(){
    gfx_SetColor(96);   //Red
    gfx_FillTriangle(0,0,190,0,96,96); 
    gfx_SetColor(100);  //Green
    gfx_FillTriangle(0,0,0,190,96,96);
    gfx_SetColor(197);  //Yellow
    gfx_FillTriangle(0,190,190,190,96,96);
    gfx_SetColor(57);   //Blue
    gfx_FillTriangle(190,0,190,190,96,96);
    gfx_ScaledTransparentSprite_NoClip(SimonCircle, 0, 0, 3, 3);
}

void litGame(int num){
    unlitGame();
    switch (num){
        case 0:
            gfx_SetColor(224);  //Red
            gfx_FillTriangle(0,0,190,0,96,96);
            break;
        case 1:
            gfx_SetColor(103);  //Green
            gfx_FillTriangle(0,0,0,190,96,96);
            break;
        case 2:
            gfx_SetColor(231);  //Yellow
            gfx_FillTriangle(0,190,190,190,96,96);
            break;
        case 3:
            gfx_SetColor(63);   //Blue
            gfx_FillTriangle(190,0,190,190,96,96);
            break;
        default:
            break;
    }
    gfx_ScaledTransparentSprite_NoClip(SimonCircle, 0, 0, 3, 3);
}

void generateMoves(){
    int i;
    for(i = 0; i < 31; i++){
        moves[i] = random()%4;
    }
}

void showComputerLights(int curMove){
    int i;
    for(i = 0; i < curMove; i++){
        litGame(moves[i]);
        gfx_SwapDraw();
        delay(750);
        unlitGame();
        gfx_SwapDraw();
        delay(500);
    }
}

void runGame(){
    bool playing = true, timeToMove = true;
    bool key, prevKey;
    int curMove = 1;
    generateMoves();
    while(playing){
        int i;
        showComputerLights(curMove);
        for(i = 0; i < curMove; i++){
            reset_timer();
            while(timeToMove){
                if (timer_IntStatus & TIMER1_RELOADED) {
                    timer_IntAcknowledge = TIMER1_RELOADED;
                    timeToMove = false;
                }
            }
        }
        playing = false;
    }
}

void reset_timer(){
    timer_Control = TIMER1_DISABLE;
    timer_1_ReloadValue = timer_1_Counter = ONE_SECOND;
    timer_Control = TIMER1_ENABLE | TIMER1_32K | TIMER1_0INT | TIMER1_DOWN;
}


Edit: Changed to show full code.
irc wrote:
12:50 <MateoC> why are you reseting the timer every time
12:50 <MateoC> Once it hits 0, it will be reloaded
12:50 <saxjax> [matkeller19] Ohhhhh
12:50 <MateoC> Reseting it constantly in the loop causes it to never hit 0 again
Canít wait to see how this turns out! Do you have any screenshots? Looking forward to seeing more updates on this.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are GMT - 5 Hours
 
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
You cannot vote in polls in this forum

 

Advertisement