Advertisement

Help with Tetris type game

Started by January 13, 2003 11:36 PM
0 comments, last by tylerbingham 22 years, 1 month ago
her is the problem....I need a way to draw a block at the top of the screen and move it around (x and y coords) until it hits the bottom. After that, I need the block that hit the bottom to stay there and not move or disapear!!! Here is the code I have, but it sux and doesn''t work right.I would really appreciate the help. #include <windows.h> #include <gl\gl.h> #include <gl\glu.h> #include "NeHeGL.h" #include "Board.h" #include "Piece.h" #include "Picloader.h" #include "Font.h" #include "Particle.h" #pragma comment( lib, "opengl32.lib" ) #pragma comment( lib, "glu32.lib" ) #ifndef CDS_FULLSCREEN #define CDS_FULLSCREEN 4 #endif #define BLOCK_MAX 10 #define SCREEN_BOTTOM -3 #define SCREEN_TOP 2 GL_Window* g_window; Keys* g_keys; Application* app; int current_block = 0; float block_x[]= {0.0f}; float block_y[BLOCK_MAX] = {SCREEN_TOP,SCREEN_TOP,SCREEN_TOP,SCREEN_TOP,SCREEN_TOP, SCREEN_TOP,SCREEN_TOP,SCREEN_TOP,SCREEN_TOP,SCREEN_TOP}; void Block(int block_number) { glColor3f(1,1,1); glPushMatrix(); glTranslatef(0,0,-8); glTranslatef(block_x[block_number], block_y[block_number],0); glBegin(GL_QUADS); glVertex3f(-0.25f,-0.25f,0.0f); glVertex3f(-0.25f,0.25f,0.0f); glVertex3f(0.25f,0.25f,0.0f); glVertex3f(0.25f,-0.25f,0.0f); glEnd(); glPopMatrix(); } void Draw(void) { glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer glLoadIdentity(); float speed_pressed = 0.08f; for (int x1=0;x1<10;x1++) { if (x1 == current_block) { if (g_keys->keyDown[VK_LEFT]==TRUE) { block_x[x1]-=speed_pressed; } else if (g_keys->keyDown[VK_RIGHT]==TRUE) { block_x[x1]+=speed_pressed; } else if (g_keys->keyDown[VK_UP]==TRUE) { block_y[x1]+=speed_pressed; } else if (g_keys->keyDown[VK_DOWN]==TRUE) { block_y[x1]-=speed_pressed; } } Block(x1); } if (block_y[current_block]keyDown [VK_ESCAPE] == TRUE) // Is ESC Being Pressed? { TerminateApplication (g_window); // Terminate The Program } if (g_keys->keyDown [VK_F1] == TRUE) // Is F1 Being Pressed? { ToggleFullscreen (g_window); // Toggle Fullscreen Mode } } /********************************************************************* ---------Initialize---------- *********************************************************************/ BOOL Initialize(GL_Window* window, Keys* keys) { g_window = window; g_keys = keys; glClearColor (0.0f, 0.0f, 0.0f, 0.5f); // Black Background glClearDepth (1.0f); // Depth Buffer Setup glShadeModel(GL_SMOOTH); // use smooth shading glEnable(GL_DEPTH_TEST); // hidden surface removal return true; } /********************************************************************* ---------Deinitailize--------------- *********************************************************************/ void Deinitialize(void) {
In the code you've attached, I can't see where you are checking that the block position hasn't gone past the screen edges. One way around this is to amend the code where you read the keys pressed to do something like :


  if (g_keys->keyDown[VK_DOWN]==TRUE)         // Check for down key{  block_y[x1] = block_y[x1]-speed_pressed;  // Decrease block y position  if (block_y[x1]) < SCREEN_BOTTOM)         // Is it past the bottom of the screen?  {    block_y[x1] = SCREEN_BOTTOM;            // Set it to screen bottom if so  {}  


Most likely not the quickest of code, but it's readable.

Cheers,
Jeroen


[edited by - BigBoy on January 14, 2003 7:36:23 AM]
Cheers,Jeroen

This topic is closed to new replies.

Advertisement