-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello.cpp
139 lines (100 loc) · 3.32 KB
/
hello.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
#include <iostream>
#include "Desk.hpp"
#include <chrono>
#include <SFML/Graphics.hpp>
#define timeStep 10
using namespace std;
#define clockObject std::chrono::time_point<std::chrono::steady_clock>
auto dummy = chrono::steady_clock::now();
void keyPressEventMonitor(HumanPlayer& humanObj, clockObject& globalClock, Ball& ballObj)
{
// measure the time
clockObject clockNow = chrono::steady_clock::now();
int elapsedTime = chrono::duration_cast<chrono::milliseconds>(clockNow - globalClock).count();
// if (elapsedTime % 1000 == 0)
// std::cout << "Elapsed Time: " << elapsedTime << std::endl;
// not enought time has passed
if (elapsedTime <= timeStep)
return;
// update global clock, so we can keep track of time
// thing it as a "lap", global clock set to the last valid action frame
globalClock = chrono::steady_clock::now();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
humanObj.getStick().moveDown();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){
humanObj.getStick().moveUp();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::R)){
humanObj.getStick().resetPositionToOriginal();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
ballObj.moveYPos();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
ballObj.moveYNeg();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
ballObj.moveXNeg();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
ballObj.moveXPos();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Subtract))
{
std::cout << "Ball Size-" << std::endl;
ballObj.decreaseSize();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Add))
{
std::cout << "Ball Size+" << std::endl;
ballObj.increaseSize();
}
}
int main()
{
int windowWidth = 480;
int windowHeight = 480;
HumanPlayer humanPlayer = HumanPlayer();
Desk desk = Desk(windowWidth, windowHeight);
Ball ball = Ball(50, 50, -0.01, 5);
auto globalClock = chrono::steady_clock::now();
// stick drawing object inside Desk
desk.drawStickFirstTime(humanPlayer);
// ball drawing object inside Desk
desk.drawBallFirstTime(ball);
sf::RenderWindow window(sf::VideoMode(windowWidth, windowHeight), "Happy go PingPong!");
while (window.isOpen())
{
int currentState = desk.getDeskState();
// game is idle
if (currentState == 0){
window.clear();
window.display();
continue;
}
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
// detect key press, take necessary actions
// TODO: update based on time count, return from keyPress, not void
keyPressEventMonitor(humanPlayer, globalClock, ball);
// TODO: put desk into keyPress...
desk.oneFrameBallAction(ball);
// things have been updated, update their position in desk
desk.redrawStick(humanPlayer);
desk.redrawBall(ball);
window.clear();
window.draw(desk.getSFStick());
window.draw(desk.getSFBall());
window.display();
}
return 0;
}