hartk1213 0 Newbie Poster

hi i am making my own version of tetris and i have most of it working but for some reason they tetrominos are not being drawing correctly.

its 9 files of code so its going to be a wall of code
here is a screenshot of what it is looking like and its supposed to look like tetrisscreenshot
here it is

here are all the files for the game

header files first

Shape.h

class Draw;
class Shape {
public:
    enum Direction {LEFT = -1, RIGHT = 1};
    enum Name{I,J,L,O,S,Z,T};

    Shape(Name);
    void draw(Draw &) const;
    void move(int dx, int dy);
    void rotate(Direction);
    bool map(int x, int y) const;
    int x() const {

        return x_;
    }
    int y() const {

        return y_;
    }

private:
    Name name_;
    int angle_;
    int x_;
    int y_;
};        

game.h

#pragma once
#include "shape.h"
#include "background.h"

class Draw;

class Game {
public:
    enum Direction { UP, DOWN, LEFT, RIGHT};
    Game();
    void drawShape(Draw &);
    void tick();
    void restart();
    void keyEvent(Direction);
private:
    Background background_;
    Shape shape_;
};

draw.h

#pragma once

class Draw
{
public:
  enum Color { RED, WHITE, MAGENTA, DARK_BLUE, 
           GREEN, BROWN, CYAN, BLACK };
  void rect(int x1, int y1, int x2, int y2);
  void setColor(Color);
};

background.h

#pragma once

class Draw;
class Shape;

class Background {
public:
    enum { WIDTH = 22,HEIGHT = 20};
    Background();
    void drawShape(Draw &) const;
    bool isCollision(const Shape &) const;
    void unite(const Shape &);
    int removeSolidLines();
    void nextBlock();
private:
    bool map_[HEIGHT][WIDTH];
};

now for the cpp files

tetris.cpp

#include "draw.h"
#include "game.h"
#include "glut.h"

Background background;
Game game;

void myDisplay() {

    glClearColor(0.0,0.0,1.0,1.0);
    glClear(GL_COLOR_BUFFER_BIT);
    Draw p;
    game.drawShape(p);
    glutSwapBuffers();

}

void timer(int) {
    game.tick();
    myDisplay();
    glutTimerFunc(1000,timer,0);

}

void keyboardFunc(int key, int x, int y) {

    switch (key) {
    case GLUT_KEY_LEFT:
        game.keyEvent(Game::LEFT);
        break;
    case GLUT_KEY_RIGHT:
        game.keyEvent(Game::RIGHT);
        break;
    case GLUT_KEY_UP:
        game.keyEvent(Game::UP);
        break;
    case GLUT_KEY_DOWN:
        game.keyEvent(Game::DOWN);
        break;
    }
    myDisplay();
}

int main (int argc, char **argv) {

  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  glutInitWindowSize(Background::WIDTH * 30, Background::HEIGHT * 30); 
  glutInitWindowPosition(300, 100);
  glutCreateWindow("CS440001_13 - Tetris");
  glClearColor(0, 0, 0, 1);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0, Background::WIDTH * 8, 
      Background::HEIGHT * 8, 0, 
      -1.0, 1.0);
  glutDisplayFunc(myDisplay);
  timer(0);
  glutSpecialFunc(keyboardFunc);

  glutMainLoop();
}

shape.cpp

#include "shape.h"
#include "draw.h"

Shape::Shape(Name name): name_(name),
    angle_(0),
    x_(3),
    y_(0)
{}
    void Shape::draw(Draw &p) const {

        p.setColor(static_cast<Draw::Color>(name_));
        for(int y = 0; y < 4; ++y)
            for(int x = 0; x < 4; ++x)
                if( map(x,y))
                    p.rect((x + x_) * 8 + 1,(y + y_) * 8 + 1,(x + x_ + 1) * 8 - 1,(y + y_ + 1) * 8 - 1);
}

bool Shape::map(int x, int y) const {

    static const char *SHAPES[] =

    { 
      "  0 " // I
      "  0 "
      "  0 "
      "  0 ", 

      "  0 " // J
      "  0 "
      " 00 "
      "    ", 

      " 0  " // L
      " 0  "
      " 00 "
      "    ", 

      "    " // O
      " 00 "
      " 00 "
      "    ", 

      "  0 " // S
      " 00 "
      " 0  "
      "    ", 

      " 0  " // Z
      " 00 "
      "  0 "
      "    ", 

      "    " // T
      " 000"
      "  0 "
      "    " 
    };

    static const struct {
        int x;
        int y;
    }
    ROTATE[][16] = {
            {
      { 0, 0 }, { 0, 1 }, { 0, 2 }, { 0, 3 }, 
      { 1, 0 }, { 1, 1 }, { 1, 2 }, { 1, 3 }, 
      { 2, 0 }, { 2, 1 }, { 2, 2 }, { 2, 3 }, 
      { 3, 0 }, { 3, 1 }, { 3, 2 }, { 3, 3 } 
    },
    {
      { 3, 0 }, { 2, 0 }, { 1, 0 }, { 0, 0 }, 
      { 3, 1 }, { 2, 1 }, { 1, 1 }, { 0, 1 }, 
      { 3, 2 }, { 2, 2 }, { 1, 2 }, { 0, 2 }, 
      { 3, 3 }, { 2, 3 }, { 1, 3 }, { 0, 3 } 
    },
    {
      { 3, 3 }, { 3, 2 }, { 3, 1 }, { 3, 0 }, 
      { 2, 3 }, { 2, 2 }, { 2, 1 }, { 2, 0 }, 
      { 1, 3 }, { 1, 2 }, { 1, 1 }, { 1, 0 }, 
      { 0, 3 }, { 0, 2 }, { 0, 1 }, { 0, 0 } 
    },
    {
      { 0, 3 }, { 1, 3 }, { 2, 3 }, { 3, 3 }, 
      { 0, 2 }, { 1, 2 }, { 2, 2 }, { 3, 2 }, 
      { 0, 1 }, { 1, 1 }, { 2, 1 }, { 3, 1 }, 
      { 0, 0 }, { 1, 0 }, { 2, 0 }, { 3, 0 } 
    }
  };

    return SHAPES[name_]
    [ROTATE[angle_][y * 4 + x].y * 4 + ROTATE[angle_][y * 4 + x].x != ' '];
}

void Shape::move(int dx, int dy) {

    x_ += dx;
    y_ += dy;
}

void Shape::rotate(Direction d) {

    angle_ = (angle_ + d + 4) % 4;
}

draw.cpp

#include "draw.h"

#include "glut.h"


void Draw::rect(int x1, int y1, int x2, int y2)
{
  glBegin(GL_QUADS);
  glVertex2i(x1, y1);
  glVertex2i(x2, y1);
  glVertex2i(x2, y2);
  glVertex2i(x1, y2);
  glEnd();

}

void Draw::setColor(Color color)
{
  static const struct
  {
    float r, g, b;
  } colors[] =
      {
    { 1, 0, 0 }, // red
    { 1, 1, 1 }, // white
    { 1, 0, 1 }, // magenta
    { 0, 0, 0.5 }, // dark blue
    { 0, 1, 0 }, // green
    { 0.5, 0.5, 0 }, // brown
    { 0, 1, 1 }, // cyan
    { 0, 0, 0 } // black
      };
  glColor3f(
        colors[color].r, 
        colors[color].g, 
        colors[color].b
        );
}

game.cpp

#include "game.h"
#include <cstdlib>

Game::Game():
shape_(static_cast<Shape::Name>(rand() % 7)) {}

void Game::drawShape(Draw &p) {

    background_.drawShape(p);
    shape_.draw(p);

}

void Game::tick() {

    Shape s = shape_;
    s.move(0,1);
    if ( !background_.isCollision(s))
        shape_ = s;
    else
    {
        background_.unite(shape_);
        background_.removeSolidLines();
        shape_ = Shape(static_cast<Shape::Name>(rand() % 7));
        if (background_.isCollision(shape_))
            restart();
    }
}

void Game::restart() {

    background_ = Background();

}

void Game::keyEvent(Direction d) {

    Shape s = shape_;
    switch (d) {
    case UP: s.rotate(Shape::LEFT);
        break;
    case DOWN: s.move(0,1);
        break;
    case LEFT: s.move(-1,0);
        break;
    case RIGHT: s.move (1,0);
        break;
    }
    if (!background_.isCollision(s))
        shape_ = s;
}

background.cpp

#include "background.h"
#include "draw.h"
#include "shape.h"
#include "glut.h"

Background::Background() {

    for (int y = 0; y < (HEIGHT); y++)
        for (int x = 0; x < (WIDTH - 10); x++)
            map_[y][x] = false;
}

void Background::nextBlock() {
    glBegin(GL_QUADS);
    glVertex2i(WIDTH - 8, HEIGHT - 5);
    glVertex2i(WIDTH - 8, HEIGHT);
    glVertex2i(WIDTH, HEIGHT);
    glVertex2i(WIDTH, HEIGHT - 5);
    glEnd();
}

void Background::drawShape(Draw &p) const {
    p.setColor(Draw::);
    for ( int y = 0; y < (HEIGHT); y++)
        for (int x = 0; x < (WIDTH - 10); x++)
            if (map_[y][x])
                p.rect(x * 8 + 1, y * 8 + 1, (x + 1) * 8 - 1, (y + 1) * 8 - 1);
            else 
                p.rect(x * 8 + 3, y * 8 + 3, (x + 1) * 8 - 4, (y + 1) * 8 - 4);
}

bool Background::isCollision(const Shape &s) const {
    for (int y = 0; y < 4; y++) 
        for(int x = 0; x < 4; x++)
            if (s.map(x,y)) {
                int wx = x + s.x();
                int wy = y + s.y();
                if(wx < 0 || wx >= (WIDTH - 10) || wy < 0 || wy > (HEIGHT))
                    return true;
                if( map_[wy][wx])
                    return true;
            }
            return false;
}

void Background::unite(const Shape &s) {
    for (int y = 0; y < 4; y++)
        for (int x = 0; x < 4; x++) {
            int wx = x + s.x();
            int wy = y + s.y();
            if (wx >= 0 && wx < (WIDTH - 10) && wy >= 0 && wy < (HEIGHT))
                map_[wy][wx] = map_[wy][wx] || s.map(x,y);
        }
}

int Background::removeSolidLines() {

    int res = 0; 
    for(int y = 0; y < (HEIGHT); y++) {
        bool solid = true;
        for (int x = 0; x < (WIDTH - 10); x++)
            if ( !map_[y][x]) {
                solid = false;
                break;
            }
            if (solid) {
                res++;
                for (int yy= y-1; yy >= 0; yy--)
                    for (int x= 0; x < (WIDTH - 10); x++)
                        map_[yy + 1][x] = map_[yy][x];
            }
            for (int x = 0; x < (WIDTH - 10); x++)
                map_[0][x] = false;
    }
    return res;
}

SORRY FOR WALL OF TEXT

`

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.