-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
156 lines (127 loc) · 4.52 KB
/
game.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
#include <iostream>
#include <iomanip>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>
#include <sstream>
#include <cstdlib>
#include <ctime>
using namespace sf;
using namespace std;
int main() {
RenderWindow window(VideoMode(800, 600), "10911235 trash");
window.setFramerateLimit(70);//fps
//¦rÅé
Font arial;
arial.loadFromFile("Arial.ttf");
//¤À¼Æ
int score = 0;
ostringstream ssScore;
ssScore << "Score: " << score;
//¤À¼Æ¦rÅé³]¸m
Text Score;
Score.setFont(arial);
Score.setCharacterSize(30);
Score.setPosition(0.f, window.getSize().y / 2);
Score.setString(ssScore.str());
int health = 3;
ostringstream ssHealth;
ssHealth << "Health: " << health;
Text Health;
Health.setFont(arial);
Health.setCharacterSize(30);
Health.setPosition(0.f, (window.getSize().y / 2 + 35));
Health.setString(ssHealth.str());
Text gameOver;
gameOver.setFont(arial);
gameOver.setFillColor(Color::Red);
gameOver.setCharacterSize(60);
gameOver.setPosition(window.getSize().x / 4, window.getSize().y / 2);
gameOver.setString("you are noob qq");
CircleShape fruit;
fruit.setRadius(20.f);
fruit.setFillColor(Color::Green);
fruit.setPosition(Vector2f(rand() % (window.getSize().x - (int)(2 * fruit.getRadius())), 10.f));
RectangleShape player;
player.setSize(Vector2f(100.f, 50.f));
player.setFillColor(Color::Red);
player.setPosition(Vector2f(window.getSize().x / 2 - player.getSize().x / 2, window.getSize().y - player.getSize().y));
bool isMovingLeft = false;
bool isMovingRight = false;
while (window.isOpen()) {
Event event;
while (window.pollEvent(event)) {
if (event.type == Event::Closed) {
window.close();
}
}
if (Keyboard::isKeyPressed(Keyboard::R)) {
score = 0;
health = 3;
ssScore.str("");
ssScore << "Score: " << score;
Score.setString(ssScore.str());
ssHealth.str("");
ssHealth << "Health: " << health;
Health.setString(ssHealth.str());
fruit.setPosition(Vector2f(static_cast<float>(rand() % static_cast<int>(window.getSize().x - 2 * fruit.getRadius())), 10.f));
}
if(health==0){
fruit.setPosition(Vector2f(fruit.getPosition().x, fruit.getPosition().y));
player.setPosition(Vector2f(player.getPosition().x, player.getPosition().y));
}
else{
// Move player
if (Keyboard::isKeyPressed(Keyboard::Left)) {
isMovingLeft = true;
isMovingRight = false;
} else if (Keyboard::isKeyPressed(Keyboard::Right)) {
isMovingLeft = false;
isMovingRight = true;
} else {
isMovingLeft = false;
isMovingRight = false;
}
if (isMovingLeft && player.getPosition().x > 0) {
player.move(-5.f, 0.f);
}
if (isMovingRight && player.getPosition().x + player.getSize().x < window.getSize().x) {
player.move(5.f, 0.f);
}
// Move fruit
fruit.move(0.f, 5.f);
}
// Check collisions
if (fruit.getPosition().y + 2 * fruit.getRadius() >= window.getSize().y) {
// Fruit missed
fruit.setPosition(Vector2f(static_cast<float>(rand() % static_cast<int>(window.getSize().x - 2 * fruit.getRadius())), 10.f));
health--;
ssHealth.str("");
ssHealth << "Health: " << health;
Health.setString(ssHealth.str());
if (health == 0) {
fruit.setPosition(fruit.getPosition().x, fruit.getPosition().y);
}
}
if (fruit.getGlobalBounds().intersects(player.getGlobalBounds())) {
// Fruit caught
fruit.setPosition(Vector2f(static_cast<float>(rand() % static_cast<int>(window.getSize().x - 2 * fruit.getRadius())), 10.f));
score++;
ssScore.str("");
ssScore << "Score: " << score;
Score.setString(ssScore.str());
}
// Draw
window.clear(Color::Black);
window.draw(fruit);
window.draw(Score);
window.draw(Health);
window.draw(player);
if (health == 0) {
window.draw(gameOver);
}
window.display();
}
return 0;
}