GO home viking raiders "your drunk"

Started by
4 comments, last by Vortez 10 years, 10 months ago

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

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;
}
Advertisement

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

If you get it finished make sure you announce it here as well:

http://www.worldofspectrum.org/forums/index.php

I did a Manic Miner remake, but didn't finish it. I wrote it on the Spectrum using an emulator in Z80 asm ;)

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

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

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

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.

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.

This topic is closed to new replies.

Advertisement