![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
All there but errors need fixing
TetrisGame/TetrisGame.cpp@3:522c6f850e91, 2020-05-24 (annotated)
- Committer:
- el18rs
- Date:
- Sun May 24 11:18:33 2020 +0000
- Revision:
- 3:522c6f850e91
- Child:
- 4:7ddd287a5d28
Game engine added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el18rs | 3:522c6f850e91 | 1 | #include "TetrisGame.h" |
el18rs | 3:522c6f850e91 | 2 | |
el18rs | 3:522c6f850e91 | 3 | TetrisGame::TetrisGame() |
el18rs | 3:522c6f850e91 | 4 | { |
el18rs | 3:522c6f850e91 | 5 | |
el18rs | 3:522c6f850e91 | 6 | } |
el18rs | 3:522c6f850e91 | 7 | |
el18rs | 3:522c6f850e91 | 8 | TetrisGame::~TetrisGame() |
el18rs | 3:522c6f850e91 | 9 | { |
el18rs | 3:522c6f850e91 | 10 | |
el18rs | 3:522c6f850e91 | 11 | } |
el18rs | 3:522c6f850e91 | 12 | |
el18rs | 3:522c6f850e91 | 13 | void TetrisGame::init(int tetromino_width, int tetromino_height, int tetromino_size, int speed) |
el18rs | 3:522c6f850e91 | 14 | { |
el18rs | 3:522c6f850e91 | 15 | _tetromino_width = tetromino_width; |
el18rs | 3:522c6f850e91 | 16 | _tetromino_height = tetromino_height; |
el18rs | 3:522c6f850e91 | 17 | _tetromino_size = tetromino_size; |
el18rs | 3:522c6f850e91 | 18 | _speed = speed; |
el18rs | 3:522c6f850e91 | 19 | |
el18rs | 3:522c6f850e91 | 20 | // CHANGE SO STARTS AT TOP? |
el18rs | 3:522c6f850e91 | 21 | _p1x = GAP; |
el18rs | 3:522c6f850e91 | 22 | _p1.init(_p1x,_tetromino_height,_tetromino_width); |
el18rs | 3:522c6f850e91 | 23 | _tetromino.init(_tetromino_height, _tetromino_width, _tetromino_size,_speed); |
el18rs | 3:522c6f850e91 | 24 | } |
el18rs | 3:522c6f850e91 | 25 | |
el18rs | 3:522c6f850e91 | 26 | void TetrisGame::read_input(Gamepad &pad) |
el18rs | 3:522c6f850e91 | 27 | { |
el18rs | 3:522c6f850e91 | 28 | _d = pad.get_direction(); |
el18rs | 3:522c6f850e91 | 29 | _mag = pad.getmag(); |
el18rs | 3:522c6f850e91 | 30 | } |
el18rs | 3:522c6f850e91 | 31 | |
el18rs | 3:522c6f850e91 | 32 | void TetrisGame::draw(N5110 &lcd) |
el18rs | 3:522c6f850e91 | 33 | { |
el18rs | 3:522c6f850e91 | 34 | |
el18rs | 3:522c6f850e91 | 35 | lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); |
el18rs | 3:522c6f850e91 | 36 | |
el18rs | 3:522c6f850e91 | 37 | _p1.draw(lcd); |
el18rs | 3:522c6f850e91 | 38 | } |
el18rs | 3:522c6f850e91 | 39 | |
el18rs | 3:522c6f850e91 | 40 | void TetrisGame::update(Gamepax &pad) |
el18rs | 3:522c6f850e91 | 41 | { |
el18rs | 3:522c6f850e91 | 42 | check_goal(pad); |
el18rs | 3:522c6f850e91 | 43 | |
el18rs | 3:522c6f850e91 | 44 | _p1.update(_d,_mag); |
el18rs | 3:522c6f850e91 | 45 | |
el18rs | 3:522c6f850e91 | 46 | check_floor_collision(pad); |
el18rs | 3:522c6f850e91 | 47 | check_tetromino_collisions(pad); |
el18rs | 3:522c6f850e91 | 48 | } |
el18rs | 3:522c6f850e91 | 49 | |
el18rs | 3:522c6f850e91 | 50 | void TetrisGame::check_floor_collision(Gamepad &pad) |
el18rs | 3:522c6f850e91 | 51 | { |
el18rs | 3:522c6f850e91 | 52 | Vector2D tetromino_pos = _tetromino.get_pos(); |
el18rs | 3:522c6f850e91 | 53 | |
el18rs | 3:522c6f850e91 | 54 | if (tetromino_pos.y + _tetromino_size >= (HEIGHT-1) ) { |
el18rs | 3:522c6f850e91 | 55 | |
el18rs | 3:522c6f850e91 | 56 | tetromino_pos.y = (HEIGHT-1) - _tetromino_size; |
el18rs | 3:522c6f850e91 | 57 | tetromino_velocity.y = 0; |
el18rs | 3:522c6f850e91 | 58 | |
el18rs | 3:522c6f850e91 | 59 | pad.tone(750.0,0.1); |
el18rs | 3:522c6f850e91 | 60 | } |
el18rs | 3:522c6f850e91 | 61 | // update tetromino parameters?? |
el18rs | 3:522c6f850e91 | 62 | } |
el18rs | 3:522c6f850e91 | 63 | |
el18rs | 3:522c6f850e91 | 64 | void TetrisGame::check_tetromino_collisions(Gamepad &pad) |
el18rs | 3:522c6f850e91 | 65 | { |
el18rs | 3:522c6f850e91 | 66 | Vector2D tetromino_pos = _tetromino.get_pos(); |
el18rs | 3:522c6f850e91 | 67 | Vector2D tetromino_velocity = _tetromino.get_velocity(); |
el18rs | 3:522c6f850e91 | 68 | |
el18rs | 3:522c6f850e91 | 69 | Vector2D p1_pos = _p1.get_pos(); |
el18rs | 3:522c6f850e91 | 70 | |
el18rs | 3:522c6f850e91 | 71 | if ( |
el18rs | 3:522c6f850e91 | 72 | (tetromino_pos.y >= p1_pos.y) && |
el18rs | 3:522c6f850e91 | 73 | (tetromino_pos.y <= p1_pos.y + _tetromino_height) && |
el18rs | 3:522c6f850e91 | 74 | (tetromino_pos.x >= _p1x) && |
el18rs | 3:522c6f850e91 | 75 | (tetromino_pos.x <= _plx + _tetromino_width) |
el18rs | 3:522c6f850e91 | 76 | ) { |
el18rs | 3:522c6f850e91 | 77 | tetromino_pos.x = _plx + _tetromino_width; |
el18rs | 3:522c6f850e91 | 78 | tetromino_velocity.x = 0; |
el18rs | 3:522c6f850e91 | 79 | |
el18rs | 3:522c6f850e91 | 80 | pad.tone(1000.0,0.1); |
el18rs | 3:522c6f850e91 | 81 | } |
el18rs | 3:522c6f850e91 | 82 | |
el18rs | 3:522c6f850e91 | 83 | |
el18rs | 3:522c6f850e91 | 84 | |
el18rs | 3:522c6f850e91 | 85 | |
el18rs | 3:522c6f850e91 | 86 | |
el18rs | 3:522c6f850e91 | 87 | |
el18rs | 3:522c6f850e91 | 88 | |
el18rs | 3:522c6f850e91 | 89 | |
el18rs | 3:522c6f850e91 | 90 |