init commit over here
This commit is contained in:
@@ -0,0 +1,164 @@
|
||||
@page "/snake"
|
||||
|
||||
<div @onkeydown="OnKeyDown" tabindex="0" style="background-color: #333;" >
|
||||
<h1 id="Score" style="width: 100%; text-align: center; color:#fff;">Score : 0</h1>
|
||||
<div id="BODY" style="position: relative; margin-bottom: 5px; margin-left: 50%; right: 300px; background-color: #666; width: 600px; height: 600px;">
|
||||
<!-- Game -->
|
||||
|
||||
@foreach(SnakePart part in SnakeParts ) {
|
||||
<div style="position: absolute; left: @part.getX(); top: @part.getY(); width: 10px; height: 10px; background-color: #111; border: 0; padding: 0; margin: 0;"></div>
|
||||
}
|
||||
<div style="position: absolute; left: @Collectable.getX(); top: @Collectable.getY(); width: 10px; height: 10px; background-color: #f00; border: 0; padding: 0; margin: 0;"></div>
|
||||
|
||||
<!-- End Game -->
|
||||
</div>
|
||||
<div id="PauseScreen"style="position: relative; display: none; width: 500px; margin-left: 50%; right: 250px;">
|
||||
<h2 style="text-align: center; color: #f00; ">Game Paused</h2>
|
||||
</div>
|
||||
<div style="margin: 0 40px; width: calc(100% - 80px); background-color: #777;">
|
||||
<h3 style="text-align: center; font-size: 25px; color: #0f0; padding: 0; margin: 0; border: 0;">LEADERBOARD</h3>
|
||||
</div>
|
||||
<hr style="margin: 0px; width:calc(100% - 82px);" />
|
||||
<div id="Scoreboard" style="margin: 0 40px; width: calc(100% - 80px); background-color: #777;"></div>
|
||||
<h3 style="position: absolute; right: 10px; bottom: 3px; color: #fff;">Designed by Derek in California</h3>
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
@code {
|
||||
|
||||
struct SnakePart {
|
||||
public int X{ get; set; }
|
||||
public int Y{ get; set; }
|
||||
public int Hue{ get; set; }
|
||||
|
||||
public string getX() {
|
||||
return X * 10 + "px";
|
||||
}
|
||||
public string getY() {
|
||||
return Y * 10 + "px";
|
||||
}
|
||||
}
|
||||
enum Direction {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right
|
||||
}
|
||||
|
||||
List<SnakePart> SnakeParts = new List<SnakePart>(){
|
||||
new SnakePart{ X = 5, Y = 10 }, // Tail
|
||||
new SnakePart{ X = 6, Y = 10 },
|
||||
new SnakePart{ X = 7, Y = 10 },
|
||||
new SnakePart{ X = 8, Y = 10 } // Head
|
||||
};
|
||||
int Score = 0;
|
||||
bool Paused = false;
|
||||
Direction SnakeDirection = Direction.Right;
|
||||
int FrameRate = 1000 / 10;
|
||||
int HueRate = 5;
|
||||
|
||||
SnakePart Collectable = new SnakePart{ X = 15, Y = 15 };
|
||||
|
||||
|
||||
void OnKeyDown(KeyboardEventArgs e) {
|
||||
Console.WriteLine(e.Key);
|
||||
if (e.Key.ToLower() == "w" ) {
|
||||
SnakeDirection = Direction.Up;
|
||||
}else if (e.Key.ToLower() == "a" ) {
|
||||
SnakeDirection = Direction.Left;
|
||||
}else if (e.Key.ToLower() == "s" ) {
|
||||
SnakeDirection = Direction.Down;
|
||||
}else if (e.Key.ToLower() == "d" ) {
|
||||
SnakeDirection = Direction.Right;
|
||||
}
|
||||
}
|
||||
|
||||
bool CheckSelfHit() {
|
||||
foreach(SnakePart cur in SnakeParts ) {
|
||||
if (SnakeParts[SnakeParts.Count-1].X == cur.X ) {
|
||||
if (SnakeParts[SnakeParts.Count-1].Y == cur.Y ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CheckBounds() {
|
||||
if (SnakeParts[SnakeParts.Count-1].X > 0 && SnakeParts[SnakeParts.Count-1].X < 50 ) {
|
||||
if (SnakeParts[SnakeParts.Count-1].Y > 0 && SnakeParts[SnakeParts.Count-1].Y < 50 ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CheckCollectibleHit() { // return true if hit
|
||||
if (SnakeParts[SnakeParts.Count-1].X == Collectable.X ) {
|
||||
if (SnakeParts[SnakeParts.Count-1].Y == Collectable.Y ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void ResetCollectable() {
|
||||
Collectable.X = new Random().Next(1, 40);
|
||||
Collectable.Y = new Random().Next(1, 40);
|
||||
}
|
||||
|
||||
void Die() {
|
||||
|
||||
}
|
||||
|
||||
void Update() {
|
||||
// Get Next Position
|
||||
if( SnakeDirection == Direction.Up ) {
|
||||
SnakeParts.Add( new SnakePart {
|
||||
X = SnakeParts[SnakeParts.Count-1].X,
|
||||
Y = SnakeParts[SnakeParts.Count-1].Y - 1,
|
||||
Hue = SnakeParts[SnakeParts.Count-1].Hue + HueRate
|
||||
});
|
||||
}else if (SnakeDirection == Direction.Right ) {
|
||||
SnakeParts.Add( new SnakePart {
|
||||
X = SnakeParts[SnakeParts.Count-1].X + 1,
|
||||
Y = SnakeParts[SnakeParts.Count-1].Y,
|
||||
Hue = SnakeParts[SnakeParts.Count-1].Hue + HueRate
|
||||
});
|
||||
}else if (SnakeDirection == Direction.Down ) {
|
||||
SnakeParts.Add( new SnakePart {
|
||||
X = SnakeParts[SnakeParts.Count-1].X,
|
||||
Y = SnakeParts[SnakeParts.Count-1].Y + 1,
|
||||
Hue = SnakeParts[SnakeParts.Count-1].Hue + HueRate
|
||||
});
|
||||
}else if (SnakeDirection == Direction.Left ) {
|
||||
SnakeParts.Add( new SnakePart {
|
||||
X = SnakeParts[SnakeParts.Count-1].X - 1,
|
||||
Y = SnakeParts[SnakeParts.Count-1].Y,
|
||||
Hue = SnakeParts[SnakeParts.Count-1].Hue + HueRate
|
||||
});
|
||||
}
|
||||
if (CheckSelfHit() && CheckBounds() ) {
|
||||
if( CheckCollectibleHit() ) {
|
||||
Score += 1;
|
||||
ResetCollectable();
|
||||
} else {
|
||||
SnakeParts.RemoveAt( 0 );
|
||||
}
|
||||
} else {
|
||||
Die();
|
||||
}
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
protected override void OnInitialized() {
|
||||
ResetCollectable();
|
||||
var timer = new System.Threading.Timer((e) => {
|
||||
if (!Paused){
|
||||
Update();
|
||||
}
|
||||
}, null, 0, FrameRate );
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user