collision done

This commit is contained in:
Rorikstr | Rust Dev 2025-09-27 00:09:18 +03:00
parent 15703b5f28
commit c4171dc5d5
7 changed files with 436 additions and 266 deletions

1
.gitignore vendored
View file

@ -52,3 +52,4 @@ Mkfile.old
dkms.conf
src/project.md
src/tetris_bin
src/.gpskip

View file

@ -0,0 +1,71 @@
.git/
.vscode/
.idea/
target/
build/
dist/
node_modules/
*.log
*.tmp
*.swp
*.bak
.DS_Store
Thumbs.db
# Prerequisites
*.d
# Object files
*.o
*.ko
*.obj
*.elf
# Linker output
*.ilk
*.map
*.exp
# Precompiled Headers
*.gch
*.pch
# Libraries
*.lib
*.a
*.la
*.lo
# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib
# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex
# Debug files
*.dSYM/
*.su
*.idb
*.pdb
# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf
project.md
tetris_bin
ginpee.toml
project.md

View file

@ -1,14 +1,32 @@
.PHONY: all clean valgrind test style format gcov_report all install uninstall
# Автоопределение компилятора и флагов
CC ?= gcc
CFLAGS ?= -Wall -Wextra -std=c11 -g
CHECK_CFLAGS ?= -I/usr/include/check
# Проверяем наличие библиотек
CHECK_LIBS_AVAILABLE := $(shell ldconfig -p 2>/dev/null | grep -q libsubunit && echo yes || echo no)
ifeq ($(CHECK_LIBS_AVAILABLE),yes)
LDFLAGS ?= -lcheck -lrt -lpthread -lm -lncurses -lsubunit
else
LDFLAGS ?= -lcheck -lrt -lpthread -lm -lncurses
endif
SRCDIR = .
TESTDIR = test
BUILDDIR = build
# src/Makefile
CC = gcc
CFLAGS = -std=c11 -Wall -Wextra -Werror -g
LDFLAGS = -lncurses
TETRISDIR = brick_game/tetris
CLIDIR = gui/cli
# Файлы
TETRIS_SRC = $(TETRISDIR)/tetris.c
CLI_SRC = $(CLIDIR)/main.c $(CLIDIR)/display.c
OBJ = $(TETRIS_SRC:.c=.o) $(CLI_SRC:.c=.o)
TETRIS_SRC = $(shell find $(TETRISDIR) -name "*.c")
CLI_SRC = $(shell find $(CLIDIR) -name "*.c")
ALL_SRC = $(TETRIS_SRC) $(CLI_SRC)
OBJ = $(ALL_SRC:.c=.o)
# Имя исполняемого файла
TARGET = tetris_bin
@ -17,8 +35,6 @@ TARGET = tetris_bin
PREFIX ?= /usr/local
BINDIR = $(PREFIX)/bin
.PHONY: all install uninstall clean test gcov_report
all: $(TARGET)
$(TARGET): $(OBJ)
@ -46,4 +62,14 @@ gcov_report: clean $(TARGET)
gcov $(TETRIS_SRC)
lcov --capture --directory . --output-file coverage.info
genhtml coverage.info --output-directory coverage_report
@echo "Coverage report generated in coverage_report/index.html"
@echo "Coverage report generated in coverage_report/index.html"
style:
@cp ../materials/linters/.clang-format .
@clang-format -n *.c *.h
@rm .clang-format
format:
cp ../materials/linters/.clang-format .
clang-format -i *.c *.h
rm .clang-format

View file

