Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <conio.h>
- #include <windows.h>
- #include "pt.h"
- #define COLS 30
- #define ROWS 21
- #define BRICK_ROWS 5
- #define BEAT_SIZE 10
- #define SPEED 10
- char field[ROWS][COLS];
- char fieldChanged;
- static void setCell(int x,int y, char value){
- if(field[y][x] != value){
- field[y][x] = value;
- fieldChanged = 1;
- }
- }
- static int printField(struct pt *pt) {
- int x,y;
- static int bricks;
- static HANDLE console;
- PT_BEGIN(pt);
- field[0][0]=field[0][COLS-1]=field[ROWS-1][0]=field[ROWS-1][COLS-1]='+';
- for(y=1;y<COLS-1;y++) field[ROWS-1][y]=field[0][y]='-';
- for(y=1;y<ROWS-1;y++) {
- field[y][0]=field[y][COLS-1]='|';
- for(x=1;x<COLS-1;x++)
- if(y<=BRICK_ROWS)field[y][x]='#';
- else field[y][x]=' ';
- }
- fieldChanged = 1;
- console = GetStdHandle(STD_OUTPUT_HANDLE);
- do{
- PT_WAIT_UNTIL(pt,fieldChanged);
- bricks = 0;
- for(y=0;y<ROWS;y++){
- COORD position = { 0, y };
- SetConsoleCursorPosition( console, position );
- WriteConsoleA(console,&field[y][0],COLS,NULL,NULL);
- for(x=0;x<COLS;x++)
- if( field[y][x] == '#') bricks++;
- }
- fieldChanged=0;
- }while(bricks);
- PT_END(pt);
- }
- static int controlThr(struct pt *pt) {
- int c;
- static int x=1;
- PT_BEGIN(pt);
- do{
- for(c=x;c<x+BEAT_SIZE;c++)
- setCell(c,ROWS-2,'=');
- PT_WAIT_UNTIL(pt,kbhit());
- c=getch();
- if(c==0 || c==224){
- c=getch();
- if(x>1 && c==75)
- setCell(BEAT_SIZE-1+x--,ROWS-2,' ');
- if(x < COLS-BEAT_SIZE-1 && c==77)
- setCell(x++,ROWS-2,' ');
- }
- } while (c != 'q' && c != 27);
- PT_END(pt);
- }
- static int pauseThread(struct pt *pt, unsigned int time) {
- static unsigned int start;
- PT_BEGIN(pt);
- start = GetTickCount();
- PT_WAIT_UNTIL(pt,GetTickCount()>start+time);
- PT_END(pt);
- }
- static int ballThread(struct pt *pt) {
- static int y=10,x=6;
- static int dx=-1,dy=-1;
- static struct pt pausePt;
- PT_BEGIN(pt);
- do{
- do{
- if(field[y+dy][x]!=' '){
- if(field[y+dy][x] =='#')setCell(x,y+dy,' ');
- dy=-dy;
- }
- if(field[y][x+dx]!=' '){
- if(field[y][x+dx] == '#')setCell(x+dx,y,' ');
- dx=-dx;
- }
- if(field[y+dy][x+dx]!=' '){
- if(field[y+dy][x+dx] =='#')setCell(x+dx,y+dy,' ');
- dy=-dy,dx=-dx;
- }
- } while (field[y+dy][x+dx] != ' ');
- setCell(x,y,' ');
- x+=dx;y+=dy;
- setCell(x,y,'*');
- PT_SPAWN(pt,&pausePt,pauseThread(&pausePt,1000/SPEED));
- } while (y<(ROWS-2));
- PT_END(pt);
- }
- int main(void)
- {
- struct pt fieldPt, ctrlPt, ballPt;
- PT_INIT(&fieldPt);
- PT_INIT(&ballPt);
- PT_INIT(&ctrlPt);
- while( PT_SCHEDULE(printField(&fieldPt)) && PT_SCHEDULE(controlThr(&ctrlPt)) && PT_SCHEDULE(ballThread(&ballPt)) )
- Sleep(10);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement