Student project simplified version of classic game frogger

Dependencies:   4DGL-uLCD-SE mbed-rtos mbed

Fork of uLCD144G2_demo by jim hamblen

Files at this revision

API Documentation at this revision

Comitter:
acorness
Date:
Wed Oct 22 19:12:15 2014 +0000
Parent:
8:31e63caf37e2
Commit message:
O______O what?

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 31e63caf37e2 -r d33e61dab7dd main.cpp
--- a/main.cpp	Sat Nov 30 02:06:03 2013 +0000
+++ b/main.cpp	Wed Oct 22 19:12:15 2014 +0000
@@ -2,170 +2,289 @@
 //
 #include "mbed.h"
 #include "uLCD_4DGL.h"
+#include "rtos.h"
 
-uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+DigitalOut led4(LED4);
+DigitalIn  pb1(p24);        //Right
+DigitalIn  pb2(p23);        //Left
+DigitalIn  pb3(p22);        //Down
+DigitalIn  pb4(p21);        //Up
+uLCD_4DGL uLCD(p9,p10,p11); //Screen
+Mutex key;                  //Pushbutton key
 
-int main()
+   int i=2;                           //Win variable  
+   int x,y;                         //Frogger Location  
+   int u,v;                         //Previous Location
+   int k=1;                         //Change?
+   int l;
+   int gametime;                        //Game over condition
+   
+struct cars
+{
+    int x;
+    int y;
+    int u;
+    int v;  
+};   
+
+struct logs
 {
-    // basic printf demo = 16 by 18 characters on screen
-    uLCD.printf("\nHello uLCD World\n"); //Default Green on black text
-    uLCD.printf("\n  Starting Demo...");
-    uLCD.text_width(4); //4X size text
-    uLCD.text_height(4);
-    uLCD.color(RED);
-    for (int i=10; i>=0; --i) {
-        uLCD.locate(1,2);
-        uLCD.printf("%2D",i);
-        wait(.5);
-    }
-    uLCD.cls();
-    uLCD.printf("Change baudrate......");
-    uLCD.baudrate(3000000); //jack up baud rate to max for fast display
-    //if demo hangs here - try lower baud rates
-    //
-    // printf text only full screen mode demo
-    uLCD.background_color(BLUE);
-    uLCD.cls();
-    uLCD.locate(0,0);
-    uLCD.color(WHITE);
-    uLCD.textbackground_color(BLUE);
-    uLCD.set_font(FONT_7X8);
-    uLCD.text_mode(OPAQUE);
-    int i=0;
-    while(i<64) {
-        if(i%16==0) uLCD.cls();
-        uLCD.printf("TxtLine %2D Page %D\n",i%16,i/16 );
-        i++; //16 lines with 18 charaters per line
-    }
-    wait(0.5);
-    //demo graphics commands
-    uLCD.background_color(BLACK);
-    uLCD.cls();
-    uLCD.background_color(DGREY);
-    uLCD.filled_circle(60, 50, 30, 0xFF00FF);
-    uLCD.triangle(120, 100, 40, 40, 10, 100, 0x0000FF);
-    uLCD.line(0, 0, 80, 60, 0xFF0000);
-    uLCD.filled_rectangle(50, 50, 100, 90, 0x00FF00);
-    uLCD.pixel(60, 60, BLACK);
-    uLCD.read_pixel(120, 70);
-    uLCD.circle(120, 60, 10, BLACK);
-    uLCD.set_font(FONT_7X8);
-    uLCD.text_mode(TRANSPARENT);
-    uLCD.text_bold(ON);
-    uLCD.text_char('B', 9, 8, BLACK);
-    uLCD.text_char('I',10, 8, BLACK);
-    uLCD.text_char('G',11, 8, BLACK);
-    uLCD.text_italic(ON);
-    uLCD.text_string("This is a test of string", 1, 4, FONT_7X8, WHITE);
-    wait(2);
+    int x1;
+    int x2;
+    int x3;
+    int y;
+    int u1;
+    int u2;
+    int u3;
+    int v;
+    int speed;           
+};
+
+struct logs brown[5];
+struct cars  red[5];
 
-//Bouncing Ball Demo
-    float fx=50.0,fy=21.0,vx=1.0,vy=0.4;
-    int x=50,y=21,radius=4;
-    uLCD.background_color(BLACK);
-    uLCD.cls();
-    //draw walls
-    uLCD.line(0, 0, 127, 0, WHITE);
-    uLCD.line(127, 0, 127, 127, WHITE);
-    uLCD.line(127, 127, 0, 127, WHITE);
-    uLCD.line(0, 127, 0, 0, WHITE);
-    for (int i=0; i<1500; i++) {
-        //draw ball
-        uLCD.filled_circle(x, y, radius, RED);
-        //bounce off edge walls and slow down a bit?
-        if ((x<=radius+1) || (x>=126-radius)) vx = -.90*vx;
-        if ((y<=radius+1) || (y>=126-radius)) vy = -.90*vy;
-        //erase old ball location
-        uLCD.filled_circle(x, y, radius, BLACK);
-        //move ball
-        fx=fx+vx;
-        fy=fy+vy;
-        x=(int)fx;
-        y=(int)fy;
+void Print_Background()      //Prints Map
+{
+    uLCD.filled_rectangle(gametime/12,1,128,5,0x000000);
+    if(gametime/12==35){i=0;}
+    gametime-=4;
+    l+=1;
+    l= l%4;
+    for(int j=0;j<5;j++)
+    {
+            switch(red[j].v)                       //Water or land?
+        {
+            case 12:
+            case 11:
+            case 10:
+            case 9:
+            case 8:
+            case 7:
+            case 6:
+            case 0:
+            uLCD.filled_rectangle(red[j].u*8,11+(red[j].v*9),(red[j].u*8)+7,19+(red[j].v*9),0x000000);
+            break;
+            default:
+            uLCD.filled_rectangle(red[j].u*8,11+(red[j].v*9),(red[j].u*8)+7,19+(red[j].v*9),0x0000FF);
+        }    
+        uLCD.filled_rectangle(red[j].x*8,11+(red[j].y*9),(red[j].x*8)+7,19+(red[j].y*9), 0xFF0000);
+        red[j].u=red[j].x;
+        red[j].v=red[j].y;
+        red[j].x= (red[j].x+1)%16;
+        if(x==red[j].x&y==red[j].y){i=0;}
+        if(x==red[j].u&y==red[j].y){i=0;}
     }
-    wait(0.5);
-//draw an image pixel by pixel
-    int pixelcolors[50][50];
-    uLCD.background_color(BLACK);
-    uLCD.cls();
-//compute Mandelbrot set image for display
-//image size in pixels
-    const unsigned ImageHeight=128;
-    const unsigned ImageWidth=128;
-    //"c" region to display
-    double MinRe = -0.75104;
-    double MaxRe = -0.7408;
-    double MinIm = 0.10511;
-    double MaxIm = MinIm+(MaxRe-MinRe)*ImageHeight/ImageWidth;
-    double Re_factor = (MaxRe-MinRe)/(ImageWidth-1);
-    double Im_factor = (MaxIm-MinIm)/(ImageHeight-1);
-    unsigned MaxIterations = 2048;
-    for(unsigned y=0; y<ImageHeight; ++y) {
-        double c_im = MaxIm - y*Im_factor;
-        for(unsigned x=0; x<ImageWidth; ++x) {
-            double c_re = MinRe + x*Re_factor;
-            double Z_re = c_re, Z_im = c_im;
-            int niterations=0;
-            for(unsigned n=0; n<MaxIterations; ++n) {
-                double Z_re2 = Z_re*Z_re, Z_im2 = Z_im*Z_im;
-                if(Z_re2 + Z_im2 > 4) {
-                    niterations = n;
-                    break;
-                }
-                Z_im = 2*Z_re*Z_im + c_im;
-                Z_re = Z_re2 - Z_im2 + c_re;
-            }
-            if (niterations!=(MaxIterations-1))
-                uLCD.pixel(x,y,((niterations & 0xF00)<<12)+((niterations & 0xF0)<<8)+((niterations & 0x0F)<<4) );
+    for(int j=0;j<5;j++)
+    {
+        if(l==3)
+        {
+            uLCD.filled_rectangle(brown[j].u1*8,11+(brown[j].v*9),(brown[j].u1*8)+7,19+(brown[j].v*9),0x0000FF);
+            uLCD.filled_rectangle(brown[j].u2*8,11+(brown[j].v*9),(brown[j].u2*8)+7,19+(brown[j].v*9),0x0000FF);
+            uLCD.filled_rectangle(brown[j].u3*8,11+(brown[j].v*9),(brown[j].u3*8)+7,19+(brown[j].v*9),0x0000FF);
+            uLCD.filled_rectangle(brown[j].x1*8,11+(brown[j].y*9),(brown[j].x1*8)+7,19+(brown[j].y*9), 0x660000);
+            uLCD.filled_rectangle(brown[j].x2*8,11+(brown[j].y*9),(brown[j].x2*8)+7,19+(brown[j].y*9), 0x660000);            
+            uLCD.filled_rectangle(brown[j].x3*8,11+(brown[j].y*9),(brown[j].x3*8)+7,19+(brown[j].y*9), 0x660000);
+            brown[j].u1=brown[j].x1;
+            brown[j].u2=brown[j].x2;
+            brown[j].u3=brown[j].x3;
+            brown[j].v=brown[j].y;
+            brown[j].x1= (brown[j].x1+brown[j].speed)%16;
+            brown[j].x2= (brown[j].x2+brown[j].speed)%16;
+            brown[j].x3= (brown[j].x3+brown[j].speed)%16;
         }
-    }
-    wait(5);
-// PLASMA wave BLIT animation
-//draw an image using BLIT (Block Image Transfer) fastest way to transfer pixel data
-    uLCD.cls();
-    int num_cols=50;
-    int num_rows=50;
-    int frame=0;
-    double a,b,c=0.0;
-    while(frame<75) {
-        for (int k=0; k<num_cols; k++) {
-            b= (1+sin(3.14159*k*0.75/(num_cols-1.0)+c))*0.5;
-            for (int i=0; i<num_rows; i++) {
-                a= (1+sin(3.14159*i*0.75/(num_rows-1.0)+c))*0.5;
-                // a and b will be a sine wave output between 0 and 1
-                // sine wave was scaled for nice effect across array
-                // uses a and b to compute pixel colors based on rol and col location in array
-                // also keeps colors at the same brightness level
-                if ((a+b) <.667)
-                    pixelcolors[i][k] =  (255-(int(254.0*((a+b)/0.667)))<<16) | (int(254.0*((a+b)/0.667))<<8) | 0;
-                else if ((a+b)<1.333)
-                    pixelcolors[i][k] = (0 <<16) | (255-(int (254.0*((a+b-0.667)/0.667)))<<8) | int(254.0*((a+b-0.667)/0.667));
-                else
-                    pixelcolors[i][k] = (int(255*((a+b-1.333)/0.667))<<16) | (0<<8)  | (255-(int (254.0*((a+b-1.333)/0.667))));
-            }
+        if(y==brown[j].y)
+        {
+        if(x==brown[j].u1){}
+        else if(x==brown[j].u2){}
+        else if(x==brown[j].u3){}
+        else i=0;
         }
-        uLCD.BLIT(39, 39, 50, 50, &pixelcolors[0][0]);
-        c = c + 0.0314159*3.0;
-        if (c > 6.2831) c = 0.0;
-        frame++;
-    }
-    //Load Image Demo
-    uLCD.cls();
-    //SD card needed with image and video files for last two demos
-    uLCD.cls();
-    uLCD.media_init();
-    uLCD.printf("\n\nAn SD card is needed for image and video data");
-    uLCD.set_sector_address(0x001D, 0x4C01);
-    uLCD.display_image(0,0);
-    wait(10);
-    //Play video demo
-    while(1) {
-        uLCD.cls();
-        uLCD.media_init();
-        uLCD.set_sector_address(0x001D, 0x4C42);
-        uLCD.display_video(0,0);
+
     }
 }
 
 
 
+
+void Print_Frogger(int x, int y)    //Print Player
+{
+    if(k)
+    {
+        key.lock();                     //Prevents position change as writing onto screen
+        switch(v)                       //Water or land?
+        {
+            case 12:
+            case 11:
+            case 10:
+            case 9:
+            case 8:
+            case 7:
+            case 6:
+            case 0:
+            uLCD.filled_rectangle(u*8,11+(v*9),(u*8)+7,19+(v*9),0x000000);
+            break;
+            default:
+            uLCD.filled_rectangle(u*8,11+(v*9),(u*8)+7,19+(v*9),0x0000FF);
+        }    
+        uLCD.filled_rectangle(x*8,11+(y*9),(x*8)+7,19+(y*9),0x00FF00);
+        if(y==0){i=1;}
+        u=x;
+        v=y;
+        k=0;
+        key.unlock();
+    }
+}
+
+void Pushbutton_Left(void const *args)          //Check Left to change x
+{
+    while(1)
+    {
+        if((x>0)&!pb2&!k)                         //Is it not at the left wall?
+        {
+            key.lock();
+            x-=1;
+            Thread::wait(50);    
+            k=1;
+            key.unlock();   
+        }        
+    }
+}
+
+void Pushbutton_Right(void const *args)         //Check Right to change x
+{
+    while(1)
+    {
+        if((x<15)&!pb1&!k)                        //Is it not at the right wall?
+        {
+            key.lock();
+            x+=1;
+            Thread::wait(50);  
+            k=1;  
+            key.unlock();   
+        }     
+    }
+}
+
+void Pushbutton_Up(void const *args)            //Check Up to change y
+{
+     while(1)
+     {
+        if((y>0)&!pb4&!k)
+        {
+            key.lock();
+            y-=1;
+            Thread::wait(50);    
+            k=1;
+            key.unlock();           
+        }
+    }
+}
+
+void Pushbutton_Down(void const *args)          //Check Down to change y
+{
+     while(1)
+     {
+        if((y<12)&!pb3&!k)
+        {
+            key.lock();
+            y+=1;
+            Thread::wait(50);    
+            k=1;
+            key.unlock();   
+        }
+    }
+}
+
+
+int main()
+{
+   pb1.mode(PullUp);                //initiation
+   pb2.mode(PullUp);
+   pb3.mode(PullUp);      
+   pb4.mode(PullUp);
+   led1=1;
+   led2=1;
+   led3=1;
+   led4=1;
+   uLCD.baudrate(3000000);          //Set Baud at MAX
+   
+   while(1)
+   {
+        uLCD.cls();
+        switch(i)
+        {
+            case 2:
+            uLCD.printf("Simple Frogger");  //Start Screen
+            break;
+            case 1:
+            uLCD.printf("VICTORY");
+            break;
+            case 0:
+            uLCD.printf("DEFEAT");
+        }
+        while(pb1&pb2&pb3&pb4)   //Push any button to start
+        {
+           led1=!led1;
+           wait(.1);
+           led2=!led2;
+           wait(.1);
+           led3=!led3;
+           wait(.1);
+           led4=!led4;
+           wait(.1);
+        }
+        
+        uLCD.cls();
+        i=2;                    //Reset Win Condition
+        x=15;                   //Start at right wall
+        u=15;
+        y=12;                   //Start at bottom
+        v=12;
+        l=0;
+        gametime=1524;               //Game Over condition
+        uLCD.locate(0,0);
+        uLCD.printf("Time:");
+        uLCD.filled_rectangle(35,1,127,5,0x00FF00);     //Time bar
+        uLCD.filled_rectangle(0,20,128,64,0x0000FF);    //Water
+        
+        for(int j=0; j<5; j++)
+        {
+            brown[j].x1= rand()%12;
+            brown[j].x2= brown[j].x1+1;
+            brown[j].x3= brown[j].x1+2;
+            brown[j].y= j+1;
+            brown[j].u1= brown[j].x1;
+            brown[j].u2= brown[j].x2;
+            brown[j].u3= brown[j].x3;
+            brown[j].v=brown[j].y;
+            brown[j].speed= 1+rand()%2;
+        }
+        
+        for(int j=0;j<5;j++)
+        {
+            red[j].x= rand()%15;
+            red[j].y= 7+rand()%6;
+            red[j].u=red[j].x;
+            red[j].v=red[j].y;
+        }
+
+        Thread push1(Pushbutton_Up);
+        Thread push2(Pushbutton_Down);
+        Thread push3(Pushbutton_Left);
+        Thread push4(Pushbutton_Right);
+        
+        while(i==2)                //Check Win Condition
+        {                    
+        
+            Print_Background();    //Brackground print
+            Print_Frogger(x,y);    //Frogger print            
+        }
+
+    }   
+}
+
+
+
diff -r 31e63caf37e2 -r d33e61dab7dd mbed-rtos.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Wed Oct 22 19:12:15 2014 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/mbed_official/code/mbed-rtos/#631c0f1008c3
diff -r 31e63caf37e2 -r d33e61dab7dd mbed.bld
--- a/mbed.bld	Sat Nov 30 02:06:03 2013 +0000
+++ b/mbed.bld	Wed Oct 22 19:12:15 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/f37f3b9c9f0b
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/552587b429a1
\ No newline at end of file