Thread: Need help with movement in tetris c++ game

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    26

    Need help with movement in tetris c++ game

    I am making a tetris game for my intro c++ class. It is in c++ using Dark GDK.

    I am getting one error:
    Code:
    1>i:\ school\ spring 2012\comsc-048\tetris code\tetris code\tetris code\tetris\dark gdk - tetris\dark gdk - tetris\main.cpp(208) : error C2451: conditional expression of type 'void' is illegal
    1>Expressions of type void cannot be converted to other types
    
    
    1>Dark GDK - tetris - 1 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Here is the code:
    Code:
    //Dark GDK - The Game Creators - www.thegamecreators.com
    
    //the wizard has created a very simple project that uses Dark GDK
    //it contains the basic code for a GDK application
    
    
    //whenever using Dark GDK you must ensure you include the header file
    #include "DarkGDK.h"
    
    
    
    
    //Global
    int Block_Size=20;
    int grid[20][10];
    const DWORD Red = dbRGB(255,0,0);
    const DWORD Green = dbRGB(0,255,0);
    const DWORD Blue = dbRGB(0,0,255);
    const DWORD Magenta = dbRGB(255,0,255);
    const DWORD Black = dbRGB(0,0,0);
    const DWORD White = dbRGB(255,255,255);
    const DWORD Yellow = dbRGB(255,255,0);
    const DWORD Cyan = dbRGB(0,255,255);
    const DWORD Orange = dbRGB(255,165,0);
    const DWORD Grey = dbRGB(177,177,177);
    //end global
    
    
    //Classes
    
    
    class Block {
    private:
    	DWORD color;
    	//int blocks[4];
    public:
    	int x,y;
    	Block();
    	Block(int, int, DWORD);
    	void draw();
    	void move(int, int);
    	void clear();
    	//bool col();
    
    
    };
    Block::Block(){
    x = 0;
    y =0;
    color = White;
    }
    //Methods (Block)
    
    
    Block::Block(int X, int Y, DWORD COLOR)
    {
    	x=X;
    	y=Y;
    	color=COLOR;
    }
    
    
    void Block::draw()
    {
    	int x1, y1, x2, y2;
    	x1=x*Block_Size;
    	y1=y*Block_Size;
    	x2=x1+Block_Size;
    	y2=y1+Block_Size;
    
    
    //	dbInk(color, Black);
    	dbBox(x1,y1,x2,y2);
    }
    
    
    void Block::clear()
    {
    
    
    	dbInk(Black, Black);
    	//dbBox(x1,y1,x2,y2);
    	draw();
    }
    
    
    void Block::move(int dx, int dy)
    {
    	x=x+dx;
    	y=y+dy;
    	dbInk(color, Black);
    	draw();
    }
    
    
    //End Method (Block)
    
    
    class Shape {
    
    
    private:
    	Block blocks[4];
    	//int pos[8];
    public:
    	int pos[8];
    	DWORD color;
        Shape();
    	void make_ishape();
    	void move_shape(int,int);
    	void draw_shape();
    	bool col(); 
    };
    //Methods (Shape)
    Shape::Shape() {
    blocks[0] = Block(0,0, Red);
    blocks[1] = Block(0,0, Red);
    blocks[2] = Block(0,0, Red);
    blocks[3] = Block(0,0, Red);
    }
    bool Shape::col()
    {
    	for (int i=0; i<=4; i++)
    	{
    		if (blocks[i].y >= 19)
    		{
    			return false;
    		}
    		/*/else if (blocks[i].x >= 0 && blocks[i].x <= 9)
    		{
    			return false;
    		}/*/
    	}
    	return true;
    }
    
    
    void Shape::make_ishape()
    {
    	blocks[0] = Block(pos[0],pos[1],Blue);
    	blocks[1] = Block(pos[2],pos[3],Blue);
    	blocks[2] = Block(pos[4],pos[5],Blue);
    	blocks[3] = Block(pos[6],pos[7],Blue);
    	
    }
    
    
    void Shape::draw_shape()
    {
    	for (int i=0; i<4; i++)
    	{
    		blocks[i].draw();
    	}
    }
    
    
    void Shape::move_shape(int dx, int dy)
    {
    	for (int i=0; i<4; i++)
    	{
    		blocks[i].clear();
    	}
    	for (int i=0; i<4; i++)
    	{
    		blocks[i].move(dx,dy);
    	}
    }
    
    
    
    
    class I_Shape: public Shape{
    public:
    	I_Shape(int,int);
    	
    private:
    	DWORD color;
    	//int pos[8];
    };
    
    
    //Methods (I_Shape)
    
    
    
    
    I_Shape::I_Shape(int x, int y):Shape()
    {
    //make_shape(pos[8],color);
    	color=Blue;
    	pos[0]=x-1;
    	pos[1]=y;
    	pos[2]=x;
    	pos[3]=y;
    	pos[4]=x+1;
    	pos[5]=y;
    	pos[6]=x+2;
    	pos[7]=y;
    	//Shape();  //makes one block
    	make_ishape(); //makes I_Shape
    }
    
    
    // the main entry point for the application is this function
    void DarkGDK ( void )
    {
    
    
    	
    	I_Shape First(5,0);
    	// turn on sync rate and set maximum rate to 1 fps
    	dbSyncOn   ( );
    	dbSyncRate (2);
    	First.draw_shape();
    	//First.move_shape(5,4);
    	
    
    
    
    
    	
    	// our main loop
    	while ( LoopGDK ( ) )
    	{
    		if (First.col())
    		{
    			if (dbLeftKey())
    			{
    				First.move_shape(-1,0);
    			}
    			else if (dbRightKey())
    			{
    				First.move_shape(1,0);
    			}
    			else if (First.move_shape(0,1))
    			{
    				First.move_shape(0,1);	
    			}
    		}
    	
    		
    	
    		
    		// update the screen
    		dbSync ( );
    		//dbWaitKey();
    	}
    	
    	
    	// return back to windows
    	return;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    1>i:\ school\ spring 2012\comsc-048\tetris code\tetris code\tetris code\tetris\dark gdk - tetris\dark gdk - tetris\main.cpp(208) : error C2451: conditional expression of type 'void' is illegal
    1>Expressions of type void cannot be converted to other types
    So put your cursor on line 208 and read what it says (or tell us which line in the code you posted corresponds to line 208 in your real code).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    26
    Quote Originally Posted by Salem View Post
    1>i:\ school\ spring 2012\comsc-048\tetris code\tetris code\tetris code\tetris\dark gdk - tetris\dark gdk - tetris\main.cpp(208) : error C2451: conditional expression of type 'void' is illegal
    1>Expressions of type void cannot be converted to other types
    So put your cursor on line 208 and read what it says (or tell us which line in the code you posted corresponds to line 208 in your real code).
    Okay I fixed the error, but now I am having trouble with the code not recognizing when the left or right key is pressed. You need to hold down the key for it to move.

    code:
    Code:
    //Dark GDK - The Game Creators - www.thegamecreators.com
    
    //the wizard has created a very simple project that uses Dark GDK
    //it contains the basic code for a GDK application
    
    
    //whenever using Dark GDK you must ensure you include the header file
    #include "DarkGDK.h"
    
    
    
    
    //Global
    int Block_Size=20;
    int grid[20][10];
    const DWORD Red = dbRGB(255,0,0);
    const DWORD Green = dbRGB(0,255,0);
    const DWORD Blue = dbRGB(0,0,255);
    const DWORD Magenta = dbRGB(255,0,255);
    const DWORD Black = dbRGB(0,0,0);
    const DWORD White = dbRGB(255,255,255);
    const DWORD Yellow = dbRGB(255,255,0);
    const DWORD Cyan = dbRGB(0,255,255);
    const DWORD Orange = dbRGB(255,165,0);
    const DWORD Grey = dbRGB(177,177,177);
    //end global
    
    
    //Classes
    
    
    class Block {
    private:
    	DWORD color;
    	//int blocks[4];
    public:
    	int x,y;
    	Block();
    	Block(int, int, DWORD);
    	void draw();
    	void move(int, int);
    	void clear();
    	//bool col();
    
    
    };
    Block::Block(){
    x = 0;
    y =0;
    color = White;
    }
    //Methods (Block)
    
    
    Block::Block(int X, int Y, DWORD COLOR)
    {
    	x=X;
    	y=Y;
    	color=COLOR;
    }
    
    
    void Block::draw()
    {
    	int x1, y1, x2, y2;
    	x1=x*Block_Size;
    	y1=y*Block_Size;
    	x2=x1+Block_Size;
    	y2=y1+Block_Size;
    
    
    //	dbInk(color, Black);
    	dbBox(x1,y1,x2,y2);
    }
    
    
    void Block::clear()
    {
    
    
    	dbInk(Black, Black);
    	//dbBox(x1,y1,x2,y2);
    	draw();
    }
    
    
    void Block::move(int dx, int dy)
    {
    	x=x+dx;
    	y=y+dy;
    	dbInk(color, Black);
    	draw();
    }
    
    
    //End Method (Block)
    
    
    class Shape {
    
    
    private:
    	Block blocks[4];
    	//int pos[8];
    public:
    	int pos[8];
    	DWORD color;
        Shape();
    	void make_ishape();
    	void move_shape(int,int);
    	void draw_shape();
    	bool col(); 
    };
    //Methods (Shape)
    Shape::Shape() {
    blocks[0] = Block(0,0, Red);
    blocks[1] = Block(0,0, Red);
    blocks[2] = Block(0,0, Red);
    blocks[3] = Block(0,0, Red);
    }
    bool Shape::col()
    {
    	for (int i=0; i<=4; i++)
    	{
    		if (blocks[i].y >= 19)
    		{
    			return false;
    		}
    		/*/else if (blocks[i].x >= 0 && blocks[i].x <= 9)
    		{
    			return false;
    		}/*/
    	}
    	return true;
    }
    
    
    void Shape::make_ishape()
    {
    	blocks[0] = Block(pos[0],pos[1],Blue);
    	blocks[1] = Block(pos[2],pos[3],Blue);
    	blocks[2] = Block(pos[4],pos[5],Blue);
    	blocks[3] = Block(pos[6],pos[7],Blue);
    	
    }
    
    
    void Shape::draw_shape()
    {
    	for (int i=0; i<4; i++)
    	{
    		blocks[i].draw();
    	}
    }
    
    
    void Shape::move_shape(int dx, int dy)
    {
    	for (int i=0; i<4; i++)
    	{
    		blocks[i].clear();
    	}
    	for (int i=0; i<4; i++)
    	{
    		blocks[i].move(dx,dy);
    	}
    }
    
    
    
    
    class I_Shape: public Shape{
    public:
    	I_Shape(int,int);
    	
    private:
    	DWORD color;
    	//int pos[8];
    };
    
    
    //Methods (I_Shape)
    
    
    
    
    I_Shape::I_Shape(int x, int y):Shape()
    {
    //make_shape(pos[8],color);
    	color=Blue;
    	pos[0]=x-1;
    	pos[1]=y;
    	pos[2]=x;
    	pos[3]=y;
    	pos[4]=x+1;
    	pos[5]=y;
    	pos[6]=x+2;
    	pos[7]=y;
    	//Shape();  //makes one block
    	make_ishape(); //makes I_Shape
    }
    
    
    // the main entry point for the application is this function
    void DarkGDK ( void )
    {
    
    
    	
    	I_Shape First(5,0);
    	// turn on sync rate and set maximum rate to 1 fps
    	dbSyncOn   ( );
    	dbSyncRate (1);
    	First.draw_shape();
    	//First.move_shape(5,4);
    	
    
    
    
    
    	
    	// our main loop
    	while ( LoopGDK ( ) )
    	{
    		if (First.col())
    		{
    			if (dbLeftKey())
    			{
    				First.move_shape(-1,0);
    			}
    			else if (dbRightKey())
    			{
    				First.move_shape(1,0);
    			}
    			else if (First.col())
    			{
    				First.move_shape(0,1);	
    			}
    		}
    	
    	
    	
    		
    		// update the screen
    		dbSync ( );
    		//dbWaitKey();
    	}
    	
    	
    	// return back to windows
    	return;
    }

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    In 237 above (post #1), you're using a void function in a condition.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    26
    Quote Originally Posted by oogabooga View Post
    In 237 above (post #1), you're using a void function in a condition.
    I fixed the code in post #1. I now having no errors, but it barley recognizes when the left or right key is pressed.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Then change the way you're reading the keyboard.
    Use a method where the OS tells you about changes in the up/down state of keys, rather than a method where the OS tells you what it is at that instant.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Feb 2012
    Posts
    26

    Post

    Quote Originally Posted by iMalc View Post
    Then change the way you're reading the keyboard.
    Use a method where the OS tells you about changes in the up/down state of keys, rather than a method where the OS tells you what it is at that instant.
    Okay the movement is working now, but when the shape hits the side(s) it stops moving.

    code:
    Code:
    //Dark GDK - The Game Creators - www.thegamecreators.com
    
    //the wizard has created a very simple project that uses Dark GDK
    //it contains the basic code for a GDK application
    
    
    //whenever using Dark GDK you must ensure you include the header file
    #include "DarkGDK.h"
    
    
    
    
    //Global
    int Block_Size=20;
    int grid[20][10];
    //colors
    const DWORD Red = dbRGB(255,0,0);
    const DWORD Green = dbRGB(0,255,0);
    const DWORD Blue = dbRGB(0,0,255);
    const DWORD Magenta = dbRGB(255,0,255);
    const DWORD Black = dbRGB(0,0,0);
    const DWORD White = dbRGB(255,255,255);
    const DWORD Yellow = dbRGB(255,255,0);
    const DWORD Cyan = dbRGB(0,255,255);
    const DWORD Orange = dbRGB(255,165,0);
    const DWORD Grey = dbRGB(177,177,177);
    //end colors
    //end global
    
    
    //Classes
    
    
    class Block {
    private:
    	DWORD color;
    	//int blocks[4];
    public:
    	int x,y;
    	Block();
    	Block(int, int, DWORD);
    	void draw();
    	void move(int, int);
    	void clear();
    	//bool col();
    
    
    };
    Block::Block(){
    x = 0;
    y =0;
    color = White;
    }
    //Methods (Block)
    
    
    Block::Block(int X, int Y, DWORD COLOR)
    {
    	x=X;
    	y=Y;
    	color=COLOR;
    }
    
    
    void Block::draw()
    {
    	int x1, y1, x2, y2;
    	x1=x*Block_Size;
    	y1=y*Block_Size;
    	x2=x1+Block_Size;
    	y2=y1+Block_Size;
    
    
    //	dbInk(color, Black);
    	dbBox(x1,y1,x2,y2);
    }
    
    
    void Block::clear()
    {
    
    
    	dbInk(Black, Black);
    	//dbBox(x1,y1,x2,y2);
    	draw();
    }
    
    
    void Block::move(int dx, int dy)
    {
    	x=x+dx;
    	y=y+dy;
    	dbInk(color, Black);
    	draw();
    }
    
    
    //End Method (Block)
    
    
    class Shape {
    
    
    private:
    	Block blocks[4];
    	//int pos[8];
    public:
    	int pos[8];
    	DWORD color;
        Shape();
    	void make_ishape();
    	void move_shape(int,int);
    	void draw_shape();
    	bool col(); 
    	
    };
    //Methods (Shape)
    Shape::Shape() {
    blocks[0] = Block(0,0, Red);
    blocks[1] = Block(0,0, Red);
    blocks[2] = Block(0,0, Red);
    blocks[3] = Block(0,0, Red);
    }
    bool Shape::col()
    {
    	for (int i=0; i<=4; i++)
    	{
    		if (blocks[i].y >= 19 || blocks[i].x <= 0 || blocks[i].x >= 9)
    		{
    			return false;
    		}
    		
    	}
    	return true;
    }
    
    
    
    
    void Shape::make_ishape()
    {
    	blocks[0] = Block(pos[0],pos[1],Blue);
    	blocks[1] = Block(pos[2],pos[3],Blue);
    	blocks[2] = Block(pos[4],pos[5],Blue);
    	blocks[3] = Block(pos[6],pos[7],Blue);
    	
    }
    
    
    void Shape::draw_shape()
    {
    	for (int i=0; i<4; i++)
    	{
    		blocks[i].draw();
    	}
    }
    
    
    void Shape::move_shape(int dx, int dy)
    {
    	for (int i=0; i<4; i++)
    	{
    		blocks[i].clear();
    	}
    	for (int i=0; i<4; i++)
    	{
    		blocks[i].move(dx,dy);
    	}
    }
    
    
    
    
    class I_Shape: public Shape{
    public:
    	I_Shape(int,int);
    	
    private:
    	DWORD color;
    	//int pos[8];
    };
    
    
    //Methods (I_Shape)
    
    
    
    
    I_Shape::I_Shape(int x, int y):Shape()
    {
    //make_shape(pos[8],color);
    	color=Blue;
    	pos[0]=x-1;
    	pos[1]=y;
    	pos[2]=x;
    	pos[3]=y;
    	pos[4]=x+1;
    	pos[5]=y;
    	pos[6]=x+2;
    	pos[7]=y;
    	//Shape();  //makes one block
    	make_ishape(); //makes I_Shape
    }
    
    
    // the main entry point for the application is this function
    void DarkGDK ( void )
    {
    	dbInk(White,Black);
    	dbBox(0,0,200,400);
    	
    	I_Shape First(3,1);
    	// turn on sync rate and set maximum rate to 1 fps
    	dbSyncOn   ( );
    	dbSyncRate (2);
    	First.draw_shape();
    	//First.move_shape(5,4);
    	
    
    
    
    
    	
    	// our main loop
    	while ( LoopGDK ( ) )
    	{ 
    		if (First.col())
    		{
    			if (dbLeftKey())
    			{
    				First.move_shape(-1,0);
    			}
    			else if (dbRightKey())
    			{
    				First.move_shape(1,0);
    			}
    			else if (First.col())
    			{	
    				First.move_shape(0,1);
    				
    			}
    		}
    	
    	
    	
    		
    		// update the screen
    		dbSync ( );
    		//dbWaitKey();
    	}
    	
    	
    	// return back to windows
    	return;
    }

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    It is going to take a little more code than that to get a "Tetris" clone working.

    From what I can see, your code doesn't "stop" the piece from moving at all; it just renders it beyond the limits of the draw area.

    Soma

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    1
    Dylan,

    I don't know if you're still struggling but if so, I whipped up a Tetris clone in C++ yesterday and posted the source code after each milestone on my blog:

    Coding Challenge: Write Tetris In 8 Hours or Less « Katy's Code

    It's class-based like yours, I didn't have long to write it though so it's a bit messy and all over the place, you can get the game logic out of the source code fairly easily though and see how I built it up in stages.

    (for the rest of you I'm trying to get my new blog going so any feedback would be appreciated)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with tetris game...
    By Sokay in forum C Programming
    Replies: 4
    Last Post: 10-11-2009, 03:16 AM
  2. Tetris game
    By Da-Nuka in forum Game Programming
    Replies: 1
    Last Post: 01-16-2005, 09:27 AM
  3. My tetris game 2.0
    By PJYelton in forum Game Programming
    Replies: 12
    Last Post: 04-25-2003, 03:40 AM
  4. a good tetris game for ti-83 plus
    By Commander in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 02-28-2003, 04:11 PM
  5. tetris game
    By mrt in forum Game Programming
    Replies: 2
    Last Post: 04-08-2002, 10:01 AM

Tags for this Thread