Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Programing: Reuse your code from part A, add a dog at spot (200,200). This d

ID: 3580214 • Letter: C

Question

C++ Programing:

Reuse your code from part A, add a dog at spot (200,200). This dog will walk towards the cat at speed 25 units. Choose the x or y direction that is the furthest distance away from the cat. If there is a tie (i.e. both the x-distance and y-distance are the same), then the dog should move closer in the ydirection. If the dog gets within 50 units of the cat, you should cout “The dog bites you!” and stop your program. You should first move the cat, then the dog

Here is part A:

For this problem, you will have to use GraphX (this means you must code or remote connect to a cselabs machine, GraphX will probably not work on your personal computer). Make the GraphX window 400 by 400. Draw a cat at spot (350,350) and a food item at (50,50). If you press 'w', move the cat up, 'a' moves the cat left, 's' moves the cat down and 'd' moves the cat right. (See: readKeys.cpp for an example on how to read input from the GraphX window.) Each time the cat moves, it will move 50 units. If the cat moves outside the window (or on the edge), stop the program and say (i.e. cout) “You fell off the world”. If you move on top of the food, cout “You win”. Note: you will not be graded on your artistic capabilities. Example 1 (user input is underlined, bolded input is typed into GraphX window)

this is the example code( readKeys.cpp)

Explanation / Answer

Solution:

#include <iostream>

#include <conio>

#include <stdlib>

#include <array>

#include <string>

#include "GraphX.hpp" // look here for a list & description of functions

using namespace std;

int main()
{
clrscr();
//MAKE SURE YOU HAVE THE GRAPHX WINDOW HIGHLIGHTED
// (yes, the window is blank)
// if you have the terminal selected, it will not work!
string input;
GraphX mywin; // the class that does the window for us
char c = 'h'; // c is the last character read

while(c != 'q') // stop when we press 'q' (continue when not 'q')
{
input = mywin.input(); // read what type of input we got
if(mywin.key.keysym.sym == SDLK_DOWN) // hey! we pressed a key!

{
// Clear screen
SDL_FillRect(screen, NULL, black);

//apply
apply_surface(0, 10, background, screen);
draw_surface(srcX, srcY, dstX, dstY+50, width, heigth, background, screen);

//update screen
SDL_Flip(screen);
}
else if(mywin.key.keysym.sym == SDLK_UP) // hey! we pressed a key!
{
// Clear screen
SDL_FillRect(screen, NULL, black);

//apply
apply_surface(0, 10, background, screen);
draw_surface(srcX, srcY, dstX+50, dstY, width, heigth, background, screen);

//update screen
SDL_Flip(screen);
}
else if(mywin.key.keysym.sym == SDLK_LEFT) // hey! we pressed a key!
{
// Clear screen
SDL_FillRect(screen, NULL, black);

//apply
apply_surface(0, 10, background, screen);
draw_surface(srcX+50, srcY, dstX, dstY, width, heigth, background, screen);

//update screen
SDL_Flip(screen);
}
else if(mywin.key.keysym.sym == SDLK_RIGHT) // hey! we pressed a key!
{
// Clear screen
SDL_FillRect(screen, NULL, black);

//apply
apply_surface(0, 10, background, screen);
draw_surface(srcX, srcY+50, dstX, dstY, width, heigth, background, screen);

//update screen
SDL_Flip(screen);

}
}

void reDraw__Ssurface(int srcX, int srcY, int dstX, int dstY, int width, int height, SDL_Surface *source, SDL_Surface *destination)
{
SDL_Rect src;
src.x = srcX;
src.y = srcY;
src.w = width;
src.h = height;

SDL_Rect dst;
dst.x = dstX;
dst.y = dstY;
dst.w = width;
dst.h = height;

SDL_BlitSurface(source, &src, destination, &dst);
}
return 0;
getch();

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote