This repository was archived by the owner on Nov 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
89 lines (80 loc) · 2.57 KB
/
game.cpp
File metadata and controls
89 lines (80 loc) · 2.57 KB
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
//
// Created by alane on 04.01.2018.
//
#include <cstdio>
#include <cstring>
#include "game.h"
#include "gameProperties.h"
#include "gameDraw.h"
#include "stdafx.h"
#include "map.h"
#include "helpers.h"
#include "ai.h"
#include "gameStates.h"
#include "input.h"
long long lastTurnTime = 0;
int startingUp = true;
void initialSetup() {
gSnakes = getDynamicSizedSnakeArray(gPlayerCount + gEnemyCount);
setupDraw();
setRandomPositionForSnakes(gSnakes, gPlayerCount + gEnemyCount);
}
void putReward() {
do {
gCurrRewardPos[0] = getRandomPos();
gCurrRewardPos[1] = getRandomPos();
} while(isCollidingPoint(
gCurrRewardPos[0],
gCurrRewardPos[1],
-1,
false
));
drawReward(gCurrRewardPos[0], gCurrRewardPos[1]);
}
void finishGame() {
isGameStarted = false;
isGameOverScreenShowing = true;
}
void drawGameScreen(char *input) {
if (strlen(input) > 0) {
Direction newDirection = convertInputToNewDirection(input);
if(newDirection != NONE) {
Snake *playerSnake = &gSnakes[getPlayerIndexByInput(input)];
int isColliding = isCollidingDirections((*playerSnake).currDirection, newDirection);
if(!isColliding) (*playerSnake).currDirection = newDirection;
}
}
if(timeMs() - lastTurnTime >= gTurnTimeMs) {
if (startingUp) {
initialSetup();
putReward();
initialDraw(gSnakes);
startingUp = false;
} else {
int onlyHadInactivePlayers = true;
int hadPlayer = false;
setEnemyDirections();
for(int a = 0; a < gPlayerCount + gEnemyCount; a++) {
int snakeColor = 0;
if(gSnakes[a].isPlayer && !hadPlayer) {
snakeColor = RED;
hadPlayer = true;
}
else if(gSnakes[a].isPlayer && hadPlayer) snakeColor = GREEN;
else snakeColor = WHITE;
if(gSnakes[a].active) {
SnakeMoveResult moveResult = moveSnake(&gSnakes[a], snakeColor, gCurrRewardPos);
if(moveResult == GOT_REWARD) putReward();
if(moveResult == HIT_WALL) gSnakes[a].active = false;
if(gSnakes[a].isPlayer) onlyHadInactivePlayers = false;
}
}
checkSnakeCollisions();
if(onlyHadInactivePlayers && gPlayerCount > 0) {
startingUp = true;
finishGame();
}
}
lastTurnTime = timeMs();
}
}