All there but errors need fixing

Dependencies:   mbed

Overview:

Rookie Tetris is a jigsaw style game based on the classic Tetris.

A block will appear at the top of the screen, you must move it (your options for movement are left, right and down - you cannot move up the board). The block will stop when it if placed either on the floor of the board or on-top of another block.

Your goal is to fill a complete row of the board with the blocks; when you do so the row will delete and the pattern above it will drop down. The game is over when your pattern is tall enough to reach to the top of the board!

Controls:

Use the joystick to move your block! Your block cannot move out of the parameters of the board.

Pot 2 controls the contrast of the screen.

Committer:
el18rs
Date:
Wed Jun 03 16:44:09 2020 +0000
Revision:
22:403759d2f093
Parent:
17:70d45e9e44a8
Child:
23:cbfa9d1ee3db
line clears when full

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el18rs 2:55092965eadd 1 /*
eencae 0:b7f1f47bb26a 2 ELEC2645 Embedded Systems Project
eencae 0:b7f1f47bb26a 3 School of Electronic & Electrical Engineering
eencae 0:b7f1f47bb26a 4 University of Leeds
eencae 0:b7f1f47bb26a 5 2019/20
eencae 0:b7f1f47bb26a 6
el18rs 1:35dba0833c7d 7 Name: Ruby Smith
el18rs 1:35dba0833c7d 8 Username: el18rs
el18rs 1:35dba0833c7d 9 Student ID Number: 201259470
el18rs 1:35dba0833c7d 10 Date: 22/05/20
eencae 0:b7f1f47bb26a 11 */
eencae 0:b7f1f47bb26a 12
eencae 0:b7f1f47bb26a 13 // includes
eencae 0:b7f1f47bb26a 14 #include "mbed.h"
eencae 0:b7f1f47bb26a 15 #include "Gamepad.h"
eencae 0:b7f1f47bb26a 16 #include "N5110.h"
el18rs 3:522c6f850e91 17 #include "TetrisGame.h"
el18rs 3:522c6f850e91 18
el18rs 15:68114eae4053 19 // set defaults
el18rs 3:522c6f850e91 20 #define TETROMINO_WIDTH 4
el18rs 3:522c6f850e91 21 #define TETROMINO_HEIGHT 4
el18rs 4:7ddd287a5d28 22 // #define TETROMINO_SIZE 4
el18rs 11:0183a52c1077 23 #define SPEED 1
el18rs 4:7ddd287a5d28 24 #define HEIGHT 48
el18rs 4:7ddd287a5d28 25 #define WIDTH 84
el18rs 4:7ddd287a5d28 26 // STRUCTS //
el18rs 3:522c6f850e91 27
el18rs 3:522c6f850e91 28 struct UserInput {
el18rs 3:522c6f850e91 29 Direction d;
el18rs 3:522c6f850e91 30 float mag;
el18rs 3:522c6f850e91 31 };
el18rs 15:68114eae4053 32 // variables //
el18rs 15:68114eae4053 33
el18rs 15:68114eae4053 34 volatile extern int iH;
el18rs 22:403759d2f093 35 volatile extern int iB;
el18rs 3:522c6f850e91 36
el18rs 4:7ddd287a5d28 37 // OBJECTS //
el18rs 4:7ddd287a5d28 38
el18rs 3:522c6f850e91 39 N5110 lcd;
el18rs 3:522c6f850e91 40 Gamepad pad;
el18rs 3:522c6f850e91 41 TetrisGame tetris;
el18rs 3:522c6f850e91 42
el18rs 15:68114eae4053 43 // PROTOTBPES //
el18rs 4:7ddd287a5d28 44
el18rs 3:522c6f850e91 45 void init();
el18rs 3:522c6f850e91 46 void update_game(UserInput input);
el18rs 3:522c6f850e91 47 void render();
el18rs 3:522c6f850e91 48 void welcome();
el18rs 15:68114eae4053 49 void fallenblocks(N5110 &lcd, int iH);
el18rs 22:403759d2f093 50 void clearline(N5110 &lcd, int iB);
el18rs 15:68114eae4053 51 void set_values();
eencae 0:b7f1f47bb26a 52
el18rs 15:68114eae4053 53 // FUNCTIONS //
el18rs 2:55092965eadd 54
el18rs 3:522c6f850e91 55 int main()
el18rs 2:55092965eadd 56 {
el18rs 15:68114eae4053 57 int fps = 6; // 6 frames per second
el18rs 15:68114eae4053 58
el18rs 4:7ddd287a5d28 59 init(); // initialise and display welcome screen
el18rs 4:7ddd287a5d28 60 welcome(); // wait for user to press start
el18rs 15:68114eae4053 61
el18rs 15:68114eae4053 62 render(); // draw initial frame
el18rs 15:68114eae4053 63 wait(1.0f/fps); // wait one frame period
el18rs 4:7ddd287a5d28 64
el18rs 15:68114eae4053 65 set_values();
el18rs 15:68114eae4053 66
el18rs 4:7ddd287a5d28 67 // game loop
el18rs 3:522c6f850e91 68 while(1) {
el18rs 17:70d45e9e44a8 69 tetris.floor_hit_flag = 0;
el18rs 15:68114eae4053 70 tetris.read_input(pad); // read input
el18rs 6:39cbec524483 71 tetris.update(pad, lcd);
el18rs 15:68114eae4053 72 lcd.refresh();
el18rs 15:68114eae4053 73 lcd.clear();
el18rs 15:68114eae4053 74 tetris.fallenblocks(lcd, iH);
el18rs 15:68114eae4053 75 tetris.draw(lcd); // re-draw
el18rs 15:68114eae4053 76 if (tetris.floor_hit_flag==1) {
el18rs 22:403759d2f093 77 tetris.fallenblocks(lcd, iH);
el18rs 15:68114eae4053 78 iH++;
el18rs 15:68114eae4053 79 tetris.X[iH] = tetris.a;
el18rs 15:68114eae4053 80 tetris.Y[iH] = tetris.b;
el18rs 15:68114eae4053 81 tetris.floor_hit_flag = 0;
el18rs 15:68114eae4053 82 printf("iH increased\n");
el18rs 15:68114eae4053 83 }
el18rs 22:403759d2f093 84 else if (tetris.clear_line_flag==1) {
el18rs 22:403759d2f093 85 tetris.clearline(lcd, iB);
el18rs 22:403759d2f093 86 iB++;
el18rs 22:403759d2f093 87 tetris.Z[iB] = tetris.c;
el18rs 22:403759d2f093 88 tetris.clear_line_flag==0;
el18rs 22:403759d2f093 89 printf("iB increased\n");
el18rs 22:403759d2f093 90 }
el18rs 22:403759d2f093 91 else if (tetris.
el18rs 15:68114eae4053 92 wait(1.0f/fps); // wait one frame period
el18rs 15:68114eae4053 93 }
el18rs 4:7ddd287a5d28 94 }
el18rs 3:522c6f850e91 95
el18rs 4:7ddd287a5d28 96
el18rs 4:7ddd287a5d28 97 // initialise classes and libraries
el18rs 15:68114eae4053 98 void init()
el18rs 15:68114eae4053 99 {
el18rs 15:68114eae4053 100 lcd.init(); // initialise lcd
el18rs 4:7ddd287a5d28 101 pad.init(); // initialise Gamepad
el18rs 15:68114eae4053 102
el18rs 4:7ddd287a5d28 103 // initialise the game with correct tetromino sizes etc
el18rs 11:0183a52c1077 104 tetris.init(SPEED);
el18rs 15:68114eae4053 105
el18rs 2:55092965eadd 106 }
el18rs 2:55092965eadd 107
el18rs 15:68114eae4053 108 void set_values()
el18rs 15:68114eae4053 109 {
el18rs 15:68114eae4053 110 for(int i=0; i<=50; i++) {
el18rs 15:68114eae4053 111 tetris.X[i]=0;
el18rs 15:68114eae4053 112 tetris.Y[i]=0;
el18rs 15:68114eae4053 113 }
el18rs 15:68114eae4053 114 }
el18rs 15:68114eae4053 115
el18rs 15:68114eae4053 116 void render() // draws each frame on the lcd
el18rs 15:68114eae4053 117 {
el18rs 4:7ddd287a5d28 118
el18rs 4:7ddd287a5d28 119 lcd.clear(); // clear screen
el18rs 15:68114eae4053 120 tetris.draw(lcd); // re-draw
el18rs 4:7ddd287a5d28 121 lcd.refresh(); // refresh screen
el18rs 15:68114eae4053 122
eencae 0:b7f1f47bb26a 123 }
eencae 0:b7f1f47bb26a 124
el18rs 4:7ddd287a5d28 125 // welcome screen display
el18rs 15:68114eae4053 126 void welcome()
el18rs 15:68114eae4053 127 {
el18rs 15:68114eae4053 128
el18rs 7:80e1c83b9b6a 129 lcd.printString(" TETRIS ",0,1);
el18rs 7:80e1c83b9b6a 130 lcd.printString(" Press Start ",0,4);
el18rs 3:522c6f850e91 131 lcd.refresh();
el18rs 15:68114eae4053 132
el18rs 4:7ddd287a5d28 133 // flash LEDs untile start button pressed
el18rs 3:522c6f850e91 134 while (pad.start_pressed() == false) {
el18rs 3:522c6f850e91 135 lcd.setContrast(pad.read_pot1());
el18rs 3:522c6f850e91 136 pad.leds_on();
el18rs 3:522c6f850e91 137 wait(0.1);
el18rs 3:522c6f850e91 138 pad.leds_off();
el18rs 3:522c6f850e91 139 wait(0.1);
el18rs 3:522c6f850e91 140 }
el18rs 4:7ddd287a5d28 141 }