-
Notifications
You must be signed in to change notification settings - Fork 1
/
Player.cs
106 lines (98 loc) · 2.55 KB
/
Player.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using SplashKitSDK;
public class Player : IMovable
{
private Window _gameWindow;
public Bitmap CarBitmap;
public double X;
public double Y;
public bool Quit;
public delegate string GetBitmapName(Bitmap bitmap);
GetBitmapName GetBitmap = delegate (Bitmap bitmap)
{
return SplashKit.BitmapName(bitmap);
};
public Player(Window window)
{
CarBitmap = SplashKit.BitmapNamed("Player1");
_gameWindow = window;
X = Map.LANE_LEFT + Map.LANE_WIDTH * 2;
Y = _gameWindow.Height - CarBitmap.Height;
}
public void SwapPlayer(Bitmap p, GetBitmapName getname)
{
if (getname(p) == "Player1")
{
CarBitmap = SplashKit.BitmapNamed("Player2");
}
if (getname(p) == "Player2")
{
CarBitmap = SplashKit.BitmapNamed("Player1");
}
if (getname(p) == "Player1S")
{
CarBitmap = SplashKit.BitmapNamed("Player2S");
}
if (getname(p) == "Player2S")
{
CarBitmap = SplashKit.BitmapNamed("Player1S");
}
}
public void HandleInput()
{
int movement = Map.LANE_WIDTH;
int speed = 4;
if (SplashKit.KeyReleased(KeyCode.RightKey) || SplashKit.KeyReleased(KeyCode.DKey))
{
X += movement;
}
if (SplashKit.KeyReleased(KeyCode.LeftKey) || SplashKit.KeyReleased(KeyCode.AKey))
{
X -= movement;
}
if (SplashKit.KeyDown(KeyCode.UpKey) || SplashKit.KeyDown(KeyCode.WKey))
{
Y -= speed;
}
if (SplashKit.KeyDown(KeyCode.DownKey) || SplashKit.KeyDown(KeyCode.SKey))
{
Y += speed;
}
if (SplashKit.KeyReleased(KeyCode.LeftCtrlKey))
{
SwapPlayer(CarBitmap, GetBitmap);
}
if (SplashKit.KeyReleased(KeyCode.EscapeKey))
{
Quit = true;
}
}
public void Move()
{
HandleInput();
StayInTrack();
}
public void Draw()
{
CarBitmap.Draw(X, Y);
}
private void StayInTrack()
{
if (X >= Map.LANE_LEFT + Map.LANE_WIDTH * 5) //the right side of track
{
X -= Map.LANE_WIDTH;
}
if (X < Map.LANE_LEFT) //the left side of track
{
X += Map.LANE_WIDTH;
}
if (Y > _gameWindow.Height - CarBitmap.Height)
{
Y = _gameWindow.Height - CarBitmap.Height;
}
if (Y < 0)
{
Y = 0;
}
}
}