🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

rolling dice

Started by
27 comments, last by Warp9 1 year, 11 months ago

I am trying to roll some dice and move a spaceship sprite. however, when I move the spaceship sprite it rolls the dice which a I don't want it to do. I think that the function glutPostRedisplay() is not working properly. let me know if you need more code.

void handleKeypress(unsigned char key, int x, int y)
{
	switch (key)
	{
	case 27:
		exit(0);
		break;
	case 's':
		dice_one();
		dice_two();
		dice_three();
		dice_four();
		dice_five();
		break;
	}
	glutPostRedisplay();
}

void handleSpecialKeypress(int key, int x, int y)
{
	switch (key)
	{
	case GLUT_KEY_LEFT:
		move_x--;
		break;
	case GLUT_KEY_RIGHT:
		move_x++;
		break;
	case GLUT_KEY_UP:
		break;
	case GLUT_KEY_DOWN:
		break;
	}
	glutPostRedisplay();
}
Advertisement

im not much sure but as far i know the Switch parameter manage INT value

then if you put CASE ‘s’ probably dont work much…. may be what you have to do there is put the ASCII code (int number 115 in ascii tables) in that case of the dices functions

keep in mind that you referencing this function as void handleKeypress(unsigned char key, int x, int y) ….like unsigned char key when it must be a int key parameter for fit with Switch definition

Snaked said:
then if you put CASE ‘s’ probably dont work much

They work, and it is well defined in the language.

It is an integer literal that represents the literal with the value that matches on the target environment. For most modern environments it is 115, because nearly every system has solidified around ASCII or Unicode for the 95 printable values they have in common. For certain mainframes it is 162. In theory it could be any system-specific value, if a system uses a different character set.

Since there was no size specifier it is an int. If there were specifiers it could be a value of type char, char16_t, char32_t, or wchar_t rather than int.

Snaked said:
keep in mind that you referencing this function as void handleKeypress(unsigned char key, int x, int y) ….like unsigned char key when it must be a int key parameter for fit with Switch definition

This is subject to the language's standard conversion. Yes it is better programming style for them to be the same types if possible, but the integral promotion is automatic in the language, doesn't even generate a warning. The conversion from from unsigned char to int is fine since the target type can represent all the values of the source.

I tried unsigned char to int, but it does not work

I posted my code to blog called Dice Space Game.

@pbivens67 : That code looks fine to me. I would guess that the error lies somewhere else.

I think that the function glutPostRedisplay() is not working properly.

Why do you think that?

Because when I take it out the ship does not move at all. I do not know where to look for the error.

@pbivens67 : What does your draw code look like ?

I posted my code in a blog called Dice Space Game

@pbivens67 : I'm glad you posted that stuff over in a blog. But that still doesn't help out our conversation on this site. ;-)

Based on what you wrote in the very first post, I assume that you think your keyboard case ‘s’ (where you have the dice code) is somehow getting called when you try to use the move arrow keys. I would suggest either putting in some debug break statements, or some cout output statements to find out what is actually going on when you run your code. Are you actually triggering that case ‘s’ ?

On thing did occur to me, many games use the ‘w’ ‘a’ ‘s’ and ‘d’ keys to move around, as if they were arrow keys. Is it possible that your system is somehow mapping the output of the arrow keys to wasd ? It seems strange that would happen, but if it did, it might explain why trying to move your ship with the arrow keys activates the case ‘s’ stuff.

This topic is closed to new replies.

Advertisement