Advertisement
ldir

arkanoid.c

May 2nd, 2012
1,104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <windows.h>
  4. #include "pt.h"
  5.  
  6. #define COLS 30
  7. #define ROWS 21
  8. #define BRICK_ROWS 5
  9. #define BEAT_SIZE 10
  10. #define SPEED 10
  11.  
  12. char field[ROWS][COLS];
  13. char fieldChanged;
  14.  
  15. static void setCell(int x,int y, char value){
  16.     if(field[y][x] != value){
  17.         field[y][x] = value;
  18.         fieldChanged = 1;
  19.     }
  20. }
  21.  
  22. static int printField(struct pt *pt) {
  23.     int x,y;
  24.     static int bricks;
  25.     static HANDLE console;
  26.     PT_BEGIN(pt);
  27.     field[0][0]=field[0][COLS-1]=field[ROWS-1][0]=field[ROWS-1][COLS-1]='+';
  28.     for(y=1;y<COLS-1;y++) field[ROWS-1][y]=field[0][y]='-';
  29.     for(y=1;y<ROWS-1;y++) {
  30.         field[y][0]=field[y][COLS-1]='|';
  31.         for(x=1;x<COLS-1;x++)
  32.             if(y<=BRICK_ROWS)field[y][x]='#';
  33.             else field[y][x]=' ';
  34.     }
  35.     fieldChanged = 1;
  36.     console = GetStdHandle(STD_OUTPUT_HANDLE);
  37.     do{
  38.         PT_WAIT_UNTIL(pt,fieldChanged);
  39.         bricks = 0;
  40.         for(y=0;y<ROWS;y++){
  41.             COORD position = { 0, y };
  42.             SetConsoleCursorPosition( console, position );
  43.             WriteConsoleA(console,&field[y][0],COLS,NULL,NULL);
  44.             for(x=0;x<COLS;x++)
  45.                 if( field[y][x] == '#') bricks++;
  46.         }
  47.         fieldChanged=0;
  48.     }while(bricks);
  49.     PT_END(pt);
  50. }
  51.  
  52. static int controlThr(struct pt *pt) {
  53.     int c;
  54.     static int x=1;
  55.     PT_BEGIN(pt);
  56.     do{
  57.         for(c=x;c<x+BEAT_SIZE;c++)
  58.             setCell(c,ROWS-2,'=');
  59.         PT_WAIT_UNTIL(pt,kbhit());
  60.         c=getch();
  61.         if(c==0 || c==224){
  62.             c=getch();
  63.             if(x>1 && c==75)
  64.                 setCell(BEAT_SIZE-1+x--,ROWS-2,' ');
  65.             if(x < COLS-BEAT_SIZE-1 && c==77)
  66.                 setCell(x++,ROWS-2,' ');
  67.         }
  68.     } while (c != 'q' && c != 27);
  69.     PT_END(pt);
  70. }
  71.  
  72. static int pauseThread(struct pt *pt, unsigned int time) {
  73.     static unsigned int start;
  74.     PT_BEGIN(pt);
  75.     start = GetTickCount();
  76.     PT_WAIT_UNTIL(pt,GetTickCount()>start+time);
  77.     PT_END(pt);
  78. }
  79. static int ballThread(struct pt *pt) {
  80.     static int y=10,x=6;
  81.     static int dx=-1,dy=-1;
  82.     static struct pt pausePt;
  83.     PT_BEGIN(pt);
  84.     do{
  85.         do{
  86.             if(field[y+dy][x]!=' '){
  87.                 if(field[y+dy][x] =='#')setCell(x,y+dy,' ');
  88.                 dy=-dy;
  89.             }
  90.             if(field[y][x+dx]!=' '){
  91.                 if(field[y][x+dx] == '#')setCell(x+dx,y,' ');
  92.                 dx=-dx;
  93.             }
  94.             if(field[y+dy][x+dx]!=' '){
  95.                 if(field[y+dy][x+dx] =='#')setCell(x+dx,y+dy,' ');
  96.                 dy=-dy,dx=-dx;
  97.             }
  98.         } while (field[y+dy][x+dx] != ' ');
  99.         setCell(x,y,' ');
  100.         x+=dx;y+=dy;
  101.         setCell(x,y,'*');
  102.         PT_SPAWN(pt,&pausePt,pauseThread(&pausePt,1000/SPEED));
  103.     } while (y<(ROWS-2));
  104.     PT_END(pt);
  105. }
  106.  
  107. int main(void)
  108. {
  109.     struct pt fieldPt, ctrlPt, ballPt;
  110.     PT_INIT(&fieldPt);
  111.     PT_INIT(&ballPt);
  112.     PT_INIT(&ctrlPt);
  113.     while( PT_SCHEDULE(printField(&fieldPt)) && PT_SCHEDULE(controlThr(&ctrlPt)) && PT_SCHEDULE(ballThread(&ballPt)) )
  114.         Sleep(10);
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement