diff options
author | Dominique Martinet <asmadeus@codewreck.org> | 2011-12-14 11:30:46 +0100 |
---|---|---|
committer | Dominique Martinet <asmadeus@codewreck.org> | 2011-12-14 11:30:46 +0100 |
commit | a5b31900f2cc9cb58db5f6144a8aae3ac2ab4616 (patch) | |
tree | b1a59015ba4a198fcba451aab1ebac334a5ac0d2 | |
parent | 13396e6c49a0e1882f5cdde67306a836a78b5657 (diff) |
the ball moves. fixed that velocity is signed
-rw-r--r-- | init.c | 9 | ||||
-rw-r--r-- | pong.c | 87 | ||||
-rw-r--r-- | pong.h | 14 |
3 files changed, 77 insertions, 33 deletions
@@ -40,7 +40,7 @@ void Timer0_Init(void) { } void wait500ns(){ - TL0 = 255-(SYSCLK/100000); // an edge every 10us; + TL0 = 255-(SYSCLK/50000); // an edge every 20us; TF0=0; TR0=1; while (!TF0); @@ -65,6 +65,13 @@ void LCD_Init() { LCD_Send(); RST=1; + + // DB=0xC0; + RS=0; RW=0; + DB=0b11000000; + LCD_Send(); + + DB=0x3F; LCD_Send(); } @@ -30,6 +30,7 @@ // Clock stuff __bit top_sec=0; +__bit gametick=0; unsigned char SS=0; unsigned char MM=0; unsigned char HH=0; @@ -66,7 +67,6 @@ void main (void) { EA = 1; // enable global interrupts LCD_Init(); - LCD_Clear(1, 0); InitGame(); @@ -100,15 +100,18 @@ void main (void) { } - printf("woot %02u:%02u:%02u -- %u\r\n", HH, MM, SS, X_MAX); + printf("woot %02u:%02u:%02u\r\n", HH, MM, SS); + } + if (gametick==1) { + gametick=0; + GameEngine(); LCD_DrawScreen(); } - } + } } void InitGame() { - printf("XMAX : %u\n\r", X_MAX); memset(&NextGame, 0, sizeof(NextGame)); NextGame.player1 = (X_MAX - SPLAYERHEIGHT)/ 2; @@ -120,12 +123,42 @@ void InitGame() memcpy(&OldGame, &NextGame, sizeof(NextGame)); - printf("Init pos: %u\n\r", NextGame.player1); + LCD_Clear(1, 0); } void GameEngine() { - + int absvelocityx; + if (NextGame.velocity.x >= 0) + absvelocityx = NextGame.velocity.x; + else + absvelocityx = - NextGame.velocity.x; + + NextGame.ball.x += NextGame.velocity.x; + NextGame.ball.y += NextGame.velocity.y; + if (NextGame.ball.x <= absvelocityx || NextGame.ball.x >= X_MAX - absvelocityx) { + NextGame.velocity.x = -12*(NextGame.velocity.x/10); + NextGame.ball.x += NextGame.velocity.x; + } + if (NextGame.ball.y <= SPLAYERWIDTH) { + if ((NextGame.ball.x >= NextGame.player1) && (NextGame.ball.x <= NextGame.player1 + SPLAYERHEIGHT)) { + NextGame.velocity.y = -12*(NextGame.velocity.y/10); + NextGame.ball.y += NextGame.velocity.y; + NextGame.velocity.x = 12*(NextGame.velocity.x/10); + } else { + InitGame(); + } + } + if (NextGame.ball.y >= Y_MAX - SPLAYERWIDTH) { + if ((NextGame.ball.x >= NextGame.player2) && (NextGame.ball.x <= NextGame.player2 + SPLAYERHEIGHT)) { + NextGame.velocity.y = -12*(NextGame.velocity.y/10); + NextGame.ball.y += NextGame.velocity.y; + NextGame.velocity.x = 12*(NextGame.velocity.x/10); + } else { + InitGame(); + } + } + } void hscanf(char* str, unsigned char* a, unsigned char* b, unsigned char* c) { @@ -209,22 +242,22 @@ void KbdHandler (void) // Called by KbdHandler void InputCtrl (unsigned char kbd) { - if (KB_TEST(kbd, KUP1) && NextGame.player1 < X_MAX - SPLAYERHEIGHT - KEYINC) + if (KB_TEST(kbd, KUP1) && NextGame.player1 <= X_MAX - SPLAYERHEIGHT - KEYINC) { NextGame.player1 += KEYINC; printf("KUP1 pressed : %x %u\n\r", kbd, NextGame.player1); } - if (KB_TEST(kbd, KDOWN1) && NextGame.player1 > KEYINC) + if (KB_TEST(kbd, KDOWN1) && NextGame.player1 >= KEYINC) { NextGame.player1 -= KEYINC; printf("KDOWN1 pressed : %x %u\n\r", kbd, NextGame.player1); } - if (KB_TEST(kbd, KUP2) && NextGame.player2 < X_MAX - SPLAYERHEIGHT - KEYINC) + if (KB_TEST(kbd, KUP2) && NextGame.player2 <= X_MAX - SPLAYERHEIGHT - KEYINC) { NextGame.player2 += KEYINC; printf("KUP2 pressed : %x %u\n\r", kbd, NextGame.player2); } - if (KB_TEST(kbd, KDOWN2) && NextGame.player2 > KEYINC) + if (KB_TEST(kbd, KDOWN2) && NextGame.player2 >= KEYINC) { NextGame.player2 -= KEYINC; printf("KDOWN2 pressed : %x %u\n\r", kbd, NextGame.player2); @@ -247,6 +280,9 @@ void Timer2_ISR (void) __interrupt 5 // if(tick) TF2H = 0; x--; + if (x % (TMR2DIVIDER/10) ==0) { + gametick=1; + } if (x==0){ x=TMR2DIVIDER; LED = !LED; @@ -254,7 +290,6 @@ void Timer2_ISR (void) __interrupt 5 } KbdHandler(); - GameEngine(); } void SW_ISR (void) __interrupt 2 { @@ -287,8 +322,8 @@ void LCD_Clear(__bit _CS1, __bit _CS2) { RS=1; DB=0xFF; LCD_Send(); - } - } + } + } } @@ -298,28 +333,24 @@ void LCD_DrawScreen() { x = (OldGame.player1 >> X_SHIFT); LCD_Set(1, 0, x/8, 1, 0xFF); - LCD_Set(1, 0, x/8+1, 1, 0xFF); + LCD_Set(1, 0, x/8, 2, 0xFF); if (x % 8 != 0) { - LCD_Set(1, 0, x/8, 2, 0xFF); + LCD_Set(1, 0, x/8+1, 1, 0xFF); LCD_Set(1, 0, x/8+1, 2, 0xFF); - } + } x = (OldGame.player2 >> X_SHIFT); + LCD_Set(1, 0, x/8, 61, 0xFF); LCD_Set(1, 0, x/8, 62, 0xFF); + LCD_Set(1, 0, x/8+1, 61, 0xFF); LCD_Set(1, 0, x/8+1, 62, 0xFF); - if (x % 8 != 0) { - LCD_Set(1, 0, x/8, 61, 0xFF); - LCD_Set(1, 0, x/8+1, 61, 0xFF); - } x = (OldGame.ball.x >> X_SHIFT); y = (OldGame.ball.y >> Y_SHIFT); LCD_Set(1, 0, x/8, y, 0xFF); LCD_Set(1, 0, x/8, y+1, 0xFF); - if (x % 8 == 7) { - LCD_Set(1, 0, x/8+1, y, 0xFF); - LCD_Set(1, 0, x/8+1, y+1, 0xFF); - } + LCD_Set(1, 0, x/8+1, y, 0xFF); + LCD_Set(1, 0, x/8+1, y+1, 0xFF); x = (NextGame.player1 >> X_SHIFT); xOverFlow = x % 8; @@ -339,16 +370,16 @@ void LCD_DrawScreen() { LCD_Set(1, 0, x/8+1, 61, (0xFF << xOverFlow)); } - x = (OldGame.ball.x >> X_SHIFT); - y = (OldGame.ball.y >> Y_SHIFT); + x = (NextGame.ball.x >> X_SHIFT); + y = (NextGame.ball.y >> Y_SHIFT); xOverFlow = x % 8; LCD_Set(1, 0, x/8, y, ~(0x03 << xOverFlow)); LCD_Set(1, 0, x/8, y+1, ~(0x03 << xOverFlow)); if (x % 8 == 7) { LCD_Set(1, 0, x/8+1, y, 0xFE); LCD_Set(1, 0, x/8+1, y+1, 0xFE); - } + } - memcpy(&OldGame, &NextGame, sizeof(NextGame)); + memcpy(&OldGame, &NextGame, sizeof(NextGame)); } @@ -18,6 +18,11 @@ typedef struct Vector unsigned int x; unsigned int y; } Vector; +typedef struct VelocityVector +{ + int x; + int y; +} VelocityVector; // Game struct typedef struct Game @@ -25,7 +30,7 @@ typedef struct Game unsigned int player1; unsigned int player2; Vector ball; - Vector velocity; + VelocityVector velocity; } Game; // values for position precision @@ -34,10 +39,11 @@ typedef struct Game #define X_MAX (unsigned int)((64 << X_SHIFT) -1) #define Y_MAX (unsigned int)((64 << Y_SHIFT) -1) + #define PLAYERHEIGHT 8 -#define PLAYERWIDTH 2 -#define SPLAYERHEIGHT (unsigned int)(PLAYERHEIGHT << X_SHIFT) -#define SPLAYERWIDTH (unsigned int)(PLAYERWIDTH << X_SHIFT) +#define PLAYERWIDTH 3 +#define SPLAYERHEIGHT ((unsigned int)(PLAYERHEIGHT << X_SHIFT) -1) +#define SPLAYERWIDTH ((unsigned int)(PLAYERWIDTH << X_SHIFT) -1) #define KEYINC (1 << 9) |