Merge branch 'master' of git.kemt.fei.tuke.sk:oh735kh/pvjc21
Merge
This commit is contained in:
commit
e85723d41c
54
a4/.gitignore
vendored
54
a4/.gitignore
vendored
@ -1,54 +0,0 @@
|
|||||||
game
|
|
||||||
|
|
||||||
# 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
|
|
||||||
29
a4/LICENSE
29
a4/LICENSE
@ -1,29 +0,0 @@
|
|||||||
BSD 3-Clause License
|
|
||||||
|
|
||||||
Copyright (c) 2019, Daniel Hládek
|
|
||||||
All rights reserved.
|
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without
|
|
||||||
modification, are permitted provided that the following conditions are met:
|
|
||||||
|
|
||||||
* Redistributions of source code must retain the above copyright notice, this
|
|
||||||
list of conditions and the following disclaimer.
|
|
||||||
|
|
||||||
* Redistributions in binary form must reproduce the above copyright notice,
|
|
||||||
this list of conditions and the following disclaimer in the documentation
|
|
||||||
and/or other materials provided with the distribution.
|
|
||||||
|
|
||||||
* Neither the name of the copyright holder nor the names of its
|
|
||||||
contributors may be used to endorse or promote products derived from
|
|
||||||
this software without specific prior written permission.
|
|
||||||
|
|
||||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
||||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
||||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
||||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
||||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
||||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
||||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
||||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
78
a4/README.md
78
a4/README.md
@ -1,78 +0,0 @@
|
|||||||
# World Game Library
|
|
||||||
|
|
||||||
Learn asycnronous C programming by a game.
|
|
||||||
|
|
||||||
The library implements a game loop for a character-based ncurses game;
|
|
||||||
|
|
||||||
The library finds out the event types:
|
|
||||||
|
|
||||||
- start and end
|
|
||||||
- mouse events
|
|
||||||
- keyboard events
|
|
||||||
- screen resize events
|
|
||||||
|
|
||||||
It can print colors and arbitrary characters on the screen.
|
|
||||||
Running is interrupted when character is drawn out of the screen.
|
|
||||||
|
|
||||||
## Installation and Running
|
|
||||||
|
|
||||||
Make sure, that `ncurses` is installed.
|
|
||||||
|
|
||||||
Clone this repository.
|
|
||||||
|
|
||||||
Compile:
|
|
||||||
|
|
||||||
```c
|
|
||||||
make
|
|
||||||
```
|
|
||||||
|
|
||||||
Run:
|
|
||||||
|
|
||||||
```c
|
|
||||||
./game
|
|
||||||
```
|
|
||||||
|
|
||||||
## Make your own game
|
|
||||||
|
|
||||||
The game happens in a rectangular world of colorful characters.
|
|
||||||
Your goal as a programmer is to modify the world according to the pressed keys and the internal game state.
|
|
||||||
The library implements the game loop and draws the world to screen.
|
|
||||||
|
|
||||||
Your game in file `game.c` consists of two functions:
|
|
||||||
|
|
||||||
- `void* init_game()` is called in the beginning. Here you can initialize the internal state of the game.
|
|
||||||
The function should return a pointer to an instance of the initial game state `game`.
|
|
||||||
- `int game_event(struct event* event,void* game)`
|
|
||||||
is called by the game loop in periodic interval or when a key was pressed. Non-zero return value or `Ctrl+C` key interrupts the game loop.
|
|
||||||
|
|
||||||
The world variable represents state of two-dimensional grid of characters on the screen. The screen matrix looks like this:
|
|
||||||
|
|
||||||
```
|
|
||||||
origin
|
|
||||||
[0,0] width
|
|
||||||
+--------------------+
|
|
||||||
h | |
|
|
||||||
e | |
|
|
||||||
i | |
|
|
||||||
g | |
|
|
||||||
h | |
|
|
||||||
t | |
|
|
||||||
+--------------------+
|
|
||||||
```
|
|
||||||
|
|
||||||
The world has the following parameters:
|
|
||||||
|
|
||||||
- `int height`: y-dimension of the grid.
|
|
||||||
- `int width`: x-dimension of the grid.
|
|
||||||
- `int interval`: maximum time between steps in milliseconds.
|
|
||||||
|
|
||||||
### The Event Function
|
|
||||||
|
|
||||||
The `int game_event(struct event* event,void* game)`
|
|
||||||
function should:
|
|
||||||
|
|
||||||
1. Read the game state (from the `void* game`) pointer.
|
|
||||||
1. Examine the pressed key from event pointer. If the `key` variable is non-zero, a key was pressed. According to the pressed key, modify the game state `game`.
|
|
||||||
1. Draw the game state. In the beginning of the step function the screen is empty.
|
|
||||||
1. Returning non-zero value ends the game loop.
|
|
||||||
|
|
||||||
42
a4/game.c
42
a4/game.c
@ -10,14 +10,14 @@ void* init_game(){
|
|||||||
struct game* st = calloc(1,(sizeof(struct game)));
|
struct game* st = calloc(1,(sizeof(struct game)));
|
||||||
// Initialize state
|
// Initialize state
|
||||||
for(int i = 0; i < MOUSE_COUNT; i++){
|
for(int i = 0; i < MOUSE_COUNT; i++){
|
||||||
st->mousex[i] = rand()%15;
|
st->mousex[i] = rand()%15; // Рандомная позиция мышей на карте
|
||||||
st->mousey[i] = rand()%15;
|
st->mousey[i] = rand()%15; // --//--
|
||||||
st->mouse_state[i] = 0;
|
st->mouse_state[i] = 0; // состояние мышы -- жива
|
||||||
}
|
} //генерация мышей
|
||||||
st->catx = 0;
|
st->catx = 0; //скорость кота
|
||||||
st->caty = 0;
|
st->caty = 0; // --//--
|
||||||
st->catx_position = 15;
|
st->catx_position = 15; // Позиция кота на карте
|
||||||
st->caty_position = 15;
|
st->caty_position = 15; // --//--
|
||||||
// Store pointer to the state to the world variable
|
// Store pointer to the state to the world variable
|
||||||
return st;
|
return st;
|
||||||
}
|
}
|
||||||
@ -26,9 +26,9 @@ void* init_game(){
|
|||||||
// It should modify the state and draw it.
|
// It should modify the state and draw it.
|
||||||
int game_event(struct event* event,void* game){
|
int game_event(struct event* event,void* game){
|
||||||
// Get state pointer
|
// Get state pointer
|
||||||
struct game* state = game;
|
struct game* state = game; // Зоздание игры
|
||||||
char msg[200];
|
char msg[200];
|
||||||
sprintf(msg,"%d",event->type);
|
sprintf(msg,"%d",event->type);
|
||||||
set_message(msg,10,0);
|
set_message(msg,10,0);
|
||||||
if ( event->type == EVENT_ESC){
|
if ( event->type == EVENT_ESC){
|
||||||
// Non zero means finish the loop and stop the game.
|
// Non zero means finish the loop and stop the game.
|
||||||
@ -69,20 +69,7 @@ int game_event(struct event* event,void* game){
|
|||||||
int cx = state->catx_position + state->catx;
|
int cx = state->catx_position + state->catx;
|
||||||
int cy = state->caty_position + state->caty;
|
int cy = state->caty_position + state->caty;
|
||||||
if (cx < 1 || cy < 0 || cx > event->width || cy > event->height){
|
if (cx < 1 || cy < 0 || cx > event->width || cy > event->height){
|
||||||
/*state->catx_position = cx;
|
|
||||||
state->caty_position = cy;
|
|
||||||
if(state->catx_position < 0){
|
|
||||||
state->catx_position = 0;
|
|
||||||
}
|
|
||||||
else if(state->catx_position >= event->width){
|
|
||||||
state->catx_position = event->width-1;
|
|
||||||
}
|
|
||||||
else if(state->caty_position < 0){
|
|
||||||
state->caty_position = 0;
|
|
||||||
}
|
|
||||||
else if(state->caty_position >= event->height){
|
|
||||||
state->caty_position = event->height-1;
|
|
||||||
}*/
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
state->catx_position = cx;
|
state->catx_position = cx;
|
||||||
@ -108,8 +95,7 @@ int game_event(struct event* event,void* game){
|
|||||||
else if (m == 3 && state->mousey[i] < event->height){
|
else if (m == 3 && state->mousey[i] < event->height){
|
||||||
state->mousey[i] += 1;
|
state->mousey[i] += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//Je
|
|
||||||
}
|
}
|
||||||
else if (event->type == EVENT_KEY){
|
else if (event->type == EVENT_KEY){
|
||||||
// Move cat according to keyboard
|
// Move cat according to keyboard
|
||||||
@ -131,14 +117,14 @@ int game_event(struct event* event,void* game){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Draw world state
|
// Draw world state
|
||||||
//
|
|
||||||
// Draw cat
|
// Draw cat
|
||||||
clear_screen();
|
clear_screen();
|
||||||
set_color_cell('c',state->catx_position,state->caty_position,COLOR_YELLOW,COLOR_RED);
|
set_color_cell('c',state->catx_position,state->caty_position,COLOR_YELLOW,COLOR_RED);
|
||||||
set_color_cell('-',state->catx_position-1,state->caty_position,COLOR_YELLOW,COLOR_GREEN);
|
set_color_cell('-',state->catx_position-1,state->caty_position,COLOR_YELLOW,COLOR_GREEN);
|
||||||
//set_cell('c',state->catx_position,state->caty_position);
|
//set_cell('c',state->catx_position,state->caty_position);
|
||||||
// Draw mouse
|
|
||||||
|
|
||||||
|
// Draw mouse
|
||||||
for(int i = 0; i < MOUSE_COUNT; i++){
|
for(int i = 0; i < MOUSE_COUNT; i++){
|
||||||
if(state->mouse_state[i] != 1){
|
if(state->mouse_state[i] != 1){
|
||||||
set_cell('m', state->mousex[i], state->mousey[i]);
|
set_cell('m', state->mousex[i], state->mousey[i]);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user