Skip to content

Commit

Permalink
all but drop command
Browse files Browse the repository at this point in the history
  • Loading branch information
sinbrive committed Dec 29, 2022
1 parent aba87e6 commit f8a644f
Show file tree
Hide file tree
Showing 13 changed files with 175 additions and 78 deletions.
28 changes: 28 additions & 0 deletions src/GameOver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "GameOver.h"

GameOver::GameOver() {
}

void GameOver::setup() {

}

void GameOver::update() {

}

void GameOver::draw() {
ofApp::game.draw();
ofPushStyle();
ofSetColor(0, 255, 0);
ofDrawBitmapString("Restart: F1", 210, ofGetHeight() - 50);
ofPopStyle();
}

void GameOver::inputKey(int key) {
if (key == OF_KEY_F1) {
State::setState(&ofApp::game);
State::getState()->setup();
return;
}
};
28 changes: 9 additions & 19 deletions src/GameOver.h
Original file line number Diff line number Diff line change
@@ -1,33 +1,23 @@
#pragma once

#include "State.h"
#include "game.h"

//#include "game.h"
#include "ofApp.h"
#include "State.h"

class GameOver : public State {


public:


GameOver() {

}


void setup() {
GameOver();

}
void setup();

void update() {
void update();

}
void draw();

void draw() {
ofSetColor(255);
ofDrawBitmapString("game over: ENTER to restart", 6, 12);
}
void inputKey(int key);

void inputKey(int key) {};
};

};
27 changes: 27 additions & 0 deletions src/GamePause.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "GamePause.h"

GamePause::GamePause() {
}

void GamePause::setup() {

}

void GamePause::update() {

}

void GamePause::draw() {
ofApp::game.draw();
ofPushStyle();
ofSetColor(255, 0, 0);
ofDrawBitmapString("Resume: 'p'", 210, ofGetHeight()-50);
ofPopStyle();
}

void GamePause::inputKey(int key) {
if (key == 'p') {
State::setState(&ofApp::game);
return;
}
};
21 changes: 21 additions & 0 deletions src/GamePause.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include "State.h"
#include "ofApp.h"


// the start screen
class GamePause : public State {

public:

GamePause();

void setup();

void update();

void draw();

void inputKey(int key);
};
39 changes: 39 additions & 0 deletions src/GameStart.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "GameStart.h"

GameStart::GameStart() {

}

void GameStart::setup() {
ofBackground(255);
}

void GameStart::update() {

}

void GameStart::draw() {

ofBackground(100,100,200);

ofSetColor(200, 200, 0);

ofDrawBitmapString("C++ Tetris Game", 40, 50);
ofSetColor(255, 255, 0);
ofDrawBitmapString("LEFT and RIGHT for horizontal movement.", 30, 100);
ofDrawBitmapString("DOWN to move down quickly,", 30, 120);
ofDrawBitmapString("UP arrow for rotations.", 30, 140);
ofDrawBitmapString("SPACE for dropping down.", 30, 160);
ofDrawBitmapString("Pressing 'q' quits the game.", 30, 180);
ofDrawBitmapString("Pressing 'p' pauses the game.", 30, 200);
ofDrawBitmapString("Enter to start the game", 30, 250);
}

void GameStart::inputKey(int key) {

if (key == OF_KEY_RETURN) {
State::setState(&ofApp::game);
State::getState()->setup();
return;
}
}
38 changes: 7 additions & 31 deletions src/GameStart.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,21 @@


#include "State.h"
#include "game.h"
#include "ofApp.h"


// the start screen
class GameStart : public State {

private:


public:

GameStart();

GameStart() {

}

void setup() {
ofBackground(255);
}

void update() {

}

void draw() {
ofSetColor(0);
ofDrawBitmapString("start: click to begin", 6, 12);
void setup();

//game.displayGrid();
//game.displaySideBoard();
ofSetColor(255, 100, 100);
void update();

ofDrawBitmapString("LEFT and RIGHT for horizontal movement.", 30, 100);
ofDrawBitmapString("DOWN to move down quickly,", 30, 120);
ofDrawBitmapString("UP arrow for rotations.", 30, 140);
ofDrawBitmapString("SPACE for dropping down.", 30, 160);
ofDrawBitmapString("Pressing 'q' quits the game.", 30, 180);
ofDrawBitmapString("Pressing 'p' pauses the game.", 30, 200);
ofDrawBitmapString("Enter to start the game", 30, 250);
}
void draw();

void inputKey(int key) {};
void inputKey(int key);
};
6 changes: 3 additions & 3 deletions src/State.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#pragma once
// credit : https://github.com/danomatika/CreativeCodingExamples/blob/master/GameState/src/State.h
#include "ofMain.h"


