Problem A: Hungry, hungry, kitty! (20 points) For this problem, you will have to
ID: 3576497 • Letter: P
Question
Problem A: Hungry, hungry, kitty! (20 points) 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
#include <iostream>
#include "GraphX.hpp" // look here for a list & description of functions
using namespace std;
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);
}
int main()
{
//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);
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.