@ -0,0 +1,258 @@
#include "tetris.h"
// I
const int (*i_fig_up())[4] {
static const int shape[4][4] = {
{0, 0, 0, 0},
{1, 1, 1, 1},
{0, 0, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*i_fig_right())[4] {
static const int shape[4][4] = {
{0, 0, 1, 0},
{0, 0, 1, 0},
{0, 0, 1, 0},
{0, 0, 1, 0}
};
return (const int (*)[4])shape;
}
const int (*i_fig_down())[4] {
static const int shape[4][4] = {
{0, 0, 0, 0},
{0, 0, 0, 0},
{1, 1, 1, 1},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*i_fig_left())[4] {
static const int shape[4][4] = {
{0, 1, 0, 0},
{0, 1, 0, 0},
{0, 1, 0, 0},
{0, 1, 0, 0}
};
return (const int (*)[4])shape;
}
// O
const int (*o_fig())[4] {
static const int shape[4][4] = {
{0, 1, 1, 0},
{0, 1, 1, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
// T
const int (*t_fig_up())[4] {
static const int shape[4][4] = {
{0, 1, 0, 0},
{1, 1, 1, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*t_fig_right())[4] {
static const int shape[4][4] = {
{0, 1, 0, 0},
{0, 1, 1, 0},
{0, 1, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*t_fig_down())[4] {
static const int shape[4][4] = {
{0, 0, 0, 0},
{1, 1, 1, 0},
{0, 1, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*t_fig_left())[4] {
static const int shape[4][4] = {
{0, 1, 0, 0},
{1, 1, 0, 0},
{0, 1, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
// L
const int (*l_fig_up())[4] {
static const int shape[4][4] = {
{0, 0, 1, 0},
{1, 1, 1, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*l_fig_right())[4] {
static const int shape[4][4] = {
{1, 0, 0, 0},
{1, 0, 0, 0},
{1, 1, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*l_fig_down())[4] {
static const int shape[4][4] = {
{0, 0, 0, 0},
{1, 1, 1, 0},
{1, 0, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*l_fig_left())[4] {
static const int shape[4][4] = {
{1, 1, 0, 0},
{0, 1, 0, 0},
{0, 1, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
// J
const int (*j_fig_up())[4] {
static const int shape[4][4] = {
{1, 0, 0, 0},
{1, 1, 1, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*j_fig_right())[4] {
static const int shape[4][4] = {
{0, 1, 1, 0},
{0, 1, 0, 0},
{0, 1, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*j_fig_down())[4] {
static const int shape[4][4] = {
{0, 0, 0, 0},
{1, 1, 1, 0},
{0, 0, 1, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*j_fig_left())[4] {
static const int shape[4][4] = {
{0, 1, 0, 0},
{0, 1, 0, 0},
{1, 1, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
// S
const int (*s_fig_up())[4] {
static const int shape[4][4] = {
{0, 1, 1, 0},
{1, 1, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*s_fig_right())[4] {
static const int shape[4][4] = {
{0, 1, 0, 0},
{0, 1, 1, 0},
{0, 0, 1, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*s_fig_down())[4] {
static const int shape[4][4] = {
{0, 1, 1, 0},
{1, 1, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*s_fig_left())[4] {
static const int shape[4][4] = {
{0, 1, 0, 0},
{0, 1, 1, 0},
{0, 0, 1, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
// Z
const int (*z_fig_up())[4] {
static const int shape[4][4] = {
{1, 1, 0, 0},
{0, 1, 1, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*z_fig_right())[4] {
static const int shape[4][4] = {
{0, 0, 1, 0},
{0, 1, 1, 0},
{0, 1, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*z_fig_down())[4] {
static const int shape[4][4] = {
{1, 1, 0, 0},
{0, 1, 1, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*z_fig_left())[4] {
static const int shape[4][4] = {
{0, 0, 1, 0},
{0, 1, 1, 0},
{0, 1, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}

View file

@ -7,263 +7,6 @@
static GameStateData game_state = {0};
static bool initialized = false;
// I
const int (*i_fig_up())[4] {
static const int shape[4][4] = {
{0, 0, 0, 0},
{1, 1, 1, 1},
{0, 0, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*i_fig_right())[4] {
static const int shape[4][4] = {
{0, 0, 1, 0},
{0, 0, 1, 0},
{0, 0, 1, 0},
{0, 0, 1, 0}
};
return (const int (*)[4])shape;
}
const int (*i_fig_down())[4] {
static const int shape[4][4] = {
{0, 0, 0, 0},
{0, 0, 0, 0},
{1, 1, 1, 1},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*i_fig_left())[4] {
static const int shape[4][4] = {
{0, 1, 0, 0},
{0, 1, 0, 0},
{0, 1, 0, 0},
{0, 1, 0, 0}
};
return (const int (*)[4])shape;
}
// O
const int (*o_fig())[4] {
static const int shape[4][4] = {
{0, 1, 1, 0},
{0, 1, 1, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
// T
const int (*t_fig_up())[4] {
static const int shape[4][4] = {
{0, 1, 0, 0},
{1, 1, 1, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*t_fig_right())[4] {
static const int shape[4][4] = {
{0, 1, 0, 0},
{0, 1, 1, 0},
{0, 1, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*t_fig_down())[4] {
static const int shape[4][4] = {
{0, 0, 0, 0},
{1, 1, 1, 0},
{0, 1, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*t_fig_left())[4] {
static const int shape[4][4] = {
{0, 1, 0, 0},
{1, 1, 0, 0},
{0, 1, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
// L
const int (*l_fig_up())[4] {
static const int shape[4][4] = {
{0, 0, 1, 0},
{1, 1, 1, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*l_fig_right())[4] {
static const int shape[4][4] = {
{1, 0, 0, 0},
{1, 0, 0, 0},
{1, 1, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*l_fig_down())[4] {
static const int shape[4][4] = {
{0, 0, 0, 0},
{1, 1, 1, 0},
{1, 0, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*l_fig_left())[4] {
static const int shape[4][4] = {
{1, 1, 0, 0},
{0, 1, 0, 0},
{0, 1, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
// J
const int (*j_fig_up())[4] {
static const int shape[4][4] = {
{1, 0, 0, 0},
{1, 1, 1, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*j_fig_right())[4] {
static const int shape[4][4] = {
{0, 1, 1, 0},
{0, 1, 0, 0},
{0, 1, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*j_fig_down())[4] {
static const int shape[4][4] = {
{0, 0, 0, 0},
{1, 1, 1, 0},
{0, 0, 1, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*j_fig_left())[4] {
static const int shape[4][4] = {
{0, 1, 0, 0},
{0, 1, 0, 0},
{1, 1, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
// S
const int (*s_fig_up())[4] {
static const int shape[4][4] = {
{0, 1, 1, 0},
{1, 1, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*s_fig_right())[4] {
static const int shape[4][4] = {
{0, 1, 0, 0},
{0, 1, 1, 0},
{0, 0, 1, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*s_fig_down())[4] {
static const int shape[4][4] = {
{0, 1, 1, 0},
{1, 1, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*s_fig_left())[4] {
static const int shape[4][4] = {
{0, 1, 0, 0},
{0, 1, 1, 0},
{0, 0, 1, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
// Z
const int (*z_fig_up())[4] {
static const int shape[4][4] = {
{1, 1, 0, 0},
{0, 1, 1, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*z_fig_right())[4] {
static const int shape[4][4] = {
{0, 0, 1, 0},
{0, 1, 1, 0},
{0, 1, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*z_fig_down())[4] {
static const int shape[4][4] = {
{1, 1, 0, 0},
{0, 1, 1, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*z_fig_left())[4] {
static const int shape[4][4] = {
{0, 0, 1, 0},
{0, 1, 1, 0},
{0, 1, 0, 0},
{0, 0, 0, 0}
};
return (const int (*)[4])shape;
}
const int (*get_figure_shape(FigureType type, int rotation))[4] {
const int (*result)[4] = NULL;
switch (type) {
@ -339,6 +82,10 @@ void user_input(UserAction_t action) {
}
if (game_state.figure_active) {
int old_x = game_state.current_figure.x;
int old_y = game_state.current_figure.y;
int old_rot = game_state.current_figure.rotation;
if (action == Left) game_state.current_figure.x--;
if (action == Right) game_state.current_figure.x++;
if (action == Down) game_state.current_figure.y++;
@ -346,9 +93,34 @@ void user_input(UserAction_t action) {
if (action == Rotate) {
game_state.current_figure.rotation = (game_state.current_figure.rotation + 1) % 4;
}
if (check_collision()) {
// Возвращаем старые значения
game_state.current_figure.x = old_x;
game_state.current_figure.y = old_y;
game_state.current_figure.rotation = old_rot;
}
}
}
GameStateData* get_game_state() {
return &game_state;
}
static bool check_collision() {
Figure* f = &game_state.current_figure;
const int (*shape)[4] = get_figure_shape(f->type, f->rotation);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (shape[i][j]) {
int x = f->x + j;
int y = f->y + i;
if (x < 0 || x >= FIELD_WIDTH || y >= FIELD_HEIGHT) {
return true; // Коллизия
}
}
}
}
return false; // Нет коллизии
}

View file

@ -58,8 +58,41 @@ typedef enum {
// Основные функции библиотеки
void user_input(UserAction_t action);
static bool check_collision();
GameStateData* get_game_state(void);
const int (*get_figure_shape(FigureType type, int rotation))[4];
const int (*i_fig_up())[4];
const int (*i_fig_right())[4];
const int (*i_fig_down())[4];
const int (*i_fig_left())[4];
const int (*o_fig())[4];
const int (*t_fig_up())[4];
const int (*t_fig_right())[4];
const int (*t_fig_down())[4];
const int (*t_fig_left())[4];
const int (*l_fig_up())[4];
const int (*l_fig_right())[4];
const int (*l_fig_down())[4];
const int (*l_fig_left())[4];
const int (*j_fig_up())[4];
const int (*j_fig_right())[4];
const int (*j_fig_down())[4];
const int (*j_fig_left())[4];
const int (*s_fig_up())[4];
const int (*s_fig_right())[4];
const int (*s_fig_down())[4];
const int (*s_fig_left())[4];
const int (*z_fig_up())[4];
const int (*z_fig_right())[4];
const int (*z_fig_down())[4];
const int (*z_fig_left())[4];
#endif

View file

@ -0,0 +1,9 @@
[top]
text = ""
[down]
text = ""
[files]
include = ["*.c", "*.h", "Makefile"]