Browse Source

update

master
老赵 4 months ago
parent
commit
76b36b971c
2 changed files with 16 additions and 11 deletions
  1. 10
    10
      src/game.cc
  2. 6
    1
      src/game.hh

+ 10
- 10
src/game.cc View File

@@ -10,7 +10,8 @@ bool Game::init() {
}

if ((window = SDL_CreateWindow(
"Tetris", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480,
WINDOW_TITLE, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
WINDOW_WIDTH, WINDOW_HEIGHT,
SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI)) == NULL) {
SDL_Log("SDL create window error: %s", SDL_GetError());
return false;
@@ -49,9 +50,9 @@ void Game::restart() {
lines = 0;
score = 0;
// 清空矩阵
for (int i = 0; i < 24; i++) {
for (int j = 0; j < 12; j++) {
if ((j == 0) || (j == 11) || (i == 23)) {
for (int i = 0; i <= PLAYFIELD_HEIGHT; i++) {
for (int j = 0; j <= PLAYFIELD_WIDTH; j++) {
if ((j == 0) || (j == PLAYFIELD_WIDTH) || (i == 0)) {
playfield[i][j] = 0; // Border
} else {
playfield[i][j] = 9; // Space
@@ -65,16 +66,15 @@ void Game::render() {
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
SDL_RenderClear(renderer);

int color[7][3] = {{0, 240, 240}, {240, 240, 0}, {160, 0, 240}, {0, 240, 0},
{240, 0, 0}, {0, 0, 240}, {240, 160, 0}};
int size = 50;
for (int i = 0; i < 7; i++) {
SDL_Rect a = {i * size + 1, size + 1, size - 2, size - 2};
int color[8][3] = {{0, 0, 0}, {0, 240, 240}, {240, 240, 0}, {160, 0, 240},
{0, 240, 0}, {240, 0, 0}, {0, 0, 240}, {240, 160, 0}};
for (int i = 1; i <= 7; i++) {
SDL_Rect a = {i * BLOCK_SIZE + 1, 1, BLOCK_SIZE - 2, BLOCK_SIZE - 2};
SDL_SetRenderDrawColor(renderer, color[i][0], color[i][1], color[i][2],
0xFF);
SDL_RenderFillRect(renderer, &a);
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_Rect b = {i * size, size, size, size};
SDL_Rect b = {i * BLOCK_SIZE, 0, BLOCK_SIZE, BLOCK_SIZE};
SDL_RenderDrawRect(renderer, &b);
}


+ 6
- 1
src/game.hh View File

@@ -4,7 +4,12 @@

#include "random.hh"

#define GAME_NAME "Tetris"
#define BLOCK_SIZE 36
#define PLAYFIELD_HEIGHT 22
#define PLAYFIELD_WIDTH 11
#define WINDOW_TITLE "Tetris"
#define WINDOW_WIDTH 360
#define WINDOW_HEIGHT 720

class Game {
private:

Loading…
Cancel
Save