Question
Please Help me convert my C game into a structure ( The current code works already). The information of a game character should be stored
Please Help me convert my C game into a structure ( The current code works already). The information of a game character should be stored in a structure Exercise:
Rewrite the code in such a way that certain functionalities are no longer in the main function itself but that one takes functions for it. These functions should be used within the main function.
We have three functions:
1) draw_figure Draws the figure with the correct graphic at the right place 2) are_colliding Takes over two figure structures and checks if they are colliding 3) move_up, move_down, move_left, move_right Moves a figure up, down, left, right and makes sure that it does not that it does not move from the playing field
Here is my C code:
#include "mcigraph.hpp" #include
typedef struct Figure{ int x,y; char *img; }Figure;
int main(int argc, char *argv[]) { Figure c1 {32,24, "character.bmp"}; // current position of the character Figure c2 {16,24, "character2.bmp"};// current position of the character 2 while (running()) { for (int x = 0; x < 1024; x += 16) // fill the whole window with grass for (int y = 0; y < 768; y += 16) draw_image("grass.bmp", x, y); // Move figure according to keypresses if (was_pressed(KEY_LEFT)) { c1.x--; if (c1.x < 0||(c1.x== c2.x&&c1.y==c2.y)) c1.x++; } if (was_pressed(KEY_RIGHT)) { c1.x++; if (c1.x > 63||(c1.x== c2.x&&c1.y==c2.y)) c1.x--; } if (was_pressed(KEY_UP)) { c1.y--; if (c1.y < 0||(c1.x== c2.x&&c1.y==c2.y)) c1.y++; } if (was_pressed(KEY_DOWN)) { c1.y++; if (c1.y > 47||(c1.x== c2.x&&c1.y==c2.y)) c1.y--; }
//second character move
if (was_pressed(KEY_A)) { c2.x--; if ( c2.x < 0||(c1.x== c2.x&&c1.y==c2.y)) c2.x++; } if (was_pressed(KEY_D)) { c2.x++; if ( c2.x > 63||(c1.x== c2.x&&c1.y==c2.y)) c2.x--; } if (was_pressed(KEY_W)) { c2.y--; if (c2.y < 0||(c1.x== c2.x&&c1.y==c2.y)) c2.y++; } if (was_pressed(KEY_S)) { c2.y++; if (c2.y > 47||(c1.x== c2.x&&c1.y==c2.y)) c2.y--; }
draw_image(c1.img, c1.x * 16, c1.y * 16); // Set the character draw_image(c2.img, c2.x * 16, c2.y * 16); // Set the second character present(); } return 0; }
Write me a comment if you want have my files. I can give you my email so I can sand the file to you.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started