0

I'm trying to simulate a simple pacman game using pic 16f877a and compiler MikroCpro for pic but I have two problems and I hope someone can help: 1- I don't know how to create a random fruit. I was trying to use this code:

#include <time.h>
#include <stdlib.h>
srand(time(NULL));
int r = rand(); 

but I can't include time.h so I just displayed the fruit in fixed positions.

2-The following code works fine on the pic simulator IDE but in reality a monster keeps blinking on the lcd instead of the fruit. The monster itself moves fine.

// LCD module connections
sbit LCD_RS at PORTD.B2;
sbit LCD_EN at PORTD.B3;
sbit LCD_D4 at PORTD.B4;
sbit LCD_D5 at PORTD.B5;
sbit LCD_D6 at PORTD.B6;
sbit LCD_D7 at PORTD.B7;

sbit LCD_RS_Direction at TRISD.B2;
sbit LCD_EN_Direction at TRISD.B3;
sbit LCD_D4_Direction at TRISD.B4;
sbit LCD_D5_Direction at TRISD.B5;
sbit LCD_D6_Direction at TRISD.B6;
sbit LCD_D7_Direction at TRISD.B7;
// End LCD module connections


char k =0;
char j =1;
const char MonsterMouthOpened[] = {31,23,30,28,24,28,30,31};
const char MonsterMouthClosed[] = {31,23,31,31,24,31,31,31};
const char fruit[] = {0,4,14,31,14,4,0,0};

void MonsterMouth1(char pos_row, char pos_char) {
  char i;
    Lcd_Cmd(64);
    for (i = 0; i<=7; i++) Lcd_Chr_CP(MonsterMouthOpened[i]);
    Lcd_Cmd(_LCD_RETURN_HOME);
    Lcd_Chr(pos_row, pos_char, 0);
}

void MonsterMouth2(char pos_row, char pos_char) {
  char i;
    Lcd_Cmd(64);
    for (i = 0; i<=7; i++) Lcd_Chr_CP(MonsterMouthClosed[i]);
    Lcd_Cmd(_LCD_RETURN_HOME);
    Lcd_Chr(pos_row, pos_char, 0);
}


  void fruit1(char pos_row, char pos_char) {
  char i;
    Lcd_Cmd(64);
    for (i = 0; i<=7; i++) Lcd_Chr_CP(fruit[i]);
    Lcd_Cmd(_LCD_RETURN_HOME);
    Lcd_Chr(pos_row, pos_char, 0);
}
void main(){

  Lcd_Init();                        // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off

   while(1)
   {
   if (j==1)
   {
     if(k<8)
    { 
     fruit1(j,8);
     //delay_us(1);
     delay_ms(100);
     }
     else if (k>8)
    {
     fruit1(2,8 );
     //delay_us(1);
     delay_ms(100);
    }
   }
   else if (j==2)
   {
      if(k<8)
    {
     fruit1(j,8);
     //delay_us(1);
     delay_ms(100);
     }
     else if (k>8)
    {
     fruit1(1,8 );
     //delay_us(1);
     delay_ms(100);
    }
   }

   MonsterMouth1(j, k)  ;      
  // delay_us(1);
  delay_ms(100);

   MonsterMouth2(j, k)  ;       
   //delay_us(1);
   delay_ms(100);

   k++;
   Lcd_Cmd(_LCD_CLEAR);

   if((k==16)&&(j==1)) {k=0;
   j=2;}
     else if ((k==16)&&(j==2)) {k=0;
   j=1;}
    }
  }
Nemo
  • 85
  • 2
  • 11
  • for ease of readability and understanding by us humans: 1) consistently indent the code. 2) replace 'magic' numbers with meaningful names, using #define statements or an enum then use those meaningful names throughout the code. 3) separate code blocks (for, if, else, while, do...while, switch, case, default) via blank line – user3629249 Mar 20 '16 at 22:27
  • normally, fruit does not move, so the last line in function `fruit1()` probably should not be there. – user3629249 Mar 20 '16 at 22:40
  • there seems to be a problem with the program logic. It displays the fruit, then, AT THE SAME LOCATION, displays a monster. – user3629249 Mar 20 '16 at 22:40

0 Answers0