Skip to content

corrected issue Status: Open. #28 #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 29 additions & 14 deletions Projects/Tanks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,22 +155,37 @@ static void Render(string @string, bool renderSpace = false)

if (tank.IsShooting)
{
tank.Bullet = new Bullet()
// Calculate the potential bullet position
int bulletX = tank.Direction switch
{
X = tank.Direction switch
{
Direction.Left => tank.X - 3,
Direction.Right => tank.X + 3,
_ => tank.X,
},
Y = tank.Direction switch
{
Direction.Up => tank.Y - 2,
Direction.Down => tank.Y + 2,
_ => tank.Y,
},
Direction = tank.Direction,
Direction.Left => tank.X - 3,
Direction.Right => tank.X + 3,
_ => tank.X,
};
int bulletY = tank.Direction switch
{
Direction.Up => tank.Y - 2,
Direction.Down => tank.Y + 2,
_ => tank.Y,
};

// Create a temporary bullet to check for collision
var tempBullet = new Bullet
{
X = bulletX,
Y = bulletY,
Direction = tank.Direction
};

// Check if the bullet would spawn in a wall
bool initialCollision = BulletCollisionCheck(tempBullet, out _);

// Only create the bullet if there's no initial collision
if (!initialCollision)
{
tank.Bullet = tempBullet;
}

tank.IsShooting = false;
continue;
}
Expand Down