Sign in to follow this  
bluwind

GO home viking raiders "your drunk"

Recommended Posts

bluwind    181

Ok, so this is my first C++ code, I am not making any excuses though because I have been programming off and on in VB for a few years so I am aware of a number of bad practises I committed here.

So here is some code for you to feast your eyes upon, you can see how I jumped from one preferred grid referencing method to another, Coordsx, VX, GridRefx... that's what happens when you don't have a clear plan though.

I am currently restarting the project in a much more manageable way because even I have lost track of what is going on in may sections.

I posted the game running (or at least doing a good imitation of running) on youtube here http://www.youtube.com/watch?v=Hp5wsjfUNjY

The game is a Viking raiders (spectrum) remake.

int Player1::Check_Collision(int Directions)
{
	int i;
	switch (Directions)
	{
	case 1:
		//Bottom Left
		if (Coordsx - vx <= -30 || Coordsy + vy >= (SCREEN_HEIGHT - 90) || Map[GridRefx - 1][GridRefy + 1].style == 1 || Map[GridRefx - 1][GridRefy + 1].Occupied == 1)
		{
			
			if (Map[GridRefx - 1][GridRefy + 1].Occupied == 1)
			{
				Fighting = true;	
				Stepinto = 2;
			
				AnimOverlord = 0;
				
			
			}
			
			LockPSel = false;
			Active = false;
			Direction = 0;
			return 1;
			
		}
		break;
		//Down
	case 2:
		if (Coordsy + vy >= (SCREEN_HEIGHT - 90) || Map[GridRefx][GridRefy + 1].style == 1 || Map[GridRefx][GridRefy + 1].Occupied == 1) 
		{
			if (Map[GridRefx][GridRefy + 1].Occupied == 1)
			{
				Fighting = true;	
				Stepinto = 2;
				
				AnimOverlord = 0;
				
			}
			Direction = 0;
			LockPSel = false;
			Active = false;
			return 1;
		}
		break;
	case 3:
		//Bottom Right
		if (Coordsx + vx >= SCREEN_WIDTH || Coordsy + vy >= (SCREEN_HEIGHT - 90) || Map[GridRefx + 1][GridRefy + 1].style == 1 || Map[GridRefx + 1][GridRefy + 1].Occupied == 1)
		{ 
			if (Map[GridRefx + 1][GridRefy + 1].Occupied == 1)
			{
				Fighting = true;	
				Stepinto = 2;
			
				AnimOverlord = 0;
				
			}
			Direction = 0;
			LockPSel = false;
			Active = false;
			return 1;
		}
		break;
	case 4:
		//left
		if (Coordsx - vx <= -30 || Map[GridRefx - 1][GridRefy].style == 1 || Map[GridRefx - 1][GridRefy].Occupied == 1)
		{ 
			if (Map[GridRefx - 1][GridRefy].Occupied == 1)
			{
				Fighting = true;	
				Stepinto = 2;
				
				AnimOverlord = 0;
			
			}
			Direction = 0;
			LockPSel = false;
			Active = false;
			return 1;
			
		}
		break;
	case 6:
		//right
		if (Coordsx + vx >= SCREEN_WIDTH || Map[GridRefx + 1][GridRefy].style == 1 || Map[GridRefx + 1][GridRefy].Occupied == 1 )
		{ 
			if (Map[GridRefx + 1][GridRefy].Occupied == 1)
			{
				Fighting = true;	
				Stepinto = 2;
			
				AnimOverlord = 0;
			

			}
			Direction = 0;
			LockPSel = false;
			Active = false;
			return 1;
			
		}
		break;
	case 7:
		//top left
		if (Coordsx - vx <= -30 || Coordsy - vy <= -30 || Map[GridRefx - 1][GridRefy - 1].style == 1 || Map[GridRefx - 1][GridRefy - 1].Occupied == 1)
		{ 
			if (Map[GridRefx - 1][GridRefy - 1].Occupied == 1)
			{
				Fighting = true;	
				Stepinto = 2;
			
				AnimOverlord = 0;
			
			}
			Direction = 0;
			LockPSel = false;
			Active = false;
			return 1;
		}
		break;
	case 8:
		//up
		if (Coordsy - vy <= -30 || Map[GridRefx][GridRefy - 1].style == 1 || Map[GridRefx][GridRefy - 1].Occupied == 1 )
		{ 
			if (Map[GridRefx][GridRefy - 1].Occupied == 1)
			{
				Fighting = true;
				Stepinto = 2;
				AnimationCycle = true;
				AnimOverlord = 0;
			
			}
			Direction = 0;
			LockPSel = false;
			Active = false;
			return 1;
			
		}
		break;
	case 9:
		//top right
		if (Coordsx +vx >= SCREEN_WIDTH || Coordsy - vy <= -30 || Map[GridRefx + 1][GridRefy - 1].style == 1 || Map[GridRefx + 1][GridRefy - 1].Occupied == 1)
		{ 
			
			if (Map[GridRefx + 1][GridRefy - 1].Occupied == 1)
			{
				Fighting = true;
				Stepinto = 2;
				AnimationCycle = true;
				AnimOverlord = 0;
			}
			
				Direction = 0;
				LockPSel = false;
				Active = false;
			 return 1;

			
		}
		break;
	}
	return 0;
}

