-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
102 lines (98 loc) · 2.1 KB
/
game.cpp
File metadata and controls
102 lines (98 loc) · 2.1 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
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "game.hpp"
#include <stdexcept>
#include <iostream>
Game::Game():
tetromino_{static_cast<Tetromino::Type>(rand() % 7)},
moveTime_(SDL_GetTicks())
{
if (SDL_Init(SDL_INIT_VIDEO) != 0)
throw std::runtime_error("SDL_Init(SDL_INIT_VIDEO)");
SDL_CreateWindowAndRenderer(720 / 2, 720, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE, &window_, &renderer_);
SDL_SetWindowPosition(window_, 65, 126);
}
Game::~Game()
{
SDL_DestroyRenderer(renderer_);
SDL_DestroyWindow(window_);
SDL_Quit();
}
bool Game::tick()
{
SDL_Event e;
if (SDL_WaitEventTimeout(&e, 250))
{
switch (e.type)
{
case SDL_KEYDOWN:
{
switch (e.key.keysym.sym)
{
case SDLK_DOWN:
{
Tetromino t = tetromino_;
t.move(0, 1);
if (!well_.isCollision(t))
tetromino_ = t;
}
break;
case SDLK_RIGHT:
{
Tetromino t = tetromino_;
t.move(1, 0);
if (!well_.isCollision(t))
tetromino_ = t;
}
break;
case SDLK_LEFT:
{
Tetromino t = tetromino_;
t.move(-1, 0);
if (!well_.isCollision(t))
tetromino_ = t;
}
break;
case SDLK_UP:
{
Tetromino t = tetromino_;
t.rotate();
if (!well_.isCollision(t))
tetromino_ = t;
}
break;
}
}
break;
case SDL_QUIT:
return false;
}
}
SDL_SetRenderDrawColor(renderer_, 0, 0, 0, 0xff);
SDL_RenderClear(renderer_);
well_.draw(renderer_);
tetromino_.draw(renderer_);
if (SDL_GetTicks() > moveTime_)
{
moveTime_ += 1000;
Tetromino t = tetromino_;
t.move(0, 1);
check(t);
}
SDL_RenderPresent(renderer_);
return true;
};
void Game::check(const Tetromino &t)
{
if (well_.isCollision(t))
{
well_.unite(tetromino_);
tetromino_ = Tetromino{static_cast<Tetromino::Type>(rand() % 7)};
if (well_.isCollision(tetromino_))
{
well_ = Well();
}
}
else
{
tetromino_ = t;
}
}