// credit : https://github.com/danomatika/CreativeCodingExamples/blob/master/GameState/src/State.h
// a base game state class,
// kind of like a mini Sketch
//
Expand All @@ -11,7 +10,7 @@
class State {

public:

private:
static State* currentState;

Expand All @@ -28,5 +27,6 @@ class State {
virtual void update() = 0;

virtual void draw() = 0;

virtual void inputKey(int key) = 0;
};
7 changes: 6 additions & 1 deletion src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ void Game::removeLine(float row) {
void Game::checkGameOver() {
for (const auto& obj : lines) {
if (obj.y < 60) {
State::setState(ofApp::gameOver);
State::setState(&ofApp::gameOver);
State::getState()->setup();
return;
}
Expand Down Expand Up @@ -160,6 +160,11 @@ void Game::inputKey(int key) {

float x, y;

if (key == 'p') {
State::setState(&ofApp::gamePause);
return;
}

if (key == OF_KEY_RIGHT) {
x = shape.getXoffset();
if (x < ofGetWidth() - 100 - 20 * shape.getNbX()) shape.move(20, 0);
Expand Down
3 changes: 2 additions & 1 deletion src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#include "State.h"
#include "ofMain.h"
#include "shape.h"
#include "GameOver.h"
//#include "GameOver.h"


class Game : public State {

Expand Down
26 changes: 10 additions & 16 deletions src/ofApp.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#include "ofApp.h"

State& ofApp::gameStart=GameStart();

/*State* ofApp::gameStart= new GameStart();
State* ofApp::gameOver= new GameOver();
State* ofApp::game = new Game();
*/
State& ofApp::game=Game();

State& ofApp::gameOver = GameOver();

State& ofApp::gamePause = GamePause();

State* ofApp::gameOver = new GameOver();

//--------------------------------------------------------------
void ofApp::setup() {
Expand All @@ -19,7 +20,7 @@ void ofApp::setup() {

ofSetFrameRate(60);

State::setState(gameStart);
State::setState(&gameStart);

State::getState()->setup();
}
Expand All @@ -32,22 +33,15 @@ void ofApp::draw() {
//----------------------------------
void ofApp::update() {
State::getState()->update();

}


//--------------------------------------------------------------
void ofApp::keyPressed(int key) {
void ofApp::keyPressed(int key) {

if (State::getState() == game) {
State::getState()->inputKey(key);
return;
}
if (key == 'q') ofExit();

if (key == OF_KEY_RETURN) {
State::setState(game);
State::getState()->setup();
}
State::getState()->inputKey(key);
}

//--------------------------------------------------------------
Expand Down
12 changes: 5 additions & 7 deletions src/ofApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "GameStart.h"
#include "Game.h"
#include "GameOver.h"
//#include "GamePlaying.h"
#include "GamePause.h"

class ofApp : public ofBaseApp {

Expand All @@ -26,11 +26,9 @@ class ofApp : public ofBaseApp {
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);


State* gameStart=new GameStart();
static State* gameOver;
State* game=new Game();


static State& game;
static State& gameStart;
static State& gameOver;
static State& gamePause;

};
5 changes: 5 additions & 0 deletions tetris-game-v2.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,19 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="src\game.cpp" />
<ClCompile Include="src\GameOver.cpp" />
<ClCompile Include="src\GamePause.cpp" />
<ClCompile Include="src\GameStart.cpp" />
<ClCompile Include="src\main.cpp" />
<ClCompile Include="src\ofApp.cpp" />
<ClCompile Include="src\shape.cpp" />
<ClCompile Include="src\State.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\tetris-game-v2\src\ofApp.h" />
<ClInclude Include="src\game.h" />
<ClInclude Include="src\GameOver.h" />
<ClInclude Include="src\GamePause.h" />
<ClInclude Include="src\GameStart.h" />
<ClInclude Include="src\ofApp.h" />
<ClInclude Include="src\shape.h" />
Expand Down
Loading

0 comments on commit f8a644f

Please sign in to comment.