Share this post


Link to post
Share on other sites
dtkaos    123

Tip for your code. Within the switch call methods/functions instead of having the code display there. It will clean it up nice!  If you notice you are repeating yourself... create a method becuase the further you get the more the code blurs together. If you need to change a value... you are going to have to change it 9 times ... I know you don't want to do that. :)

 

Also for the switch statments, use a variable name that has some meaning so when you go back to look at it  say something meaningful like

private static final int DOWN = 0;
private static final int UP = 1;

switch(DIRECTION){

   case DOWN: 
            moveDown();
            break;
    
    case UP:
           moveUp();
           break;

     default:
            System.out.println("Doing something in default, Always good pratice to have a default");
            break;
}

private void moveDown(){
         // move that code from the switch statement here 
}

private void moveUp(){
       //move that code from the switch statement here
}

However,

My first game code was pretty rubish. While yours is nice a neat I do belever everything I wrote was declared public and condensed to one file haha. Felt like a lot of code at the time but I believe it was only 50 -100 lines and I have work projects that spread thousands of line that are easier to read then it was. haha

Share this post


Link to post
Share on other sites
Bacterius    13165

Doesn't beat my wonderful "String to Char" function I designed back in 2008. A masterpiece. With original comments!

 

function StrToChr(S: String): Char;  // Fonction pour transformer un String en Char
begin                                // Je n'en disposait pas de préfaite... si vous connaissez un
     if UpperCase(S) = 'A' then Result := 'A';    // moyen plus rapide, faites le moi savoir :)
     if UpperCase(S) = 'B' then Result := 'B';
     if UpperCase(S) = 'C' then Result := 'C';
     if UpperCase(S) = 'D' then Result := 'D';
     if UpperCase(S) = 'E' then Result := 'E';
     if UpperCase(S) = 'F' then Result := 'F';
     if UpperCase(S) = 'G' then Result := 'G';
     if UpperCase(S) = 'H' then Result := 'H';
     if UpperCase(S) = 'I' then Result := 'I';
     if UpperCase(S) = 'J' then Result := 'J';
     if UpperCase(S) = 'K' then Result := 'K';
     if UpperCase(S) = 'L' then Result := 'L';
     if UpperCase(S) = 'M' then Result := 'M';
     if UpperCase(S) = 'N' then Result := 'N';
     if UpperCase(S) = 'O' then Result := 'O';
     if UpperCase(S) = 'P' then Result := 'P';
     if UpperCase(S) = 'Q' then Result := 'Q';
     if UpperCase(S) = 'R' then Result := 'R';
     if UpperCase(S) = 'S' then Result := 'S';
     if UpperCase(S) = 'T' then Result := 'T';
     if UpperCase(S) = 'U' then Result := 'U';
     if UpperCase(S) = 'V' then Result := 'V';
     if UpperCase(S) = 'W' then Result := 'W';
     if UpperCase(S) = 'X' then Result := 'X';
     if UpperCase(S) = 'Y' then Result := 'Y';
     if UpperCase(S) = 'Z' then Result := 'Z';
