Преглед на файлове

Junior programmers bots

Pabloader преди 5 години
родител
ревизия
f6c7b0e724
променени са 9 файла, в които са добавени 245 реда и са изтрити 150 реда
  1. 1
    1
      Makefile
  2. 5
    2
      include/debuggers.h
  3. 8
    2
      include/entity.h
  4. 3
    13
      include/player.h
  5. 31
    0
      include/programmer.h
  6. 51
    29
      src/debuggers.cpp
  7. 1
    0
      src/entity.cpp
  8. 7
    103
      src/player.cpp
  9. 138
    0
      src/programmer.cpp

+ 1
- 1
Makefile Целия файл

@@ -19,7 +19,7 @@ else
19 19
 	CPP=g++
20 20
 endif
21 21
 ifeq ($(DEBUG),1)
22
-	CPPFLAGS+=-g -Og -D_DEBUG
22
+	CPPFLAGS+=-g -O0 -D_DEBUG
23 23
 else
24 24
 	CPPFLAGS+=-flto -O3
25 25
 endif

+ 5
- 2
include/debuggers.h Целия файл

@@ -4,6 +4,7 @@
4 4
 #include "bug.h"
5 5
 #include "olcPGEX_Sound.h"
6 6
 #include "olcPixelGameEngine.h"
7
+#include "programmer.h"
7 8
 #include "player.h"
8 9
 #include <vector>
9 10
 #include <string>
