#include <math.h> #include <stdlib.h> #include <string.h> #include <cab202_graph
ID: 3727590 • Letter: #
Question
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <cab202_graphics.h>
#include <cab202_sprites.h>
#include <cab202_timers.h>
// Configuration
#define DELAY (10) /* Millisecond delay between game updates */
#define ZOMBIE_WIDTH (5)
#define ZOMBIE_HEIGHT (5)
// Game state.
bool game_over = false; /* Set this to true when game is over */
bool update_screen = true; /* Set to false to prevent screen update. */
char * zombie_image =
/**/ "ZZZZZ"
/**/ " Z "
/**/ " Z "
/**/ " Z "
/**/ "ZZZZZ";
// (a) Declare a sprite_id called zombie
// Setup game.
void setup(void) {
// (b) Assign a value to zombie by creating a sprite at integer
// coordinates (x,y). Use integer arithmetic to calculate
// the location relative to the screen dimensions:
// x = 20% of available horizontal space on the screen.
// y = 60% of available vertical space on the screen.
// Use the formula set out in the statement of the problem.
// (c) Draw the zombie.
}
// Play one turn of game.
void process(void) {
// (d) Get a character code from standard input without waiting.
// (e) Get the current screen coordinates of the zombie in integer variables
// by rounding the actual coordinates. Store the coordinates in a pair of
// variables called x and y, both of type int.
// (f) Move zombie left, ensuring that it always remains within the screen.
// (g) Move zombie right, ensuring that it always remains within the screen.
// (h) Move zombie up, ensuring that it always remains within the screen.
// (i) Move zombie down according to specification.
// (j) If the key is 'q', set game_over to true.
// Leave next line intact.
clear_screen();
// (k) Draw the zombie.
}
// Program entry point.
int main(void) {
setup_screen();
setup();
show_screen();
while ( !game_over ) {
process();
if ( update_screen ) {
show_screen();
}
timer_pause(DELAY);
}
return 0;
Complete the implementation of the running_zombie program. This function will enhance your zombie game coding powers. exercise is a straight-forward extension of the worked example presented in Lecture 2. The aim of the task is to let you build skills at character input handling, multi-way if statements, and working with sprites in the zombie game framework. These techniques used here are directly applicable to your assignment. submission box below contains a skeleton function in which a number of statements have been replaced by itemised instructions labelled (a) through (k). Each item requires the insertion of a small number (between 1 and 3) of C language instructions which are similar to code demonstrated in Lecture 2, and described in detail in the Topic 2 Lecture Notes. e program requirements are as follows: .A zombie avatar, represented by a 5x5 'Z' made of Z' characters, must be placed on the screen. . The initial position of the sprite is measured relative to the available space on the screen. The position consists of two percentage values, one for the horizontal coordinate, and one for the vertical coordinate. These values are shown in the source code When calculating the location of the zombie, be sure to take the size of the zombie into account. Thus, an x position of 0% corresponds to the left-most character position in the terminal window while an x position of 100% would place the zombie so that the right-hand column ofthe image occupies the nght most column of the terminal window . Given a percentage p. In a window of width w. with a sprite of width ws, the corresponding coordinate position is x=(w-ws) *p/100 . The zombie moves in response to the following character input o 2 '-move down. o 4' move lelt. 0 '6'-move right. . 8 ' move up. . No part of the zombic is permitted to extend beyond the bounds of the terminal window at any time When the 'q' key is pressed, the game should exil. Notes . Make no assumption about the se of the terminal window, your program will have to work correctly in a resized widow with arbitrary dimensions. ·The program should produce a display 1 at resembles lhe image below. Your results will not be exactly the same as this view, because each user gets a unique sel parameters. However, it should give you an idea what to aim for /cygdrive/a/AMS/AMS_ 2016/CAB202/ExerciseDevelopment...L-Explanation / Answer
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <cab202_graphics.h>
#include <cab202_sprites.h>
#include <cab202_timers.h>
// Configuration
#define DELAY (10) /* Millisecond delay between game updates */
#define ZOMBIE_WIDTH (5)
#define ZOMBIE_HEIGHT (5)
// Game state.
bool game_over = false; /* Set this to true when game is over */
bool update_screen = true; /* Set to false to prevent screen update. */
char * zombie_image =
/**/ "ZZZZZ"
/**/ " Z "
/**/ " Z "
/**/ " Z "
/**/ "ZZZZZ";
// (a) Declare a sprite_id called zombie
sprite_id zombie;
// Setup game.
void setup(void) {
// (b) Assign a value to zombie by creating a sprite at integer
// coordinates (x,y). Use integer arithmetic to calculate
// the location relative to the screen dimensions:
// x = 60% of available horizontal space on the screen.
// y = 60% of available vertical space on the screen.
// Use the formula set out in the statement of the problem.
int x = ((screen_width() - ZOMBIE_WIDTH) * 60)/100;
int y = ((screen_height() - ZOMBIE_HEIGHT) * 60)/100;
zombie = sprite_create(x, y, ZOMBIE_WIDTH, ZOMBIE_HEIGHT, zombie_image);
// (c) Draw the zombie.
sprite_draw(zombie);
}
// Play one turn of game.
void process(void) {
// (d) Get a character code from standard input without waiting.
int key = get_char();
// (e) Get the current screen coordinates of the zombie in integer variables
// by rounding the actual coordinates. Store the coordinates in a pair of
// variables called x and y, both of type int.
int x = round(sprite_x(zombie));
int y = round(sprite_y(zombie));
// (f) Move zombie left, ensuring that it always remains within the screen.
int w = screen_width(), h = screen_height();
if ( key == '4' && x > 0 ){
sprite_move(zombie, -1, 0);
}
// (g) Move zombie right, ensuring that it always remains within the screen.
if ( key == '6' && x < w - sprite_width(zombie)){
sprite_move(zombie, +1, 0);
}
// (h) Move zombie up, ensuring that it always remains within the screen.
if ( key == '8' && y > 0 ){
sprite_move(zombie, 0, -1);
}
// (i) Move zombie down according to specification.
if ( key == '2' && y < h - sprite_height(zombie) ){
sprite_move(zombie, 0, +1);
}
// (j) If the key is 'q', set game_over to true.
if ( key == 'q' ) {
game_over = true;
}
// Leave next line intact.
clear_screen();
// (k) Draw the zombie.
sprite_draw(zombie);
}
// Program entry point.
int main(void) {
setup_screen();
setup();
show_screen();
while ( !game_over ) {
process();
if ( update_screen ) {
show_screen();
}
timer_pause(DELAY);
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.