asdf

Dependencies:   mbed

Fork of manworm_ticker_tv by Bayley Wang

Revision:
3:d9edc0575aa3
Parent:
2:1d16e99f334b
Child:
4:02e3ae7a3aea
diff -r 1d16e99f334b -r d9edc0575aa3 main.cpp
--- a/main.cpp	Sat Nov 18 08:36:25 2017 +0000
+++ b/main.cpp	Sun Nov 19 05:00:14 2017 +0000
@@ -10,7 +10,7 @@
 int SX_MIN = 30;
 int SX_MAX = 95;
 int SY_MIN = 10;
-int SY_MAX = 50;
+int SY_MAX = 48;
 int PADDLE_LEN = 10;
 
 #define Y_0 ( (Y_MIN + Y_MAX)/2 )
@@ -67,7 +67,95 @@
 0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,1,0,0,1,1,1,0,0,0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,0,0,1,1,1,0,0,0,0,0,0,0,0};
 
+uint8_t chr_A[] = {
+0,0,1,1,0,0,
+0,1,0,0,1,0,
+1,0,0,0,0,1,
+1,1,1,1,1,1,
+1,0,0,0,0,1,
+1,0,0,0,0,1,
+1,0,0,0,0,1,};
 
+uint8_t chr_D[] = {
+1,1,1,1,1,0,
+1,0,0,0,0,1,
+1,0,0,0,0,1,
+1,0,0,0,0,1,
+1,0,0,0,0,1,
+1,0,0,0,0,1,
+1,1,1,1,1,0,};
+
+uint8_t chr_F[] = {
+1,1,1,1,1,1,
+1,0,0,0,0,0,
+1,0,0,0,0,0,
+1,1,1,1,1,1,
+1,0,0,0,0,0,
+1,0,0,0,0,0,
+1,0,0,0,0,0,};
+
+uint8_t chr_G[] = {
+0,1,1,1,1,0,
+1,0,0,0,0,1,
+1,0,0,0,0,0,
+1,0,0,1,1,1,
+1,0,0,0,0,1,
+1,0,0,0,0,1,
+0,1,1,1,1,0,};
+
+uint8_t chr_H[] = {
+1,0,0,0,0,1,
+1,0,0,0,0,1,
+1,0,0,0,0,1,
+1,1,1,1,1,1,
+1,0,0,0,0,1,
+1,0,0,0,0,1,
+1,0,0,0,0,1,};
+
+uint8_t chr_M[] = {
+1,0,0,0,0,1,
+1,1,0,0,1,1,
+1,0,1,1,0,1,
+1,0,1,1,0,1,
+1,0,0,0,0,1,
+1,0,0,0,0,1,
+1,0,0,0,0,1,};
+
+uint8_t chr_N[] = {
+1,0,0,0,0,1,
+1,1,0,0,0,1,
+1,0,1,0,0,1,
+1,0,0,1,0,1,
+1,0,0,0,1,1,
+1,0,0,0,0,1,
+1,0,0,0,0,1,};
+
+uint8_t chr_O[] = {
+0,1,1,1,1,0,
+1,0,0,0,0,1,
+1,0,0,0,0,1,
+1,0,0,0,0,1,
+1,0,0,0,0,1,
+1,0,0,0,0,1,
+0,1,1,1,1,0,};
+
+uint8_t chr_P[] = {
+1,1,1,1,1,0,
+1,0,0,0,0,1,
+1,0,0,0,0,1,
+1,1,1,1,1,0,
+1,0,0,0,0,0,
+1,0,0,0,0,0,
+1,0,0,0,0,0,};
+
+uint8_t chr_S[] = {
+0,1,1,1,1,1,
+1,0,0,0,0,0,
+1,0,0,0,0,0,
+0,1,1,1,1,0,
+0,0,0,0,0,1,
+0,0,0,0,0,1,
+1,1,1,1,1,0,};
 
 uint16_t l=0; //current line of scan
 
@@ -114,6 +202,19 @@
 
 int s1 = 0, s2 = 0;
 
