Browse Source

refactoring: get_next_move

remotes/origin/David
David Moeller 11 months ago
parent
commit
1c7dfb8237
  1. 36
      src/main/c/Snake/snake_start.c
  2. 1
      test/Snake/test_part_of_snake.c

36
src/main/c/Snake/snake_start.c

@ -5,13 +5,13 @@
#include <stdlib.h>
#include "snake_start.h"
#include "get_character.h"
#define TIME_TURN 0.5;
#define TIME_TURN 0.5
void main_menu();
void game();
Snake initialize_snake();
int *get_next_move(double limit);
void get_next_move(double limit, Snake *snake);
void move_snake(Snake *snake);
void draw(Snake *snake);
int part_of_snake(Snake *snake, unsigned char tile);
@ -56,16 +56,16 @@ void game(){
bool running = true;
clock_t t = clock();
while (running)
{
//while (running){
for(int i = 0; i < 100; i++){
system("clear");
draw(&snake);
t = clock() - t;
int *mv = get_next_move((double)t / CLOCKS_PER_SEC);
get_next_move(TIME_TURN - (double)t / CLOCKS_PER_SEC, &snake);
t = clock();
move_snake(&snake);
draw(&snake);
running = false;
}
@ -81,13 +81,13 @@ Snake initialize_snake(){
snake.segments[0] = 8 + 16 * 8;
snake.segments[1] = 7 + 16 * 8;
snake.segments[2] = 6 + 16 * 8;
snake.direction[0] = 1;
snake.direction[1] = 0;
return snake;
}
int *get_next_move(double limit){
limit = TIME_TURN - limit;
int *ptr = (int*) malloc(2 * sizeof(int));
clock_t t = clock();
void get_next_move(double limit, Snake *snake){
char c;
c = get_character(limit);
@ -95,26 +95,28 @@ int *get_next_move(double limit){
switch (c){
case 'w':
case 'A':
printf("Hoch\n");
snake->direction[0] = 0;
snake->direction[1] = -1;
break;
case 'a':
case 'D':
printf("Links\n");
snake->direction[0] = -1;
snake->direction[1] = 0;
break;
case 's':
case 'B':
printf("Runter\n");
snake->direction[0] = 0;
snake->direction[1] = 1;
break;
case 'd':
case 'C':
printf("Rechts\n");
snake->direction[0] = 1;
snake->direction[1] = 0;
break;
default:
break;
}
return ptr;
}
void move_snake(Snake *snake){

1
test/Snake/test_part_of_snake.c

@ -1,6 +1,7 @@
#ifdef TEST
#include "unity.h"
#include "../../src/main/c/Snake/snake_start.h"
#include "../../src/main/c/Snake/get_character.h"
void setUp(void){

Loading…
Cancel
Save