Question 2:
Create a simple pixelated snake game with a start button and a score for each fruit you eat. If you touch the borders go back to the start scene.
You must complete this question using true OOP with classes:
- inheritance
- polymorphism
- encapsulation
#include <iostream>
#include <windows.h>
using namespace std;
struct position {
int x,y;
};
class field_cls {
static const int height;
static const int width;
char ** field;
field_cls(const field_cls &);
field_cls operator=(const field_cls
&);
public:
field_cls() {
field = new
char*[field_cls::height];
for(int c = 0; c <
field_cls::height; ++c) {
field[c] = new char[field_cls::width];
}
}
~field_cls() {
for(int c = 0; c <
field_cls::height; ++c) {
delete[] field[c];
}
delete[] field;
}
void print() {
for(int c = 0; c <
height; ++c) {
for(int r = 0; r < width; ++r) {
cout << field[c][r];
}
cout << endl;
}
}
void clear() {
for(int c = 0; c <
height; ++c) {
for(int r = 0; r < width; ++r) {
field[c][r] = ' ';
}
}
}
int get_width() const {return width;}
int get_height() const {return height;}
void draw(int y, int x, char what) {
//y = (y < 0) ? 0 :
(y >= height ? height - 1 : y);
//x = (x < 0) ? 0 :
(x >= width ? width - 1 : x);
field[y][x] =
what;
}
} field;
class food_cls {
position pos;
char symbol;
public:
food_cls(): symbol('X'), pos() {
pos.x = pos.y =
-1;
}
void set_pos(int x, int y) {
pos.x = x;
pos.y = y;
}
void reposition(const field_cls & field)
{
pos.x = rand() %
field.get_width();
pos.y = rand() %
field.get_height();
}
int get_x() const {return pos.x;}
int get_y() const {return pos.y;}
char get_symbol() const {return symbol;}
} food;
class snake_cls {
enum {UP, DOWN, LEFT, RIGHT} dir;
char symbol, head_symbol;
position pos[100];
position & head;
int speed;
int size;
bool can_turn;
public:
snake_cls(int x, int y):
symbol('#'),
head_symbol('@'), pos(),
speed(1), size(1),
dir(RIGHT),
head(pos[0]),
can_turn(true)
{
pos[0].x = x;
pos[0].y = y;
}
bool check_food(const food_cls & food)
{
if(food.get_x() ==
head.x && food.get_y() == head.y) {
size += 1;
return true;
}
return false;
}
void get_input(const field_cls & field)
{
if(GetAsyncKeyState(VK_UP) && dir != DOWN) {
dir = UP;
}
if(GetAsyncKeyState(VK_DOWN) && dir != UP) {
dir = DOWN;
}
if(GetAsyncKeyState(VK_LEFT) && dir != RIGHT) {
dir = LEFT;
}
if(GetAsyncKeyState(VK_RIGHT) && dir != LEFT) {
dir = RIGHT;
}
}
void move(const field_cls & field)
{
position next = {0,
0};
switch(dir) {
case UP:
next.y = -speed;
break;
case DOWN:
next.y = speed;
break;
case LEFT:
next.x = -speed;
break;
case RIGHT:
next.x = speed;
}
for(int c = size - 1; c
> 0; --c) {
pos[c] = pos[c-1];
}
head.x += next.x;
head.y += next.y;
if(head.x < 0 ||
head.y < 0 || head.x >= field.get_width() || head.y >=
field.get_height()) {
throw "DEADD!!!!";
}
}
void draw(field_cls & field) {
for(int c = 0; c <
size; ++c) {
if(c == 0) {
field.draw(pos[c].y, pos[c].x, head_symbol);
} else {
field.draw(pos[c].y, pos[c].x, symbol);
}
}
}
int get_x() const { return head.x; }
int get_y() const { return head.y; }
char get_symbol() const { return symbol; }
} snake(1, 1);
const int field_cls::height = 24;
const int field_cls::width = 79;
int main() {
field.clear();
food.set_pos(5, 5);
while(1) {
field.clear();
snake.get_input(field);
try {
snake.move(field);
} catch (const char *
er) {
field.clear();
cerr << er << endl;
system("pause");
return -1;
}
snake.draw(field);
field.draw(food.get_y(),
food.get_x(), food.get_symbol());
if(snake.check_food(food)) {
food.reposition(field);
}
field.print();
Sleep(1000/30);
system("cls");
}
return 0;
}
Question 2: Create a simple pixelated snake game with a start button and a score for...
Using Java develop a GUI based simple quiz game. The questions and answers of the game will be stored in a text file (i.e. our database). On the first page the users will be provided with some instructions on how to play the game and next button to go to the next page. On the next page will be a start button to either start the game and an exit button to exit the game. Once the game starts there...
Game Description: Most of you have played a very interesting game “Snake” on your old Nokia phones (Black & White). Now it is your time to create it with more interesting colors and features. When the game is started a snake is controlled by up, down, left and right keys to eat food which appears on random locations. By eating food snake’s length increases one unit and player’s score increases by 5 points. Food disappears after 15 seconds and appears...
Solve the Sudoku game using the inputs available online. You can create your own input file as long is it is in the same format as the sample files given. Lookup online the rules for Sudoku if you are uncertain. You will be required to use concepts we have gone over in the class. Do not use techniques we have not discussed! Items that are required: Read in game board from a .txt file. Write out solution to game board...
For this assignment, your job is to create a simple game called
Opoly.
The objectives of this assignment are to:
Break down a problem into smaller, easier problems.
Write Java methods that call upon other methods to accomplish
tasks.
Use a seed value to generate random a sequence of random
numbers.
Learn the coding practice of writing methods that perform a
specific task.
Opoly works this way: The board is a circular track of variable
length (the user determines the...
Project 2 – Memory Match Game Purpose This Windows Classic Desktop application plays a simple matching game. The game simulates a card game where the cards a placed face down and the player flips over pairs of cards in an attempt to find matching cards. Program Procedure Display a 4x4 grid of “face down” cards. Assign the letters A through H randomly to the cards in pairs. Allow the user to click on a card to “flip” it over and...
C++ Hangman (game) In this project, you are required to implement a program that can play the hangman game with the user. The hangman game is described in https://en.wikipedia.org/wiki/Hangman_(game) . Your program should support the following functionality: - Randomly select a word from a dictionary -- this dictionary should be stored in an ASCII text file (the format is up to you). The program then provides the information about the number of letters in this word for the user to...
For this assignment you will create a one-player variation of the game BlackJack. The game will be divided into five rounds. The goal of each round is to accumulate a hand of cards that gets as close to 21 without going over. At the beginng of the game you will start with a score of 100 points. After each round, the difference between your hand and 21 will be subtracted from your total score. The object of the entire game...
This simple practical task is the start of a series of exercises in which you will develop a small bank- ing system applying all important OOP concepts. Your first task is to complete the Account class, one of the core classes of the future program. You will need to focus on the use of classes and ob- jects and the ability to capture knowledge and behaviour within objects. The banking system you are developing must have an account, which allows...
java In this project you will implement a trivia game. It will ask random trivia questions, evaluate their answers and keep score. The project will also have an administrative module that will allow for managing the question bank. Question bank management will include adding new questions, deleting questions and displaying all of the questions, answers and point values. 2. The project can be a GUI or a menu based command line program. 3. Project details 1. Create a class to...