+//fish variables
+#define FDEATH_RESET 30000
+float fx = (SX_MAX+SX_MIN)/2.f, fy = (SY_MIN+SY_MAX)/2.f, fv = 0.02f, ftheta = 0.0, fa = 0.002;
+float uv = 0.005;
+float ux[8] = {53,70,78,78,70,53,45,45};
+float uy[8] = {10,10,20,35,50,50,35,20};
+float uvx[8] = {-1,1,1,1,1,-1,-1,-1};
+float uvy[8] = {1,1,1,-1,-1,-1,-1,1};
+int ulive[8] = {1,1,1,1,1,1,1,1};
+
+int fdeath_ticker = FDEATH_RESET;
+float fs = 0;
+
 DigitalIn p1_in(A1);
 DigitalIn p2_in(A0);
 
@@ -125,9 +226,17 @@
     
 }
 
+void clr()
+{
+    for(int i = 0; i < H_RES; i++)
+        for(int j = 0; j < V_RES; j++)
+            im_line_va[i+j*H_RES] = 0;
+    
+}
+
 void init_buffers()
 {
-    make_checkerboard();
+    clr();
     for(int i = 0; i < H_RES; i++)
     {
         im_line_s[i] = 1;   
@@ -430,77 +539,220 @@
     }
 }
 
-int main() {
-    potato:
-    init_buffers();
-    t.attach_us(&isr,63);
+int flappy_pong() {
+    
+    //clear frame
+    for(int y = 0; y < PADDLE_LEN; y++) {
+        im_line_va[H_RES*(y+(int)p1)+SX_MIN] = 0;
+        im_line_va[H_RES*(y+(int)p2)+SX_MAX] = 0;
+    }
+    im_line_va[H_RES*(int)by+(int)bx] = 0;
+    //read inputs
+    int cmd1 = p1_in.read();
+    int cmd2 = p2_in.read();
+    if (cmd1 == 0) p1v = 0.1; else p1v = -0.1f;
+    if (cmd2 == 0) p2v = 0.1; else p2v = -0.1f;
+    
+    p1 += p1v;
+    p2 += p2v;
+    bx += bxv;
+    by += byv;
+    
+    if (p1 < SY_MIN) p1 = SY_MIN;
+    if (p1 > SY_MAX-PADDLE_LEN) p1 = SY_MAX-PADDLE_LEN;
+    if (p2 < SY_MIN) p2 = SY_MIN;
+    if (p2 > SY_MAX-PADDLE_LEN) p2 = SY_MAX-PADDLE_LEN;
+    
+    if (bx < SX_MIN) {
+        bxv = -bxv; 
+        bx = SX_MIN;
+        if (by < p1 - 3 || by > p1 + PADDLE_LEN + 3) {
+            im_line_va[H_RES*(SY_MIN-2)+2*s1+SX_MIN] = 1;
+            bx = (SX_MIN+SX_MAX)/2, by = (SY_MIN+SY_MAX)/2;
+            s1++;
+            if (s1>14) return 0;
+        }
+    } 
+    if (bx > SX_MAX) {
+        bxv = -bxv; 
+        bx = SX_MAX;
+        if (by < p2 - 3 || by > p2 + PADDLE_LEN + 3) {
+            im_line_va[H_RES*(SY_MAX+2)+2*s2+SX_MIN] = 1;
+            bx = (SX_MIN+SX_MAX)/2, by = (SY_MIN+SY_MAX)/2;
+            s2++;
+            if (s2>14) return 0;
+        }
+    }
+    if (by < SY_MIN) {byv = -byv; by = SY_MIN;}
+    if (by > SY_MAX) {byv = -byv; by = SY_MAX;}
+    
     for(int y = 0; y < PADDLE_LEN; y++) {
         im_line_va[H_RES*(y+(int)p1)+SX_MIN] = 1;
         im_line_va[H_RES*(y+(int)p2)+SX_MAX] = 1;
     }
     im_line_va[H_RES*(int)by+(int)bx] = 1;
-    for(;;)
+    
+    wait(1/1200.0f);
+    return 1;
+}
+
+int fishy() {
+    uint16_t xstart, ystart, xend, yend;
+    float s = sinf(ftheta);
+    float c = cosf(ftheta);
+    
+    xstart = (uint16_t) fx;
+    ystart = (uint16_t) fy;
+    
+
+    for(int x = xstart-5; x < xstart + 5; x++) {
+        for (int y = ystart - 5; y < ystart + 5; y++) {
+            im_line_va[H_RES*y+x] = 0;
+        }
+    }
+    for (int i = 0; i < 8; i++) {
+        int index = H_RES*(int)uy[i]+(int)ux[i];
+        im_line_va[index] = 0;
+    }    
+
+    float fx_old = fx, fy_old = fy;
+    fx += fv * s;
+    fy += fv * c;
+    int cmd = p1_in.read();
+    if (cmd) ftheta += fa; else ftheta -= fa;
+    if (ftheta >= 2 * 3.1415926) ftheta -= 2 * 3.1415926;
+    if (ftheta < 0) ftheta += 2 * 3.1415926;
+    
+    s = sinf(ftheta);
+    c = cosf(ftheta);
+    
+    xstart = (uint16_t)fx;
+    ystart = (uint16_t)fy;
+    xend = (uint16_t)(fx+s*4);
+    yend = (uint16_t)(fy+c*4);
+    
+    if (xstart < SX_MIN || xstart > SX_MAX ||
+        ystart < SY_MIN || ystart > SY_MAX ||
+        xend < SX_MIN || xend > SX_MAX ||
+        yend < SY_MIN || yend > SY_MAX) {
+        fx = fx_old;
+        fy = fy_old;
+        xstart = (uint16_t)fx;
+        ystart = (uint16_t)fy;
+        xend = (uint16_t)(fx+s*4);
+        yend = (uint16_t)(fy+c*4);
+    }
+
+    draw_line(xstart, ystart, xend, yend);
+    for (int i = 0; i < 8; i++) {
+        if (!ulive[i]) continue;
+        if (xstart == (int)ux[i] && ystart == (int)uy[i]) {
+            fdeath_ticker = FDEATH_RESET;
+            fs+=1;
+            ulive[i] = 0;
+        }
+        ux[i]+= uv*uvx[i];
+        uy[i]+= uv*uvy[i];
+        if (ux[i] < SX_MIN) {ux[i] = SX_MIN; uvx[i] = -uvx[i];}
+        if (ux[i] > SX_MAX) {ux[i] = SX_MAX; uvx[i] = -uvx[i];}
+        if (uy[i] < SY_MIN) {uy[i] = SY_MIN; uvy[i] = -uvy[i];}
+        if (uy[i] > SY_MAX) {uy[i] = SY_MAX; uvy[i] = -uvy[i];}  
+                  
+        int index = H_RES*(int)uy[i]+(int)ux[i];
+        im_line_va[index] = 1;
+    }
+    for (int i = 0; i < 8; i++) {
+        im_line_va[H_RES*(SY_MAX+6)+2*(int)i+SX_MIN] = 0;
+    }
+    for (int i = 0; i < fs; i++) {
+        im_line_va[H_RES*(SY_MAX+6)+2*(int)i+SX_MIN] = 1;
+    }
+    fdeath_ticker--;
+    if (fdeath_ticker < 0) fs -= 0.0001f;
+    if (fs < 0) fs = 0;
+
+    return 1;
+}
+
+void dispchr(int x0, int y0, uint8_t* chr) {
+    int bmi = 0;
+    for(int y = SY_MIN + y0; y < SY_MIN + y0 + 7; y++)
     {
-        //clear frame
-        for(int y = 0; y < PADDLE_LEN; y++) {
-            im_line_va[H_RES*(y+(int)p1)+SX_MIN] = 0;
-            im_line_va[H_RES*(y+(int)p2)+SX_MAX] = 0;
+        for(int x = SX_MIN + x0; x < SX_MIN + x0 + 6; x++)
+        {
+            im_line_va[H_RES*y + x] = chr[bmi];
+            bmi++;
         }
-        im_line_va[H_RES*(int)by+(int)bx] = 0;
-        //read inputs
+    }
+}
+
+int main() {
+    potato:
+    init_buffers();
+    t.attach_us(&isr,63);
+    
+    int bmi = 0;
+    
+    for(int y = SY_MIN; y < SY_MIN + BY; y++)
+    {
+        for(int x = SX_MIN; x < SX_MIN + BX; x++)
+        {
+            im_line_va[H_RES*y + x] = miters[bmi];
+            bmi++;
+        }
+    }
+    
+    dispchr(4, BY+2, chr_P);
+    dispchr(11, BY+2, chr_A);
+    dispchr(18, BY+2, chr_N);
+    dispchr(25, BY+2, chr_G);
+    
+    dispchr(4, BY+10, chr_F);
+    dispchr(11, BY+10, chr_A);
+    dispchr(18, BY+10, chr_S);
+    dispchr(25, BY+10, chr_H);
+    
+    dispchr(4, BY+18, chr_D);
+    dispchr(11, BY+18, chr_A);
+    dispchr(18, BY+18, chr_M);
+    dispchr(25, BY+18, chr_O);
+    
+    int cursor_pos = 0;
+    
+    for (;;) {
         int cmd1 = p1_in.read();
         int cmd2 = p2_in.read();
-        if (cmd1 == 0) p1v = 0.1; else p1v = -0.1f;
-        if (cmd2 == 0) p2v = 0.1; else p2v = -0.1f;
+        
+        int cursor_x = SX_MIN;
+        int cursor_y = SY_MIN + 8*cursor_pos + BY + 5;
         
-        p1 += p1v;
-        p2 += p2v;
-        bx += bxv;
-        by += byv;
+        if (cmd1 || cmd2) im_line_va[H_RES*cursor_y+cursor_x] = 0;
         
-        if (p1 < SY_MIN) p1 = SY_MIN;
-        if (p1 > SY_MAX-PADDLE_LEN) p1 = SY_MAX-PADDLE_LEN;
-        if (p2 < SY_MIN) p2 = SY_MIN;
-        if (p2 > SY_MAX-PADDLE_LEN) p2 = SY_MAX-PADDLE_LEN;
+        if (!cmd1) cursor_pos++;
+        if (!cmd2) cursor_pos--;
+        if (!cmd1 && !cmd2) break;
         
-        if (bx < SX_MIN) {
-            bxv = -bxv; 
-            bx = SX_MIN;
-            if (by < p1 || by > p1 + PADDLE_LEN) {
-                im_line_va[H_RES*(SY_MIN-2)+2*s1+SX_MIN] = 1;
-                s1++;
-            }
-        } 
-        if (bx > SX_MAX) {
-            bxv = -bxv; 
-            bx = SX_MAX;
-            if (by < p2 || by > p2 + PADDLE_LEN) {
-                im_line_va[H_RES*(SY_MAX+2)+2*s2+SX_MIN] = 1;
-                s2++;
-            }
-        }
-        if (by < SY_MIN) {byv = -byv; by = SY_MIN;}
-        if (by > SY_MAX) {byv = -byv; by = SY_MAX;}
+        if (cursor_pos < 0) cursor_pos = 0;
+        if (cursor_pos > 2) cursor_pos = 2;
         
-        for(int y = 0; y < PADDLE_LEN; y++) {
-            im_line_va[H_RES*(y+(int)p1)+SX_MIN] = 1;
-            im_line_va[H_RES*(y+(int)p2)+SX_MAX] = 1;
-        }
-        im_line_va[H_RES*(int)by+(int)bx] = 1;
+        cursor_x = SX_MIN;
+        cursor_y = SY_MIN + 8*cursor_pos + BY + 5;
         
-        wait(1/1200.0f);
+        im_line_va[H_RES*cursor_y+cursor_x] = 1;
+        
+        wait(0.1);
     }
     
-    p1 = (SY_MIN+SY_MAX)/2-PADDLE_LEN/2, p2 = (SY_MIN+SY_MAX)/2-PADDLE_LEN/2;
-    p1v = 0.1, p2v = -0.1;
-
-    bx = (SX_MIN+SX_MAX)/2, by = (SY_MIN+SY_MAX)/2;
-    bxv = 0.03, byv = 0.03;
+    init_buffers();
     
-    for (;;) 
-    {
-        for(int i = 0; i < 20000; i++)
-            update_image();
-        if(!p1_in.read() || !p2_in.read()) goto potato;
+    if (cursor_pos == 0) {
+        for (;;) {
+            if (!flappy_pong()) break;
+        }
+    } else if (cursor_pos == 1) {
+        for (;;) fishy();
+    } else {
+        for (;;) update_image();
     }
+    goto potato;
 }