Untitled
unknown
plain_text
a year ago
5.5 kB
10
Indexable
#include "Game.h"
#include <cstdlib>
#include <ctime>
Game::Game() : window(sf::VideoMode(360, 480), "The Game!"), dx(0), rotate(false), timer(0), delay(0.3), gameOver(false) {
srand(time(0));
t1.loadFromFile("images/tiles.png");
t2.loadFromFile("images/background.png");
t3.loadFromFile("images/frame.png");
s.setTexture(t1);
background.setTexture(t2);
frame.setTexture(t3);
font.loadFromFile("Arial.ttf");
scoreText.setFont(font);
highScoreText.setFont(font);
gameOverText.setFont(font);
playAgainText.setFont(font);
nextText.setFont(font);
scoreText.setFillColor(sf::Color::Cyan);
highScoreText.setFillColor(sf::Color::Cyan);
gameOverText.setFillColor(sf::Color::Red);
playAgainText.setFillColor(sf::Color::Blue);
nextText.setFillColor(sf::Color::Black);
scoreText.setCharacterSize(20);
highScoreText.setCharacterSize(20);
gameOverText.setCharacterSize(30);
playAgainText.setCharacterSize(20);
nextText.setCharacterSize(20);
scoreText.setPosition(250, 20);
highScoreText.setPosition(250, 50);
gameOverText.setPosition(40, 220);
playAgainText.setPosition(70, 260);
nextText.setPosition(270, 375);
nextFrame.setSize(sf::Vector2f(75, 75));
nextFrame.setFillColor(sf::Color::Transparent);
nextFrame.setOutlineThickness(2);
nextFrame.setOutlineColor(sf::Color::Black);
nextFrame.setPosition(255, 285);
for (int i = 0; i < 23; ++i) {
for (int j = 0; j < 10; ++j) {
gridLines[i * 10 + j].setSize(sf::Vector2f(17, 17));
gridLines[i * 10 + j].setFillColor(sf::Color::Transparent);
gridLines[i * 10 + j].setOutlineThickness(1);
gridLines[i * 10 + j].setOutlineColor(sf::Color::Black);
gridLines[i * 10 + j].setPosition(j * 18, i * 18);
gridLines[i * 10 + j].move(36, 18 * 3);
}
}
resetGame();
}
void Game::resetGame() {
field.clear();
score.reset();
current.reset(rand() % 7, rand() % 9);
next.reset(rand() % 7, 0);
gameOver = false;
timer = 0;
delay = 0.3;
}
void Game::run() {
sf::Clock clock;
while (window.isOpen()) {
float time = clock.getElapsedTime().asSeconds();
clock.restart();
timer += time;
processEvents();
if (!gameOver) {
update(time);
}
render();
}
}
void Game::processEvents() {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
if (gameOver) {
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Space) {
resetGame();
}
continue;
}
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Up)
rotate = true;
else if (event.key.code == sf::Keyboard::Left)
dx = -1;
else if (event.key.code == sf::Keyboard::Right)
dx =1;
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
delay = 0.05;
}
void Game::update(float time) {
for (int i = 0; i < 4; i++) {
current.a[i].x += dx;
}
if (!current.checkCollision(field.getField(), 23, 10)) {
for (int i = 0; i < 4; i++) {
current.a[i].x -= dx;
}
}
if (rotate) {
current.rotate();
if (!current.checkCollision(field.getField(), 23, 10)) {
current.rotate();
current.rotate();
current.rotate();
}
}
if (timer > delay) {
for (int i = 0; i < 4; i++) {
current.a[i].y += 1;
}
if (!current.checkCollision(field.getField(), 23, 10)) {
for (int i = 0; i < 4; i++) {
current.a[i].y -= 1;
field.getField()[current.a[i].y][current.a[i].x] = current.colorNum;
}
current = next;
next.reset(rand() % 7, 0);
if (!current.checkCollision(field.getField(), 23, 10)) {
gameOver = true;
}
int lines = 0;
field.checkLines(lines);
score.updateScore(lines);
delay = 0.3;
}
timer = 0;
}
dx = 0;
rotate = false;
}
void Game::render() {
window.clear(sf::Color::White);
window.draw(background);
for (int i = 0; i < 23; i++) {
for (int j = 0; j < 10; j++) {
if (field.getField(i, j) == 0)
continue;
s.setTextureRect(sf::IntRect(field.getField(i, j) * 18, 0, 18, 18));
s.setPosition(j * 18, i * 18);
s.move(36, 54); // offset
window.draw(s);
}
}
for (int i = 0; i < 4; i++) {
s.setTextureRect(sf::IntRect(current.colorNum * 18, 0, 18, 18));
s.setPosition(current.a[i].x * 18, current.a[i].y * 18);
s.move(36, 54); // offset
window.draw(s);
}
for (int i = 0; i < 4; i++) {
s.setTextureRect(sf::IntRect(next.colorNum * 18, 0, 18, 18));
s.setPosition(next.a[i].x * 18 + 255, next.a[i].y * 18 + 285);
window.draw(s);
}
window.draw(frame);
window.draw(nextFrame);
window.draw(scoreText);
window.draw(highScoreText);
if (gameOver) {
window.draw(gameOverText);
window.draw(playAgainText);
}
window.display();
}Editor is loading...
Leave a Comment