Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Revision 3:522c6f850e91, committed 2020-05-24
- Comitter:
- el18rs
- Date:
- Sun May 24 11:18:33 2020 +0000
- Parent:
- 2:55092965eadd
- Child:
- 4:7ddd287a5d28
- Commit message:
- Game engine added
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/TetrisGame/TetrisGame.cpp Sun May 24 11:18:33 2020 +0000
@@ -0,0 +1,90 @@
+#include "TetrisGame.h"
+
+TetrisGame::TetrisGame()
+{
+
+}
+
+TetrisGame::~TetrisGame()
+{
+
+}
+
+void TetrisGame::init(int tetromino_width, int tetromino_height, int tetromino_size, int speed)
+{
+ _tetromino_width = tetromino_width;
+ _tetromino_height = tetromino_height;
+ _tetromino_size = tetromino_size;
+ _speed = speed;
+
+ // CHANGE SO STARTS AT TOP?
+ _p1x = GAP;
+ _p1.init(_p1x,_tetromino_height,_tetromino_width);
+ _tetromino.init(_tetromino_height, _tetromino_width, _tetromino_size,_speed);
+}
+
+void TetrisGame::read_input(Gamepad &pad)
+{
+ _d = pad.get_direction();
+ _mag = pad.getmag();
+}
+
+void TetrisGame::draw(N5110 &lcd)
+{
+
+ lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
+
+ _p1.draw(lcd);
+}
+
+void TetrisGame::update(Gamepax &pad)
+{
+ check_goal(pad);
+
+ _p1.update(_d,_mag);
+
+ check_floor_collision(pad);
+ check_tetromino_collisions(pad);
+}
+
+void TetrisGame::check_floor_collision(Gamepad &pad)
+{
+ Vector2D tetromino_pos = _tetromino.get_pos();
+
+ if (tetromino_pos.y + _tetromino_size >= (HEIGHT-1) ) {
+
+ tetromino_pos.y = (HEIGHT-1) - _tetromino_size;
+ tetromino_velocity.y = 0;
+
+ pad.tone(750.0,0.1);
+ }
+ // update tetromino parameters??
+}
+
+void TetrisGame::check_tetromino_collisions(Gamepad &pad)
+{
+ Vector2D tetromino_pos = _tetromino.get_pos();
+ Vector2D tetromino_velocity = _tetromino.get_velocity();
+
+ Vector2D p1_pos = _p1.get_pos();
+
+ if (
+ (tetromino_pos.y >= p1_pos.y) &&
+ (tetromino_pos.y <= p1_pos.y + _tetromino_height) &&
+ (tetromino_pos.x >= _p1x) &&
+ (tetromino_pos.x <= _plx + _tetromino_width)
+ ) {
+ tetromino_pos.x = _plx + _tetromino_width;
+ tetromino_velocity.x = 0;
+
+ pad.tone(1000.0,0.1);
+ }
+
+
+
+
+
+
+
+
+
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/TetrisGame/TetrisGame.h Sun May 24 11:18:33 2020 +0000
@@ -0,0 +1,45 @@
+#ifndef TETRISGAME_H
+#define TETRISGAME_H
+
+#include "mbed.h"
+#include "N5110.h"
+#include "Gamepad.h"
+#include "Tetromino.h"
+
+#define GAP 0
+
+class TetrisGame
+{
+
+ public:
+ TetrisGame();
+ ~TetrisGame();
+
+ void init(int tetromino_width, int tetromino_height, int tetromino_size, int speed);
+ void read_input(Gamepad &pad);
+ void update(Gamepad &pad);
+ void draw(N5110 &lcd);
+
+ private:
+
+ void check_wall_collision(Gamepad &pad);
+ void check_tetromino_collision(Gamepad &pad);
+ void check_goal(Gamepad &pad);
+
+ Tetromino _p1;
+
+ int _tetromino_width;
+ int _tetromino_height;
+ int _tetromino_size;
+ int _speed;
+
+ int _p1x;
+
+ Tetromino _tetromino;
+
+ Direction _d;
+ float _mag;
+
+};
+
+#endif
\ No newline at end of file
--- a/Tetromino.cpp Sat May 23 13:59:06 2020 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,40 +0,0 @@
-#include "Tetromino.h"
-
-Tetromino::Tetromino()
-
-
-
-const int ISprite[4][4] = {
- {0,0,1,0,},
- {0,0,1,0,},
- {0,0,1,0,},
- {0,0,1,0,},
-};
-
-const int LSprite[4][4] = {
- {0,1,0,0,},
- {0,1,0,0,},
- {0,1,1,0,},
- {0,0,0,0,},
-};
-
-const int ZSprite[4][4] = {
- {0,1,0,0,},
- {0,1,1,0,},
- {0,0,1,0,},
- {0,0,0,0,},
-};
-
-const int OSprite[4][4] = {
- {0,0,0,0,},
- {0,1,1,0,},
- {0,1,1,0,},
- {0,0,0,0,},
-};
-
-const int TSprite[4][4] = {
- {0,1,0,0,},
- {0,1,1,0,},
- {0,1,0,0,},
- {0,0,0,0,},
-};
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Tetromino/Tetromino.cpp Sun May 24 11:18:33 2020 +0000
@@ -0,0 +1,124 @@
+#include "Tetromino.h"
+
+const int ISprite[4][4] = {
+ {0,0,1,0,},
+ {0,0,1,0,},
+ {0,0,1,0,},
+ {0,0,1,0,},
+};
+
+const int L0Sprite[4][4] = {
+ {0,1,0,0,},
+ {0,1,0,0,},
+ {0,1,1,0,},
+ {0,0,0,0,},
+};
+
+const int Z0Sprite[4][4] = {
+ {0,1,0,0,},
+ {0,1,1,0,},
+ {0,0,1,0,},
+ {0,0,0,0,},
+};
+
+const int OSprite[4][4] = {
+ {0,0,0,0,},
+ {0,1,1,0,},
+ {0,1,1,0,},
+ {0,0,0,0,},
+};
+
+const int TSprite[4][4] = {
+ {0,1,0,0,},
+ {0,1,1,0,},
+ {0,1,0,0,},
+ {0,0,0,0,},
+};
+
+const int L1Sprite[4][4] = {
+ {0,0,1,0,},
+ {0,0,1,0,},
+ {0,1,1,0,},
+ {0,0,0,0,},
+};
+
+const int Z1Sprite[4][4] = {
+ {0,0,1,0,},
+ {0,1,1,0,},
+ {0,1,0,0,},
+ {0,0,0,0,},
+};
+
+Tetromino::Tetromino()
+{
+
+}
+
+Tetromino::~Tetromino()
+{
+
+}
+
+void Tetromino::init(int x, int height, int width)
+{
+ _x = x;
+ _y = HEIGHT/2 - height/2;
+ _height = height;
+ _width = width;
+ _speed = 1;
+
+}
+
+
+
+void Tetromino::draw(N5110 &lcd, int x)
+{
+ int shape = rand() % 6;
+
+ if (shape == 0) {
+ lcd.drawSprite(_x, _y, 4, 4, (int*)ISprite); /*_shape_type = 0;*/
+ } else if (shape == 1) {
+ lcd.drawSprite(_x, _y, 4, 4, (int*)L0Sprite); /*_shape_type = 1;*/
+ } else if (shape == 2) {
+ lcd.drawSprite(_x, _y, 4, 4, (int*)Z0Sprite); /*_shape_type = 2;*/
+ } else if (shape == 3) {
+ lcd.drawSprite(_x, _y, 4, 4, (int*)OSprite); /*_shape_type = 3;*/
+ } else if (shape == 4) {
+ lcd.drawSprite(_x, _y, 4, 4, (int*)TSprite); /*_shape_type = 4;*/
+ } else if (shape == 5) {
+ lcd.drawSprite(_x, _y, 4, 4, (int*)L1Sprite); /*_shape_type = 5;*/
+ } else if (shape == 6) {
+ lcd.drawSprite(_x, _y, 4, 4, (int*)Z1Sprite); /*_shape_type = 6;*/
+ }
+// return _shape_type;
+}
+
+
+
+
+
+void Tetromino::update(Direction d, float mag)
+{
+ _speed = int(mag*10.0f);
+
+ if (d == CENTRE) {
+ _y+=_speed;
+ } else if (d == S) {
+ _y+=_speed*2;
+ } else if (d == E) {
+ _x+=_speed;
+ } else if (d == W) {
+ _x-=_speed;
+ }
+
+ if (_x < 1) {
+ _x = 1;
+ }
+ if (_x > WIDTH - _width - 1) {
+ _x = WIDTH - _width - 1;
+ }
+}
+Vector2D Tetromino::get_pos() {
+ Vector2D p = {_x,_y};
+ return p;
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Tetromino/Tetromino.h Sun May 24 11:18:33 2020 +0000
@@ -0,0 +1,29 @@
+#ifndef TETROMINO_H
+#define TETROMINO_H
+
+#include "mbed.h"
+#include "N5110.h"
+#include "Gamepad.h"
+
+class Tetromino
+{
+public:
+
+Tetromino();
+~Tetromino();
+void init(int x, int height, int width);
+void draw(N5110 &lcd, int x);
+void update(Direction d, float mag);
+Vector2D get_pos();
+
+private:
+
+int _height;
+int _width;
+int _x;
+int _y;
+int _speed;
+int _shape_type;
+
+};
+#endif
\ No newline at end of file
--- a/main.cpp Sat May 23 13:59:06 2020 +0000
+++ b/main.cpp Sun May 24 11:18:33 2020 +0000
@@ -14,16 +14,29 @@
#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
+#include "TetrisGame.h"
+
+#define TETROMINO_WIDTH 4
+#define TETROMINO_HEIGHT 4
+
+struct UserInput {
+ Direction d;
+ float mag;
+};
+
+N5110 lcd;
+Gamepad pad;
+TetrisGame tetris;
+
+void init();
+void update_game(UserInput input);
+void render();
+void welcome();
-// objects
-Gamepad pad;
-N5110 lcd;
-
-
-int ScreenHeight = 84;
-int ScreenWidth = 48;
-string block[7] // store tetromino blocks as a w-string
+// int ScreenHeight = 84;
+// int ScreenWidth = 48;
+// string block[7] // store tetromino blocks as a w-string
@@ -50,48 +63,51 @@
}
*/
-void title()
+int main()
{
- using namespace std:
+ int fps = 6;
- system("cls");
+ init();
+ welcome();
- cout << "--------------------------------------------------------\n";
+ render();
+ wait(1.0f/fps);
- cout << "------- ------- ------- ------- --- -----\n";
- cout << " | | | | | | | ||n";
- cout << " | | | | | | |\n";
- cout << " | ----- | ------- | -----\n";
- cout << " | | | | | | |\n";
- cout << " | | | | | | | |\n";
- cout << " | ------- | | | --- -----\n";
+ while(1) {
+ tetris.read_input(pad);
+ tetris.update(pad);
+ render();
+ wait(1.0f/fps);
+ }
+}
+
+void init()
+{
+ lcd.init();
+ padinit();
- cout << "\t<menu>\n";
- cout << "\t1: Start Game\n\t2: Quit\n\n";
-
- cout << "---------------------------------------------------------\n";
+ tetris.init(TETROMINO_WIDTH, TETROMINO_HEIGHT);
}
-int GameOver()
+void render()
{
- using namespace std;
-
- char a;
- cout << " ----- - - - ------- ------- - - -------- ------\n";
- cout << "| | | | || || | | | | | | | |\n";
- cout << "| | | | | | | | | | | | | | |\n";
- cout << "| ---- | | | | | ----- | | | | ------ ------\n";
- cout << "| | |-----| | | | | | | | | | |\n";
- cout << "| | | | | | | | | | | | | |\n";
- cout << " ----- | | | | ------- ------- | -------- | |\n";
- cout << "\n\nPress any key\n";
-
- return 0;
-}
-int main()
-{
-
-
+ lcd.clear();
+ tetris.draw(lcd);
+ lcd.refresh();
}
+void welcome() {
+
+ lcd.printString(" TETRIS ",0,1);
+ lcd.printString(" Press Start ",0,4);
+ lcd.refresh();
+
+ while (pad.start_pressed() == false) {
+ lcd.setContrast(pad.read_pot1());
+ pad.leds_on();
+ wait(0.1);
+ pad.leds_off();
+ wait(0.1);
+ }
+}
\ No newline at end of file