-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.cs
142 lines (108 loc) · 2.94 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using SplashKitSDK;
public class Player
{
private Bitmap _PlayerBitmap;
private Bitmap _LifeBitmap;
public string Name { get; set; }
public int Score;
private int _NameWidth;
public Circle c;
public int Lives = 150;
public bool fire = false;
public double Radius { get; set; } = 10;
//initializing setter and getter
public double X { get; private set; }
public double Y { get; private set; }
public int Width
{
get => _PlayerBitmap.Width;
}
public int Height
{
get => _PlayerBitmap.Height;
}
public bool Quit { get; set; }
//initializing constructor with window object
public Player(Window gameWindow,string name)
{
//decalring Bitmap
_PlayerBitmap = new Bitmap("Player", "Player.png");
_LifeBitmap =new Bitmap("life","Like.png");
X = (gameWindow.Width - Width) / 2;
Y = (gameWindow.Height - Height) / 2;
Name = name;
_NameWidth = SplashKit.TextWidth(name, "Arial", 10);
Quit = false;
}
//player bitmap drawing method
public void Draw()
{ c = SplashKit.CircleAt(X, Y, Radius);
SplashKit.DrawBitmap(_PlayerBitmap, X, Y);
SplashKit.DrawBitmap(_LifeBitmap,680,520);
SplashKit.DrawText($"{Lives}",Color.Black,"BoldFont",2,660,540);
}
//method to handle the input from the user
public void HandleInput()
{
const int SPEED = 5;
SplashKit.ProcessEvents();
if (SplashKit.KeyDown(KeyCode.UpKey))
{
Y -= SPEED;
}
if (SplashKit.KeyDown(KeyCode.DownKey))
{
Y += SPEED;
}
if (SplashKit.KeyDown(KeyCode.LeftKey))
{
X -= SPEED;
}
if (SplashKit.KeyDown(KeyCode.RightKey))
{
X += SPEED;
}
if (SplashKit.KeyDown(KeyCode.EscapeKey))
{
Quit = true;
}
if (SplashKit.MouseDown(MouseButton.LeftButton))
{
fire = true;
}
}
// method to limit the player on the screen, without overflow
public void StayOnWindow(Window limit)
{
const int GAP = 10;
if (X < GAP)
{
X = GAP;
}
if (X + Width > limit.Width - GAP)
{
X = (limit.Width - GAP) - Width;
}
if ((X + Width) > limit.Width - GAP)
{
X = (limit.Width - GAP) - Width;
}
if (Y < GAP)
{
Y = GAP;
}
if ((Y + Height) > limit.Height - GAP)
{
Y = (limit.Height - GAP) - Height;
}
}
public bool CollidedWith(Robot robo)
{
return _PlayerBitmap.CircleCollision(X, Y, robo.CollisionCircle);
}
public bool CollidedWith(NetworkPlayer op)
{
return SplashKit.CirclesIntersect(c, op.CollisionCircle);
}
}