end;

Actually I enumerated the alphabet at least three times in the code (hangman game, if you must know).

 

At least this is self-documenting, in a redundant kind of way ph34r.png

 


However,
My first game code was pretty rubish. While yours is nice a neat I do belever everything I wrote was declared public and condensed to one file haha. Felt like a lot of code at the time but I believe it was only 50 -100 lines and I have work projects that spread thousands of line that are easier to read then it was. haha

 

Agreed. The joy of being a newbie.. everything felt new and exciting smile.png

Share this post


Link to post
Share on other sites
dtkaos    123

Doesn't beat my wonderful "String to Char" function I designed back in 2008. A masterpiece. With original comments!

 

function StrToChr(S: String): Char;  // Fonction pour transformer un String en Char
begin                                // Je n'en disposait pas de préfaite... si vous connaissez un
     if UpperCase(S) = 'A' then Result := 'A';    // moyen plus rapide, faites le moi savoir :)
     if UpperCase(S) = 'B' then Result := 'B';
     if UpperCase(S) = 'C' then Result := 'C';
     if UpperCase(S) = 'D' then Result := 'D';
     if UpperCase(S) = 'E' then Result := 'E';
     if UpperCase(S) = 'F' then Result := 'F';
     if UpperCase(S) = 'G' then Result := 'G';
     if UpperCase(S) = 'H' then Result := 'H';
     if UpperCase(S) = 'I' then Result := 'I';
     if UpperCase(S) = 'J' then Result := 'J';
     if UpperCase(S) = 'K' then Result := 'K';
     if UpperCase(S) = 'L' then Result := 'L';
     if UpperCase(S) = 'M' then Result := 'M';
     if UpperCase(S) = 'N' then Result := 'N';
     if UpperCase(S) = 'O' then Result := 'O';
     if UpperCase(S) = 'P' then Result := 'P';
     if UpperCase(S) = 'Q' then Result := 'Q';
     if UpperCase(S) = 'R' then Result := 'R';
     if UpperCase(S) = 'S' then Result := 'S';
     if UpperCase(S) = 'T' then Result := 'T';
     if UpperCase(S) = 'U' then Result := 'U';
     if UpperCase(S) = 'V' then Result := 'V';
     if UpperCase(S) = 'W' then Result := 'W';
     if UpperCase(S) = 'X' then Result := 'X';
     if UpperCase(S) = 'Y' then Result := 'Y';
     if UpperCase(S) = 'Z' then Result := 'Z';
end;

Actually I enumerated the alphabet at least three times in the code (hangman game, if you must know).

 

At least this is self-documenting, in a redundant kind of way ph34r.png

 

 

That's awesome! I use java and the first thing in my mind is a string to lowercase(or uppercase) then thake that string to to char array would solved have that problem.

 

My very first code was c# to back in 2004? I was like 14 and I only messed around with c# for 2 weeks but it was  a series of dialogbox not in a loop. I literally copy and pasted that many lines of code and I tought it was haularious to have people click the first one thinking it would go away.

Edited by dtkaos

Share this post


Link to post
Share on other sites
Vortez    2714

Haha nice one Bacterius, i though i was the only one speaking french here. I think i did something similar when i was just starting with Delphi, the first real programming language i've learned. Back then you think, "Oh!, so clever!", a few years later you say "ohhh my god..." hahaha.

Edited by Vortez

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this