-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayArea.cpp
148 lines (138 loc) · 3.06 KB
/
PlayArea.cpp
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "PlayArea.h"
PlayArea::PlayArea() : hasBegun(false), isPlay(false), hasPlayerWon(false), gameOver(false)
{
setImmediateDrawMode(false);
}
PlayArea::~PlayArea() {
delete startButton;
delete hitButton;
delete standButton;
}
void PlayArea::onDraw()
{
if (!gameOver) {
clearScreen(DARK_GREEN);
setPenColour(BLACK, 1);
setTextColour(BLACK);
setBackColour(WHITE);
startButton->draw(this);
drawBitmap(L"Assets/pack.bmp", 300, 100, 44 * 3, CARD_HEIGHT * 3, 0x00FF00FF);
if (isPlay) {//after the start button has been pressed isplay=true
midGameSequence();
}
else if (hasBegun)
{
(&deck)->playCard(2);//plays cards for casino and player 2 times
midGameSequence();
}
}
EasyGraphics::onDraw();
}
void PlayArea::midGameSequence()
{
clearScreen(DARK_GREEN);
drawBitmap(L"Assets/pack.bmp", 300, 100, 44 * 3, 47 * 3, 0x00FF00FF);
(&deck)->drawCards(this);//draw cards for casino and player
hitButton->draw(this);//draw buttons
standButton->draw(this);
}
void PlayArea::hitGameSequence()
{
(&deck)->playCard(true);//true=player, false=casino
onDraw();
int playerTotal = (&deck)->cardsTotal((&deck)->playersCards);
int casinoTotal = (&deck)->cardsTotal((&deck)->casinosCards);
if (playerTotal == 21) {
//check casino
if (casinoTotal == 21){
//casino wins
hasPlayerWon = false;
}
else if(casinoTotal < 21){
//player wins
hasPlayerWon = true;
}
gameOver = true;
endGameSequence();
}
else if (playerTotal > 21)
{
hasPlayerWon = false;
gameOver = true;
endGameSequence();
}
}
void PlayArea::standGameSequence() {
int casinoTotal = (&deck)->cardsTotal((&deck)->casinosCards);
int playerTotal = (&deck)->cardsTotal((&deck)->playersCards);
if (playerTotal == 21) {
//check casino
if (casinoTotal == 21) {
//casino wins
hasPlayerWon = false;
}
else {
//player wins
hasPlayerWon = true;
}
gameOver = true;
endGameSequence();
}
else {
while (casinoTotal < playerTotal)
{
(&deck)->playCard(false);
casinoTotal = (&deck)->cardsTotal((&deck)->casinosCards);
Sleep(1000);//slight delay just to make it look more natural
onDraw();
}
if (casinoTotal > 21)
{
hasPlayerWon = true;
gameOver = true;
}
else {
hasPlayerWon = false;
gameOver = true;
}
endGameSequence();
}
}
void PlayArea::endGameSequence()
{
clearScreen(DARK_GREEN);
if (hasPlayerWon)
{
drawBitmap(L"Assets/win.bmp", 200, 200, 660, 480, 0x00FF00FF);// /2-66 is for centering the image
}
else
{
drawBitmap(L"Assets/lose.bmp", 200, 200, 720, 600, 0x00FF00FF);
}
Sleep(2000);
onDraw();
}
void PlayArea::onLButtonDown(UINT nFlags, int x, int y)//dynamic polymorphism
{
if (hasBegun) {
if (hitButton->hitTest(x, y))
{
isPlay = true;
hitGameSequence();
onDraw();
}
if(standButton->hitTest(x,y))
{
isPlay = true;
standGameSequence();
onDraw();
}
}
else {
if (startButton->hitTest(x, y))
{
hasBegun = true;
onDraw();
}
}
}