I am trying to search which elements in this c++ array contain the highest value and then output the key of that element but it keeps outputting a list of numbers like 9044007. What I want to know is how do I output the first number only because I'm guessing it is outputting all the keys of the elements with the same value.
double max = 0;
for (int i = 1; i <= 9; i++) {
if (responselayer[i] > max) {
max = responselayer[i];
}
}
for (int i = 1; i < 10; i++) {
if (responselayer[i] == max) {
return i;
break;
}
}
Here is the part of the code that fills the entire thing. It's a lot of code so it's hard to make it minimal. This is my first time trying stack so I don't really know how everything works on here.
//final layer input calculation and output//
for (int j = 1; j <= 10; j++) {
double subval = 0;
if (j == 1) {
for (int i = 0; i < 5; i++) {
subval = (midlayerweight4[i] * midlayer3[i]) + subval;
responselayer[j] = subval;
}
responselayer[j] = 1 / (1 + exp(-responselayer[j]));
}
else if (j == 2) {
int k = 0;
for (int i = 5; i < 10; i++) {
subval = (midlayerweight4[i] * midlayer3[k]) + subval;
responselayer[j] = subval;
k++;
}
responselayer[j] = 1 / (1 + exp(-responselayer[j]));
}
else if (j == 3) {
int k = 0;
for (int i = 10; i <15; i++) {
subval = (midlayerweight4[i] * midlayer3[k]) + subval;
responselayer[j] = subval;
k++;
}
responselayer[j] = 1 / (1 + exp(-responselayer[j]));
}
else if (j == 4) {
int k = 0;
for (int i = 15; i < 20; i++) {
subval = (midlayerweight4[i] * midlayer3[k]) + subval;
responselayer[j] = subval;
k++;
}
responselayer[j] = 1 / (1 + exp(-responselayer[j]));
}
else if (j == 5) {
int k = 0;
for (int i = 20; i <25; i++) {
subval = (midlayerweight4[i] * midlayer3[k]) + subval;
responselayer[j] = subval;
k++;
}
responselayer[j] = 1 / (1 + exp(-responselayer[j]));
}
else if (j == 6) {
int k = 0;
for (int i = 25; i <30; i++) {
subval = (midlayerweight4[i] * midlayer3[k]) + subval;
responselayer[j] = subval;
k++;
}
responselayer[j] = 1 / (1 + exp(-responselayer[j]));
}
else if (j == 7) {
int k = 0;
for (int i = 30; i <35; i++) {
subval = (midlayerweight4[i] * midlayer3[k]) + subval;
responselayer[j] = subval;
k++;
}
responselayer[j] = 1 / (1 + exp(-responselayer[j]));
}
else if (j == 8) {
int k = 0;
for (int i = 35; i < 40; i++) {
subval = (midlayerweight4[i] * midlayer3[k]) + subval;
responselayer[j] = subval;
k++;
}
responselayer[j] = 1 / (1 + exp(-responselayer[j]));
}
else if (j == 9) {
int k = 0;
for (int i = 40; i <45; i++) {
subval = (midlayerweight4[i] * midlayer3[k]) + subval;
responselayer[j] = subval;
k++;
}
responselayer[j] = 1 / (1 + exp(-responselayer[j]));
}
}
double max = 0;
for (int i = 1; i <10; i++) {
if (responselayer[i] > max) {
max = responselayer[i];
}
}
for (int i = 1; i < 10; i++) {
if (responselayer[i] == max) {
return i;
}
}
}
}
midlayerweight4[] was filled by an array before it in this same fashion and so was others behind that. This is the declaration for responselayer and all the other arras used were of type double and they were all declared outside of the array.
double responselayer[10];
A few things:
Array indexing in C/C++ starts at zero.
You do not show us the type of your array, so I'm going to assume it is double responselayer[SZ]; If this is not the case, you have some issues involving implicit type conversion from integral to floating point types.
It is sloppy to use 2 different styles of conditions in your for-loop.
It is sloppy to use magic numbers in your code.
I would write the code as follows:
size_t find_max_index(const double data[], size_t size) {
size_t max_index = 0;
// No need to compare data[0] to itself on the first iteration, so
// loop starts at one
for (size_t i = 1; i < size; ++i)
if (data[i] > data[max_index])
max_index = i;
return max_index;
}
Or getting a little fancier and taking care of the possibility of floating point strangeness, we can make it a templated function:
template <typename T>
size_t find_max_index(const T data[], size_t size) {
size_t max_index = 0;
for (size_t i = 1; i < size; ++i)
if (data[i] > data[max_index])
max_index = i;
return max_index;
}
Hi i'm somewhat new with c++ and openframeworks and I'm trying to get my program running more smooth by erasing the element from the vector when it goes out of the screen. At the moment as the time goes on it still gets slower and slower. I'm breaking my head over this. If you have any idea how I could improve this.
bool isDead(Particle &p) {
if (p.position.x > ofGetWindowWidth() || p.position.x == -1 || p.position.y > ofGetWindowHeight() || p.position.y == -1) {
return true;
}
else {
return false;
}
}
//--------------------------------------------------------------
void ofApp::setup() {
ofSetFrameRate(60);
ofEnableAlphaBlending();
ofBackground(ofColor::black);
for (int i = 0; i < 50; i++) {
Particle p;
p.setup(ofVec2f(ofRandom(ofGetWindowWidth() * 0.45, ofGetWindowWidth() * 0.55), ofRandom(ofGetWindowHeight() * 0.45, ofGetWindowHeight() * 0.55)));
particles.push_back(p);
}
beat1.load("beat1.wav");
beat2.load("beat2.wav");
beat3.load("beat3.wav");
fftSmooth = new float[8192];
for (int i = 0; i < 8192; i++) {
fftSmooth[i] = 0;
}
bands = 128;
beat1.setVolume(0.2);
beat2.setVolume(0.2);
beat3.setVolume(0.2);
}
//--------------------------------------------------------------
void ofApp::update() {
ofSoundUpdate();
float * value = ofSoundGetSpectrum(bands);
for (int i = 0; i < bands; i++) {
fftSmooth[i] *= 0.2f;
if (fftSmooth[i] < value[i]) {
fftSmooth[i] = value[i];
}
}
ofRemove(particles, isDead);
for (Particle& p : particles) {
if (!p.isActive) {
p.position = ofVec2f(ofRandom(ofGetWindowWidth() * 0.45, ofGetWindowWidth() * 0.55), ofRandom(ofGetWindowHeight() * 0.45, ofGetWindowHeight() * 0.55));
p.isActive = true;
return;
}
p.update();
}
if (isDead) {
Particle p;
p.setup(ofVec2f(0, 0));
particles.push_back(p);
}
}
//--------------------------------------------------------------
void ofApp::draw() {
for (Particle& p1 : particles) {
if (!p1.isActive) continue;
bool foundConnection = false;
//search for connections.
for (Particle& p2 : particles) {
if (!p2.isActive || p2.drawPosition == p1.drawPosition) continue;
float distance = p1.drawPosition.distance(p2.drawPosition);
for (int i = 0; i < bands; i++) {
if (distance > 10 && distance < 50 * fftSmooth[i]) {
ofDrawLine(p1.drawPosition, p2.drawPosition);
foundConnection = true;
}
}
}
for (int i = 0; i < 50; i++) {
p1.draw(-(fftSmooth[i] * 10));
}
}
}
I have multiple SDL_Rects creating a snake that is supposed to stay in a specific area. Sometimes when the "snake" reaches the boundaries a part disappears.
void Snake::update(SDL_Surface *screen, int level)
{
old_pos.first = snake_rect[0];
if(x_axis)
snake_rect[0].x += snake_speed * level * direction_multiplier;
else if(!x_axis)
snake_rect[0].y += snake_speed * level * direction_multiplier;
for(unsigned int i = 1; i < snake_rect.size(); ++i)
{
old_pos.second = snake_rect[i];
snake_rect[i] = old_pos.first;
old_pos.first = old_pos.second;
}
boundariesCheck(screen);
/// Making the enemy move randomly
if(rand() % 100 < 10)
{
if(x_axis)
{
x_axis = false;
direction.second = rand() % 2;
if(direction.second)
direction_multiplier = 1;
else if(!direction.second)
direction_multiplier = -1;
}
else if(!x_axis)
{
x_axis = true;
direction.first = rand() % 2;
if(direction.first)
direction_multiplier = 1;
else if(!direction.first)
direction_multiplier = -1;
}
}
}
void Snake::draw(SDL_Surface *screen)
{
for(unsigned int i = 1; i < snake_rect.size(); ++i)
{
SDL_FillRect(screen, &snake_rect[i], 0xFF0000);
}
SDL_FillRect(screen, &snake_rect[0], 0xFF5500);
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I've written a simple brick breaker game using OpenGL and OpenGLUT. Everthing works as it should, except, when a level gets over, the game hangs. Any idea why ?
I tried telling it to reset all parameters, but nothing works. :(
#include <GL/openglut.h>
#include <cmath>
#include <string>
#include <ctime>
#define RADIUS 0.025
#define P_LENGTH 0.3
using namespace std;
int WIDTH = 900,
HEIGHT = 650,
lives = 3,
SCORE = 0,
LEVEL = 2,
BRICK_ROW = 10,
BRICK_COL = 9;
double x_brick[10][10],
y_brick[10][10],
P_XPOS = 0,
P_YPOS = -0.8,
x_pos = 0,
y_pos = -0.75,
win_aspect,
SPEED = 0;
bool show[10][10],
phit_center = false,
phit_corner = false,
game_over = false,
RIGHT = true,
LEFT = false,
UP = true,
DOWN = false,
started = false,
hit = false;
char life = '0';
void user_input(unsigned char key, int x, int y)
{
if(key == 13)
started = true;
}
void b_draw()
{
glBegin(GL_QUADS);
for(int a = 0; a < BRICK_COL; a++)
{
for(int b = 0; b < BRICK_ROW; b++)
{
switch(b)
{
case 0:
glColor3f(1.0,0.0,0.0);
break;
case 1:
glColor3f(1.0,0.9,0.1);
break;
case 2:
glColor3f(0.0,1.0,0.0);
break;
case 3:
glColor3f(0.11,0.56,1.0);
break;
default:
glColor3f(1.0,0.0,0.0);
}
if(show[b][a] == true)
{
glVertex2f(x_brick[b][a]*win_aspect,y_brick[b][a]);
glVertex2f(x_brick[b][a]*win_aspect,(y_brick[b][a] - 0.10));
glVertex2f((x_brick[b][a]+0.2)*win_aspect,(y_brick[b][a] - 0.10));
glVertex2f((x_brick[b][a]+0.2)*win_aspect,y_brick[b][a]);
}
}
}
glColor3f(0.0,0.0,0.0);
glEnd();
}
void Set_Level(int level)
{
int R_Limit = 0;
switch(level)
{
case 1:
for(int a = 0; a < BRICK_ROW; a++)
{
for(int b = 0; b < BRICK_COL; b++)
{
show[a][b] = 1;
}
}
break;
case 2:
for(int a = 0; a < BRICK_ROW/2; a++)
{
for(int b = 0; b < BRICK_COL; b++)
{
if(b >= BRICK_COL/2 - R_Limit && b<=BRICK_COL/2 + R_Limit)
show[a][b] = true;
}
R_Limit++;
}
R_Limit = BRICK_ROW/2;
for(int a = BRICK_ROW/2; a <= BRICK_ROW; a++)
{
for(int b = 0; b <= BRICK_COL; b++)
{
if(b >= BRICK_COL/2 - R_Limit && b <= BRICK_COL/2 + R_Limit)
show[a][b] = true;
}
R_Limit--;
}
break;
case 3:
for(int a = 0; a <= BRICK_COL/2; a++)
{
show[a][a] = true;
}
for(int a = BRICK_COL; a >= BRICK_COL/2; a--)
{
show[BRICK_COL - a - 1][a] = true;
}
break;
}
}
bool all_gone()
{
int local_flag = 0;
for(int a = 0; a < BRICK_COL; a++)
{
for(int b = 0; b < BRICK_ROW; b++)
{
if(show[b][a] == true)
{
return false;
local_flag = 1;
break;
}
if(local_flag)
break;
}
}
return true;
}
void c_draw()
{
glColor3f(0.0f, 0.0f, 0.0f);
glBegin(GL_TRIANGLE_FAN);
glVertex2f(x_pos,y_pos);
for (float angle = 0; angle < (10); angle+=0.01)
{
glVertex2f((x_pos + sin(angle) * RADIUS), (y_pos + (cos(angle)) * RADIUS));
}
glEnd();
}
bool crashed()
{
if(y_pos < P_YPOS - 0.1)
return true;
else return false;
}
void c_move()
{
if(UP && RIGHT)
{
x_pos += SPEED;
y_pos += SPEED;
}
if(UP && LEFT)
{
x_pos -= SPEED;
y_pos += SPEED;
}
if(DOWN && RIGHT)
{
x_pos += SPEED;
y_pos -= SPEED;
}
if(DOWN && LEFT)
{
x_pos -= SPEED;
y_pos -= SPEED;
}
}
void p_draw()
{
glColor3f(0.0,0.0,0.0);
glBegin(GL_QUADS);
glVertex2f(P_XPOS-P_LENGTH, P_YPOS);
glVertex2f(P_XPOS+P_LENGTH, P_YPOS);
glVertex2f(P_XPOS+P_LENGTH, P_YPOS-0.05);
glVertex2f(P_XPOS-P_LENGTH, P_YPOS-0.05);
glEnd();
}
void SET_BRICKS(int level)
{
switch(level)
{
case 1:
BRICK_ROW = 4;
break;
case 2:
BRICK_ROW = 7;
break;
case 3:
BRICK_ROW = 7;
break;
}
}
void BallLoop()
{
const int win_width = glutGet(GLUT_WINDOW_WIDTH),
win_height = glutGet(GLUT_WINDOW_HEIGHT);
win_aspect = (float)win_width/(float)win_height;
glClearColor(1.0, 1.0, 1.0, 0);
glDisable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, win_width, win_height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-win_aspect, win_aspect, -1, 1, -1, 1);
c_draw();
b_draw();
p_draw();
life = '0' + lives;
if(started && lives > 0)
c_move();
glFlush();
glutSwapBuffers();
glutPostRedisplay();
}
void GameLogic()
{
if(all_gone())
{
x_pos = 0;
y_pos = -0.7;
LEVEL++;
for(int a = 0; a < BRICK_ROW; a++)
{
for(int b = 0; b <BRICK_COL; b++)
{
show[a][b] = true;
}
}
phit_center = false,
phit_corner = false,
game_over = false,
RIGHT = true,
LEFT = false,
UP = true,
DOWN = false,
started = false,
hit = false;
SET_BRICKS(LEVEL);
Set_Level(LEVEL);
}
if(x_pos >= win_aspect)
{
RIGHT = 0;
LEFT = 1;
}
else if(x_pos <= -win_aspect)
{
RIGHT = 1;
LEFT = 0;
}
if(y_pos >= 1-RADIUS || hit )
{
UP = 0;
DOWN = 1;
}
else if(y_pos <= -1+RADIUS || hit )
{
UP = 1;
DOWN = 0;
}
hit = false;
int flag = 1;
for(int a = 0; a < BRICK_COL; a++)
{
for(int b =0; b < BRICK_ROW; b++)
{
if(x_pos >= x_brick[b][a]*win_aspect && x_pos <= (x_brick[b][a] + 0.2)*win_aspect)
{
if(y_pos <= y_brick[b][a] && y_pos >= y_brick[b][a] - 0.1)
{
if(show[b][a] == 1)
{
show[b][a] = 0;
flag = 0;
hit = true;
break;
}
}
}
}
if(flag == 0)
break;
}
phit_corner = false;
phit_center = false;
if(x_pos <= (P_XPOS + P_LENGTH - 0.05)&& x_pos >= (P_XPOS - P_LENGTH - 0.05))
{
if(y_pos <= P_YPOS)
{
phit_center = true;
}
}
else if((x_pos >=(P_XPOS + P_LENGTH - 0.05) && x_pos <= (P_XPOS + P_LENGTH)) || (x_pos <= (P_XPOS - P_LENGTH + 0.05) && x_pos >= P_XPOS - P_LENGTH))
{
if(y_pos <= P_YPOS)
{
phit_corner = true;
}
}
if(phit_center)
{
DOWN = false;
UP = true;
}
if(phit_corner)
{
if(LEFT)
{
LEFT = false;
RIGHT = true;
}
else
{
RIGHT = false;
LEFT = true;
}
UP = false;
DOWN = true;
}
if(crashed())
{
x_pos = 0;
y_pos = -0.7;
started = false;
UP = true;
RIGHT = true;
DOWN = false;
LEFT = false;
}
BallLoop();
}
void ArrowKeys(int key, int x, int y)
{
if(key==GLUT_KEY_LEFT && P_XPOS >= -0.9*win_aspect)
{
for(float a = 0; a < 0.05; a+= 0.001)
{
P_XPOS -=0.002;
GameLogic();
}
if(!started)
started = true;
}
if(key==GLUT_KEY_RIGHT && P_XPOS <= 0.9*win_aspect)
{
for(float a = 0; a < 0.05; a+= 0.001)
{
P_XPOS +=0.002;
GameLogic();
}
if(!started)
started = true;
}
}
void Init_Game()
{
int c = 0;
for(float a = -0.94; c < BRICK_COL; a+=0.21)
{
for(int b = 0; b <= BRICK_ROW; b++)
{
x_brick[b][c] = a;
}
c++;
}
c = 0;
for(float a = 0.99; c < BRICK_ROW; a-=0.11)
{
for(int b = 0; b < BRICK_COL; b++)
{
y_brick[c][b] = a;
}
c++;
}
}
void set_speed()
{
clock_t start = clock();
for(int a = 1; a < 99999999LLU; a+= 1)
{
;
}
clock_t end = clock();
SPEED = (double)(end - start)/CLOCKS_PER_SEC;
SPEED /= 800;
}
int main(int argc, char **argv)
{
set_speed();
Init_Game();
SET_BRICKS(LEVEL);
Set_Level(LEVEL);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(0,0);
glutInitWindowSize(WIDTH,HEIGHT);
glutCreateWindow("Brick Breaker - By Viraj");
glutKeyboardFunc(user_input);
glutSpecialFunc(ArrowKeys);
glutIdleFunc(GameLogic);
glutDisplayFunc(BallLoop);
glutMainLoop();
return 0;
}
Please take a look at the corrected version of your code I posted as answer to your original question. Brick Breaker help. circles, paddles, and awkward bouncing
Reposting the GLFW version here again, for simplicity:
#include <GL/glfw.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
using namespace std;
#define RADIUS 0.025
#define RATIO (4./3.)
bool show[5][10];
float x_brick[4][9];
float y_brick[4][9];
const float SpeedFactor = 1.;
float paddle_x = 0;
float paddle_y = -0.8;
float paddle_speed = 0;
const float PaddleSpeedFactor = 3.;
bool phit_center = false, phit_corner = false;
bool game_over = false;
float speed_x = 0.;
float speed_y = 0.;
float x_pos;
float y_pos;
int lifes = 0;
void draw_bricks()
{
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_QUADS);
for (int a = 0; a < 9; a++) {
for (int b = 0; b < 4; b++) {
if (show[b][a] == 1) {
glVertex2f(x_brick[b][a], y_brick[b][a]);
glVertex2f(x_brick[b][a], y_brick[b][a] - 0.10);
glVertex2f(x_brick[b][a] + 0.2,
y_brick[b][a] - 0.10);
glVertex2f(x_brick[b][a] + 0.2, y_brick[b][a]);
}
}
}
glEnd();
}
void ball_draw()
{
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_TRIANGLE_FAN);
glVertex2f(x_pos, y_pos);
for (float angle = 0; angle < (10); angle += 0.01) {
glVertex2f((x_pos + sin(angle) * RADIUS),
(y_pos + (cos(angle)) * RADIUS));
}
glEnd();
}
bool brick_hit()
{
for (int a = 0; a < 10; a++) {
for (int b = 0; b < 4; b++) {
if (x_pos >= x_brick[b][a]
&& x_pos <= x_brick[b][a] + 0.2) {
if (y_pos <= y_brick[b][a]
&& y_pos >= y_brick[b][a] - 0.1) {
if (show[b][a] == 1) {
show[b][a] = 0;
return true;
}
}
}
}
}
return false;
}
bool crashed()
{
if (y_pos < paddle_y - 0.05)
return true;
return false;
}
void paddle_hit()
{
phit_corner = false;
phit_center = false;
if (x_pos <= paddle_x + 0.13 && x_pos >= paddle_x - 0.13) {
if (y_pos <= paddle_y) {
phit_center = true;
}
} else if ((x_pos >= paddle_x + 0.13 && x_pos <= paddle_x + 0.2) ||
(x_pos <= paddle_x - 0.13 && x_pos >= paddle_x - 0.2)) {
if (y_pos <= paddle_y) {
phit_corner = true;
}
}
}
void paddle_move(float dT)
{
if (paddle_x < RATIO && paddle_x > -RATIO)
paddle_x += paddle_speed * PaddleSpeedFactor * dT;
if (paddle_x > 1.) {
paddle_x = 1.;
paddle_speed = 0.;
}
if (paddle_x < -1.) {
paddle_x = -1.;
paddle_speed = 0.;
}
}
void ball_move(float dT)
{
x_pos += speed_x * dT;
y_pos += speed_y * dT;
if (brick_hit()) {
speed_y *= -1;
}
if (x_pos >= (RATIO - RADIUS) || x_pos <= (-RATIO + RADIUS)) {
speed_x *= -1;
}
if (y_pos >= (1. - RADIUS)) {
speed_y *= -1;
}
paddle_hit();
if (phit_center) {
speed_y *= -1;
speed_x += 0.5 * paddle_speed;
}
if (phit_corner) {
speed_x = -speed_x + 0.2 * paddle_speed;
speed_y *= -1;
}
if( (speed_x * speed_x + speed_y * speed_y) > 0.001 ) {
float k = 1./sqrt(speed_x * speed_x + speed_y * speed_y);
speed_x *= k;
speed_y *= k;
}
}
void paddle_draw()
{
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_QUADS);
glVertex2f(paddle_x - 0.2, paddle_y);
glVertex2f(paddle_x + 0.2, paddle_y);
glVertex2f(paddle_x + 0.2, paddle_y - 0.05);
glVertex2f(paddle_x - 0.2, paddle_y - 0.05);
glEnd();
}
void set_xy();
void reset_game()
{
set_xy();
lifes = 3;
speed_x = 0;
speed_y = 0;
x_pos = 0;
y_pos = -0.7;
paddle_speed = 0;
paddle_x = 0;
}
void step_game(float dT)
{
if(!lifes)
return;
paddle_move(dT * SpeedFactor);
ball_move(dT * SpeedFactor);
if (crashed()) {
lifes--;
speed_x = 0;
speed_y = 0;
x_pos = 0;
y_pos = -0.7;
}
}
static float frandom(float a, float b)
{
return a + (b - a) * (float)random() / (float)RAND_MAX;
}
void launch_ball()
{
if(!lifes)
return;
speed_y = 1.;
speed_x = frandom(-1., 1.);
float k = 1./sqrt(speed_x * speed_x + speed_y + speed_y);
speed_x *= k;
speed_y *= k;
}
void keyboard(int key, int action)
{
switch(key)
{
case GLFW_KEY_ENTER:
launch_ball();
break;
case GLFW_KEY_ESC:
reset_game();
break;
case GLFW_KEY_LEFT:
switch(action) {
case GLFW_PRESS:
paddle_speed = -1.;
break;
case GLFW_RELEASE:
paddle_speed = 0;
break;
} break;
case GLFW_KEY_RIGHT:
switch(action) {
case GLFW_PRESS:
paddle_speed = 1.;
break;
case GLFW_RELEASE:
paddle_speed = 0;
break;
} break;
}
}
void set_xy()
{
for (int a = 0; a < 5; a++) {
for (int b = 0; b < 10; b++) {
show[a][b] = 1;
}
}
int c = 0;
for (float a = -0.94; c <= 8; a += 0.21, c++) {
for (int b = 0; b <= 5; b++) {
x_brick[b][c] = a;
}
}
int d = 0;
for (float s = 0.99; d <= 3; s -= 0.11, d++) {
for (int r = 0; r < 9; r++) {
y_brick[d][r] = s;
}
}
}
float display()
{
int win_width;
int win_height;
glfwGetWindowSize(&win_width, &win_height);
const float win_aspect = (float)win_width / (float)win_height;
glfwSetTime(0.);
glViewport(0, 0, win_width, win_height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (win_aspect > RATIO) {
glOrtho(-win_aspect, win_aspect, -1., 1., -1., 1.);
} else {
glOrtho(-RATIO, RATIO, -RATIO / win_aspect, RATIO / win_aspect,
-1., 1.);
}
glMatrixMode(GL_MODELVIEW);
glClearColor(0., 0., 1., 1.);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
glColor3f(1, 1, 1);
glVertex2f(-RATIO, -1);
glVertex2f(RATIO, -1);
glVertex2f(RATIO, 1);
glVertex2f(-RATIO, 1);
glEnd();
draw_bricks();
paddle_draw();
ball_draw();
glfwSwapBuffers();
return glfwGetTime();
}
int main(int argc, char **argv)
{
srandom(time(0));
set_xy();
if( GL_FALSE == glfwInit() )
return -1;
if( GL_FALSE == glfwOpenWindow(800, 600, 8, 8, 8, 8, 0, 0, GLFW_WINDOW) )
return -2;
glfwSetWindowTitle("Viraj's Brick Breaker - GLFW version by datenwolf");
glfwSetKeyCallback(keyboard);
reset_game();
while( glfwGetWindowParam(GLFW_OPENED) ) {
glfwPollEvents();
float const dT = display();
step_game(dT);
}
glfwTerminate();
return 0;
}
I'm just telling this, because the code you posted above is (again) an utter mess. Also notice how I count down the lifes, and introduced a function reset_game that will put things back to starting conditions.
BTW: Your coding style is a mess. It looks like you've done a lot of BASIC (GW-BASIC or QBASIC programming) and are still mentally stuck in that world.
For example you should reset the game field through a single entry function, that then calls specialized functions for each reset step. Then you just call that reset function, instead of trying to keep track of all variables.
I understand that the new code in your question has new features, but you still need to restructure a few things.
One of the biggest mistakes of yours is, that you still mix input with animation:
void ArrowKeys(int key, int x, int y)
{
if(key==GLUT_KEY_LEFT && P_XPOS >= -0.9*win_aspect)
{
for(float a = 0; a < 0.05; a+= 0.001)
{
P_XPOS -=0.002;
GameLogic();
}
That inner for loop will animate the paddle in reaction to the key, and while this animation happens all further user input will pile up, leading to strange movements. Never do this. In a input handler set some state variable, that influences the GameLogic in the idle loop. But never directly animate from the input handler!
Please take a look at my code, compile it, execute it. And try to understand the separation between game logic, rendering and input processing.
I'm trying to make it where the character is in a tile and when they move up or down it moves to the next tile but I'm not sure how to do that. Right now, I have it set up where the character moves by pixels but I want it to move by 1 square.
The code right now is this, and it works, but it's glitchy in pixel mode. I believe if it was by blocks it might work better but I might change it anyway.
float spritewidth = sprite->stretchX;
float spriteheight = sprite->stretchY;
float bushwidth = bush->stretchX;
float bushheight = bush->stretchY;
//Basic border collision
if (sprite->x <= 0)
sprite->x = 0;
if (sprite->y <= 0)
sprite->y = 0;
if (sprite->x >= 455)
sprite->x = 455;
if (sprite->y >= 237)
sprite->y = 237;
if ( (sprite->x + spritewidth > bush->x) && (sprite->x < bush->x + bushwidth) && (sprite->y + spriteheight > bush->y) && (sprite->y < bush->y + bushheight) )
{
bushcol = 1;
}
else
{
bushcol = 0;
}
if (osl_keys->held.down)
{
if (bushcol == 1)
{
sprite->y = bush->y - spriteheight - 3;
bushcol = 0;
}
else
{
bushcol = 0;
sprite->y += 3;
}
}
if (osl_keys->held.up)
{
if (bushcol == 1)
{
sprite->y = bush->y + bushheight + 3;
bushcol = 0;
}
else
{
bushcol = 0;
sprite->y -= 3;
}
}
if (osl_keys->held.right)
{
if (bushcol == 1)
{
sprite->x = bush->x - spritewidth - 3;
bushcol = 0;
}
else
{
bushcol = 0;
sprite->x += 3;}
}
if (osl_keys->held.left)
{
if (bushcol == 1)
{
sprite->x = bush->x + bushwidth + 3;
bushcol = 0;
}
else
{
bushcol = 0;
sprite->x -= 3;
}
}
If you want the character to move one tile/square/block at a time, just move the sprite the number of pixels the tile is wide (or tall).
const int tile_width = 32; // or something
// and then
sprite->x += tile_width;