-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGameLevel3.cpp
211 lines (199 loc) · 5.35 KB
/
GameLevel3.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "GameLevel3.h"
GameLevel3::GameLevel3()
{
}
GameLevel3::~GameLevel3()
{
}
void GameLevel3::Init()
{
srand((unsigned int)time(NULL));
levelName = L"关卡 1 - 3";
bg_img.LoadImageFile(L"res\\images\\interface\\background1.jpg");
shadow_img.LoadImageFile(L"res\\images\\interface\\plantshadow32.png");
Sunlight_img.LoadImageFile(L"res\\images\\interface\\SunBack.png");
Sun.LoadImageFile(L"res\\images\\interface\\Sun.gif");
InitScene(0, 0, bg_img.GetImageWidth(), bg_img.GetImageHeight(), WIN_WIDTH, WIN_HEIGHT);
winImage.LoadImageFile(L"res\\images\\interface\\trophy.png");
loseImage.LoadImageFile(L"res\\images\\interface\\ZombiesWon.png");
gameFlag = 1; // 游戏运行中
sunlight = 300; //初始阳光值
//植物
PlantInit();
BullentInit();
PlantChang();
ZomArrayInit();
//过场动画
CutsceneInit();
//植物卡
CardInit();
//小推车
CarInit();
//僵尸
ZombiesInit();
//进度条
ProgressBarInit();
}
//void GameLevel3::AddZombies()
//{
//}
void GameLevel3::ZomArrayInit()
{
ZOMBIES_ARRAY zombies_array;
zombies_array.zombiesindex = 0;
zombies_array.frame = 1000;
zombiesArray.push_back(zombies_array);
for (int i = 1; i < MAXZOMBIESNUM / 3; i++) {
zombies_array.zombiesindex = i % MAXZOMBIEARRAYSUN;
zombies_array.frame = 1000 + (2 * i + 1) * 300;
zombiesArray.push_back(zombies_array);
}
for (int i = MAXZOMBIESNUM / 3; i < MAXZOMBIESNUM * 2 / 3; i++)
{
zombies_array.zombiesindex = i % MAXZOMBIEARRAYSUN;
zombies_array.frame = 1000 + (2 * MAXZOMBIESNUM / 3 + 1) * 300 + (i - MAXZOMBIESNUM / 3) * 500;
zombiesArray.push_back(zombies_array);
}
for (int i = MAXZOMBIESNUM * 2 / 3; i < MAXZOMBIESNUM; i++)
{
zombies_array.zombiesindex = i % MAXZOMBIEARRAYSUN;
zombies_array.frame = 1000 + (2 * MAXZOMBIESNUM / 3 + 1) * 300 + (MAXZOMBIESNUM / 3) * 500 + 600 + i * 150;
zombiesArray.push_back(zombies_array);
}
ZOMENTER_MAXFRAME = zombiesArray.at(MAXZOMBIESNUM - 1).frame;
}
void GameLevel3::AddZombies() {
SPRITEINFO info;
info.Active = true;
info.Dead = false;
info.Dir = DIR_LEFT;
info.Alpha = 255;
info.Score = 0;
info.Visible = true;
info.Level = 1;
info.Ratio = 1;
info.Rotation = TRANS_NONE;
info.Speed = 4;
ZOMBIES_INFO zombie_info;
/*判断当前帧是否需要新加入僵尸*/
for (int i = 0; i < zombiesArray.size(); i++)
{
if (frameCount == zombiesArray.at(i).frame) {
zombie_info.sprite = spriteZombie[zombiesArray.at(i).zombiesindex % 5];
zombie_info.row = INDEXROW + rand() % 4;
zombie_info.x = WIN_WIDTH; //僵尸横坐标
zombie_info.count = 0;
zombie_info.isChanged = false;
zombie_info.typeNum = zombiesArray.at(i).zombiesindex % 5;
info.X = zombie_info.x;
info.Y = (zombie_info.row) * PlantHeight + 10; //僵尸纵坐标
zombie_info.info = info;
zombie_info.frame = 110;
zombiesVector.push_back(zombie_info);
zombiesVector.back().sprite->Initiate(info);
zomEnter_buffer.Play(false);
}
}
}
void GameLevel3::Draw(HDC hdc)
{
trueFrame++;
if (frameCount < MaxFrameCount - 1)
{
frameCount++;
}
if (cutsceneFlag)
{
DrawCutscene(hdc);
}
else
{
//画背景
bg_img.PaintRegion(bg_img.GetBmpHandle(), hdc, 0, 0, 120, 0, WinWidth, WinHeight, 1);
//小推车
DrawCar(hdc);
//画植物
DrawPlant(hdc);
//画僵尸
DrawZombies(hdc);
//画子弹
DrawBullet(hdc);
//画植物卡
DrawCard(hdc);
if (pointState)
{
//画点击后的植物图
DrawClickPlant(hdc, mousex, mousey);
}
//画阳光面板
DrawSunLight(hdc);
//画进度条
DrawProgressBar(hdc);
if (gameFlag == 2) {
winImage.PaintImage(hdc, (WIN_WIDTH - winImage.GetImageWidth()) / 2, (WIN_HEIGHT - winImage.GetImageHeight()) / 2, winImage.GetImageWidth(), winImage.GetImageHeight(), 255);
}
if (gameFlag == 0)
{
loseImage.PaintImage(hdc, (WIN_WIDTH - loseImage.GetImageWidth()) / 2, (WIN_HEIGHT - loseImage.GetImageHeight()) / 2, loseImage.GetImageWidth(), loseImage.GetImageHeight(), 255);
}
}
}
void GameLevel3::MouseClick(int x, int y)
{
if (!pointState)
{
int card = CardMouseClick(x, y);
if (card != -1 && plantCard[card].state == true && sunlight >= plantCard[card].info.sunlight)
{
pointState = true;
pointPlant = card;
}
}
else
{
PlantMouseClick(x, y);
pointState = false;
}
sunlightMouseClick(x, y);
}
void GameLevel3::PlantMouseClick(int x, int y)
{
if (x < CarXSpace + CarWidth || y < YSpace + PlantHeight*INDEXROW || x > CarXSpace + CarWidth + PlantWidth*MAXCOLUMN || y>PlantHeight*MAXROW + YSpace + PlantHeight*(INDEXROW + 4))
{
return;
}
PLANT_INFO tempPlant;
x = x - CarXSpace - CarWidth;
y = y - YSpace;
tempPlant.position.X = x / PlantWidth;
tempPlant.position.Y = y / PlantHeight;
for (int i = 0; i < plantVector.size(); i++)
{
if (plantVector.at(i).position.X == tempPlant.position.X&&plantVector.at(i).position.Y == tempPlant.position.Y)
{
return;
}
}
click_buffer.Play(false);
tempPlant.sprite = spritePlant[pointPlant];
tempPlant.pointNum = pointPlant;
SPRITEINFO info;
info.Active = true;
info.Dead = false;
info.Dir = DIR_RIGHT;
info.Rotation = TRANS_NONE;
info.Ratio = 1.0f;
info.Level = 0;
info.Score = 0;
info.Speed = 0;
info.X = tempPlant.position.X*PlantWidth + CarXSpace + CarWidth + 5;
info.Y = tempPlant.position.Y*PlantHeight + YSpace + 10;
info.Alpha = 255;
info.Visible = true;
tempPlant.info = info;
tempPlant.life = 50;
tempPlant.attacked = false;
plantVector.push_back(tempPlant);
sunlight = sunlight - plantCard[pointPlant].info.sunlight;
plantCard[pointPlant].nowTime = 0;
}