@@ -11,8 +12,7 @@
11 12
 namespace pabloader {
12 13
 class Debuggers : public olc::PixelGameEngine {
13 14
 private:
14
-    Player* player;
15
-    std::vector<Bug*> bugs;
15
+    std::vector<Programmer*> programmers;
16 16
     uint32_t bugsCatched = 0;
17 17
     uint32_t bugsMissed = 0;
18 18
 
@@ -33,11 +33,14 @@ public:
33 33
     int GameScreenHeight() { return ScreenHeight() - linesOfCode * 10; };
34 34
 
35 35
 private:
36
+    void SpawnProgrammer();
36 37
     void SpawnBug();
37 38
     Bug* CreateBug();
38 39
     bool GameOver();
39 40
 
40 41
 public:
42
+    std::vector<Bug*> bugs;
43
+
41 44
     olc::ResourcePack pack;
42 45
     olc::Sprite playerSprite;
43 46
     olc::Sprite enemiesSprite;

+ 8
- 2
include/entity.h Целия файл

@@ -3,8 +3,9 @@
3 3
 
4 4
 #include "olcPGEX_Graphics2D.h"
5 5
 #include "olcPixelGameEngine.h"
6
+#include <cmath>
6 7
 
7
-namespace pabloader {    
8
+namespace pabloader {
8 9
 class Debuggers;
9 10
 
10 11
 class Entity {
@@ -18,10 +19,15 @@ protected:
18 19
 public:
19 20
     Entity(Debuggers* game_);
20 21
     virtual ~Entity(){};
21
-    
22
+
22 23
     virtual void Update(float dt) = 0;
23 24
     virtual void Draw() = 0;
25
+
26
+    float GetX() { return x; }
27
+    float GetY() { return y; }
28
+
24 29
     bool Collides(Entity* entity);
30
+    float Dist(Entity* entity) { return std::hypot(x - entity->x, y - entity->y); }
25 31
 };
26 32
 }
27 33
 

+ 3
- 13
include/player.h Целия файл

@@ -1,25 +1,15 @@
1 1
 #ifndef PLAYER_H
2 2
 #define PLAYER_H
3 3
 
4
-#include "entity.h"
5 4
 #include "olcPGE_Common.h"
5
+#include "programmer.h"
6 6
 
7 7
 namespace pabloader {
8 8
 
9
-class Player : public Entity {
10
-private:
11
-    uint8_t tile = 0;
12
-    uint8_t skin = 0;
13
-    uint32_t tileCounter = 0;
14
-    bool onGround = false;
15
-
9
+class Player : public Programmer {
16 10
 public:
17 11
     Player(Debuggers* game);
18
-    void Update(float dt) override;
19
-    void Draw() override;
20
-    bool IsOnGround() { return onGround; }
21
-    uint8_t GetSkin() { return skin; }
22
-    void UpgradeLevel() { skin = std::min(skin + 1, 3); }
12
+    void Think() override;
23 13
 };
24 14
 }
25 15
 

+ 31
- 0
include/programmer.h Целия файл

@@ -0,0 +1,31 @@
1
+#ifndef PROGRAMMER_H
2
+#define PROGRAMMER_H
3
+
4
+#include "entity.h"
5
+#include "olcPGE_Common.h"
6
+
7
+namespace pabloader {
8
+
9
+class Programmer : public Entity {
10
+protected:
11
+    uint8_t tile = 0;
12
+    uint32_t tileCounter = 0;
13
+    uint8_t skin = 0;
14
+    bool onGround = false;
15
+    bool doJump = false;
16
+    bool walkLeft = false;
17
+    bool walkRight = false;
18
+    bool moveDown = false;
19
+
20
+public:
21
+    Programmer(Debuggers* game);
22
+    virtual void Think();
23
+    virtual void Update(float dt) override;
24
+    virtual void Draw() override;
25
+    bool IsOnGround() { return onGround; }
26
+    uint8_t GetSkin() { return skin; }
27
+    void UpgradeLevel() { skin = std::min(skin + 1, 2); }
28
+};
29
+}
30
+
31
+#endif

+ 51
- 29
src/debuggers.cpp Целия файл

@@ -1,6 +1,8 @@
1 1
 #include "debuggers.h"
2 2
 #include <cmath>
3 3
 #include <iostream>
4
+#include <random>
5
+#include <ctime>
4 6
 
5 7
 namespace pabloader {
6 8
 std::string& rtrim(std::string& str, const std::string& chars = "\t\n\v\f\r ")
@@ -12,6 +14,7 @@ std::string& rtrim(std::string& str, const std::string& chars = "\t\n\v\f\r ")
12 14
 bool Debuggers::OnUserCreate()
13 15
 {
14 16
     SetPixelMode(olc::Pixel::MASK);
17
+    std::srand(std::time(0));
15 18
 
16 19
     if (!olc::SOUND::InitialiseAudio()) {
17 20
         std::cerr << "Cannot init audio" << std::endl;
@@ -88,7 +91,9 @@ bool Debuggers::OnUserCreate()
88 91
 
89 92
     // Creating entities
90 93
 
91
-    player = new Player(this);
94
+    auto player = new Player(this);
95
+    programmers.push_back(player);
96
+
92 97
     return true;
93 98
 }
94 99
 
@@ -103,32 +108,19 @@ bool Debuggers::OnUserUpdate(float fElapsedTime)
103 108
 
104 109
     DrawSprite(0, 0, &backgroundSprite);
105 110
 
106
-    DrawString(1, 1, "A,S,D - move; SPACE - jump", olc::CYAN);
107
-    DrawString(1, 10, "Don't let the bugs destroy your code!", olc::YELLOW);
108
-
109
-    player->Update(fElapsedTime);
110
-    player->Draw();
111
-
112 111
     uint32_t bugsSize = bugs.size();
113 112
 
114
-    if (bugsSize == 0 && player->IsOnGround()) {
115
-        SpawnBug();
116
-    }
117
-
118
-    if (bugsCatched >= bugsSize * 25 && player->GetSkin() < bugsSize) {
119
-        player->UpgradeLevel();
120
-        SpawnBug();
121
-    }
122
-
123 113
     for (auto bug : bugs) {
124 114
         if (bug->IsActive()) {
125 115
             bug->Update(fElapsedTime);
126 116
             bug->Draw();
127
-            if (player->Collides(bug)) {
128
-                bugsCatched++;
129
-                bug->Kill();
130
-                olc::SOUND::PlaySample(bugCatchSample);
131
-                SpawnBug();
117
+            for (auto& programmer : programmers) {
118
+                if (programmer->Collides(bug)) {
119
+                    bugsCatched++;
120
+                    bug->Kill();
121
+                    olc::SOUND::PlaySample(bugCatchSample);
122
+                    SpawnBug();
123
+                }
132 124
             }
133 125
         } else {
134 126
             bugsMissed++;
@@ -139,21 +131,45 @@ bool Debuggers::OnUserUpdate(float fElapsedTime)
139 131
         }
140 132
     }
141 133
 
142
-    linesOfCode = (sourceCode.size() * 4 / totalLines)  +1;
134
+    linesOfCode = (sourceCode.size() * 4 / totalLines) + 1;
135
+
136
+    for (auto it = programmers.rbegin(); it != programmers.rend(); it++) {
137
+        auto& programmer = *it;
138
+        programmer->Think();
139
+        programmer->Update(fElapsedTime);
140
+        programmer->Draw();
141
+    }
142
+
143
+    Player* player = dynamic_cast<Player*>(programmers.at(0));
144
+
145
+    if (bugsSize == 0 && player->IsOnGround()) {
146
+        SpawnBug();
147
+    }
148
+
149
+    if (bugsCatched >= bugsSize * (15 * programmers.size()) && player->GetSkin() < bugsSize) {
150
+        player->UpgradeLevel();
151
+        SpawnBug();
152
+        SpawnProgrammer();
153
+    }
143 154
 
144 155
 #ifdef _DEBUG
145 156
     DrawString(1, 37, "Bugs: " + std::to_string(bugs.size()));
146 157
     if (GetKey(olc::B).bPressed) {
147 158
         SpawnBug();
148 159
     }
160
+    if (GetKey(olc::P).bPressed) {
161
+        SpawnProgrammer();
162
+    }
149 163
 #endif
164
+    DrawString(1, 1, "A,S,D - move; SPACE - jump", olc::CYAN);
165
+    DrawString(1, 10, "Don't let the bugs destroy your code!", olc::YELLOW);
150 166
     DrawString(1, 19, "Bugs catched: " + std::to_string(bugsCatched));
151 167
     DrawString(1, 28, "Bugs missed: " + std::to_string(bugsMissed));
152 168
 
153 169
     int y = GameScreenHeight();
154 170
     FillRect(0, y - 1, ScreenWidth(), ScreenHeight() - y + 1, olc::VERY_DARK_GREY);
155 171
 
156
-    for (auto line : sourceCode) {
172
+    for (auto& line : sourceCode) {
157 173
         DrawString(1, y, line);
158 174
         y += 10;
159 175
         if (y >= ScreenHeight())
@@ -174,18 +190,18 @@ bool Debuggers::GameOver()
174 190
     return true;
175 191
 }
176 192
 
177
-bool Debuggers::OnUserDestroy()
178
-{
179
-    olc::SOUND::DestroyAudio();
180
-    return true;
181
-}
182
-
183 193
 void Debuggers::SpawnBug()
184 194
 {
185 195
     auto bug = CreateBug();
186 196
     bug->ResetPosition();
187 197
 }
188 198
 
199
+void Debuggers::SpawnProgrammer()
200
+{
201
+    auto programmer = new Programmer(this);
202
+    programmers.push_back(programmer);
203
+}
204
+
189 205
 Bug* Debuggers::CreateBug()
190 206
 {
191 207
     for (auto bug : bugs) {
@@ -196,4 +212,10 @@ Bug* Debuggers::CreateBug()
196 212
     bugs.push_back(bug);
197 213
     return bug;
198 214
 }
215
+
216
+bool Debuggers::OnUserDestroy()
217
+{
218
+    olc::SOUND::DestroyAudio();
219
+    return true;
220
+}
199 221
 }

+ 1
- 0
src/entity.cpp Целия файл

@@ -14,4 +14,5 @@ bool Entity::Collides(Entity* entity)
14 14
     bool collidesY = (y - h / 2 < entity->y + entity->h / 2) && (entity->y - entity->h / 2 < y + h / 2);
15 15
     return collidesX && collidesY;
16 16
 }
17
+
17 18
 }

+ 7
- 103
src/player.cpp Целия файл

@@ -4,31 +4,17 @@
4 4
 #include <iostream>
5 5
 
6 6
 namespace pabloader {
7
-Player::Player(Debuggers* game_)
8
-    : Entity(game_)
7
+Player::Player(Debuggers* game)
8
+    : Programmer(game)
9 9
 {
10
-    w = game->playerSprite.width / 4;
11
-    h = game->playerSprite.height / 4;
12
-    x = (game->ScreenWidth() - w) / 2;
13
-    y = game->GameScreenHeight() / 2;
14 10
 }
15 11
 
16
-void Player::Update(float dt)
12
+void Player::Think()
17 13
 {
18
-    x += xv * dt;
19
-    y += yv * dt;
20
-
21
-    xv *= 1 - 0.2 * dt;
22
-
23
-    if (!onGround) {
24
-        yv += (70 + skin * 20) * dt;
25
-        yv *= 1 - 0.05 * dt;
26
-    } else {
27
-        xv *= 1 - 0.5 * dt;
28
-    }
29
-
30
-    transform.Reset();
31
-
14
+    doJump = game->GetKey(olc::SPACE).bHeld;
15
+    walkLeft = game->GetKey(olc::A).bHeld;
16
+    walkRight = game->GetKey(olc::D).bHeld;
17
+    moveDown = game->GetKey(olc::S).bHeld;
32 18
 #ifdef _DEBUG
33 19
     // game->DrawString(1, 19, std::to_string(x) + " " + std::to_string(y));
34 20
     // game->DrawString(1, 28, std::to_string(xv) + " " + std::to_string(yv));
@@ -36,88 +22,6 @@ void Player::Update(float dt)
36 22
     if (game->GetKey(olc::J).bPressed) {
37 23
         skin = (skin + 1) % 4;
38 24
     }
39
-#endif
40
-    bool userInput = false;
41
-
42
-    if (game->GetKey(olc::SPACE).bHeld && onGround) {
43
-        yv = -100;
44
-        userInput = true;
45
-    }
46
-
47
-    if (game->GetKey(olc::A).bHeld) {
48
-        xv -= (100 + skin * 50) * dt;
49
-        userInput = true;
50
-    }
51
-
52
-    if (game->GetKey(olc::D).bHeld) {
53
-        xv += (100 + skin * 50) * dt;
54
-        userInput = true;
55
-    }
56
-
57
-    if (game->GetKey(olc::S).bHeld) {
58
-        if (!onGround) {
59
-            yv += (100 + skin * 50) * dt;
60
-        }
61
-        userInput = true;
62
-    }
63
-
64
-    if (x > game->ScreenWidth() - w / 2) {
65
-        xv *= -0.3;
66
-        x = game->ScreenWidth() - w / 2;
67
-        olc::SOUND::PlaySample(game->playerFallSample);
68
-    }
69
-
70
-    if (x < w / 2) {
71
-        xv *= -0.3;
72
-        x = w / 2;
73
-        olc::SOUND::PlaySample(game->playerFallSample);
74
-    }
75
-
76
-    if (y > game->GameScreenHeight() - h / 2) {
77
-        yv *= -0.3;
78
-
79
-        if (std::abs(yv) < 2) {
80
-            yv = 0;
81
-        }
82
-        y = game->GameScreenHeight() - h / 2;
83
-    }
84
-
85
-    bool newOnGround = y >= game->GameScreenHeight() - h / 2;
86
-    if (newOnGround && !onGround) {
87
-        olc::SOUND::PlaySample(game->playerFallSample);
88
-    }
89
-    onGround = newOnGround;
90
-
91
-    if (onGround) {
92
-        tile = 2;
93
-        tileCounter = static_cast<uint32_t>(0.1 / dt);
94
-    } else if (tileCounter <= 0) {
95
-        tile = 0;
96
-    } else {
97
-        tileCounter--;
98
-    }
99
-
100
-    if (!userInput && std::abs(xv) < 2) {
101
-        xv = 0;
102
-    }
103
-    if (xv < 0) {
104
-        transform.Scale(-1, 1);
105
-    }
106
-    transform.Translate(x, y);
107
-}
108
-
109
-void Player::Draw()
110
-{
111
-    int xOff = xv < 0 ? -1 : 0;
112
-    if (std::abs(xv) < 1) {
113
-        olc::GFX2D::DrawPartialSprite(&game->playerSprite, skin * w + xOff, h + tile * h, w, h, transform, true);
114
-    } else {
115
-        olc::GFX2D::DrawPartialSprite(&game->playerSprite, skin * w + xOff, 0 + tile * h, w, h, transform, true);
116
-    }
117
-#ifdef _DEBUG
118
-    float x, y;
119
-    transform.Forward(0, 0, x, y);
120
-    game->DrawRect((int)x - w / 2, (int)y - h / 2, w, h, olc::RED);
121 25
 #endif
122 26
 }
123 27
 }

+ 138
- 0
src/programmer.cpp Целия файл

@@ -0,0 +1,138 @@
1
+#include "programmer.h"
2
+#include "debuggers.h"
3
+#include "olcPGEX_Sound.h"
4
+#include <iostream>
5
+
6
+namespace pabloader {
7
+Programmer::Programmer(Debuggers* game)
8
+    : Entity(game)
9
+{
10
+    w = game->playerSprite.width / 4;
11
+    h = game->playerSprite.height / 4;
12
+    x = (game->ScreenWidth() - w) / 2;
13
+    y = game->GameScreenHeight() / 2;
14
+}
15
+
16
+void Programmer::Think()
17
+{
18
+    Bug* closest = nullptr;
19
+    float dist = 1e100;
20
+    for (auto bug : game->bugs) {
21
+        float d = bug->Dist(this);
22
+        if (d < dist && bug->GetY() > 0) {
23
+            dist = d;
24
+            closest = bug;
25
+        }
26
+    }
27
+
28
+    if (closest != nullptr) {
29
+        doJump = closest->GetY() < y;
30
+        moveDown = closest->GetY() > y;
31
+        walkLeft = closest->GetX() < x;
32
+        walkRight = closest->GetX() > x;
33
+    } else {
34
+        doJump = false;
35
+        walkLeft = false;
36
+        walkRight = false;
37
+        moveDown = false;
38
+    }
39
+}
40
+
41
+void Programmer::Update(float dt)
42
+{
43
+    x += xv * dt;
44
+    y += yv * dt;
45
+
46
+    xv *= 1 - 0.2 * dt;
47
+
48
+    if (!onGround) {
49
+        yv += (70 + skin * 30) * dt;
50
+        yv *= 1 - 0.05 * dt;
51
+    } else {
52
+        xv *= 1 - 0.5 * dt;
53
+    }
54
+
55
+    transform.Reset();
56
+
57
+    bool userInput = false;
58
+
59
+    if (doJump && onGround) {
60
+        yv = -100 - 50 * skin;
61
+        userInput = true;
62
+    }
63
+
64
+    if (walkLeft) {
65
+        xv -= (150 + skin * 50) * dt;
66
+        userInput = true;
67
+    }
68
+
69
+    if (walkRight) {
70
+        xv += (150 + skin * 50) * dt;
71
+        userInput = true;
72
+    }
73
+
74
+    if (moveDown) {
75
+        if (!onGround) {
76
+            yv += (150 + skin * 50) * dt;
77
+        }
78
+        userInput = true;
79
+    }
80
+
81
+    if (x > game->ScreenWidth() - w / 2) {
82
+        xv *= -0.3;
83
+        x = game->ScreenWidth() - w / 2;
84
+        olc::SOUND::PlaySample(game->playerFallSample);
85
+    }
86
+
87
+    if (x < w / 2) {
88
+        xv *= -0.3;
89
+        x = w / 2;
90
+        olc::SOUND::PlaySample(game->playerFallSample);
91
+    }
92
+
93
+    if (y > game->GameScreenHeight() - h / 2) {
94
+        yv *= -0.3;
95
+
96
+        if (std::abs(yv) < 2) {
97
+            yv = 0;
98
+        }
99
+        y = game->GameScreenHeight() - h / 2;
100
+    }
101
+
102
+    bool newOnGround = y >= game->GameScreenHeight() - h / 2;
103
+    if (newOnGround && !onGround) {
104
+        olc::SOUND::PlaySample(game->playerFallSample);
105
+    }
106
+    onGround = newOnGround;
107
+
108
+    if (onGround) {
109
+        tile = 2;
110
+        tileCounter = static_cast<uint32_t>(0.1 / dt);
111
+    } else if (tileCounter <= 0) {
112
+        tile = 0;
113
+    } else {
114
+        tileCounter--;
115
+    }
116
+
117
+    if (!userInput && std::abs(xv) < 2) {
118
+        xv = 0;
119
+    }
120
+    if (xv < 0) {
121
+        transform.Scale(-1, 1);
122
+    }
123
+    transform.Translate(x, y);
124
+}
125
+
126
+void Programmer::Draw()
127
+{
128
+    int xOff = xv < 0 ? -1 : 0;
129
+    if (std::abs(xv) < 1) {
130
+        olc::GFX2D::DrawPartialSprite(&game->playerSprite, skin * w + xOff, h + tile * h, w, h, transform, true);
131
+    } else {
132
+        olc::GFX2D::DrawPartialSprite(&game->playerSprite, skin * w + xOff, 0 + tile * h, w, h, transform, true);
133
+    }
134
+#ifdef _DEBUG
135
+    game->DrawRect(x - w / 2, y - h / 2, w, h, olc::GREEN);
136
+#endif
137
+}
138
+}

Loading…
Отказ
Запис