Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <iostream>
- #include <stdlib.h> // need this for system
- #include "platform_conio.h"
- using namespace std;
- void resetToStart() {
- platform_move(0,0);
- }
- char getCH() { // get a single key from the user
- return platform_getchar();
- }
- struct Vec2 {
- int x, y;
- bool isEqual(Vec2 point) {
- return x == point.x && y == point.y;
- }
- };
- struct Entity {
- Vec2 position;
- char icon;
- };
- int whichPlayerIsHere(Vec2 point, Entity * list, int listSize) {
- for(int i = 0; i < listSize; ++i) {
- if(list[i].position.isEqual(point)) {
- return i;
- }
- }
- return -1;
- }
- int main() {
- Vec2 size = {20, 15};
- Entity players[] = {
- {{ 4, 5}, '@'},
- {{ 2, 4}, 'A'},
- {{12, 8}, 'X'}
- };
- const int playersCount = sizeof(players)/sizeof(players[0]);
- int userInput;
- bool gameRunning = true;
- int selectedPlayer = 0;
- // gameloop
- while(gameRunning) {
- // draw
- resetToStart();
- Vec2 cursor;
- for(cursor.y = 0; cursor.y < size.y; ++cursor.y) {
- for(cursor.x = 0; cursor.x < size.x; ++cursor.x) {
- putchar('.');
- }
- putchar('\n');
- }
- for(int i = 0; i < playersCount; ++i)
- {
- platform_move(players[i].position.y, players[i].position.x);
- putchar(players[i].icon);
- }
- while(!platform_kbhit()) { platform_sleep(1); } // wait for key press
- // get input from user
- userInput = platform_getchar();
- // process game based on user input
- switch(userInput) {
- case 'w': players[selectedPlayer].position.y--; break;
- case 'a': players[selectedPlayer].position.x--; break;
- case 's': players[selectedPlayer].position.y++; break;
- case 'd': players[selectedPlayer].position.x++; break;
- case 'q': gameRunning = false; break;
- case '+':
- selectedPlayer++;
- if(selectedPlayer >= playersCount)
- selectedPlayer = 0;
- break;
- case '-':
- selectedPlayer--;
- if(selectedPlayer <= 0)
- selectedPlayer = playersCount-1;
- break;
- case 'e': players[selectedPlayer].icon++; break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement