Skip to content
Snippets Groups Projects
Commit 7f16dc4f authored by Janos's avatar Janos
Browse files

Erzeugung von Fahrzeugen modularisiert und Bilder für Rennautos hinzugefügt

parent 9fb4f7a4
Branches
No related tags found
No related merge requests found
#include "hDateien/auto.h"
// Vorbedinung:
// 0 < |speed| < 10, 0 < width < 500, 0 < height < 100,
// -5000 <= pred_pos < 5000 <- Position des Vorgängerautos
// 0 <= pred_width < 500
// Nachbedingung: Ein Auto wurde aus den Parametern initialisiert
// Nachbedingung: Ein Fahrzeug wurde aus den Parametern initialisiert
struct Car* init_car(int speed,int width,int height,int pred_pos,int pred_width,enum vehicle t){
struct Car* init_race_car(int speed,int pred_pos,int pred_width){
struct Car *b = malloc(sizeof(*b));
if (b == NULL){
perror("kein Speicherplatz");
}
int min_distance;
int distance;
int p = rand() % 10;
if (p == 0 && t == car){ // Erzeuge eine Münze
b->width = 50;
b->height = 50;
b->type = coin;
b->width = 150;
b->height = 60;
b->type = race_car;
if (pred_width == 0){ // kein Vorgänger
b->x_pos = pred_pos;
if (pred_width == 0){ // kein Vorgänger
b->x_pos = pred_pos;
}else{
min_distance = 40 * abs(speed) + 50;
distance = (rand() % 400) + min_distance;
if (speed < 0){
b->x_pos = pred_pos + pred_width + distance;
}else{
distance = 25;
b->x_pos = pred_pos - b->width - distance;
}
}
return(b);
}
struct Car* init_train(int speed,int pred_pos,int pred_width){
struct Car *b = malloc(sizeof(*b));
if (b == NULL){
perror("kein Speicherplatz");
}
int min_distance;
int distance;
b->width = 350;
b->height = 60;
b->type = train;
if (pred_width == 0){ // kein Vorgänger
b->x_pos = pred_pos;
}else{
min_distance = 40 * abs(speed) + 50;
distance = (rand() % 400) + min_distance;
if (speed < 0){
b->x_pos = pred_pos + pred_width + distance;
}else{
b->x_pos = pred_pos - b->width - distance;
}
}
return(b);
}
struct Car* init_boat(int speed,int pred_pos,int pred_width){
struct Car *b = malloc(sizeof(*b));
if (b == NULL){
perror("kein Speicherplatz");
}
int min_distance;
int distance;
b->width = 200;
b->height = 60;
b->type = boat;
if (pred_width == 0){ // kein Vorgänger
b->x_pos = pred_pos;
}else{
min_distance = 50 * abs(speed);
distance = (rand() % 300) + min_distance;
b->width = width;
b->height = height;
if (t == coin || t == coin_gathered){
b->type = car;
if (speed < 0){
b->x_pos = pred_pos + pred_width + distance;
}else{
b->type = t;
b->x_pos = pred_pos - b->width - distance;
}
if (pred_width == 0){ // kein Vorgänger
b->x_pos = pred_pos;
}
return(b);
}else{
if (t == car || t == boat){
min_distance = 50 * abs(speed);
distance = (rand() % 300) + min_distance;
}
}
else if (t == (train)){
min_distance = 40 * abs(speed) + 50;
distance = (rand() % 400) + min_distance;
}else if (t == race_car){
min_distance = 20 * abs(speed) + 100;
distance = (rand() % 300) + min_distance;
}
struct Car* init_coin(int speed,int pred_pos,int pred_width){
struct Car *b = malloc(sizeof(*b));
if (speed < 0){
b->x_pos = pred_pos + pred_width + distance;
}else{
b->x_pos = pred_pos - b->width - distance;
}
if (b == NULL){
perror("kein Speicherplatz");
}
int distance;
b->width = 50;
b->height = 50;
b->type = coin;
if (pred_width == 0){ // kein Vorgänger
b->x_pos = pred_pos;
}else{
distance = 25;
}
if (speed < 0){
b->x_pos = pred_pos + pred_width + distance;
}else{
b->x_pos = pred_pos - b->width - distance;
}
return(b);
}
struct Car* init_car(int speed,int pred_pos,int pred_width){
struct Car *b = malloc(sizeof(*b));
if (b == NULL){
perror("kein Speicherplatz");
}
int min_distance;
int distance;
b->width = 150;
b->height = 60;
b->type = car;
if (pred_width == 0){ // kein Vorgänger
b->x_pos = pred_pos;
}else{
min_distance = 50 * abs(speed);
distance = (rand() % 300) + min_distance;
if (speed < 0){
b->x_pos = pred_pos + pred_width + distance;
}else{
b->x_pos = pred_pos - b->width - distance;
}
}
return(b);
}
......@@ -82,19 +167,48 @@ struct Car* init_car(int speed,int width,int height,int pred_pos,int pred_width,
// Nachbedingung: Initialisierte LinkedList von Autos, abhängig von den Parametern
struct LinkedList_car* init_car_list(int speed,int width,int height,int start_pos,enum vehicle t){
struct LinkedList_car* init_car_list(int speed,int start_pos,enum vehicle t){
struct LinkedList_car *car_list = malloc(sizeof(*car_list));
if (car_list == NULL){
perror("kein Speicherplatz");
}
struct Car *a = init_car(speed,width,height,start_pos,0,t);
car_list->head = a;
struct Car *a;
int p;
if (t == car){
a = init_car(speed,start_pos,0);
car_list->head = a;
}else if(t == train){
a = init_train(speed,start_pos,0);
car_list->head = a;
}else if(t == boat){
a = init_boat(speed,start_pos,0);
car_list->head = a;
}else if(t == race_car){
a = init_race_car(speed,start_pos,0);
car_list->head = a;
}
for(int i = 0;i<5;i++){
struct Car *b = init_car(speed,width,height,a->x_pos,a->width,t);
struct Car *b;
if (t == car){
p = rand() % 10;
if (p == 0){
b = init_coin(speed,a->x_pos,a->width);
}else{
b = init_car(speed,a->x_pos,a->width);
}
}else if(t == train){
b = init_train(speed,a->x_pos,a->width);
}else if(t == boat){
b = init_boat(speed,a->x_pos,a->width);
}else if(t == race_car){
b = init_race_car(speed,a->x_pos,a->width);
}
a->next = b;
a = b;
......@@ -122,15 +236,12 @@ struct LinkedList_car* init_car_list(int speed,int width,int height,int start_po
// Wenn sich der Spieler auf einer Wasserreihe befindet, dann ist diese gültige Kollision zwingend.
int move_car(SDL_Renderer* renderer,struct Row *n){
int length;
int height;
int onBoat = false;
struct LinkedList_car *car_list = n->cars;
struct Car *cur = car_list->head;
bool newAuto = false;
enum vehicle newAutotype;
if ((cur->x_pos < -(cur-> width) && n->speed < 0) || (cur->x_pos > SCREEN_WIDTH && n->speed > 0)){
......@@ -138,7 +249,6 @@ int move_car(SDL_Renderer* renderer,struct Row *n){
cur = cur->next;
car_list->head = cur;
newAuto = true;
newAutotype = cur->type;
free(temp);
}
......@@ -170,28 +280,26 @@ int move_car(SDL_Renderer* renderer,struct Row *n){
cur = cur->next;
}
struct Car *new;
int p;
if (newAuto){
if (n->row_type == traintrack){
length = 300;
height = 60;
new = init_train(n->speed,cur->x_pos,cur->width);
}else if (n->row_type == streetSingle || n->row_type == streetMultiple){
if (newAutotype == coin || newAutotype == coin_gathered){
cur->x_pos = cur->x_pos - n->speed*100;
newAutotype = car;
p = rand() % 10;
if (p == 0){
new = init_coin(n->speed,cur->x_pos,cur->width);
}else{
new = init_car(n->speed,cur->x_pos,cur->width);
}
length = 150;
height = 60;
}else if (n->row_type == raceStreet){
length = 150;
height = 60;
new = init_race_car(n->speed,cur->x_pos,cur->width);
}else{
length = 200;
height = 60;
new = init_boat(n->speed,cur->x_pos,cur->width);
}
struct Car *new = init_car(n->speed,length,height,cur->x_pos,cur->width,newAutotype);
cur->next = new;
new->next= NULL;
}
......
......@@ -9,8 +9,14 @@
#include "header.h"
//Declarations of functions
struct LinkedList_car* init_car_list( int, int,int,int,enum vehicle);
struct Car* init_car(int,int,int,int,int,enum vehicle);
struct Car* init_race_car(int,int,int );
struct Car* init_train(int,int,int );
struct Car* init_coin(int,int,int );
struct Car* init_boat(int,int,int );
struct Car* init_car(int,int,int );
struct LinkedList_car* init_car_list( int, int,enum vehicle);
int move_car(SDL_Renderer* ,struct Row*);
......
......@@ -38,5 +38,7 @@ extern SDL_Texture *img_duck;
extern SDL_Texture *img_3D_Duck;
extern SDL_Texture *img_stone;
extern SDL_Texture *img_race_street;
extern SDL_Texture *img_race_carL;
extern SDL_Texture *img_race_carR;
#endif
\ No newline at end of file
images/race_carL.png

82.4 KiB

images/race_carR.png

82.4 KiB

......@@ -23,6 +23,8 @@ SDL_Texture *img_duck;
SDL_Texture *img_3D_Duck;
SDL_Texture *img_stone;
SDL_Texture *img_race_street;
SDL_Texture *img_race_carL;
SDL_Texture *img_race_carR;
TTF_Font* font;
SDL_Window* init_window(){
......@@ -93,6 +95,8 @@ int init_images(SDL_Renderer* renderer) {
img_3D_Duck = IMG_LoadTexture(renderer,"images/3D_Duck.png");
img_stone = IMG_LoadTexture(renderer,"images/stone.png");
img_race_street = IMG_LoadTexture(renderer,"images/race_street.jpg");
img_race_carL = IMG_LoadTexture(renderer,"images/race_carL.png");
img_race_carR = IMG_LoadTexture(renderer,"images/race_carR.png");
//Initiierung der Bilder
int flags = IMG_INIT_PNG | IMG_INIT_JPG;
......@@ -142,6 +146,8 @@ int exitGame(SDL_Renderer* renderer, SDL_Window* window) {
SDL_DestroyTexture(img_3D_Duck);
SDL_DestroyTexture(img_stone);
SDL_DestroyTexture(img_race_street);
SDL_DestroyTexture(img_race_carL);
SDL_DestroyTexture(img_race_carR);
TTF_CloseFont(font);
SDL_Quit();
......
......@@ -85,6 +85,10 @@ int paste_row(SDL_Renderer* renderer, int y, enum row row_type){
int paste_car(SDL_Renderer* renderer, int x, int y, int width, int height,int speed,enum vehicle type) {
//Prevents cars from being displayed at the wrong position
int offset = 20;
if (type == race_car){
offset = 20;
}
if (type == coin){
offset = 25;
......@@ -104,7 +108,7 @@ int paste_car(SDL_Renderer* renderer, int x, int y, int width, int height,int sp
return(1);
}
return(0);
}else if(type == car || type == race_car){
}else if(type == car){
if (speed > 0) {
if (SDL_RenderCopy(renderer, img_car_trans, NULL, &rect) != 0) {
SDL_Log("Bild konnte nicht kopiert werden! SDL_Error Error: %s\n",SDL_GetError());
......@@ -118,6 +122,22 @@ int paste_car(SDL_Renderer* renderer, int x, int y, int width, int height,int sp
}
return(0);
}
}else if(type == race_car){
if (speed > 0){
if (SDL_RenderCopy(renderer, img_race_carR, NULL, &rect) != 0) {
SDL_Log("Bild konnte nicht kopiert werden! SDL_Error Error: %s\n",SDL_GetError());
return(1);
}
}else{
if (SDL_RenderCopy(renderer, img_race_carL, NULL, &rect) != 0) {
SDL_Log("Bild konnte nicht kopiert werden! SDL_Error Error: %s\n",SDL_GetError());
return(1);
}
}
return(0);
}else if(type == train){
if (speed > 0){
if (SDL_RenderCopy(renderer, img_trainR, NULL, &rect) != 0) {
......
......@@ -13,11 +13,9 @@ struct Row *race(int y_pos){
n->row_type = raceStreet;
n->y_pos = y_pos;
int width = 150;
int height = 60;
n->speed = (((rand()%2) * 2) -1) * 16;
n->cars = init_car_list(n->speed,width,height,(900-n->speed) % 900,race_car);
n->cars = init_car_list(n->speed,(900-n->speed) % 900,race_car);
n->stone = -1;
return(n);
......@@ -69,11 +67,9 @@ struct Row* street(bool first,int y_pos){
}
n->y_pos = y_pos;
int width = 150;
int height = 60;
n->speed = (((rand()%2) * 2) -1) * ((rand()%3)+2);
n->cars = init_car_list(n->speed,width,height,(900-n->speed) % 900,car);
n->cars = init_car_list(n->speed,(900-n->speed) % 900,car);
n->stone = -1;
return(n);
}
......@@ -88,11 +84,9 @@ struct Row* track(int y_pos){
n->y_pos = y_pos;
n->row_type = traintrack;
int width = 350;
int height = 60;
n->speed = 7 *(((rand()%2)*2)-1);
n->cars = init_car_list(n->speed,width,height,(SCREEN_WIDTH-n->speed) % SCREEN_WIDTH,train);
n->cars = init_car_list(n->speed,(SCREEN_WIDTH-n->speed) % SCREEN_WIDTH,train);
n->stone = -1;
return(n);
}
......@@ -121,11 +115,9 @@ struct Row* water(bool first,bool dark,int y_pos){
}
n->y_pos = y_pos;
int width = 200;
int length = 60;
n->speed = 2 *(((rand()%2)*2)-1);
n->cars = init_car_list(n->speed,width,length,(SCREEN_WIDTH-n->speed)%SCREEN_WIDTH,boat);
n->cars = init_car_list(n->speed,(SCREEN_WIDTH-n->speed)%SCREEN_WIDTH,boat);
n->stone = -1;
return(n);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment