-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.c
85 lines (73 loc) · 2.5 KB
/
game.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "game.h"
int getRand(int a, int b)
{
return rand() % (b - a + 1) + a;
}
void initGame( Game *theGame )
{
srand(time(NULL));
theGame->mPiece = getRand(0,6);
theGame->mRot = getRand(0, 3);
int pX, pY;
getStartPos(theGame->mPiece, theGame->mRot, &pX, &pY);
theGame->mXloc = (BOARD_WIDTH / 2) + pX;
theGame->mYloc = pY;
theGame->nextP = getRand(0, 6);
theGame->nextRot = getRand(0,3);
theGame->nextX = BOARD_WIDTH + 5;
theGame->nextY = 5;
}
void newPiece(Game *theGame)
{
theGame->mPiece = theGame->nextP;
theGame->mRot = theGame->nextRot;
int pX, pY;
getStartPos(theGame->mPiece, theGame->mRot, &pX, &pY);
theGame->mXloc = (BOARD_WIDTH / 2) + pX;
theGame->mYloc = pY;
theGame->nextP = getRand(0, 6);
theGame->nextRot = getRand(0,3);
}
void drawPiece(int x, int y, int thePiece, int pRot)
{
color_t pColor;
int newX, newY;
getPiecePosition( x, y, &newX, &newY );
for (int i = 0; i < PIECE_BLOCKS; i++){
for (int j = 0; j < PIECE_BLOCKS; j++){
int cTest = getBlockType(thePiece, pRot, j, i);
if (cTest == 1) pColor = color_from_name("orange");
else if (cTest == 2) pColor = color_from_name("blue");
if (cTest != 0){
terminal_color(pColor);
terminal_put(newX + i, newY + j, 0x2588);
}
}
}
}
void drawBoard(Board *theBoard, Game *theGame )
{
int mx1 = BOARD_POSITION - (BLOCK_SIZE * (BOARD_WIDTH / 2));
int mx2 = BOARD_POSITION + (BLOCK_SIZE * (BOARD_WIDTH / 2));
int my = SCREEN_HEIGHT - (BLOCK_SIZE * BOARD_HEIGHT);
drawRectangle(mx1 - BOARD_LINE_WIDTH, my, mx1, SCREEN_HEIGHT - 1, "blue");
drawRectangle(mx2, my, mx2 + BOARD_LINE_WIDTH, SCREEN_HEIGHT - 1, "blue");
for(int i = BOARD_POSITION - (BOARD_WIDTH / 2) - 1; i < BOARD_POSITION + (BOARD_WIDTH / 2)+1; i++){
terminal_color(color_from_name("blue"));
terminal_put(i, 24, 0x2588);
}
//mx1 += 1;
for (int i = 0; i < BOARD_WIDTH; i++){
for(int j = 0; j < BOARD_HEIGHT; j++){
if (!isFreeBlock(theBoard, i, j)){
terminal_color(color_from_name("red"));
terminal_put(mx1 + i, my+ j-1, 0x2588);
}
}
}
}
void drawScene(Board *theBoard, Game *theGame){
drawBoard(theBoard, theGame);
drawPiece(theGame->mXloc, theGame->mYloc, theGame->mPiece, theGame->mRot);
drawPiece(theGame->nextX, theGame->nextY, theGame->nextP, theGame->nextRot);
}